模擬實現基於文本界面的《客戶信息管理軟件》。java
該軟件可以實現對客戶對象的插入、修改和刪除(用數組實現),並可以打印客戶明細表。數組
主菜單界面工具
-----------------客戶信息管理軟件----------------- 1 添 加 客 戶 2 修 改 客 戶 3 刪 除 客 戶 4 客 戶 列 表 5 退 出 請選擇(1-5):_
添加客戶界面oop
請選擇(1-5):1 ---------------------添加客戶--------------------- 姓名:佟剛 性別:男 年齡:35 電話:010-56253825 郵箱:tongtong@atguigu.com ---------------------添加完成---------------------
修改客戶界面ui
請選擇(1-5):2 ---------------------修改客戶--------------------- 請選擇待修改客戶編號(-1退出):1 姓名(佟剛):<直接回車表示不修改> 性別(男): 年齡(35): 電話(010-56253825): 郵箱(tongtong@163.com):tongg@123.com ---------------------修改完成---------------------
刪除客戶界面this
請選擇(1-5):3 ---------------------刪除客戶--------------------- 請選擇待刪除客戶編號(-1退出):1 確認是否刪除(Y/N):y ---------------------刪除完成---------------------
客戶列表界面spa
請選擇(1-5):4 ---------------------------客戶列表--------------------------- 編號 姓名 性別 年齡 電話 郵箱 1 佟剛 男 45 010-56253825 tong@abc.com 2 封捷 女 36 010-56253825 fengjie@ibm.com 3 雷豐陽 男 32 010-56253825 leify@163.com -------------------------客戶列表完成-------------------------
軟件由如下模塊組成設計
軟件內存調用簡圖code
enterMainMenu方法的調用圖orm
項目採用MVC設計結構,對項目的包結構進行分層管理
bean包下存放Customer類
存放客戶對象,設置客戶屬性實現get set 方法,提供構造器
service包存放CustomerList類
用於實現增刪改查等功能
util包下存放通用抽取類CMUtility
用於實現鍵盤讀入操做
view包下存放界面顯示類CustomerView
用於在操做臺顯示界面進行交互操做
設計通用的鍵盤讀取方法,將全部的鍵盤讀取操做進行封裝
package com.bruce.project.project02.util; import java.util.Scanner; /** * CMUtility工具類: 將不一樣的功能封裝爲方法,就是能夠直接經過調用方法使用它的功能,而無需考慮具體的功能實現細節。 */ public class CMUtility { 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.print("選擇錯誤,請從新輸入:"); } 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位的整數,並將其做爲方法的返回值。 */ public static int readInt() { int n; for (;;) { String str = readKeyBoard(2, false); try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("數字輸入錯誤,請從新輸入:"); } } return n; } /** * 從鍵盤讀取一個長度不超過2位的整數,並將其做爲方法的返回值。 若是用戶不輸入字符而直接回車,方法將以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("數字輸入錯誤,請從新輸入:"); } } return n; } /** * 從鍵盤讀取一個長度不超過limit的字符串,並將其做爲方法的返回值。 */ public static String readString(int limit) { return readKeyBoard(limit, false); } /** * 從鍵盤讀取一個長度不超過limit的字符串,並將其做爲方法的返回值。 若是用戶不輸入字符而直接回車,方法將以defaultValue 做爲返回值。 */ 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.print("輸入長度(不大於" + limit + ")錯誤,請從新輸入:"); continue; } break; } return line; } }
對用戶的屬性進行封裝,提供空參數構造器和帶有全部參數的構造器
package com.bruce.project.project02.bean; 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; } /** * * @Description 顯示用戶信息的方法 * @author Bruce * @return String */ public String getDetails() { return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t" + email; } }
設計項目中客戶的添加、刪除、修改、顯示等功能
package com.bruce.project.project02.service; import com.bruce.project.project02.bean.Customer; public class CustomerList { private Customer[] customers; private int total = 0; /** * 構造器,用來初始化customers數組 * * @param totalCustomer */ public CustomerList(int totalCustomer) { customers = new Customer[totalCustomer]; } /** * * @Description 添加客戶的方法 * @author Bruce * @param customer * @return boolean 添加成功返回true;false表示數組已滿,沒法添加 */ public boolean addCustomer(Customer customer) { if (total >= customers.length) { return false; } customers[total++] = customer; return true; } /** * * @Description 修改客戶信息的方法 * @author Bruce * @param index * 指定所替換對象在數組中的位置,從0開始 * @param cust * @return boolean 替換成功返回true;false表示索引無效,沒法替換 * */ public boolean repalceCustomer(int index, Customer cust) { if (index < 0 || index >= total) { return false; } customers[index] = cust; return true; } /** * * @Description 刪除客戶信息的方法 * @author Bruce * @param index 指定所刪除對象在數組中的索引位置,從0開始 * @return boolean 刪除成功返回true;false表示索引無效,沒法刪除 */ public boolean deleteCustomer(int index) { if (index < 0 || index >= total) { return false; } for (int i = 0; i < total - 1; i++) { customers[i] = customers[i + 1]; } customers[--total] = null; return true; } /** * * @Description 獲取所有客戶信息 * @author Bruce * @return Customer[]數組中包含了當前全部客戶對象,該數組長度與對象個數相同。 */ public Customer[] getAllCustomers() { Customer[] custs = new Customer[total]; for (int i = 0; i < total; i++) { custs[i] = customers[i]; } return custs; } /** * * @Description TODO * @author Bruce * @param index * @return 封裝了客戶信息的Customer對象 */ public Customer getCustomer(int index) { if (index < 0 || index >= total) { return null; } return customers[index]; } public int getTotal() { return total; } }
在控制檯顯示的主要界面
package com.bruce.project.project02.view; import com.bruce.project.project02.bean.Customer; import com.bruce.project.project02.service.CustomerList; import com.bruce.project.project02.util.CMUtility; public class CustomerView { private CustomerList customers = new CustomerList(10); // 初始創建一個用戶 public CustomerView() { Customer cust = new Customer("張三", '男', 30, "1151511215", "abs@163.com"); customers.addCustomer(cust); } /** * * @Description 軟件主界面,顯示功能 * @author Bruce void */ public void enterMainMenu() { boolean loopFlag = true; do { System.out.println("\n-----------------客戶信息管理軟件-----------------\n"); System.out.println(" 1 添 加 客 戶"); System.out.println(" 2 修 改 客 戶"); System.out.println(" 3 刪 除 客 戶"); System.out.println(" 4 客 戶 列 表"); System.out.println(" 5 退 出\n"); System.out.print(" 請選擇(1-5):"); char key = CMUtility.readMenuSelection(); System.out.println(); switch (key) { case '1': addNewCustomer(); break; case '2': modifyCustomer(); break; case '3': deleteCustomer(); break; case '4': listAllCustomer(); break; case '5': System.out.println("是否退出(Y/N):"); char yn = CMUtility.readConfirmSelection(); if (yn == 'Y') { loopFlag = false; } break; default: break; } } while (loopFlag); } /** * * @Description 顯示添加用戶界面 * @author Bruce void */ private void addNewCustomer() { System.out.println("---------------------添加客戶---------------------"); System.out.print("姓名:"); String name = CMUtility.readString(4); System.out.println("性別"); char gender = CMUtility.readChar(); System.out.println("年齡"); int age = CMUtility.readInt(); System.out.println("電話"); String phone = CMUtility.readString(15); System.out.println("郵箱"); String email = CMUtility.readString(15); Customer cust = new Customer(name, gender, age, phone, email); boolean flag = customers.addCustomer(cust); if (flag) { System.out.println("---------------------添加完成---------------------"); } else { System.out.println("----------------記錄已滿,沒法添加-----------------"); } } /** * * @Description 顯示修改用戶信息界面 * @author Bruce void */ private void modifyCustomer() { System.out.println("---------------------修改客戶---------------------"); int index = 0; Customer cust = null; for (;;) { System.out.print("請選擇待修改客戶編號(-1退出):"); index = CMUtility.readInt(); if (index == -1) { return; } cust = customers.getCustomer(index - 1); if (cust == null) { System.out.println("沒法找到該用戶"); } else break; } System.out.println("姓名(" + cust.getName() + "):"); String name = CMUtility.readString(4, 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 phone = CMUtility.readString(15, cust.getPhone()); System.out.println("郵箱(" + cust.getEmail() + "):"); String email = CMUtility.readString(15, cust.getEmail()); cust = new Customer(name, gender, age, phone, email); boolean flag = customers.repalceCustomer(index - 1, cust); if (flag) { System.out.println("---------------------修改完成---------------------"); } else { System.out.println("----------沒法找到指定客戶,修改失敗--------------"); } } /** * * @Description 刪除用戶界面 * @author Bruce void */ private void deleteCustomer() { System.out.println("---------------------刪除客戶---------------------"); int index = 0; Customer cust = null; for (;;) { System.out.println("選擇待刪除客戶的編號(-1退出)"); index = CMUtility.readInt(); if (index == -1) { return; } cust = customers.getCustomer(index - 1); if (cust == null) { System.out.println("沒法找到指定客戶"); } else break; } // 跳出循環是否刪除 System.out.println("是否刪除客戶(Y/N)"); char yn = CMUtility.readChar(); if (yn == 'N') return; boolean flag = customers.deleteCustomer(index - 1); if (flag) { System.out.println("---------------------刪除完成---------------------"); } else { System.out.println("----------沒法找到指定客戶,刪除失敗--------------"); } } /** * * @Description 列出客戶列表 * @author Bruce void */ private void listAllCustomer() { System.out.println("---------------------------客戶列表---------------------------"); Customer[] custs = customers.getAllCustomers(); if (custs.length == 0) { System.out.println("沒有客戶編號!"); } else { System.out.println("編號\t姓名\t性別\t年齡\t\t電話\t\t郵箱"); for (int i = 0; i < custs.length; i++) { System.out.println((i + 1) + "\t" + custs[i].getDetails()); } } System.out.println("-------------------------客戶列表完成-------------------------"); } }
軟件調用類CustomerTest類設計
package com.bruce.project.project02; import com.bruce.project.project02.view.CustomerView; public class CustomerTest { public static void main(String[] args) { CustomerView cView = new CustomerView(); cView.enterMainMenu(); } }