工具類 CMUtility
1 package day1_18; 2 3 import java.util.Scanner; 4 5 public class CMUtility { 6 7 //讀取主菜單選項 8 public static char readMenuSelection() { 9 for (; ; ) { 10 String str = readKeyBoard(1, false); 11 char c = str.charAt(0); 12 if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') { 13 System.out.println("輸入選項錯誤,請從新輸入:"); 14 } else { 15 return c; 16 } 17 } 18 } 19 20 // 讀取字符串 21 public static String readString(int limit) { 22 return readKeyBoard(limit, false); 23 } 24 25 //讀取字符串(直接Enter換行的話,就返回默認值defaultValue) 26 public static String readString(int limit,String defaultValue) { 27 String str = readKeyBoard(limit, true); 28 return str.equals("") ? defaultValue : str; 29 } 30 31 32 //讀取整數 33 public static int readInt() { 34 for (; ; ) { 35 String str = readKeyBoard(2, false); 36 try { 37 int n = Integer.parseInt(str); 38 return n; 39 } catch (Exception e) { 40 System.out.print("請輸入不超過兩位數的數字:"); 41 } 42 } 43 } 44 45 //讀取整數(直接Enter換行的話,就返回默認值defaultValue) 46 public static int readInt(int defaultValue) { 47 String str = readKeyBoard(2, true); 48 if (str.equals("")) { 49 return defaultValue; 50 } 51 try { 52 int n = Integer.parseInt(str); 53 return n; 54 } catch (Exception e) { 55 System.out.print("請輸入不超過兩位數的數字:"); 56 } 57 return 111111111; 58 } 59 60 //讀取字符 61 public static char readChar() { 62 String str = readKeyBoard(1, false); 63 return str.charAt(0); 64 } 65 66 67 //讀取字符(直接Enter換行的話,就返回默認值defaultValue) 68 public static char readChar(char defaultValue) { 69 String str = readKeyBoard(1, true); 70 return str.equals("") ? defaultValue : str.charAt(0); 71 } 72 73 //是否確認退出選項 74 public static char readConfirmSelection() { 75 char selection; 76 for (; ; ) { 77 String str = readKeyBoard(1, false); 78 selection = str.toUpperCase().charAt(0); 79 if (selection != 'Y' && selection != 'N') { 80 System.out.println("輸入選項錯誤,請從新輸入:"); 81 } else { 82 break; 83 } 84 } 85 return selection; 86 } 87 88 89 //讀取用戶輸入 90 private static String readKeyBoard(int limit, boolean blankEnter) { 91 92 String str = ""; 93 for (; ; ) { 94 Scanner scanner = new Scanner(System.in); 95 if (scanner.hasNextLine()) { 96 str = scanner.nextLine().trim(); 97 if (str.length() == 0) { 98 if (blankEnter) { 99 return str; 100 } 101 System.out.print("請從新輸入:"); 102 }else if (str.length() > limit) { 103 System.out.print("輸入長度不能大於" + limit + ",請從新輸入:"); 104 } else { 105 break; 106 } 107 } 108 } 109 return str; 110 } 111 112 }
客戶實體類 Customer
1 package day1_18; 2 3 public class Customer { 4 private String name; 5 private char gender; 6 private int age; 7 private String phone; 8 private String email; 9 10 public Customer() { 11 } 12 13 public Customer(String name, char gender, int age, String phone, String email) { 14 this.name = name; 15 this.gender = gender; 16 this.age = age; 17 this.phone = phone; 18 this.email = email; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public char getGender() { 30 return gender; 31 } 32 33 public void setGender(char gender) { 34 this.gender = gender; 35 } 36 37 public int getAge() { 38 return age; 39 } 40 41 public void setAge(int age) { 42 this.age = age; 43 } 44 45 public String getPhone() { 46 return phone; 47 } 48 49 public void setPhone(String phone) { 50 this.phone = phone; 51 } 52 53 public String getEmail() { 54 return email; 55 } 56 57 public void setEmail(String email) { 58 this.email = email; 59 } 60 }
操做客戶類 CustomerList
1 package day1_18; 2 3 public class CustomerList { 4 private Customer[] customers; 5 private int total; 6 7 public CustomerList(int totalCustomer) { 8 customers = new Customer[totalCustomer]; 9 } 10 11 //添加客戶 12 public boolean addCustomer(Customer customer) { 13 if (total >= customers.length) { 14 return false; 15 } 16 customers[total++] = customer; 17 return true; 18 } 19 20 //查詢客戶列表 21 public boolean listCustomers() { 22 if (total == 0) { 23 return false; 24 } 25 System.out.print("編號\t姓名\t性別\t年齡\t手機號碼\t電子郵箱\n"); 26 for (int i=0;i< total;i++) { 27 System.out.print((i+1) + "\t\t" + customers[i].getName() + "\t" + customers[i].getGender() + 28 "\t\t" + customers[i].getAge() + "\t\t" + customers[i].getPhone() + 29 "\t\t" + customers[i].getEmail() + "\n"); 30 } 31 return true; 32 } 33 34 35 //修改指定位置的客戶信息 36 public boolean replaceCustomer(int index,Customer customer) { 37 if (index < 0 || index >= total) { 38 return false; 39 } 40 customers[index] = customer; 41 return true; 42 } 43 44 //查找指定位置的客戶 45 public Customer getCustomer(int index){ 46 if (index < 0 || index >= total) { 47 return null; 48 } else { 49 return customers[index]; 50 } 51 } 52 53 //刪除指定位置的客戶 54 public boolean deleteCustomer(int index) { 55 if (index < 0 || index >= total) { 56 return false; 57 } 58 for (int i = index; i < total; i++) { 59 customers[i] = customers[i + 1]; 60 } 61 total--; 62 return true; 63 } 64 }
客戶端交互類 CustomerView
1 package day1_18; 2 3 public class CustomerView { 4 public CustomerList customerList; 5 6 public static void main(String[] args) { 7 CustomerView view = new CustomerView(); 8 view.enterMainMenu(); 9 } 10 11 public CustomerView() { 12 customerList = new CustomerList(10); 13 } 14 15 //主菜單 16 public void enterMainMenu() { 17 boolean isFlag = true; 18 while (isFlag) { 19 System.out.println("\n-----------------客戶信息管理軟件-----------------\n"); 20 System.out.println(" 1 添 加 客 戶"); 21 System.out.println(" 2 修 改 客 戶"); 22 System.out.println(" 3 刪 除 客 戶"); 23 System.out.println(" 4 客 戶 列 表"); 24 System.out.println(" 5 退 出\n"); 25 System.out.print(" 請選擇(1-5):"); 26 char menu = CMUtility.readMenuSelection(); 27 switch (menu) { 28 case '1': 29 //System.out.println("添加客戶操做"); 30 saveCustomer(); 31 break; 32 case '2': 33 //System.out.println("修改客戶操做"); 34 replaceCustomer(); 35 break; 36 case '3': 37 //System.out.println("刪除客戶操做"); 38 removeCustomer(); 39 break; 40 case '4': 41 //System.out.println("客戶列表操做"); 42 listCustomers(); 43 break; 44 case '5': 45 //System.out.println("退出操做"); 46 System.out.print("確認是否退出(Y/N):"); 47 char isExit = CMUtility.readConfirmSelection(); 48 if (isExit == 'Y') { 49 isFlag = false; 50 } 51 } 52 } 53 } 54 55 //添加客戶 56 public void saveCustomer() { 57 System.out.println("----------添加客戶----------"); 58 System.out.print("姓名:"); 59 String name = CMUtility.readString(4); 60 System.out.print("性別:"); 61 char gender = CMUtility.readChar(); 62 System.out.print("年齡:"); 63 int age = CMUtility.readInt(); 64 System.out.print("手機號碼:"); 65 String phone = CMUtility.readString(13); 66 System.out.print("電子郵箱:"); 67 String email = CMUtility.readString(20); 68 Customer customer = new Customer(name, gender, age, phone, email); 69 boolean isSaved = customerList.addCustomer(customer); 70 if (isSaved) { 71 System.out.println("----------添加客戶成功----------"); 72 } else { 73 System.out.println("----------添加客戶成功----------"); 74 } 75 } 76 77 //查看客戶列表 78 public boolean listCustomers() { 79 System.out.println("----------客戶列表----------"); 80 boolean isListed = customerList.listCustomers(); 81 if (!isListed) { 82 System.out.println("客戶信息爲空"); 83 } 84 System.out.println("----------客戶列表完成----------"); 85 return isListed; 86 } 87 88 //修改指定位置的客戶信息 89 public void replaceCustomer() { 90 int index; 91 Customer customer; 92 if (listCustomers()) { 93 System.out.print("請輸入要修改客戶的編號:"); 94 for (; ; ) { 95 index = CMUtility.readInt() -1; 96 customer = customerList.getCustomer(index); 97 if (customer == null) { 98 System.out.print("抱歉,沒有這個客戶,請從新輸入:"); 99 } else { 100 break; 101 } 102 } 103 System.out.print("姓名(" + customer.getName() + "):"); 104 String name = CMUtility.readString(4, customer.getName()); 105 System.out.print("性別(" + customer.getGender() + "):"); 106 char gender = CMUtility.readChar(customer.getGender()); 107 System.out.print("年齡(" + customer.getAge() + "):"); 108 int age = CMUtility.readInt(customer.getAge()); 109 System.out.print("電話(" + customer.getPhone() + "):"); 110 String phone = CMUtility.readString(13, customer.getPhone()); 111 System.out.print("郵箱(" + customer.getEmail() + "):"); 112 String email = CMUtility.readString(20, customer.getEmail()); 113 114 customer = new Customer(name, gender, age, phone, email); 115 boolean isReplaced = customerList.replaceCustomer(index, customer); 116 if (isReplaced) { 117 System.out.println("-----------修改客戶成功-----------"); 118 } else { 119 System.out.println("-----------修改客戶失敗-----------"); 120 } 121 } else { 122 System.out.print("\n舒適提示:客戶列表爲空,請先添加客戶\n"); 123 enterMainMenu(); 124 } 125 } 126 127 //刪除客戶 128 public void removeCustomer() { 129 if (listCustomers()) { 130 System.out.print("請輸入要刪除客戶的編號:"); 131 for (; ; ) { 132 int index = CMUtility.readInt() -1; 133 boolean isRemove = customerList.deleteCustomer(index); 134 if (isRemove) { 135 System.out.println("----------刪除成功----------"); 136 break; 137 } else { 138 System.out.print("您將刪除的客戶不存在,請從新選擇編號:"); 139 } 140 } 141 } else { 142 System.out.print("\n舒適提示:客戶列表爲空,請選擇其餘操做\n"); 143 enterMainMenu(); 144 } 145 } 146 }
主菜單效果
筆記java
關於客戶管理軟件項目編程思路的梳理 1.主菜單列表選項 1.1 添加客戶 ①獲取鍵盤輸入,根據屬性的類型不一樣和字符長度的限制,在工具類裏編寫 相應的獲取鍵盤輸入的方法 ②客戶端輸入數據賦值給每一個屬性後,利用構造器建立Customer對象,再添 加到customers數組中,而且數組中實際存儲的Customer對象的個數total加1 1.2 查看客戶列表 ①遍歷customers數組中每一個customer對象,獲取每一個屬性的值 1.3 修改客戶列表中某個編號的客戶信息 ①編號是指在客戶列表中例如1,2,3,...的一系列天然數,注意它與數組索引的關係:編號-1 = 數組索引 ②客戶端鍵盤輸入接收到指定編號後,首先須要判斷這個編號在客戶列表中是否存在,即以(指定編號-1) 做爲數組索引,查看數組中這個索引的對象是否爲空,若是爲空,說明用戶列表中是沒有這個用戶的,既然 沒有這個用戶那麼修改客戶也是不合邏輯的 ③若是客戶列表中存在須要修改的客戶,那麼接收鍵盤輸入做爲屬性值的方法就須要根據狀況來從新定義了, 由於若是客戶的某個屬性不想修改,那麼就設計爲輸入時直接Enter,表示當接收空字符串時,新customer 對象的屬性值仍是爲原來的屬性值 1.4 刪除客戶列表中某個編號的客戶信息 ①編號是指在客戶列表中例如1,2,3,...的一系列天然數,注意它與數組索引的關係:編號-1 = 數組索引 ②客戶端鍵盤輸入接收到指定編號後,首先須要判斷這個編號在客戶列表中是否存在,即以(指定編號-1) 做爲數組索引,查看數組中這個索引的對象是否爲空,若是爲空,說明用戶列表中是沒有這個用戶的,既然 沒有這個用戶那麼刪除客戶也是不合邏輯的 ③若是客戶列表中存在須要刪除的客戶,從須要刪除的這個客戶在數組customers的索引位置開始,後面的元 素覆蓋前面前一個元素,這樣實現刪除操做 1.5 退出主菜單 獲取鍵盤輸入,取字符,轉爲大寫字母后若是爲‘Y’,則退出;若是爲‘N’則不退出