利用java實現一個客戶信息管理系統

用博客記錄我自學的點點滴滴

類圖:

Customer類:

public class Customer {
    /**
     * @name 客戶姓名
     * @sex 性別
     * @age 年齡
     * @phone 電話號碼
     * @email 郵箱
     */
    private String name;
    private String sex;
    private int age;
    private String phone;
    private String email;
    public Customer(){};
    public Customer(String name,String sex,int age,String phone,String email){
        this.name=name;
        this.sex=sex;
        this.age=age;
        this.phone=phone;
        this.email=email;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getSex(){
        return this.sex;
    }
    public void setSex(String sex){
        this.sex=sex;
    }
    public String getPhone(){
        return phone;
    }
    public void setPhone(String phone){
        this.phone=phone;
    }
    public int getAge(){
        return this.age;
    }
    public void setAge(int age){
        this.age=age;
    }
    public String getEmail(){
        return this.email;
    }
    public void setEmail(String email){
        this.email=email;
    }
}

CustomerList 類:

public class CustomerList {
        private Customer [] customers;
        private static int total = 0;
    /**
     * 構造器初始化對象數組
     * @param totalCustmoers 客戶的總數
     */
    public CustomerList(int totalCustmoers){
        customers = new Customer[totalCustmoers];
    }

    /**
     * 增加客戶
     * @param customer 客戶
     * @return 返回是否添加成功
     */
        public boolean addCustomer(Customer customer){
            if(customer!=null&&total<customers.length)
            {customers[total]=customer;
                total++;
              return true;}
            else
            { return false;}
        }

    /**
     *替換
     * @param index 指定的客戶的編號
     * @param cust 修改的客戶
     * @return 返回是否修改成功
     */
        public boolean replaceCustomer(int index,Customer cust){
            if(index>=0 && index <total )
            {
                customers[index]=cust;return true;
            }
            else
            {
                return false;
            }
        }

    /**
     *刪除客戶
     * @param index 指定的客戶的編號
     * @return 返回是否刪除成功
     */
    public boolean deleteCustomer(int index){
        if(index<customers.length)
        {
            for(int i=index;i<total-1;i++)
            {
                customers[i]=customers[i+1];/**把數據往前挪動*/
            }
                customers[total-1]=null;
                total--;/**存儲的數據的總數-1*/
             return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 展現客戶的信息
     * @param index
     * @return 返回客戶
     */
    public Customer getCustomer(int index){
        if(index>=0 && index<total)
        {return customers[index];}
        else {
            return null;
        }
    }

    /**
     * 獲取所有的客戶
     * @return 客戶
     */
    public Customer[] getAllCustomers(){
            Customer [] cust = new Customer[total];/**新建一個數組來接收原數組的數據*/
            for(int i=0;i<total;i++){
                cust[i]=customers[i];
            }
            return cust;
    }

    /**
     * 得到客戶的總數
     * @return
     */
    public int getTotal(){
        return total;
    }
}

CustomerVIew類:

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

    /**
     * 顯示主菜單
     */
    public void enterMainMenu(){

        while(true)
        {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("-----------------------------------------------------");
            Scanner input = new Scanner(System.in);
            int op = input.nextInt();
            switch(op)
            {
                case 1 :this.addNewCustomer();break;
                case 2 :this.modifyCustomer();break;
                case 3 :this.deleteCustomer();break;
                case 4 :this.listAllCustomers();break;
                case 5 :System.exit(0);break;
                default:
                    System.out.println("重新選擇功能");break;
            }
        }
    }


    /**
     * 增加客戶
     */
    private void addNewCustomer(){
        /**
         * 從鍵盤處接收客戶數據
         */
        System.out.println("-------------------添加客戶-------------------");
        Scanner input = new Scanner(System.in);
        System.out.println("姓名:");
        String name = input.next();
        System.out.println("性別:");
        String sex=input.next();
        System.out.println("年齡:");
        int age = input.nextInt();
        System.out.println("電話號碼:");
        String phone = input.next();
        System.out.println("電子郵箱:");
        String email = input.next();
        /**
         * 對客戶數據進行封裝
         */
        Customer person = new Customer(name,sex,age,phone,email);
        Boolean flag=customerList.addCustomer(person);
        if(flag)
        {
            System.out.println("-------------------添加成功-------------------");
        }
        else
        {
            System.out.println("-------------------添加失敗-------------------");
        }
    }

    /**
     * 修改客戶信息
     */
    private void modifyCustomer(){
        System.out.println("-------------------修改客戶-------------------");
        System.out.println("要修改的客戶id:");
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
            Customer customer = customerList.getCustomer(number);
        System.out.println("姓名:"+customer.getName());
        String name = CMUtility.readString(5,customer.getName());
        System.out.println("性別:"+customer.getSex());
        String sex = CMUtility.readString(5,customer.getSex());
        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(15, customer.getEmail());
        /**得到新的客戶數據*/
        customer = new Customer(name,sex,age,phone,email);
        Boolean flag = customerList.replaceCustomer(number,customer);
        if(flag)
        {
            System.out.println("-------------------修改成功-------------------");
        }
        else
        {
            System.out.println("-------------------修改失敗-------------------");
        }
    }

    /**
     * 刪除客戶
     */
    private void deleteCustomer(){
        System.out.println("-------------------刪除客戶-------------------");
        System.out.println("要刪除的客戶id:");
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        while(true){
            System.out.println("退出(-1)");
        if(number>=0 && number<customerList.getTotal())
        {
            System.out.println("找到指定客戶");
        }
        else if(number==-1)
        {
            return;
        }
        else
        {
            System.out.println("輸入錯誤");break;
        }}
        System.out.println("是否確認刪除該客戶?Y/N");
        String ch = input.next();
        char o = ch.charAt(0);
        if(o=='Y')
        {
           boolean flag = customerList.deleteCustomer(number);
           if(flag){
               System.out.println("-------------------刪除成功-------------------");
           }
           else
           {
               System.out.println("-------------------刪除失敗-------------------");
           }
        }
        else{
            return;
    }
}
/**
 * 獲取客戶列表
 */
private void listAllCustomers(){
    Customer [] customer=customerList.getAllCustomers();
    if(customer.length==0)
    {
        System.out.println("沒有任何客戶數據");
    }
    else{
    for(int i=0;i< customer.length;i++)
    {
        System.out.println("姓名:"+customer[i].getName()+"\t"+"性別"+customer[i].getSex()+"\t"+"年齡:"+customer[i].getAge()+"\t"+"電話號碼:"+customer[i].getPhone()+"\t"+"電子郵箱:"+customer[i].getEmail()+"\t");
    }
}}

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

工具類:

public class CMUtility {
        private static Scanner scanner = new Scanner(System.in);
           public static String readString(int limit) {
            return readKeyBoard(limit, false);
        }
           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;
        }

總結

到此這篇關於利用java實現一個客戶信息管理系統的文章就介紹到這瞭,更多相關java客戶信息管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found