zoukankan      html  css  js  c++  java
  • 面向对象(中)项目二 2.0

    工具类 CMUtility
    
    
    package day1_18;

    import java.util.Scanner;

    public class CMUtility {

    //读取主菜单选项
    public static char readMenuSelection() {
    for (; ; ) {
    String str = readKeyBoard(1, false);
    char c = str.charAt(0);
    if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
    System.out.println("输入选项错误,请重新输入:");
    } else {
    return c;
    }
    }
    }

    // 读取字符串
    public static String readString(int limit) {
    return readKeyBoard(limit, false);
    }

    //读取字符串(直接Enter换行的话,就返回默认值defaultValue)
    public static String readString(int limit,String defaultValue) {
    String str = readKeyBoard(limit, true);
    return str.equals("") ? defaultValue : str;
    }


    //读取整数
    public static int readInt() {
    for (; ; ) {
    String str = readKeyBoard(2, false);
    try {
    int n = Integer.parseInt(str);
    return n;
    } catch (Exception e) {
    System.out.print("请输入不超过两位数的数字:");
    }
    }
    }

    //读取整数(直接Enter换行的话,就返回默认值defaultValue)
    public static int readInt(int defaultValue) {
    int n;
    for (; ; ) {
    String str = readKeyBoard(2, true);
    if (str.equals("")) {
    return defaultValue;
    }
    try {
    n = Integer.parseInt(str);
    return n;
    } catch (Exception e) {
    System.out.print("请输入不超过两位数的数字:");
    }
    }
    }

    //读取字符
    public static char readChar() {
    String str = readKeyBoard(1, false);
    return str.charAt(0);
    }


    //读取字符(直接Enter换行的话,就返回默认值defaultValue)
    public static char readChar(char defaultValue) {
    String str = readKeyBoard(1, true);
    return str.equals("") ? defaultValue : str.charAt(0);
    }

    //是否确认退出选项
    public static char readConfirmSelection() {
    char selection;
    for (; ; ) {
    String str = readKeyBoard(1, false);
    selection = str.toUpperCase().charAt(0);
    if (selection != 'Y' && selection != 'N') {
    System.out.print("输入选项错误,请重新输入:");
    } else {
    break;
    }
    }
    return selection;
    }


    //读取用户输入
    private static String readKeyBoard(int limit, boolean blankEnter) {

    String str = "";
    for (; ; ) {
    Scanner scanner = new Scanner(System.in);
    if (scanner.hasNextLine()) {
    str = scanner.nextLine().trim();
    if (str.length() == 0) {
    if (blankEnter) {
    return str;
    }
    System.out.print("请重新输入:");
    }else if (str.length() > limit) {
    System.out.print("输入长度不能大于" + limit + ",请重新输入:");
    } else {
    break;
    }
    }
    }
    return str;
    }

    }
    
    
    客户实体类  Customer
    
    
    package day1_18;

    public class Customer {
    private String name;
    private char gender;
    private int age;
    private String phone;
    private String email;

    public Customer() {
    }

    public Customer(String name, char gender, int age, String phone, String email) {
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.phone = phone;
    this.email = email;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public char getGender() {
    return gender;
    }

    public void setGender(char gender) {
    this.gender = gender;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public String getPhone() {
    return phone;
    }

    public void setPhone(String phone) {
    this.phone = phone;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }
    }
    
    
    
    
    操作客户类  CustomerList
    
    
    package day1_18;

    //Customer对象的管理模块,内部使用数组管理一组Customer对象
    public class CustomerList {
    //定义一个数组用来保存客户对象
    private Customer[] customers;
    //记录保存客户对象的实际数量
    private int total;

    //初始化数组
    //totalCustomer指定数组的最大空间
    public CustomerList(int totalCustomer) {
    customers = new Customer[totalCustomer];
    }

    //添加客户
    public boolean addCustomer(Customer customer) {
    if (total >= customers.length) {
    return false;
    }
    customers[total++] = customer;
    return true;
    }


    //修改指定索引位置的客户信息
    public boolean replaceCustomer(int index,Customer customer) {
    if (index < 0 || index >= total) {
    return false;
    }
    customers[index] = customer;
    return true;
    }

    //删除指定位置的客户
    public boolean deleteCustomer(int index) {
    if (index < 0 || index >= total) {
    return false;
    }
    for (int i = index; i < total; i++) {
    customers[i] = customers[i + 1];
    }
    total--;
    return true;
    }

    //查找指定位置的客户
    public Customer getCustomer(int index){
    if (index < 0 || index >= total) {
    return null;
    } else {
    return customers[index];
    }
    }

    //查询客户列表
    public boolean listCustomers() {
    if (total == 0) {
    return false;
    }
    System.out.print("编号 姓名 性别 年龄 电话 邮箱 ");
    for (int i=0;i< total;i++) {
    Customer customer = customers[i];
    System.out.print((i+1) + " " + customer.getName() + " " + customer.getGender() +
    " " + customer.getAge() + " " + customer.getPhone() +
    " " + customer.getEmail() + " ");
    }
    return true;
    }

    //获取实际存储客户的个数
    public int getTotal() {
    return total;
    }
    }
     
    客户端交互类 CustomerView
    
    
    package day1_18;

    public class CustomerView {
    public CustomerList customerList;

    public static void main(String[] args) {
    CustomerView view = new CustomerView();
    view.enterMainMenu();
    }

    public CustomerView() {
    customerList = new CustomerList(10);
    }

    //主菜单
    public void enterMainMenu() {
    boolean isFlag = true;
    while (isFlag) {
    System.out.println(" -----------------客户信息管理软件----------------- ");
    System.out.println(" 1 添 加 客 户");
    System.out.println(" 2 修 改 客 户");
    System.out.println(" 3 删 除 客 户");
    System.out.println(" 4 客 户 列 表");
    System.out.println(" 5 退 出 ");
    System.out.print(" 请选择(1-5):");
    char menu = CMUtility.readMenuSelection();
    switch (menu) {
    case '1':
    //System.out.println("添加客户操作");
    saveCustomer();
    break;
    case '2':
    //System.out.println("修改客户操作");
    replaceCustomer();
    break;
    case '3':
    //System.out.println("删除客户操作");
    removeCustomer();
    break;
    case '4':
    //System.out.println("客户列表操作");
    listCustomers();
    break;
    case '5':
    //System.out.println("退出操作");
    System.out.print("确认是否退出(Y/N):");
    char isExit = CMUtility.readConfirmSelection();
    if (isExit == 'Y') {
    isFlag = false;
    }
    }
    }
    }

    //添加客户
    public void saveCustomer() {
    System.out.println("----------添加客户----------");
    System.out.print("姓名:");
    String name = CMUtility.readString(4);
    System.out.print("性别:");
    char gender = CMUtility.readChar();
    System.out.print("年龄:");
    int age = CMUtility.readInt();
    System.out.print("电话:");
    String phone = CMUtility.readString(13);
    System.out.print("邮箱:");
    String email = CMUtility.readString(30);
    Customer customer = new Customer(name, gender, age, phone, email);
    boolean isSaved = customerList.addCustomer(customer);
    if (isSaved) {
    System.out.println("----------添加客户成功----------");
    } else {
    System.out.println("----------客户目录已满,添加客户失败----------");
    }
    }

    //查看客户列表
    public boolean listCustomers() {
    System.out.println("----------客户列表----------");
    boolean isListed = customerList.listCustomers();
    if (!isListed) {
    System.out.println("客户信息为空");
    }
    System.out.println("----------客户列表完成----------");
    return isListed;
    }

    //修改指定位置的客户信息
    public void replaceCustomer() {
    int index;
    Customer customer;
    if (listCustomers()) {
    for (; ; ) {
    System.out.print("请输入要修改客户的编号(输入0退出):");
    index = CMUtility.readInt() -1;
    if (index == -1) {
    return;
    }
    customer = customerList.getCustomer(index);
    if (customer == null) {
    System.out.println("列表中没有指定的客户!");
    } else {
    break;
    }
    }
    System.out.print("姓名(" + customer.getName() + "):");
    String name = CMUtility.readString(4, customer.getName());
    System.out.print("性别(" + customer.getGender() + "):");
    char gender = CMUtility.readChar(customer.getGender());
    System.out.print("年龄(" + customer.getAge() + "):");
    int age = CMUtility.readInt(customer.getAge());
    System.out.print("电话(" + customer.getPhone() + "):");
    String phone = CMUtility.readString(13, customer.getPhone());
    System.out.print("邮箱(" + customer.getEmail() + "):");
    String email = CMUtility.readString(20, customer.getEmail());

    customer = new Customer(name, gender, age, phone, email);
    boolean isReplaced = customerList.replaceCustomer(index, customer);
    if (isReplaced) {
    System.out.println("-----------修改客户成功-----------");
    } else {
    System.out.println("-----------修改客户失败-----------");
    }
    } else {
    System.out.print(" 温馨提示:客户列表为空,请先添加客户 ");
    enterMainMenu();
    }
    }

    //删除客户
    public void removeCustomer() {
    int index;
    if (listCustomers()) {
    for (; ; ) {
    System.out.print("请输入要删除客户的编号(输入0退出):");
    index = CMUtility.readInt() -1;
    if (index == -1) {
    return;
    }
    Customer customer = customerList.getCustomer(index);
    if (customer == null) {
    System.out.println("列表中没有指定的客户!");
    }else {
    break;
    }
    }
    System.out.print("是否确定删除此客户(Y/N):");
    char isConfirm = CMUtility.readConfirmSelection();
    if (isConfirm == 'Y') {
    boolean isRemove = customerList.deleteCustomer(index);
    if (isRemove) {
    System.out.println("----------删除成功----------");
    } else {
    System.out.println("----------删除成功----------");
    }
    }
    } else {
    System.out.print(" 温馨提示:客户列表为空,请选择其他操作 ");
    enterMainMenu();
    }
    }
    }
     
    主菜单效果
    
    

     笔记

    关于客户管理软件项目编程思路的梳理

    1.主菜单列表选项
    1.1 添加客户
    ①获取键盘输入,根据属性的类型不同和字符长度的限制,在工具类里编写
    相应的获取键盘输入的方法
    ②客户端输入数据赋值给每个属性后,利用构造器创建Customer对象,再添
    加到customers数组中,并且数组中实际存储的Customer对象的个数total加1

    1.2 查看客户列表
    ①遍历customers数组中每个customer对象,获取每个属性的值

    1.3 修改客户列表中某个编号的客户信息
    ①编号是指在客户列表中例如1,2,3,...的一系列自然数,注意它与数组索引的关系:编号-1 = 数组索引
    ②客户端键盘输入接收到指定编号后,首先需要判断这个编号在客户列表中是否存在,即以(指定编号-1)
    作为数组索引,查看数组中这个索引的对象是否为空,如果为空,说明用户列表中是没有这个用户的,既然
    没有这个用户那么修改客户也是不合逻辑的
    ③如果客户列表中存在需要修改的客户,那么接收键盘输入作为属性值的方法就需要根据情况来重新定义了,
    因为如果客户的某个属性不想修改,那么就设计为输入时直接Enter,表示当接收空字符串时,新customer
    对象的属性值还是为原来的属性值

    1.4 删除客户列表中某个编号的客户信息
    ①编号是指在客户列表中例如1,2,3,...的一系列自然数,注意它与数组索引的关系:编号-1 = 数组索引
    ②客户端键盘输入接收到指定编号后,首先需要判断这个编号在客户列表中是否存在,即以(指定编号-1)
    作为数组索引,查看数组中这个索引的对象是否为空,如果为空,说明用户列表中是没有这个用户的,既然
    没有这个用户那么删除客户也是不合逻辑的
    ③如果客户列表中存在需要删除的客户,从需要删除的这个客户在数组customers的索引位置开始,后面的元
    素覆盖前面前一个元素,这样实现删除操作
    ④数组中实际存储的Customer对象的个数total减1

    1.5 退出主菜单
    获取键盘输入,取字符,转为大写字母后如果为‘Y’,则退出;如果为‘N’则不退出
     
     
  • 相关阅读:
    阿波罗11号登月全套高清照片(16650张,67.1G)分享
    oracle ORA-02292: 违反完整约束条件
    三十六副寺庙对联,领略真正的大智慧!
    SpringCloud微服务架构及其示例
    IDEA怎么关闭暂时不用的工程
    关于解决Incorrect result size: expected 1, actual的问题
    Centos7安装redis6.0.6教程
    VMware安装CentOS7超详细版
    Spring5--@Indexed注解加快启动速度
    《程序员修炼手册》
  • 原文地址:https://www.cnblogs.com/zui-ai-java/p/14319698.html
Copyright © 2011-2022 走看看