JAVA自学之路——客户信息管理软件

CustomerView类实现

package management;
public class CustomerView {
    public static void main(String[] args) {
        char flag;
        CustomerList customer = new CustomerList(10);//创建一个用户操作集(并非用户数组),并用构造器同时创建一个具有10个用户的用户数组
        do {
            System.out.println("------------------客户信息管理软件-------------------");
            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.println();
            System.out.print("                   请选择(1-5):_                     ");
            flag = CMU.readMenuSelection();//获取用户输入的编号
            if(flag == '5'){
                break;
            }else{
                switch (flag){
                    case '1':
                        Customer customer1 = new Customer();//创建一个对象好给形参
                        customer.addCustomer(customer1);
                        break;
                    case '2':
                        System.out.println("--------------修改客户---------------");
                        System.out.print("请选择待修改客户编号(-1退出):");
                        customer.replaceCustomer(CMU.readInt(-1));
                        break;
                    case '3':
                        System.out.println("--------------删除客户---------------");
                        System.out.print("请选择待删除客户编号(-1退出):");
                        customer.deleteCustomer(CMU.readInt(-1));
                        break;
                    case '4':
                        System.out.println("---------------客户列表--------------------");
                        System.out.println();
                        System.out.println("编号  姓名   性别  年龄  电话           邮箱");
                        customer.getAllCustomers(customer.customers);
                        break;
                }
            }
            System.out.println();
        }while (flag == '1' || flag == '2' || flag == '3' || flag == '4');
        System.out.println("程序结束!!!");
    }
}

CustomerList类实现

package management;

class CustomerList {
    Customer[] customers;
    int total;
//构造器
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];
        total = 0;
    }
//方法
    public void addCustomer(Customer customer){
        if(total >=9){
            System.out.println("客户已满,无法添加新客户!!");
        }else{
            System.out.println("--------------添加客户---------------");
            System.out.print("姓名:");
            customer.setName(CMU.readString(15,"小明"));//将用户输入的信息作为当前用户的姓名
            System.out.println();
            System.out.print("性别:");
            customer.setSex(CMU.readChar('T'));
            System.out.println();
            System.out.print("年龄:");
            customer.setAge(CMU.readInt(0));
            System.out.println();
            System.out.print("电话:");
            customer.setPhone(CMU.readString(11,"00000000000"));
            System.out.println();
            System.out.print("E_mail ");
            customer.setE_mail(CMU.readString(50,"www.chai.com"));
            System.out.println();
            customers[total++] = customer;
            System.out.println("--------------添加完成-----------------");
        }
    }
    public boolean replaceCustomer(int index){
        if(index == -1){
            return true;
        }
        else if(index > total - 1 || index < 0 || index >= 5){
            System.out.println("当前客户不存在!!");
            return false;
        }else{
            System.out.print("姓名:(" + customers[index].getName(customers[index]) + "):");
            customers[index].setName(CMU.readString(20,customers[index].getName(customers[index])));//将CMU.readString的返回值,也就是用户修改后的名字赋给被修改用户
            System.out.print("性别:(" + customers[index].getSex(customers[index]) + "):" );
            customers[index].setSex(CMU.readChar(customers[index].getSex(customers[index])));
            System.out.print("年龄:(" + customers[index].getAge(customers[index]) + "):");
            customers[index].setAge(CMU.readInt(customers[index].getAge(customers[index])));
            System.out.print("电话:(" + customers[index].getPhone(customers[index]) + "):");
            customers[index].setPhone(CMU.readString(15,customers[index].getPhone(customers[index])));
            System.out.print("邮箱:(" + customers[index].getE_mail(customers[index]) + "):");
            customers[index].setE_mail(CMU.readString(15,customers[index].getE_mail(customers[index])));
            System.out.println("--------------修改完成---------------");
            return true;
        }

    }
    public boolean deleteCustomer(int index){
        if(index ==-1){
            return true;
        }
       else if(index > total - 1 || index < 0 || index >= 5){
            System.out.println("当前客户不存在!!");
            System.out.println("---------------------删除失败---------------------");
            return false;
        }else{
            System.out.print("确认是否删除(Y/N):");
            char flag2 = CMU.readChar();
            if(flag2 == 'Y' || flag2 == 'y'){
                total --;
                System.out.println("---------------------删除完成---------------------");
                return true;
            }else{
                System.out.println("---------------------删除失败---------------------");
                return false;
            }
        }

    }
    public boolean getAllCustomers(Customer[] customers){
        if(total == 0){
            System.out.println("当前无用户!!!");
            return false;
        }else{
            int i = 1;
            for(int j = 0;j < total;j++){
                Customer cust = getCustomer(j);
                System.out.println(i++ + "  " + customers[j].getName(customers[j]) + "   " + customers[j].getSex(customers[j]) + "  " + customers[j].getAge(customers[j]) + "  " + customers[j].getPhone(customers[j]) + "           " + customers[j].getE_mail(customers[j]));
            }
            System.out.println("---------------客户列表完成------------------");
            return true;
        }

    }
    public Customer getCustomer(int index){
        if(index <0 || index >=5 || total-1 < index){
            System.out.println("没有这么多客户!!");
            return null;
        }else{
            return customers[index];
        }
    }

    public int getTotal(){
        return total;
    }
}

Customer类实现

package management;
import java.util.*;
public class Customer {
    private String name;
    private char sex;
    private int age;
    private String phone;
    private String E_mail;
    //构造器
    /*public Customer(String name,char sex,int age,String phone,String E_mail){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.E_mail = E_mail;
    }*/
    public Customer(){

    }
    //set与get方法
    public String getName(Customer customer){
        return customer.name;
    }

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

    public char getSex(Customer customer){
        return customer.sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge(Customer customer){
        return customer.age;
    }

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

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

    public String getPhone(Customer customer){
        return customer.phone;
    }

    public void setE_mail(String E_mail){
        this.E_mail = E_mail;
    }

    public String getE_mail(Customer customer){
        return customer.E_mail;
    }


}

CMU(控制用户输入)类实现

package management;
import java.util.*;
public class CMU {
    private static Scanner scanner = new Scanner(System.in);
    /**
     用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
     */
    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     */
    public static char readChar() {
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以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);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     */
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
    /**
     用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
     */
    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    private static String readKeyBoard(int limit, boolean blankReturn) {
        String line = "";

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (blankReturn) return line;
                else continue;
            }

            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

说明

项目来源——尚硅谷JAVA基础

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值