1 项目目录
2 CMUtility 工具类
代码语言:java
复制package com.binbin.p2.util;
import java.util.*;
/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
用于界面菜单的选择。该方法读取键盘,如果用户键入’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.print("选择错误,请重新输入:");
} else break;
}
return c;
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
从键盘读取一个长度不超过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;
}
/**
从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
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;
}
/**
从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
用于确认选择的输入。该方法从键盘读取‘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.print("输入长度(不大于" limit ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
3 Customer类
代码语言:java
复制package com.binbin.p2.bean;
/**
*
* @Description Cuseomer实体对象,用来封装客户信息
* @author Binbin Email:991495875@qq.com
* @version
* @date 2022年2月6日下午11:22:56
*
*/
public class Customer {
private String name; // 姓名
private char gender; // 性别
private int age; // 年龄
private String phone;
private String 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 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;
}
}
4 CustomerList类
代码语言:java
复制package com.binbin.p2.server;
import com.binbin.p2.bean.Customer;
/**
*
* @Description CustomerList 为Customer对象的管理模块
* 内部用数组管理一组Customer对象,并提供相应的添加,修改,删除和遍历, 供CustomerView调用
* @author Binbin Email:991495875@qq.com
* @version
* @date 2022年2月6日下午11:25:22
*
*/
public class CustomerList {
private Customer[] customers;// 用来保存客户对象的数组
private int total; // 记录以保存客户对象是数量
/**
* 构造器,初始化customers 数组的长度
*
* @param totalCustomer
* 指定数组的长度
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
*
* @Description 将指定的客户添加到数组中
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日上午12:07:56
* @param customer
* @return 成功:true,失败:false
*/
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;
}
customers[total ] = customer;
return true;
}
/**
*
* @Description 修改指定索引位置的客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午6:01:16
* @param index
* @param cust
* @return true修改成功,false修改失败
*/
public boolean replaceCustomer(int index, Customer cust) {
// total 如果是3个,那么数组元素索引是0,1,2;所以传入的index不能等于3
if (index < 0 || index >= total) {
return false;
}
customers[index] = cust;
return true;
}
/**
*
* @Description 删除指定索引的数据
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午6:44:37
* @param index
* 要删除的索引值
* @return true:成功,false失败
*/
public boolean deleteCustomer(int index) {
if (index < 0 || index >= total) {
return false;
}
// 终止条件 total是总数,减1刚好就是角标值(数组的索引),
for (int i = index; i < total - 1; i ) {
customers[i] = customers[i 1];
}
// 最后的那个数据置空
// 方法1
// customers[total - 1] = null;
// total--;// 总数减一
// 方法2
customers[--total] = null;
return true;
}
/**
*
* @Description 获取所有客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午9:30:07
* @return
*/
public Customer[] getAllCustomer() {
Customer[] custs = new Customer[total];
for (int i = 0; i < total; i ) {
custs[i] = customers[i];
}
return custs;
}
/**
*
* @Description 获取指定客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午9:30:33
* @param index
* @return null 表示未找到
*/
public Customer getCustomer(int index) {
if(index < 0 || index >= total){
return null;
}
return customers[index];
}
// 获取存储客户的数量
public int getTotal() {
return total;
}
}
5 CustomerView类
代码语言:java
复制package com.binbin.p2.ui;
import com.binbin.p2.bean.Customer;
import com.binbin.p2.server.CustomerList;
import com.binbin.p2.util.CMUtility;
/**
*
* @Description CustomerView为主模块,负责菜单的显示和处理用户操作
* @author Binbin Email:991495875@qq.com
* @version
* @date 2022年2月6日下午11:29:38
*
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
private String transverseLine = "------------------------";
public CustomerView() {
Customer customer = new Customer("张飞", '男', 46, "13703758899", "zhangfei@sian.com");
customerList.addCustomer(customer);
}
public void enterMainMenu() {
boolean isFlag = true;
while (isFlag) {
System.out.println("n" this.transverseLine "客户信息管理系统" this.transverseLine "n");
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 menuNumber = CMUtility.readMenuSelection();
switch (menuNumber) {
case '1':
this.addNewCustomer();
break;
case '2':
this.modifyCustomer();
break;
case '3':
this.deleteCustomer();
break;
case '4':
this.listAllCustomers();
break;
case '5':
System.out.print("您确认退出系统吗?(Y/y)|(N/n) : ");
char isLogout = CMUtility.readConfirmSelection();
if (isLogout == 'Y') {
isFlag = false;
System.out.println("已成功退出系统, 欢迎下次使用!");
}
// break; // 最后一个break可以省略...
}
}
}
/**
*
* @Description 添加客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午7:27:45
*/
private void addNewCustomer() {
System.out.println(this.transverseLine "添加客户" this.transverseLine);
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(12);
System.out.print("邮箱: ");
String email = CMUtility.readString(30);
Customer customer = new Customer(name, gender, age, phone, email);
boolean addStatus = customerList.addCustomer(customer);
if (addStatus) {
System.out.println(this.transverseLine "添加客户成功" this.transverseLine);
} else {
System.out.println(this.transverseLine "目录已满,添加失败" this.transverseLine);
}
}
/**
*
* @Description 修改客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午7:27:57
*/
private void modifyCustomer() {
System.out.println(this.transverseLine "修改客户信息" this.transverseLine);
Customer cust;
int number;
for (;;) {
System.out.println("请输入待修改的客户编号(0 退出修改): ");
number = CMUtility.readInt();
if (number == 0) {
return;
}
cust = customerList.getCustomer(number - 1);
if (cust == null) {
System.out.println("没有找到客户信息,编号为:" number);
} 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(12, cust.getPhone());
System.out.print("邮箱(" cust.getEmail() "): ");
String email = CMUtility.readString(30, cust.getEmail());
Customer newCust = new Customer(name, gender, age, phone, email);
// 注意(number -1)的说明: number是客户输入的数字,假如说是1,那就是想修改第1条数据,
// 但是第1条数据的数组索引是0,所以要-1操作才是对的.
boolean isModifySuccess = customerList.replaceCustomer(number - 1, newCust);
if (isModifySuccess) {
System.out.println(this.transverseLine "修改客户信息成功" this.transverseLine);
}
}
/**
*
* @Description 删除客户信息
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午7:28:10
*/
private void deleteCustomer() {
System.out.println(this.transverseLine "删除客户信息" this.transverseLine);
int number;
for (;;) {
System.out.println("请选择待删除的客户编号(0退出删除操作): ");
number = CMUtility.readInt();
if (number == 0) {
return;
}
Customer customer = customerList.getCustomer(number - 1);
if (customer == null) {
System.out.println("未找到指定客户!");
} else {
break;
}
}
// 找到了指定客户信息
System.out.print("您确删除吗?(Y/y)|(N/n) : ");
char isDelete = CMUtility.readConfirmSelection();
if (isDelete == 'Y') {
if (customerList.deleteCustomer(number - 1)) {
System.out.println(this.transverseLine "删除成功" this.transverseLine);
}
} else {
System.out.println(this.transverseLine "退出删除功能" this.transverseLine);
}
}
/**
*
* @Description 查看所有客户
* @author Binbin Email:991495875@qq.com
* @date 2022年2月7日下午7:28:28
*/
private void listAllCustomers() {
System.out.println(this.transverseLine "所有客户列表" this.transverseLine "n");
if (customerList.getTotal() == 0) {
System.out.println("没有客户记录!");
} else {
System.out.println("编号t姓名t性别t年龄t电话tt邮箱");
Customer[] custs = customerList.getAllCustomer();
for (int i = 0; i < custs.length; i ) {
Customer cust = custs[i];
System.out.println((i 1) "t" cust.getName() "t" cust.getGender() "t" cust.getAge()
"t" cust.getPhone() "t" cust.getEmail());
}
}
System.out.println("n" this.transverseLine "客户列表结束" this.transverseLine "n");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}
6 程序运行图片