溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何利用java實(shí)現(xiàn)一個(gè)客戶信息管理系統(tǒng)

發(fā)布時(shí)間:2021-04-13 14:29:09 來(lái)源:億速云 閱讀:192 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何利用java實(shí)現(xiàn)一個(gè)客戶信息管理系統(tǒng),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

類(lèi)圖:

如何利用java實(shí)現(xiàn)一個(gè)客戶信息管理系統(tǒng)

Customer類(lèi):

public class Customer {
    /**
     * @name 客戶姓名
     * @sex 性別
     * @age 年齡
     * @phone 電話號(hào)碼
     * @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 類(lèi):

public class CustomerList {
        private Customer [] customers;
        private static int total = 0;
    /**
     * 構(gòu)造器初始化對(duì)象數(shù)組
     * @param totalCustmoers 客戶的總數(shù)
     */
    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 指定的客戶的編號(hào)
     * @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 指定的客戶的編號(hào)
     * @return 返回是否刪除成功
     */
    public boolean deleteCustomer(int index){
        if(index<customers.length)
        {
            for(int i=index;i<total-1;i++)
            {
                customers[i]=customers[i+1];/**把數(shù)據(jù)往前挪動(dòng)*/
            }
                customers[total-1]=null;
                total--;/**存儲(chǔ)的數(shù)據(jù)的總數(shù)-1*/
             return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 展現(xiàn)客戶的信息
     * @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];/**新建一個(gè)數(shù)組來(lái)接收原數(shù)組的數(shù)據(jù)*/
            for(int i=0;i<total;i++){
                cust[i]=customers[i];
            }
            return cust;
    }

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

CustomerVIew類(lèi):

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(){
        /**
         * 從鍵盤(pán)處接收客戶數(shù)據(jù)
         */
        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("電話號(hào)碼:");
        String phone = input.next();
        System.out.println("電子郵箱:");
        String email = input.next();
        /**
         * 對(duì)客戶數(shù)據(jù)進(jìn)行封裝
         */
        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());
        /**得到新的客戶數(shù)據(jù)*/
        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("要?jiǎng)h除的客戶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("輸入錯(cuò)誤");break;
        }}
        System.out.println("是否確認(rèn)刪除該客戶?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("沒(méi)有任何客戶數(shù)據(jù)");
    }
    else{
    for(int i=0;i< customer.length;i++)
    {
        System.out.println("姓名:"+customer[i].getName()+"\t"+"性別"+customer[i].getSex()+"\t"+"年齡:"+customer[i].getAge()+"\t"+"電話號(hào)碼:"+customer[i].getPhone()+"\t"+"電子郵箱:"+customer[i].getEmail()+"\t");
    }
}}

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

工具類(lèi):

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("數(shù)字輸入錯(cuò)誤,請(qǐng)重新輸入:");
                }
            }
            return n;
        }

關(guān)于“如何利用java實(shí)現(xiàn)一個(gè)客戶信息管理系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI