Java實戰(zhàn)之客戶信息管理系統(tǒng)
一、軟件設計結構
- 對于初學者來說,弄清框架顯得尤為重要
- 首先該軟件有以下三種模塊組成
二、MVC設計模式
模型層:Customer處理數(shù)據(jù)
控制層:CustomerList處理業(yè)務邏輯
視圖層:CustomerView顯示數(shù)據(jù)
以下三點建議結合代碼理解
1.Customer為實體對象,用于封裝客戶信息
2.CustomerList為Customer對象的管理模塊,內部用數(shù)組管理一組Customer對象,并提供相應的添加、修改、刪除和遍歷的方法,供CustomerView調用
3.CustomerView為主模塊,負責菜單的顯示和處理用戶操作
四個類都在同一包下
三、Customer
package org.atjinzhao.customer; public class Customer { private String name;//姓名 private char gender;//性別 private int age;//年齡 private String phone;//電話 private 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; } 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; } }
四、CustomerList
package org.atjinzhao.customer; public class CustomerList { private Customer[] customers;//客戶列表 private int total = 0;//記錄已保存顧客數(shù)量 //構造器 public CustomerList(int totalCustomer) { customers = new Customer[totalCustomer]; } //方法 /** * 添加客戶 * return:true添加成功,false:添加失敗 */ public boolean addCustomer(Customer customer){ if (total < customers.length) { customers[total] = customer; total++; return true; }else return false; } /** * 修改指定索引位置上的客戶信息 * @param index * @param cust * @return true:修改成功 false修改失敗 */ public boolean replaceCustomer(int index,Customer cust){ if (index < 0 || index >= total) { return false; }else{ customers[index] = cust; return true; } } /** * 刪除指定索引位置上的客戶 * @param index * @return true刪除成功 false刪除失敗 */ public boolean deleteCustomer(int index){ if (index >= 0 && index < total) { for (int i = index; i < total - 1; i++) { customers[i] = customers[i+1]; } customers[--total] = null; return true; } return false; } /** * 獲取所有客戶信息 * @return 數(shù)組 */ public Customer[] getAllCustomers(){ //null的部分不返回 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; } /** * 獲取存儲客戶的數(shù)量 */ public int getTotal() { return total; } /** * 獲取最大能儲存客戶的數(shù)量 */ public int getCustomer(){ return customers.length; } }
五、CustomerView
package org.atjinzhao.customer; public class CustomerView { private CustomerList customerList = new CustomerList(10); public CustomerView() { Customer cust = new Customer("李明",'男',19,"12349982563","lm@gmail.com"); customerList.addCustomer(cust); } public void enterMainMenu(){ //顯示主頁面 boolean isFlag = true; while(isFlag){ 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): "); char selection = CMUtility.readMenuSelection(); switch (selection) { case '1': addNewCustomer(); break; case '2': modifyCustomer(); break; case '3': deleteCustomer(); break; case '4': listAllCustomers(); break; case '5': System.out.print("是否確認退出(Y/N):"); char isExit = CMUtility.readConfirmSelection(); if (isExit == 'Y') { isFlag = false; } } } } /** * 添加客戶 */ public void addNewCustomer(){ System.out.println("-----------------添加客戶-----------------"); System.out.print("姓名:"); String name = CMUtility.readString(10); System.out.print("性別:"); char gender = CMUtility.readChar(); System.out.print("年齡:"); int age = CMUtility.readInt(); System.out.print("電話:"); String phone = CMUtility.readString(13); System.out.print("郵箱:"); String email = CMUtility.readString(30); Customer customer = new Customer(name,gender,age,phone,email); boolean isSuccess = customerList.addCustomer(customer); if(isSuccess){ System.out.println("-----------------添加成功-----------------"); }else{ System.out.println("---------------目錄已滿,添加失敗---------------"); } } /** * 修改客戶 */ public void modifyCustomer(){ System.out.println("-----------------修改客戶-----------------"); Customer cust; int num; for (;;) { System.out.print("請輸入要修改的客戶序號(輸入-1退出):"); num = CMUtility.readInt(); if (num == -1) { return; } cust = customerList.getCustomer(num - 1); if (cust == null) { System.out.println("無法找到指定客戶!"); } else { break; } } System.out.println("姓名("+cust.getName()+"):"); String name = CMUtility.readString(10, cust.getName()); System.out.println("性別("+cust.getGender()+"):"); char gender = CMUtility.readChar( cust.getGender()); System.out.println("年齡("+cust.getAge()+"):"); int age = CMUtility.readInt(cust.getAge()); System.out.println("電話("+cust.getPhone()+"):"); String tel = CMUtility.readString(11, cust.getPhone()); System.out.println("郵箱("+cust.getEmail()+"):"); String email = CMUtility.readString(15, cust.getEmail()); Customer newCust = new Customer(name,gender,age,tel,email); boolean isReplaced = customerList.replaceCustomer(num - 1, newCust); if (isReplaced) { System.out.println("-----------------修 改 成 功-----------------\t"); } else { System.out.println("-----------------修 改 失 敗-----------------\t"); } } /** * 刪除客戶 */ public void deleteCustomer(){ System.out.println("-----------------刪除客戶-----------------"); Customer cust; int index; for (;;) { System.out.print("請輸入要刪除的客戶序號(輸入-1退出):"); index = CMUtility.readInt(); if (index == -1) { return; } cust = customerList.getCustomer(index - 1); if (cust == null) { System.out.println("無法找到客戶!"); } else { break; } } System.out.print("是否確認刪除(Y/N):"); char isDelete = CMUtility.readConfirmSelection(); if (isDelete == 'Y') { boolean deleteSuccess = customerList.deleteCustomer(index - 1); if (deleteSuccess) { System.out.println("-----------------刪除成功-----------------"); } else { System.out.println("-----------------刪除失敗-----------------"); } }else{ return; } } /** * 顯示客戶列表的操作 */ public void listAllCustomers(){ System.out.println("-------------------客 戶 列 表------------------\n"); int total = customerList.getTotal(); if (total == 0) { System.out.println("沒有客戶記錄!"); }else { System.out.println("編號\t姓名\t性別\t年齡\t電話\t\t郵箱"); Customer[] custList = customerList.getAllCustomers(); 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" + custList[i].getEmail()+"\t"); } } System.out.println("-----------------客戶列表完成-----------------\n"); } public static void main(String[] args) { CustomerView view = new CustomerView(); view.enterMainMenu(); } }
六、CMUtility
package org.atjinzhao.customer; import java.util.*; public class CMUtility { public static void main(String[] args) { //System.out.println(readMenuSelection()); } private static Scanner scanner = new Scanner(System.in); /** * 用于界面菜單的選擇。該方法讀取鍵盤用戶鍵入的‘1'-‘5'的任意字符,方法返回。 * */ public static char readMenuSelection() { // 獲取功能選擇 char c; for (;;) { String str = readKeyBoard(1, false); c = str.charAt(0); if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') { System.out.println("選擇錯誤,請重新輸入:"); } else break; } return c; } /** * 從鍵盤讀取一個字符,并將其作為方法的返回值。 * 獲取性別 */ public static char readChar(){ String str = readKeyBoard(1,false); return str.charAt(0); } /** *從鍵盤讀取一個字符,并將其作為方法的返回值。 *如果用戶不輸入字符而回車,方法將以defaultValue 作為返回值。 * */ public static char readChar(char defaultValue){ String str = readKeyBoard(1,true); return (str.length()==0)? defaultValue : str.charAt(0); } /** * 從鍵盤讀取一個長度不超過2位的整數(shù),并將其作為方法的返回值。 * 獲取年齡 */ public static int readInt(){ int n; for(;;){ String str = readKeyBoard(2,false); try{ n = Integer.parseInt(str); break; }catch (NumberFormatException e) { System.out.print("數(shù)字輸入錯誤,請重新輸入:"); } } return n; } /** *從鍵盤讀取一個字符,并將其作為方法的返回值。 *如果用戶不輸入字符而回車,方法將以defaultValue 作為返回值。 */ 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ù)字輸入錯誤,請重新輸入:"); } } return n; } /** * 從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。 */ public static String readString(int limit){ return readKeyBoard(limit,false); } /** * 從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。 * 如果用戶不輸入字符而直接回車,方法將以defaultVaue作為返回值。 */ public static String readString(int limit,String defaultValue){ //修改姓名、電話、郵箱時,不輸入信息直接回車 String str = readKeyBoard(limit,true); return str.equals("") ? defaultValue : str; } /** * 用于確認選擇的輸入。該方法從鍵盤讀取‘Y'或‘N',并將其作為方法的返回值。 */ public static char readConfirmSelection(){ //獲取確認的輸入 char c; for( ; ; ){ String str = readKeyBoard(1,false).toUpperCase(); c = str.charAt(0); if (c=='Y' || c=='N') { break; } else { System.out.print("選擇錯誤,請重新輸入: "); } } return c; } private static String readKeyBoard(int limit,boolean blankReturn){ String line = ""; while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.length() == 0) { if (blankReturn) return line; else continue; } if (line.length() < 1 || line.length() > limit){ System.out.println("輸入長度(不大于" + limit + ")錯誤,請重新輸入“"); continue; } break; } return line; } }
到此這篇關于Java實戰(zhàn)之客戶信息管理系統(tǒng)的文章就介紹到這了,更多相關Java客戶信息管理系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決ResourceBundle.getBundle文件路徑問題
這篇文章主要介紹了解決ResourceBundle.getBundle文件路徑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Spring?@DateTimeFormat日期格式化時注解場景分析
這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時注解場景分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05Triple協(xié)議支持Java異常回傳設計實現(xiàn)詳解
這篇文章主要為大家介紹了Triple協(xié)議支持Java異?;貍髟O計實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12詳解Java如何在CompletableFuture中實現(xiàn)日志記錄
這篇文章主要為大家詳細介紹了一種slf4j自帶的MDC類,來記錄完整的請求日志,和在CompletableFuture異步線程中如何保留鏈路id,需要的可以參考一下2023-04-04