- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java實(shí)戰之客戶(hù)信息管理系統
模型層:Customer處理數據
控制層:CustomerList處理業(yè)務(wù)邏輯
視圖層:CustomerView顯示數據
以下三點(diǎn)建議結合代碼理解
1.Customer為實(shí)體對象,用于封裝客戶(hù)信息
2.CustomerList為Customer對象的管理模塊,內部用數組管理一組Customer對象,并提供相應的添加、修改、刪除和遍歷的方法,供CustomerView調用
3.CustomerView為主模塊,負責菜單的顯示和處理用戶(hù)操作
四個(gè)類(lèi)都在同一包下
package org.atjinzhao.customer; public class Customer { private String name;//姓名 private char gender;//性別 private int age;//年齡 private String phone;//電話(huà) 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; } }
package org.atjinzhao.customer; public class CustomerList { private Customer[] customers;//客戶(hù)列表 private int total = 0;//記錄已保存顧客數量 //構造器 public CustomerList(int totalCustomer) { customers = new Customer[totalCustomer]; } //方法 /** * 添加客戶(hù) * return:true添加成功,false:添加失敗 */ public boolean addCustomer(Customer customer){ if (total < customers.length) { customers[total] = customer; total++; return true; }else return false; } /** * 修改指定索引位置上的客戶(hù)信息 * @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; } } /** * 刪除指定索引位置上的客戶(hù) * @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; } /** * 獲取所有客戶(hù)信息 * @return 數組 */ public Customer[] getAllCustomers(){ //null的部分不返回 Customer[] custs = new Customer[total]; for (int i = 0; i < total; i++) { custs[i] = customers[i]; } return custs; } /** * 獲取指定索引位置上的客戶(hù) */ public Customer getCustomer(int index){ if (index >= 0 && index < total) { return customers[index]; }else return null; } /** * 獲取存儲客戶(hù)的數量 */ public int getTotal() { return total; } /** * 獲取最大能儲存客戶(hù)的數量 */ public int getCustomer(){ return customers.length; } }
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(){ //顯示主頁(yè)面 boolean isFlag = true; while(isFlag){ System.out.println("-----------------客戶(hù)信息管理軟件-----------------"); System.out.println(" 1 添加客戶(hù)"); System.out.println(" 2 修改客戶(hù) "); System.out.println(" 3 刪除客戶(hù) "); System.out.println(" 4 客戶(hù)列表 "); 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; } } } } /** * 添加客戶(hù) */ public void addNewCustomer(){ System.out.println("-----------------添加客戶(hù)-----------------"); 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("電話(huà):"); 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("---------------目錄已滿(mǎn),添加失敗---------------"); } } /** * 修改客戶(hù) */ public void modifyCustomer(){ System.out.println("-----------------修改客戶(hù)-----------------"); Customer cust; int num; for (;;) { System.out.print("請輸入要修改的客戶(hù)序號(輸入-1退出):"); num = CMUtility.readInt(); if (num == -1) { return; } cust = customerList.getCustomer(num - 1); if (cust == null) { System.out.println("無(wú)法找到指定客戶(hù)!"); } 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("電話(huà)("+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"); } } /** * 刪除客戶(hù) */ public void deleteCustomer(){ System.out.println("-----------------刪除客戶(hù)-----------------"); Customer cust; int index; for (;;) { System.out.print("請輸入要刪除的客戶(hù)序號(輸入-1退出):"); index = CMUtility.readInt(); if (index == -1) { return; } cust = customerList.getCustomer(index - 1); if (cust == null) { System.out.println("無(wú)法找到客戶(hù)!"); } 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; } } /** * 顯示客戶(hù)列表的操作 */ public void listAllCustomers(){ System.out.println("-------------------客 戶(hù) 列 表------------------\n"); int total = customerList.getTotal(); if (total == 0) { System.out.println("沒(méi)有客戶(hù)記錄!"); }else { System.out.println("編號\t姓名\t性別\t年齡\t電話(huà)\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("-----------------客戶(hù)列表完成-----------------\n"); } public static void main(String[] args) { CustomerView view = new CustomerView(); view.enterMainMenu(); } }
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); /** * 用于界面菜單的選擇。該方法讀取鍵盤(pán)用戶(hù)鍵入的‘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; } /** * 從鍵盤(pán)讀取一個(gè)字符,并將其作為方法的返回值。 * 獲取性別 */ public static char readChar(){ String str = readKeyBoard(1,false); return str.charAt(0); } /** *從鍵盤(pán)讀取一個(gè)字符,并將其作為方法的返回值。 *如果用戶(hù)不輸入字符而回車(chē),方法將以defaultValue 作為返回值。 * */ public static char readChar(char defaultValue){ String str = readKeyBoard(1,true); return (str.length()==0)? defaultValue : str.charAt(0); } /** * 從鍵盤(pán)讀取一個(gè)長(cháng)度不超過(guò)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; } /** *從鍵盤(pán)讀取一個(gè)字符,并將其作為方法的返回值。 *如果用戶(hù)不輸入字符而回車(chē),方法將以defaultValue 作為返回值。 */ public static int readInt(int defaultValue){ //修改年齡信息時(shí),不輸入信息直接回車(chē) 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; } /** * 從鍵盤(pán)讀取一個(gè)長(cháng)度不超過(guò)limit的字符串,并將其作為方法的返回值。 */ public static String readString(int limit){ return readKeyBoard(limit,false); } /** * 從鍵盤(pán)讀取一個(gè)長(cháng)度不超過(guò)limit的字符串,并將其作為方法的返回值。 * 如果用戶(hù)不輸入字符而直接回車(chē),方法將以defaultVaue作為返回值。 */ public static String readString(int limit,String defaultValue){ //修改姓名、電話(huà)、郵箱時(shí),不輸入信息直接回車(chē) String str = readKeyBoard(limit,true); return str.equals("") ? defaultValue : str; } /** * 用于確認選擇的輸入。該方法從鍵盤(pán)讀取‘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("輸入長(cháng)度(不大于" + limit + ")錯誤,請重新輸入“"); continue; } break; } return line; } }
到此這篇關(guān)于Java實(shí)戰之客戶(hù)信息管理系統的文章就介紹到這了,更多相關(guān)Java客戶(hù)信息管理系統內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站