您好,登錄后才能下訂單哦!
這篇文章主要介紹“java如何實現簡單的客戶信息管理系統”,在日常操作中,相信很多人在java如何實現簡單的客戶信息管理系統問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java如何實現簡單的客戶信息管理系統”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
首先給大家看一下總的操作界面(別看簡單,里面的還是有東西的),后面就附上實現源碼、要求和注釋
Customer類
下面是關于Customer類的編寫要求:
* 用來封裝客戶的以下信息
* Sting name
* int age
* char gender
* Stirng phone
* String email
* 提供getset方法
* 構造器自行設定
public class Customer { private String name; private int age; private String gender; private String phone; private String email; public Customer() { } public Customer(String name,int age,String gender,String phone,String email) { this.name = name; this.age = age; this.gender = gender; this.phone = phone; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } 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類
下面是關于CustomerList類的編寫要求:
* CustomerList類的設計
* Customer[] 用來保存客戶信息
* int total 用來保存當前存入客戶數量
* 該類至少提供以下的構造器和方法
* public CustomerLIst(int totalCustomer);
* public boolean addCustomer(Customer customer);
* public boolean replaceCustomer(int index,Customer cust);
* public boolean deleteCustomer(int index);
* public Customer[] getallCustomer();
* public Customer getCustomer(int index);
* public int getToal();
public class CustomerList { private static Customer customers[]; private static int total; 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 cust) { if(index < 0 || index >= total) { return false; } customers[index] = cust; return true; } // 刪除指定位置的客戶 public boolean deleteCustomer(int index) { if(index < 0 || index >= total) { return false; } for(int i = index;i < total - 1;i++) { customers[i] = customers[i+1]; } customers[total - 1] = null; total--; return true; } // 得到所有客戶的信息 public static Customer[] getallCustomer() { 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 null; } return customers[index]; } // 得到Customers中所有客戶的個數 public static int getTotal() { return total; } }
CustomerView類
CustomerView類的編寫
* 主模塊:用于用戶界面的展示、與用戶交互
* CustomerList customerList = new CustomerList(10)
* 應含有的構造器和方法
* private void enterMainMenue()
* private void addNewCustomer()
* private void modifyCustomer()
* private void deleteCustomer()
* private void listAllCustomers()
* private static void main(String[] args)
public class CustomerView { CustomerList customerList = new CustomerList(10); public CustomerView() { Customer customer = new Customer("王龍", 20, "Male", "18965391649", "465989777@qq.com"); customerList.addCustomer(customer); } // 用戶主菜單 private void enterMainMenue() { 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("請選擇1-5:"); } // 添加客戶 private void addNewCustomer() { // System.out.println("添加客戶的操作"); System.out.println("-----------------添加客戶-------------------------"); Scanner in = new Scanner(System.in); System.out.print("姓名:"); String name = in.nextLine(); System.out.print("年齡:"); int age = in.nextInt(); System.out.print("性別:"); String gender = in.nextLine(); System.out.print("電話:"); String phone = in.nextLine(); System.out.print("郵箱:"); String email = in.nextLine(); // 將上述數據封裝到Customer中 Customer customer = new Customer(name, age, gender, phone, email); boolean isSuccess = customerList.addCustomer(customer); if (isSuccess == true) { System.out.println("---------------添加完成---------------------"); } else { System.out.println("---------------客戶目錄已滿,添加失敗!---------------------"); } } // 修改客戶 private void modifyCustomer() { // System.out.println("修改客戶的操作"); Scanner in = new Scanner(System.in); Customer cust; int num; System.out.print("請選擇待修改客戶的編號(-1退出):"); while (true) { num = in.nextInt(); if (num == -1) { return; } cust = customerList.getCustomer(num - 1); if (cust == null) { System.out.print("無法找到指定客戶,請選擇待修改客戶的編號(-1退出):"); } else { break; } } // 開始修改客戶信息 System.out.print("姓名(" + cust.getName() + "):"); String name = in.nextLine(); System.out.print("性別(" + cust.getGender() + "):"); String gender = in.nextLine(); System.out.print("年齡(" + cust.getAge() + "):"); int age = in.nextInt(); System.out.print("電話(" + cust.getPhone() + "):"); String phone = in.nextLine(); System.out.print("郵箱(" + cust.getEmail() + "):"); String email = in.nextLine(); Customer cust2 = new Customer(name, age, gender, phone, email); boolean isreplace = customerList.replaceCustomer(num - 1, cust2); if (isreplace == true) { System.out.println("---------------修改完成---------------------"); } else { System.out.println("---------------修改失敗---------------------"); } } // 刪除用戶 private void deleteCustomer() { // System.out.println("刪除客戶的操作"); Scanner in = new Scanner(System.in); int num; System.out.println("------------------刪除客戶-----------------------"); while (true) { System.out.println("輸入要刪除的客戶的序號(-1退出):"); num = in.nextInt(); if (num == -1) { return; } Customer customer = customerList.getCustomer(num - 1); if (customer == null) { System.out.println("------------------刪除失敗!-----------------------"); return; } // 執行刪除操作 System.out.print("是否確認刪除(y/n):"); char isdelete = in.nextLine().charAt(0); if (isdelete == 'y') { boolean is = customerList.deleteCustomer(num - 1); if(is) { System.out.println("------------------刪除成功-----------------------"); }else { System.out.println("------------------刪除失敗-----------------------"); } }else { break; } } } // 列出所有客戶信息 private void listAllCustomers() { System.out.println("---------------客戶列表--------------------"); int total = CustomerList.getTotal(); if (total == 0) { System.out.println("沒有客戶記錄!"); } else { System.out.println("編號\t姓名\t性別\t電話\t\t郵箱\t"); Customer custs[] = CustomerList.getallCustomer(); for (int i = 0; i < custs.length; i++) { Customer cust = custs[i]; System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + cust.getPhone() + "\t" + cust.getEmail()); } } System.out.println("---------------客戶列表加載完成--------------------"); } // 主函數 public static void main(String[] args) { CustomerView view = new CustomerView(); Scanner in = new Scanner(System.in); boolean isFlag = true; while (isFlag) { view.enterMainMenue(); char menu = in.nextLine().charAt(0); switch (menu) { case '1': view.addNewCustomer(); break; case '2': view.modifyCustomer(); break; case '3': view.deleteCustomer(); break; case '4': view.listAllCustomers(); break; case '5': System.out.print("確認是否退出(y/n):"); char flag = in.nextLine().charAt(0); if (flag == 'y') { isFlag = false; } break; } // isFlag = false; } in.close(); } }
到此,關于“java如何實現簡單的客戶信息管理系統”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。