第一個類是輸入輸出的工具類:
package com.hy.object.customerMessageManagement;
import java.util.*;
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
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);
}
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;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("數字輸入錯誤,請重新輸入:");
}
}
return n;
}
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;
}
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.equals("")? defaultValue : str;
}
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.hy.object.customerMessageManagement;
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;
}
public String info(){
return this.getName() + "\t" +this.getGender() + "\t" +this.getAge() + "\t" +this.getPhone() + "\t" +this.getEmail() + "\t";
}
}
第三個類是對客戶集合處理的類:
package com.hy.object.customerMessageManagement;
/**
*
* @author hanye
* @date 2020-06-14 10:39:18
**/
public class CustomerList {
private Customer[] customers;
int total=0;
/**
* CustomerList 的構造器
* @param totalCustomer
*/
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer){
if(total >= customers.length){
return false;
}
customers[total]=customer;
total++;
return true;
}
/**
* 替換指定位置上的客戶
* @param index :要替換的位置
* @param customer : 替換爲的對象
* @return 替換是否成功
*/
public boolean replaceCustomer(int index,Customer customer){
if(index<0 || index>=total ){
return false;
}
customers[index]=customer;
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[] getAllCustomers(){
Customer[] cust = new Customer[total];
for(int i=0;i<total;i++){
cust[i]=customers[i];
}
return cust;
}
public Customer getCustomer(int index){
if(index<0 || index>=total)
return null;
return customers[index];
}
public int getTotal(){
return total;
}
}
第四個類是用戶界面類,負責輸入輸出與界面展示
package com.hy.object.customerMessageManagement;
/**
* @author MSI-PC
* @date 2020-06-14 10:53
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
public CustomerView(){
Customer cust = new Customer("張三", '男', 30, "010-52776920", "[email protected]");
customerList.addCustomer(cust);
}
/**
* 顯示主界面
*/
public void enterMainMenu(){
boolean flag = true;
do{
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 key = CMUtility.readMenuSelection();
System.out.println();
switch(key){
case '1':
addNewCustomer();
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') flag = false;
break;
}
}while(flag);
}
/**
* 添加新客戶
*/
private void addNewCustomer(){
System.out.println("-----------------添加客戶---------------");
System.out.print("姓名:");
String name = CMUtility.readString(4);
System.out.print("性別:");
char gender = CMUtility.readChar();
System.out.print("年齡:");
int age = CMUtility.readInt();
System.out.print("電話:");
String phone = CMUtility.readString(15);
System.out.print("郵箱:");
String email = CMUtility.readString(15);
Customer customer = new Customer(name,gender,age,phone,email);
boolean flag=customerList.addCustomer(customer);
if(flag){
System.out.println("---------------------添加完成---------------------");
} else {
System.out.println("----------------記錄已滿,無法添加-----------------");
}
}
/**
* 修改客戶
*/
private void modifyCustomer(){
System.out.println("---------------------修改客戶---------------------");
int number;
Customer cust;
for(;;) {
System.out.println("請選擇待修改客戶編號(-1)退出:");
number =CMUtility.readInt();
if(number == -1){
return;
}
cust=customerList.getCustomer(number-1); //客戶編號從一開始
if(null == cust){
System.out.println("無法找到指定客戶");
}else{
break; //跳出循環
}
}
System.out.print("姓名(" + cust.getName() + "):");
String name = CMUtility.readString(4, cust.getName());
System.out.print("性別(" + cust.getGender() + "):");
char gender = CMUtility.readChar(cust.getGender());
System.out.print("年齡(" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.print("電話(" + cust.getPhone() + "):");
String phone = CMUtility.readString(15, cust.getPhone());
System.out.print("郵箱(" + cust.getEmail() + "):");
String email = CMUtility.readString(15, cust.getEmail());
cust = new Customer(name, gender, age, phone, email);
boolean isReplaced = customerList.replaceCustomer(number-1,cust);
if(isReplaced){
System.out.println("---------------修改成功--------------");
}else{
System.out.println("---------------修改失敗--------------");
}
}
/**
* 刪除客戶
*/
private void deleteCustomer(){
System.out.println("------------------刪除客戶------------------");
int number;
for(;;){
System.out.println("請選擇待刪除客戶編號(-1退出):");
number =CMUtility.readInt();
if(-1 == number){
return;
}
Customer cust = customerList.getCustomer(number-1);
if (null == cust){
System.out.println("無法找到指定客戶!");
}else{
break;
}
}
System.out.println("是否確認刪除(Y/N):");
char confirmDelete = CMUtility.readConfirmSelection();
if( 'Y' == confirmDelete){
boolean flag = customerList.deleteCustomer(number-1);
if(flag){
System.out.println("----------------刪除完成-----------------");
}else{
System.out.println("----------------刪除失敗-----------------");
}
}
}
/**
* 顯示所有客戶
*/
private void listAllCustomer(){
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");
Customer[] custs=customerList.getAllCustomers();
for(int i = 0;i<custs.length;i++){
Customer customer=custs[i];
System.out.println((i+1)+ "\t"+ customer.info());
}
}
System.out.println("----------------客戶列表完成-----------------");
}
public static void main(String args[]){
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}