java實現簡單的客戶信息管理系統
本文實例為大傢分享瞭java實現簡單客戶信息管理系統的具體代碼,供大傢參考,具體內容如下
全篇文章開源,源碼供讀者使用。這是一篇關於java的客戶信息管理系統的文章,裡面簡單實現瞭數據庫管理系統的基本功能,可以算是算筆者的學習筆記,也為大傢學習提供便利。所以代碼都是在一個包下完成的,所以沒有使用導包的操作,省去瞭外賣project的申明,剩下的就寫的文章裡瞭。話不多說,看文章吧。
首先給大傢看一下總的操作界面(別看簡單,裡面的還是有東西的),後面就附上實現源碼、要求和註釋
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", "[email protected]"); 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(); } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。