生活随笔
收集整理的這篇文章主要介紹了
客户信息管理系统——Java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
客戶信息管理系統——Java
該系統沒有涉及文件及數據庫,適合剛接觸java的新手進行練習。模擬實現基于文本界面的客戶軟件管理系統。能夠實現基本增刪改查操作以及類的使用
源代碼
package CustomerInfomationManagement;import java.util.Scanner;public class CustomerUtility {private static Scanner scanner
= new Scanner(System.in
);public static char readMenuSelection(){char c
;while (true){String str
= readKeyBoard(1,false);c
= str
.charAt(0);if(c
> '5' || c
< '1'){System.out
.print(" 輸入錯誤,請重新輸入:");}else break;}return c
;}public static char readChar(){String str
= readKeyBoard(1,false);return str
.charAt(0);}public static char readChar(char defaultValue
){String str
= readKeyBoard(1,true);return (str
.length()==0)?defaultValue
:str
.charAt(0);}public static int readInt(){int n
;while (true){String str
= readKeyBoard(2,false);try {n
= Integer.parseInt(str
);if (n
>=0)break;elseSystem.out
.print(" 輸入錯誤,請重新輸入:");}catch (NumberFormatException e
){System.out
.print(" 輸入錯誤,請重新輸入:");}}return n
;}public static int readInt(int defaultValue
){int n
;while (true){String str
= readKeyBoard(2, true);if(str
.length() == 0)return defaultValue
;try {n
= Integer.parseInt(str
);if (n
>=0)break;elseSystem.out
.print(" 輸入錯誤,請重新輸入:");}catch (NumberFormatException e
){System.out
.print(" 輸入錯誤,請重新輸入:");}}return n
;}public static String readString(int limit
){return readKeyBoard(limit
,false);}public static String readString(int limit
,String defaultValue
){String str
= readKeyBoard(limit
,true);return str
.length() == 0 ? defaultValue
:str
;}public static char readConfirmSelection(){char c
;while (true){String str
= readKeyBoard(1,false).toUpperCase();c
= str
.charAt(0);if(c
== 'Y' || c
== 'N')break;System.out
.print(" 輸入錯誤,請重新輸入:");}return c
;}public static String readKeyBoard(int n
, boolean b
){String str
="";while (scanner
.hasNextLine()){str
= scanner
.nextLine();int len
= str
.length();if (len
== 0){if (b
)return str
;continue;}else if(len
>n
){System.out
.print(" 輸入錯誤,請重新輸入:");continue;}break;}return str
;}}
package CustomerInfomationManagement;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
;}
}
package CustomerInfomationManagement;public class CustomerList {private Customer[] customers
; private int total
; private int maxTotal
; public CustomerList(int totalCustomer
) {this.customers
= new Customer[totalCustomer
];this.maxTotal
= totalCustomer
;this.total
= 0;}public boolean addCustomer(Customer customer
){if(total
>=maxTotal
)return false;customers
[total
++] = customer
;return true;}public boolean replaceCustomer(int index
,Customer cust
){if(index
>= maxTotal
)return false;customers
[index
] = cust
;return true;}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
- 1] = null;total
--;return true;}public Customer[] gerAllCustomer(){Customer[] customers1
= new Customer[total
];for (int i
= 0; i
< total
; i
++) {customers1
[i
] = customers
[i
];}return customers1
;}public Customer getCustomer(int index
){if(index
< 0 || index
>= total
)return null;return customers
[index
];}public int getTotal(){return total
;}}
package CustomerInfomationManagement;public class CustomerView {private CustomerList customerList
= new CustomerList(10);public void enterMainMenu(){boolean isFlag
= true;while (isFlag
){System.out
.println("-------------客戶信息管理軟件------------\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 choice
= CustomerUtility.readMenuSelection();switch (choice
){case '1':addCustomer();break;case '2':modifyCustomer();break;case '3':deleteCustomer();break;case '4':showAllCustomer();break;case '5':System.out
.print(" 確認是否退出(Y/N):");if(CustomerUtility.readConfirmSelection() == 'Y')isFlag
= false;break;default:System.out
.println("怎么1-5都能輸入錯誤");}}}private void addCustomer(){System.out
.println("------------- 添加客戶信息 ------------");System.out
.printf("姓名:");String name
= CustomerUtility.readString(10);System.out
.printf("性別:");char gender
= CustomerUtility.readChar();System.out
.printf("年齡:");int age
= CustomerUtility.readInt();System.out
.printf("電話:");String phone
= CustomerUtility.readString(13);System.out
.printf("Email:");String email
= CustomerUtility.readString(20);Customer customer
= new Customer(name
,gender
, age
, phone
, email
);boolean isSuccess
= customerList
.addCustomer(customer
);if(isSuccess
){System.out
.println("-------------添加成功-----------------");}else {System.out
.println("-----------空間已滿,添加失敗-----------");}System.out
.println("-------------------------------------");}private void modifyCustomer(){int number
;Customer temp
;System.out
.println("-------------修改客戶信息----------------");while (true){System.out
.printf("請選擇待修改的客戶編號(0退出):");number
= CustomerUtility.readInt();if(number
== 0){System.out
.println("------------已退出修改客戶信息------------");System.out
.println("--------------------------------------");return;}temp
= customerList
.getCustomer(number
-1);if (temp
== null){System.out
.println("無法找到指定客戶");}elsebreak;}System.out
.printf("姓名:");String name
= CustomerUtility.readString(10,temp
.getName());System.out
.printf("性別:");char gender
= CustomerUtility.readChar(temp
.getGender());System.out
.printf("年齡:");int age
= CustomerUtility.readInt(temp
.getAge());System.out
.printf("電話:");String phone
= CustomerUtility.readString(13,temp
.getPhone());System.out
.printf("Email:");String email
= CustomerUtility.readString(20,temp
.getEmail());Customer customer
= new Customer(name
,gender
, age
, phone
, email
);customerList
.replaceCustomer(number
-1,customer
);System.out
.println("--------------------------------------");}private void deleteCustomer(){System.out
.println("-------------刪除客戶信息----------------");System.out
.printf("請選擇需要刪除的客戶編號(0退出):");int number
= CustomerUtility.readInt();if(number
== 0)System.out
.println("-----------------已退出----------------");else{if(customerList
.getCustomer(number
-1) != null){System.out
.print("確認是否刪除(Y/N):");char choice
= CustomerUtility.readConfirmSelection();if (choice
=='Y') {customerList
.deleteCustomer(number
- 1);System.out
.println("----------------刪除成功----------------");}}elseSystem.out
.println("----------未找到指定編號,刪除失敗---------");}System.out
.println("--------------------------------------");}private void showAllCustomer(){System.out
.println("------------- 客戶信息顯示 ------------");int total
= customerList
.getTotal();if(total
== 0){System.out
.println("未找到客戶記錄");}else {System.out
.println("編號\t\t姓名\t\t性別\t\t年齡\t\t電話\t\t\t郵箱");Customer[] custNew
= customerList
.gerAllCustomer();for (int i
= 0; i
< custNew
.length
; i
++) {Customer temp
= custNew
[i
];System.out
.println((i
+1) + "\t\t" + temp
.getName() + "\t\t" + temp
.getGender() + "\t\t" +temp
.getAge() + "\t\t" + temp
.getPhone() + "\t\t\t" + temp
.getEmail());}}System.out
.println("-------------------------------------");}public static void main(String[] args
) {CustomerView view
= new CustomerView();view
.enterMainMenu();}}
使用截圖
總結
以上是生活随笔為你收集整理的客户信息管理系统——Java的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。