项目框架
客户信息管理软件描述:
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。
需求说明:
每个客户的信息被保存在Customer对象中。
以一个Customer类型的数组来记录当前所有的客户。
每次“添加客户”(菜单1)后,客户(Customer)对象被添加到数组中。
每次“修改客户”(菜单2)后,修改后的客户(Customer)对象替换数组中原对象。
每次“删除客户”(菜单3)后,客户(Customer)对象被从数组中清除。
执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。
软件设计结构:
该软件由以下三个模块组成:
CustomerView为主模块,负责菜单的显示和处理用户操作
CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历方法,供CustomerView调用
Customer为实体对象,用来封装客户信息
类的具体设计:
Custmer类:
Customer为实体类,用来封装客户信息
该类封装客户的以下信息:
String name :客户姓名
char gender : 性别
int age :年龄
String phone:电话号码
String email :电子邮箱
提供各属性的get/set方法 提供所需的构造器
CustomerList类:
CustomerList为Customer对象的管理模块,内部使用数组管理一组Customer对象 本类封装以下信息: Customer[]
customers:用来保存客户对象的数组 int total = 0 :记录已保存客户对象的数量
该类至少提供以下构造器和方法:
public CustomerList(int totalCustomer) public boolean
addCustomer(Customer customer) public boolean replaceCustomer(int
index, Customer cust) public boolean deleteCustomer(int index) public
Customer[] getAllCustomers() public Customer getCustomer(int index)
public int getTotal()
CustomerView类:
public void enterMainMenu()
用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法(如addNewCustomer),以完成客户信息处理。
private void addNewCustomer() private void modifyCustomer() private
void deleteCustomer() private void listAllCustomers()
用途:这四个方法分别完成“添加客户”、“修改客户”、“删除客户”和“客户列表”等各菜单功能。
这四个方法仅供enterMainMenu()方法调用。 public static void main(String[] args)
用途:创建CustomerView实例,并调用 enterMainMenu()方法以执行程序。
界面显示效果:
主页面:
添加用户页面:
查看添加的用户信息:
删除用户信息:
编号为1的客户信息已经删除:
查看客户列表信息:
备注:
其他的调试信息不在显示
此管理软件的缺点,添加的用户姓名等信息要一致,也就是说名字是三个字的都要是三个字
完整代码:
CMUtility.java
package day04.project02;
import java.util.*;
/**
* @BelongsProject: com.java.li
* @BelongsPackage: day04.project02
* @Author: Administrator
* @CreateTime: 2022-04-10 16:48
* @Description: 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;
}
}
Customer类
package day04.project02;
/**
* @BelongsProject: com.java.li
* @BelongsPackage: day04.project02
* @Author: Administrator
* @CreateTime: 2022-04-10 16:48
* @Description: 项目二客户类
*/
public class Customer {
private String name;//客户姓名
private int age;//年龄
private char sex;//性别
private String phone;//电话
private String email;//邮箱
//set和get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
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 sex,int age, String phone, String email) {
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
this.email = email;
}
}
CustomerList 类:
package day04.project02;
/**
* @BelongsProject: com.java.li
* @BelongsPackage: day04.project02
* @Author: Administrator
* @CreateTime: 2022-04-10 16:50
* @Description: 项目二列表显示
*/
public class CustomerList {
private Customer[] customers;//创建客户数组
private int total;//客户的总数
//带参数的构造器
public CustomerList(int totalCustomer) {
//初始化数组
customers = new Customer[totalCustomer];
}
//添加指定客户信息到客户数组
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;//添加失败,客户数组满了
}
customers[total++] = customer;
return true;
}
//修改指定索引上指定位置上的客户信息
public boolean replaceCustomer(int index, Customer cust) {
if (index < 0 || index >= total) {
return false;//修改失败
}
customers[index] = cust;
return true;//修改成功
}
//删除指定位置上的客户信息
public boolean deleteCustomer(int index) {
if (index < 0 || index >= total) {
return false;//删除失败
}
for (int i = index; i < total - 1; i++) {
customers[i] = customers[i + 1];
}
customers[total-1] = null;
total -= 1;
return true;
}
//获取所有的客户信息列表
public Customer[] getAllCustomer(){
Customer[] cuts = new Customer[total];
for (int i = 0; i < total; i++){
cuts[i] = customers[i];
}
return cuts;
}
public Customer getCustomer(int index){
if (index < 0 ||index >= total){
return null;
}
return customers[index];
}
//获取客户总数
public int getTotal(){
return total;
}
}
测试类:
package day04.project02;
/**
* @BelongsProject: com.java.li
* @BelongsPackage: day04.project02
* @Author: Administrator
* @CreateTime: 2022-04-10 16:51
* @Description: 项目二用户操作视图
*/
public class CustomerView {
//创建客户,初始化客户个数为10
private CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer customer = new Customer("李华华", '男', 23, "12345678945", "lh@gmail.com");
customerList.addCustomer(customer);
}
//进入主界面
public void enterMainMenu() {
boolean isFlag = true;
while (isFlag) {
System.out.println("---------------客户信息管理软件---------------");
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 Menu = CMUtility.readMenuSelection();//读取选择
switch (Menu) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listCustomer();
break;
case '5': {
System.out.print("确认是否退出:Y/N:");
char isExit = CMUtility.readConfirmSelection();
//确定退出则修改isFalg
if (isExit == 'Y') {
isFlag = false;
}
}
}
}
}
//添加新客户
public void addNewCustomer() {
System.out.println("-----------------------添加客户-----------------------\n");
//输入用户信息
System.out.print("姓名:");
String name = CMUtility.readString(10);
System.out.print("性别:");
char sex = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(11);
System.out.print("邮箱:");
String email = CMUtility.readString(13);
//将输入的用户信息加入到列表中去
Customer customer = new Customer(name, sex, age, phone, email);
boolean isSuccess = customerList.addCustomer(customer);
//判断是否加入成功
if (isSuccess) {
System.out.println("-----------------------添加完成-----------------------\n");
} else {
System.out.println("------------------客户目录已满,添加失败-----------------\n");
}
//System.out.println("添加客户信息的操作");
}
//修改客户信息
public void modifyCustomer() {
System.out.println("-----------------------修改客户-----------------------\n");
//查找客户,判断客户是否存在
Customer customer;
int number;
for (; ; ) {
System.out.println("请选择修改客户编号(-1退出):");
number = CMUtility.readInt();//从键盘读取一个整型
if (number == -1) {
return;
}
//列表下标是从0开始,但是客户的显示编号是从1开始的
customer = customerList.getCustomer(number - 1);
if (customer == null) {
System.out.println("无法找到指定的客户");//没有找到会重新回到请选择修改客户编号
} else {
break;//在循环外部输出客户信息
}
}
//来到这个地方表明已经成功找到
//修改客户信息
System.out.print("姓名(" + customer.getName() + "):");
//如果客户没有输入,则会默认使用当前客户已有的信息
String name = CMUtility.readString(10, customer.getName());
System.out.print("性别(" + customer.getSex() + "):");
char sex = CMUtility.readChar(customer.getSex());
System.out.print("年龄(" + customer.getAge() + "):");
int age = CMUtility.readInt(customer.getAge());
System.out.print("电话(" + customer.getPhone() + "):");
String phone = CMUtility.readString(13, customer.getPhone());
System.out.print("邮箱(" + customer.getEmail() + "):");
String email = CMUtility.readString(30, customer.getEmail());
Customer newCust = new Customer(name, sex, age, phone, email);
//替换修改编号的账户信息
boolean isRelpalaced = customerList.replaceCustomer(number - 1, newCust);
if (isRelpalaced) {
System.out.println("-----------------------修改完成-----------------------\n");
} else {
System.out.println("-----------------------修改失败-----------------------\n");
}
//System.out.println("修改客户信息的操作");
}
//删除客户信息
public void deleteCustomer() {
System.out.println("-----------------------删除客户-----------------------\n");
int index;
//判断删除客户信息的下表是否靠谱,只有在输入可靠的商家编号时才会跳出循环
for (; ; ) {
System.out.print("请选择删除客户编号(-1退出):");
index = CMUtility.readInt();
if (index == -1) {
return;
}
//判断是否存在此用户
Customer customer = customerList.getCustomer(index - 1);
if (customer == null) {
System.out.println("无法找到指定的用户");
} else {
break;//找到之后再循环外面执行删除操作
}
}
//执行删除操作
System.out.println("请确认是否删除(Y/N):");
char isDelete = CMUtility.readConfirmSelection();
if (isDelete == 'Y'){
boolean deleteSuccess = customerList.deleteCustomer(index-1);
if (deleteSuccess){
System.out.println("-----------------------删除完成-----------------------\n");
} else{
System.out.println("-----------------------删除失败-----------------------\n");
}
}else{
return;
}
//System.out.println("删除客户信息的操作");
}
//显示客户列表
public void listCustomer() {
System.out.println("-----------------------客户列表-----------------------\n");
//获取客户总数
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邮箱\t");
//获取客户列表信息并进行输出
Customer[] cust = customerList.getAllCustomer();
for (int i = 0; i < total; i++) {
Customer customer = cust[i];
System.out.println((i + 1) + "\t\t" + customer.getName() + "\t" + customer.getSex() + "\t\t" +
customer.getAge() + "\t\t" + customer.getPhone() + "\t" + customer.getEmail());
}
}
System.out.println("---------------------客户列表显示完成---------------------\n");
}
public static void main(String[] args) {
CustomerView customerView = new CustomerView();
customerView.enterMainMenu();
}
}
备注:
本项目是在b站上跟着康师傅视频自己写的,由于个人能力有限,文章难免有不足之处,欢迎各位大佬指正;