尚硅谷Java学习——项目二《客户信息管理软件》

CMUtility类

package cn.itcast.p2;

import java.util.Scanner;

public class CMUtility {
    private static Scanner scanner = new Scanner(System.in);

    // 主菜单选择的判断
    public static char readMeauSelection(){
        char c;
        while(true){
            String string = scanner.next();
            c = string.charAt(0);
            if(c != '1' && c!= '2' && c!= '3' && c!= '4' && c != '5'){
                System.out.println("您所输入的序号有误!请重新输入:");
                continue;
            }
             else break;
        }
        return c;
    }

    // 退出菜单的确认
    public static char readConfirmSelection(){
        char c;
        while(true){
            String str = readKeyBoard(1,false).toUpperCase();
            c = str.charAt(0);
            if(c == 'Y' || c == 'N')
                break;
            else
                System.out.print("请输入正确的Y/N:\n");
        }
        return c;
    }

    // 读取一个字符,获取性别
    public static char readChar(){
        String sex = readKeyBoard(1,false);
        return sex.charAt(0);
    }
    // 读取一个字符,直接按回车不修改性别
    public static char readChar(char defaultValue){
        String sex = readKeyBoard(1,true);
        return (sex.length()==0)? defaultValue:sex.charAt(0);
    }

    // 读取两个字符,获取年龄
    public static int readInt(){
        int age;
        while(true){
            String str = readKeyBoard(2,false);
            try {
                age = Integer.parseInt(str);
                return age;
            }catch (NumberFormatException e){
                System.out.print("数字输入有误,请重新输入:\n");
            }

        }
    }
    // 读取两个字符,修改年龄
    public static int readInt(int defaultValue){
        int age;
        while(true){
            String str = readKeyBoard(2,true);
            if(str.equals(""))
                return defaultValue;
            try {
                age = Integer.parseInt(str);
                return age;
            }catch (NumberFormatException e){
                System.out.print("数字输入有误,请重新输入:\n");
            }

        }
    }

    // 姓名、电话、邮箱的信息获取
    public static String readString(int limit){

        return readKeyBoard(limit,false);
    }
    // 姓名、电话、邮箱信息的修改,不修改直接按回车
    public static String readString(int limit,String defaultValue){
        String str = readKeyBoard(limit,true);
        return str.equals("") ? defaultValue : str;
    }

    // 读取数据
    private static String readKeyBoard(int limit, boolean blankSelection){
        String rkb = "";
        while (scanner.hasNextLine()){

            rkb = scanner.nextLine();
            if (rkb.length() == 0) {
                if (blankSelection) return rkb;
                else continue;
            }

            else if(rkb.length() < 1 || rkb.length() > limit){
                System.out.printf(String.valueOf(rkb.length()));
                System.out.println("输入长度超出!(不大于" + limit + ")");
                continue;
            }

            else break;
        }
        return rkb;
    }
}

Customer类

package cn.itcast.p2;

public class Customer {

    /* 定义成员变量 */
    String name; //姓名
    char gender; //性别
    int age; //年龄
    String phone; //电话
    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;
    }

    /* 五个属性多对应的get,set方法 */

    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;
    }
}

CustomerView类

package cn.itcast.p2;

public class CustmoerView {
    private CustomerList customerList = new CustomerList(10);

    public CustmoerView() {
        Customer customer = new Customer("Phoneix", '女', 23, "18008382128", "1281141126@qq.com");
        customerList.addCustomer(customer);
    }

    public void enterMainMeau() {

        boolean isflag = true;
        while(isflag){
            System.out.println("--------------------------------客户信息管理软件-------------------------------------\n");
            System.out.println("                                 1 添 加 客 户");
            System.out.println("                                 2 修 改 客 户");
            System.out.println("                                 3 删 除 客 户");
            System.out.println("                                 4 客 户 列 表");
            System.out.println("                                 5 退      出\n");
            System.out.print("                               请选择 < 1-5 > :");

            char str = CMUtility.readMeauSelection();
            switch (str){
                case '1':{
                    System.out.println("*****************************添 加 客 户*****************************");
                    addNewCustomer();
                    break;
                }
                case '2':{
                    System.out.println("*****************************修 改 客 户*****************************");
                    modifyCustomer();
                    break;
                }
                case '3':{
                    System.out.println("*****************************删 除 客 户*****************************");
                    deleteCustomer();
                    break;
                }
                case '4':{
                    System.out.println("*****************************客 户 列 表*****************************");
                    listAllCustomers();
                    System.out.println("********************************************************************");
                    break;
                }
                case '5':{
                    System.out.print("请您再次确认是否要退出(Y/N):\n");
                    char exit = CMUtility.readConfirmSelection();
                    if(exit == 'Y')
                        isflag = false;
                    break;
                }
            }
        }
    }

    public void addNewCustomer(){
            System.out.println("客户姓名:");
            String name = CMUtility.readString(10);
            System.out.println("客户性别:");
            char sex = CMUtility.readChar();
            System.out.println("客户年龄:");
            int age = CMUtility.readInt();
            System.out.println("客户电话:");
            String phone = CMUtility.readString(11);
            System.out.println("客户邮箱:");
            String email = CMUtility.readString(20);

            Customer cust = new Customer(name,sex,age,phone,email);
            boolean isSuccess = customerList.addCustomer(cust);
            if(isSuccess)
            System.out.println("*****************************添加客户完成*****************************");
            else
                System.out.println("*************************客户列表已满,添加失败*************************");
    }
    public void modifyCustomer(){
        int index;
        Customer cust;
        while(true){
            System.out.print("请输入要修改客户的序号(-1退出):\n");
            index = CMUtility.readInt();
            if(index == -1)
                return;
            index --;
            cust = customerList.getCustomer(index);
            if(cust == null)
                System.out.println("无法找到该客户!");
            else
                break;
        }
        System.out.print("姓名("+cust.getName()+"):");
        String name = CMUtility.readString(10, cust.getName());
        System.out.print("性别("+cust.getGender()+"):");
        char gender = CMUtility.readChar( cust.getGender());
        System.out.print("年龄("+cust.getAge()+"):");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("电话("+cust.getPhone()+"):");
        String tel = CMUtility.readString(11, cust.getPhone());
        System.out.print("邮箱("+cust.getEmail()+"):");
        String email = CMUtility.readString(15, cust.getEmail());

        Customer newCust = new Customer(name,gender,age,tel,email);
        boolean isReplaced = customerList.replaceCustomer(index, newCust);
        if (isReplaced) {
            System.out.println("*****************************修改客户完成*****************************");
        } else {
            System.out.println("*****************************修改客户失败*****************************");
        }
    }
    public void deleteCustomer(){
        Customer cust;
        int index;
        while(true){
            System.out.print("请输入你要删除的客户序号(-1退出):\n");
            index = CMUtility.readInt();
            if(index == -1)
                return;
            index --;
            cust = customerList.getCustomer(index);
            if(cust == null)
                System.out.println("无法找到该客户!");
            else break;
        }
        System.out.print("请确认是否要删除(Y/N):\n");
        char isDelete = CMUtility.readConfirmSelection();
        if(isDelete == 'Y'){
            boolean deleteSuccess = customerList.delCustomer(index);
            if(deleteSuccess)
                System.out.println("*****************************删除客户完成*****************************");
            else System.out.println("删除失败,请输入正确序号:1->" + customerList.getTotal());
        }

    }
    public void listAllCustomers(){
        int total = customerList.getTotal();
        if(total == 0)
            System.out.println("没有客户记录!");
        else{
            System.out.println("编号\t姓名\t\t性别\t年龄\t\t电话\t\t\t\t邮箱");
            Customer[] custList = customerList.getAllCustmoers();
            for(int i = 0; i < total; i ++){
                System.out.println(i+1 + "\t" + custList[i].getName() + "\t" + custList[i].getGender() + "\t" + custList[i].getAge() + "\t" +
                        custList[i].getPhone() + "\t\t" + custList[i].getEmail() + "\t");
            }
        }
    }

    public static void main(String[] args){
        CustmoerView cv = new CustmoerView();
        cv.enterMainMeau();
    }
}

CustomerList类

package cn.itcast.p2;


public class CustomerList {

    /* 定义属性和方法 */
    private Customer[] customers; // 客户列表
    private int total = 0; // 记录已保存顾客的数量



    // 定义构造器
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];
    }

    public int getTotal() {
        return total;
    }
    // 添加客户
    public boolean addCustomer(Customer customer){
        if(total < customers.length){
            customers[total ++] = customer;
            return true;
        }
        else return false;
    }
    // 删除客户
    public boolean delCustomer(int index){
        if(index >= 0 && index < total){
            for(int i = index; i < total-1; i ++){
                customers[i] = customers[i + 1];
            }
            customers[total-1] = null;
            total --;
            return true;
        }
        else return false;
    }
    // 修改客户
    public boolean replaceCustomer(int index,Customer cust){
        if(index >= 0 && index < total) {
            customers[index] = cust;
            return true;
        }
        else return false;
    }
    // 获取客户列表
    public Customer[] getAllCustmoers(){
        Customer[] custs = new Customer[total];
        for(int i = 0; i < total; i ++)
            custs[i] = customers[i];
        return custs;
    }
    // 获取客户
    public Customer getCustomer(int index){
        if(index >= 0 && index < total)
            return customers[index];
        else return null;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值