面向对象编程
项目二
1 CMUtility工具类
1) 该类的主要作用
该类主要作用是提供与读取用户键入的输入相关的方法,具体地:
- 读取用户的输入
判断用户的输入是否有效
- 有效,则读取有效输入
- 无效,则提示用户重新输入
因此,该类中的方法主要是针对不同的用户输入场景,对每个场景下用户的输入进行控制读取,若用户输入的为有效输入,则直接读取,若用户输入的为该场景下的无效输入,则提示用户重新输入。
在CMUtility工具类
中所设置的方法的逻辑是:
- 设置一个控制用户输入长度的读取用户输入的方法,该方法作为读取不同场景有效输入的方法的前提方法;
- 根绝不同场景下所需的不同输入类型,设置具有不同输入类型对应控制的读取用户输入的方法。
2) CMUtility.java
package com.llluna.p2.util;
import java.util.Scanner;
/**
*
* @Description 将不同的功能封装为方法,可以直接通过调用方法使用其功能,
* 而无需考虑具体的功能实现细节
* 具体地,本类即CMUtility包含的是读取用户键入的信息的相关方法:
* 本质上是读取有效的用户输入:先读取用户键入的输入,根据其键入内容通过提示限制用户重新键入有效的内容,
* 或者直接读取有效的内容
* @author llluna Email:liuyuhan411011@163.com
* @version
* @date 2023年9月7日下午9:08:24
*
*/
public class CMUtility {
private static Scanner s = new Scanner(System.in);
private static String readKeyBoard(int limit, boolean blankReturn) {
String input = "";
while(s.hasNextLine()) {
String str = s.nextLine();
if(str.length() == 0) {
if(blankReturn) {
input = str;
break;
}
else {
continue;
}
}
else {
if(str.length() > limit) {
System.out.print("The length of input is valid! Please input again: ");
continue;
}
else {
input = str;
break;
}
}
}
return input;
}
public static char readMenuSelection() {
char input;
boolean isValid;
do {
isValid = false;
input = readKeyBoard(1, false).charAt(0);
if(input == '1' || input == '2' || input == '3' || input == '4' || input == '5') {
isValid = true;
}
else {
System.out.print("Please re-enter the number between 1 and 5: ");
}
}while(!isValid);
return input;
}
public static String readString(int limit) {
String input = readKeyBoard(limit, false);
return input;
}
public static String readString(int limit, String defalutValue) {
String input = readKeyBoard(limit, true);
if(input.length() == 0) {
return defalutValue;
}
else {
return input;
}
}
public static char readChar() {
char input = readKeyBoard(1, false).charAt(0);
return input;
}
public static char readChar(char defaultValue) {
String input = readKeyBoard(1, true);
if(input.length() == 0) {
return defaultValue;
}
else {
return input.charAt(0);
}
}
public static int readInt() {
int input;
for(;;) {
String inputStr = readKeyBoard(2, false);
try {
input = Integer.parseInt(inputStr);
break;
}
catch(NumberFormatException e) {
System.out.print("Please re-enter a number: ");
}
}
return input;
}
public static int readInt(int defaultValue) {
int input;
for(;;) {
String inputStr = readKeyBoard(2, true);
if(inputStr.length() == 0) {
input = defaultValue;
break;
}
else {
try {
input = Integer.parseInt(inputStr);
break;
}
catch(NumberFormatException e) {
System.out.print("Please re-enter a number: ");
}
}
}
return input;
}
public static char readConfirmSelection() {
char input;
boolean isValid;
do {
isValid = false;
String inputStr = readKeyBoard(1, false);
input = inputStr.charAt(0);
if(input == 'y' || input == 'Y' || input == 'n' || input == 'N') {
isValid = true;
}
else {
System.out.print("Plead re-enter y or Y or n or N: ");
}
}while(isValid == false);
return input;
}
}
2 Customer类
1) 该类主要作用Customer类
用于封装Customer
实体对象的信息,其中包含的就是对一个Customer
对象操作的方法,通常就是初始化及读取和修改对象的属性,即对应着构造器和其getter
和setter
。
2) Customer.java
package com.llluna.p2.bean;
/**
*
* @Description Customer为实体对象,用来封装客户信息
* @author llluna Email:liuyuhan411011@163.com
* @version
* @date 2023年9月7日下午9:01:23
*
*/
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;
}
}
3 CustomerList类
1) 该类的作用CustomerList
为多个Customer
对象的管理模块。
可以与Customer
类进行比较,类比于一个CustomerList
类,该类封装CustomerList
对象,其包含的就是对一个CustomerList
对象操作的方法,通常就是初始化、增、删、改、查和遍历。
2) CustomerList.java
package com.llluna.p2.service;
import com.llluna.p2.bean.Customer;
/**
*
* @Description CustomerList为Customer对象的管理模块,
* 内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历方法,供CustomerView调用
* @author llluna Email:liuyuhan411011@163.com
* @version
* @date 2023年9月7日下午9:03:30
*
*/
public class CustomerList {
private Customer[] customers; // 用来保存客户对象的数组
private int total = 0; // 记录已保存客户对象的数量
/**
* 初始化customers数组
* @param totalCustomer:数组长度,或者说保存客户对象的最大数量
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
*
* @Description 将指定的客户添加到数组中
* @author llluna
* @date 2023年9月9日下午9:49:26
* @param customer
* @return 添加成功返回true,添加失败返回false
*/
public boolean addCustomer(Customer customer) {
if(total >= customers.length) {
return false;
}
else {
this.customers[this.total] = customer;
this.total++;
return true;
}
}
/**
*
* @Description 修改指定索引位置上的客户对象
* @author llluna
* @date 2023年9月9日下午9:52:23
* @param index
* @param customer
* @return 修改成功返回true,否则返回false
*/
public boolean replaceCustomer(int index, Customer customer) {
if(index < 0 || index >= total) {
return false;
}
else {
customers[index] = customer;
return true;
}
}
/**
*
* @Description 删除指定索引位置的客户
* @author llluna
* @date 2023年9月9日下午9:55:52
* @param index
* @return 删除成功返回true,否则返回false
*/
public boolean deleteCustomer(int index) {
if(index < 0 || index >= total) {
return false;
}
else {
for(int i = index + 1; i < total; i++) {
customers[i - 1] = customers[i];
}
customers[total - 1] = null;
total--;
return true;
}
}
/**
*
* @Description 获取所有的客户
* @author llluna
* @date 2023年9月9日下午10:00:54
* @return 带有客户对象的数组部分以数组形式返回
*/
public Customer[] getAllCustomers() {
Customer[] newCustomers = new Customer[total];
for(int i = 0; i < total; i++) {
newCustomers[i] = customers[i]; // 这里复制不是每个客户对象而是客户对象的地址值
}
return newCustomers;
}
/**
*
* @Description 获取指定索引位置上的客户
* @author llluna
* @date 2023年9月9日下午10:06:47
* @param index
* @return 如果索引值有效,则返回对应位置的客户对象,否则返回null。
*/
public Customer getCustomer(int index) {
if(index < 0 || index >= total) {
return null;
}
else {
return customers[index];
}
}
/**
*
* @Description 获取已保存的客户的数量
* @author llluna
* @date 2023年9月9日下午10:08:01
* @return 返回数量
*/
public int getTotal() {
return total;
}
4 CustomerView类
1) 该类的作用CustomerView
为主模块,负责菜单的显示和处理用户操作。即该类包含的就是显示菜单、处理用户各个操作的方法。
2) CustomerView.java
package com.llluna.p2.ui;
import com.llluna.p2.bean.Customer;
import com.llluna.p2.service.CustomerList;
import com.llluna.p2.util.CMUtility;
/**
*
* @Description CustomerView为主模块,负责菜单的显示和处理用户操作
* @author llluna Email:liuyuhan411011@163.com
* @version
* @date 2023年9月7日下午9:06:38
*
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
private CMUtility cmu = new CMUtility();
/**
*
* @Description 显示界面
* @author llluna
* @date 2023年9月9日下午10:14:07
*/
public void enterMainMenu() {
boolean isExit = false;
while (isExit == false) {
System.out.println("--------------------------客户信息管理软件----------------------------");
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();
System.out.print(" 请选择(1-5):");
char selection = cmu.readMenuSelection();
if (selection == '1') {
addNewCustomer();
System.out.println("\n\n");
} else if (selection == '2') {
modifyCustomer();
System.out.println("\n\n");
} else if (selection == '3') {
deleteCustomer();
System.out.println("\n\n");
} else if (selection == '4') {
listAllCustomers();
System.out.println("\n\n");
} else {
System.out.print("Are you sure you want to exit(y or n): ");
char confirm = cmu.readConfirmSelection();
if (confirm == 'y' || confirm == 'Y') {
isExit = true;
}
}
}
}
/**
*
* @Description 添加客户
* @author llluna
* @date 2023年9月9日下午10:14:19
*/
public void addNewCustomer() {
System.out.println("\n***************************Add Customer**************************\n");
System.out.print("Please input the name of added Customer: ");
String name = cmu.readString(10);
System.out.print("Please input the gender of added Customer: ");
char gender;
for (;;) {
gender = cmu.readChar();
if (gender == '男' || gender == '女') {
break;
} else {
System.out.print("Please input the valid gender of added Customer: ");
}
}
System.out.print("Please input the age of added Customer: ");
int age;
for (;;) {
age = cmu.readInt();
if (age > 0 && age <= 200) {
break;
} else {
System.out.print("Please input the valid age of added Customer: ");
}
}
System.out.print("Please input the phone of added Customer: ");
String phone = cmu.readString(20);
System.out.print("Please input the email of added Customer: ");
String email = cmu.readString(20);
Customer newCustomer = new Customer(name, gender, age, phone, email);
if (customerList.addCustomer(newCustomer)) {
System.out.println("Customer added successfully!");
} else {
System.out.println("Customer added unsuccessfully!");
}
System.out.println("\n***********************Add Customer Completed*********************\n");
}
/**
*
* @Description 修改客户
* @author llluna
* @date 2023年9月9日下午10:14:27
*/
public void modifyCustomer() {
System.out.println("\n**************************Modify Customer*************************\n");
System.out.print("Please input the index of modified customer(Input -1 to Exit): ");
int index;
for (;;) {
index = cmu.readInt();
if (index == -1) {
return;
} else {
index--;
if (index < customerList.getTotal() && index >= 0) {
break;
} else {
System.out.print("Please input the valid index of deleted customer: ");
}
}
}
Customer customer = customerList.getCustomer(index);
System.out.print("Please input the name of modified Customer(" + customer.getName() + "): ");
String name = cmu.readString(10, customer.getName());
System.out.print("Please input the gender of modified Customer(" + customer.getGender() + "): ");
char gender;
for (;;) {
gender = cmu.readChar(customer.getGender());
if (gender == '男' || gender == '女') {
break;
} else {
System.out.print("Please input the valid gender of added Customer: ");
}
}
System.out.print("Please input the age of modified Customer(" + customer.getAge() + "): ");
int age;
for (;;) {
age = cmu.readInt(customer.getAge());
if (age > 0 && age <= 200) {
break;
} else {
System.out.print("Please input the valid age of added Customer: ");
}
}
System.out.print("Please input the phone of modified Customer(" + customer.getPhone() + "): ");
String phone = cmu.readString(20, customer.getPhone());
System.out.print("Please input the email of modified Customer(" + customer.getEmail() + "): ");
String email = cmu.readString(20, customer.getEmail());
Customer newCustomer = new Customer(name, gender, age, phone, email);
if (customerList.replaceCustomer(index, newCustomer)) {
System.out.println("Customer modified successfully!");
} else {
System.out.println("Customer modified unsuccessfully!");
}
System.out.println("\n**********************Modify Customer Completed********************\n");
}
/**
*
* @Description 删除客户
* @author llluna
* @date 2023年9月9日下午10:14:40
*/
public void deleteCustomer() {
System.out.println("\n***************************Delete Customer************************\n");
System.out.print("Please input the index of deleted customer(Input -1 to Exit): ");
int index;
for (;;) {
index = cmu.readInt();
if(index == -1) {
return;
}
else {
index--;
if (index < customerList.getTotal() && index >= 0) {
break;
} else {
System.out.print("Please input the valid index of deleted customer: ");
}
}
}
System.out.print("Are you sure you want to delete this customer(y or n): ");
char confirm = cmu.readConfirmSelection();
if(confirm == 'n' || confirm == 'N') {
return;
}
else {
if (customerList.deleteCustomer(index)) {
System.out.println("Customer deleted successfully!");
} else {
System.out.println("Customer deleted unsuccessfully!");
}
System.out.println("\n**********************Delete Customer Completed********************\n");
}
}
/**
*
* @Description 展示所有客户
* @author llluna
* @date 2023年9月9日下午10:14:48
*/
public void listAllCustomers() {
System.out.println("\n***************************Customer List**************************\n");
Customer[] customers = customerList.getAllCustomers();
if (customers.length == 0) {
System.out.println("No customer record!");
} else {
System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱");
for (int i = 0; i < customers.length; i++) {
System.out.print(" " + (i + 1) + " \t\t");
System.out.print(customers[i].getName() + "\t\t");
System.out.print(customers[i].getGender() + " \t\t ");
System.out.print(customers[i].getAge() + " \t\t");
System.out.print(customers[i].getPhone() + "\t\t");
System.out.print(customers[i].getEmail());
System.out.println();
}
}
System.out.println("\n**********************Customer List Completed**********************\n");
}
public static void main(String[] args) {
CustomerView cv = new CustomerView();
cv.enterMainMenu();
}
}