客户信息管理系统(java)
一、系统需要的类
1、工具类:CMUtility
为系统提供输入方法,所有的数据输入均需要进行数据校验。
2、客户类:Customer
包含数据成员:客户姓名,性别,年龄,电话号码,电子邮箱等信息,以及构造器和get、set方法。
3、客户信息管理类:CustomerList
<1>成员:
➀客户对象数组:用于储存客户对象
➁整型变量total:用于记录已保存的客户数量
<2> 功能:
提供基本的构造器,get、set方法。实现对对象数组中的客户对象进行增删改查,索引定位获取所有已保存的客户对象等功能。
4、客户信息展示类:CustomerList
<1>成员:客户信息管理类的实例化对象
<2>功能:系统主界面显示、对客户对象的各项指标增删改查的具体实现
二、类的具体实现
1、工具类:CMUtility
package hu2;
import java.util.Locale;
import java.util.Scanner;
public class CMUtility {
private static Scanner sc = new Scanner(System.in);
/**
* 输入y/n,用于确认操作是否执行
* @return Y获取N(小写的y或者n经过处理变成了大写)
*/
public static char readConfirm() {
char c = '0';
while (true) {
String str = readKeyBoard(1, false);
c = str.toUpperCase(Locale.ROOT).charAt(0);
if ( c == 'Y' || c == 'N') {
break;
} else {
System.out.println("输入有误(y/n),请重新输入:");
}
}
return c;
}
/**
* 从键盘读入一个字符,用于菜单选择
* @return 返回一个字符
*/
public static char readSelection() {
char c;
while (true) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if ( c != '1' && c != '2' && c != '3' && c != '4' && c != '5' ) {
System.out.println("输入有误(1--5),请重新输入:");
} else {
break;
}
}
return c;
}
/**
* 从键盘读入最大长度为limit的字符串
* 如果直接回车则返回defaultValue,否则返回从键盘读入的字符串
* @param limit 最大长度限制
* @param defaultValue 默认值
* @return 返回字符串
*/
public static String readString(int limit, String defaultValue ) {
String str = readKeyBoard(limit, true);
if ( str.equals("") ) {
return defaultValue;
}
return str;
}
/**
* 如果直接回车则返回default,否则从键盘读入一个字符
* @param defaultValue
* @return
*/
public static char readChar( char defaultValue ) {
String str = readKeyBoard(1, true);
if ( str.equals("") ) {
return defaultValue;
}
char c = str.charAt(0);
return c;
}
/**
* 如果直接回车,则返回defaultValue,否则最大输入长度限制为limit
* @param limit 长度限制
* @param defaultValue 默认值,如果直接回车,则返回该值
* @return
*/
public static int readInt(int limit, int defaultValue ) {
int ans = 0;
while (true) {
String str = readKeyBoard(limit, true);
if ( str.equals("") ) {
return defaultValue;
}
try {
ans = Integer.parseInt(str);
break;
} catch (NumberFormatException e ) {
System.out.println("数字输入错误,请重新输入:");
}
}
return ans;
}
/**
* 从键盘读取最大长度为limit的整数
* 用于读入【年龄】
* @param limit 最大长度限制
* @return
*/
public static int readInt(int limit) {
int ans = 0;
while (true) {
String str = readKeyBoard(limit, false);
try {
ans = Integer.parseInt(str);
break;
} catch (NumberFormatException e ) {
System.out.println("数字输入错误,请重新输入:");
}
}
return ans;
}
/**
* 从键盘读入一个字符
* 用于输入【性别】
* @return
*/
public static char readChar() {
String str = readKeyBoard(1, false);
char c = str.charAt(0);
return c;
}
/**
*从键盘读入最大长度为limit的字符串
* 用于输入【电话号码,邮箱,姓名】
* @param limit 从键盘读入的最大字符串长限制
* @return 返回字符串
*/
public static String readString(int limit ) {
return readKeyBoard(limit, false);
}
/**
* 私有方法,供本类中其他方法调用,不对外暴露
* 用于从键盘读入长度不超过limit的字符串,value表示是否需要在输入为空时返回空字符串
* @param limit 待输入的字符串长度最大限制
* @param value 是否需要在输入为空时返回空字符串
* @return 返回字符串
*/
private static String readKeyBoard( int limit, boolean value ) {
String line = "";
while (sc.hasNextLine()) {
line = sc.nextLine();
if ( line.equals("")) {
if (value) {return line;}
}
if ( line.length() < 1 || line.length() > limit ) {
System.out.println("输入错误(不超过" + limit + "个字符),请重新输入:");
} else {
break;
}
}
return line;
}
}
2、客户类:Customer
package hu2;
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
package hu2;
public class CustomerList {
private Customer[] customers; //客户数组
private int total = 0; //记录已保存的客户数
/**
* 获取所有客户信息
* @return
*/
public Customer[] getAllCustomer() {
Customer[] custs = new Customer[total];
for ( int i=0; i<total; ++i ) {
custs[i] = customers[i];
}
return custs;
}
/**
* 修改指定位置的客户
* @param index 索引值
* @param cust 修改后的客户
* @return
*/
public boolean modifyCustomer( int index, Customer cust ) {
if ( index < 0 || index >= total ) {
return false;
}
customers[index] = cust;
return true;
}
/**
* 删除指定索引位置客户,删除成功返回true,否则返回false
* @param index 索引值
* @return
*/
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] = null;
return true;
}
/**
* 添加客户到客户数组
* @param cust 待添加的客户
* @return 返回值表示是否添加成功
*/
public boolean addCustomer( Customer cust ) {
if ( total >= customers.length ) {
return false;
}
customers[total++] = cust;
return true;
}
/**
* 获取指定索引位置的客户,如果索引不在指定范围则返回null,
* 否则返回该索引对应的客户
* @param index
* @return
*/
public Customer getIndexCustomer( int index ) {
if ( index < 0 || index >= total ) {
return null;
}
return customers[index];
}
/**
* 构造器,为对象数组开辟空间
* @param totalCustomer
*/
public CustomerList( int totalCustomer ) {
customers = new Customer[totalCustomer];
}
public CustomerList() {
}
public CustomerList(Customer[] customers, int total) {
this.customers = customers;
this.total = total;
}
public Customer[] getCustomers() {
return customers;
}
public void setCustomers(Customer[] customers) {
this.customers = customers;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
4、客户信息展示类:CustomerList
package hu2;
public class CustomerView {
private CustomerList customerList = new CustomerList(2000);
/**
* 修改客户信息,如果回车则该项不修改,否则该项信息修改为输入后的信息
*/
public void modifyCustomer() {
System.out.println("请输入待修改客户的编号(-1退出):");
int num = CMUtility.readInt(4);
if ( num == -1 ) {
return;
}
if ( (num <= 0 && num != -1) || num > customerList.getTotal() ) {
System.out.println("无法找到指定客户!");
return;
}
Customer indexCustomer = customerList.getIndexCustomer( num - 1 );
if ( indexCustomer == null ) {
System.out.println("无法找到指定客户!");
return;
}
//找到了指定客户
System.out.print("姓名(" + indexCustomer.getName() + "):");
String name = CMUtility.readString(5, indexCustomer.getName());
System.out.print("性别(" + indexCustomer.getGender() + "):");
char gender = CMUtility.readChar(indexCustomer.getGender());
System.out.print("年龄(" + indexCustomer.getAge() + "):");
int age = CMUtility.readInt(3, indexCustomer.getAge());
System.out.print("电话号码(" + indexCustomer.getPhone() + "):");
String phone = CMUtility.readString(11, indexCustomer.getPhone());
System.out.print("邮箱(" + indexCustomer.getEmail() + "):");
String email = CMUtility.readString(32, indexCustomer.getEmail());
boolean isModifySuccess = customerList.modifyCustomer(num - 1, new Customer(name, gender, age, phone, email));
if ( isModifySuccess ) {
System.out.println("----------------------修改成功!-------------------------");
} else {
System.out.println("--------------------修改失败!----------------------------");
}
}
/**
* 删除客户
*/
public void deleteCustomer() {
int ans;
Customer indexCustomer;
while (true) {
System.out.println("请输入您要删除客户的编号(-1退出):");
ans = CMUtility.readInt(3);
if ( ans == -1 ) {
return;
}
indexCustomer = customerList.getIndexCustomer(ans-1);
if ( indexCustomer == null ) {
System.out.println("无法找到指定客户!");
return;
} else {
break;
}
}
//找到了指定用户
System.out.println("-------");
System.out.println("姓名(" + indexCustomer.getName() + ")");
System.out.println("性别(" + indexCustomer.getGender() + ")");
System.out.println("年龄(" + indexCustomer.getAge() + ")");
System.out.println("电话号码(" + indexCustomer.getPhone() +")");
System.out.println("邮箱(" + indexCustomer.getEmail() + ")");
System.out.println("-------");
System.out.println("是否确认删除(Y/N):");
char isExit = CMUtility.readConfirm();
if ( isExit == 'N') {
return;
}
boolean isDeleteSuccess = customerList.deleteCustomer(ans-1);
if ( isDeleteSuccess == false ) {
System.out.println("---------------删除失败!-------------------");
return;
} else {
System.out.println("-------------删除成功!--------------------");
}
}
/**
* 客户列表,显示所有客户信息
*/
public void listAllCustomers() {
int flag = 1;
System.out.println("编号\t姓名\t性别\t年龄\t电话号码\t\t电子邮箱");
Customer[] allCustomer = customerList.getAllCustomer();
for ( Customer cust : allCustomer ) {
System.out.println((flag++) + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" +
cust.getAge() + "\t" + cust.getPhone() + "\t" + cust.getEmail());
}
}
/**
* 添加客户
*/
public void addCustomer() {
System.out.println("---------------添加客户-------------------");
System.out.print("请输入姓名:");
String name = CMUtility.readString(10);
System.out.print("请输入性别:");
char gender = CMUtility.readChar();
System.out.print("请输入年龄:");
int age = CMUtility.readInt(3);
System.out.print("请输入电话号码:");
String phone = CMUtility.readString(11);
System.out.print("请输入邮箱:");
String email = CMUtility.readString(30);
Customer cust = new Customer(name, gender, age, phone, email);
boolean isAddSuccess = customerList.addCustomer(cust);
if ( isAddSuccess ) {
System.out.println("---------------添加成功!-------------------");
} else {
System.out.println("---------------添加失败!--------------------");
}
}
/**
* 《客户信息管理软件》系统主界面
*/
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 退 出");
System.out.println(" 请输入您的选择(1--5):");
char selection = CMUtility.readSelection();
switch (selection) {
case '1':
// System.out.println("添加客户");
addCustomer();
break;
case '2':
// System.out.println("删除客户");
deleteCustomer();
break;
case '3':
// System.out.println("修改客户");
modifyCustomer();
break;
case '4':
// System.out.println("客户列表");
listAllCustomers();
break;
case '5':
// System.out.println("退出");
break;
}
}
}
public static void main(String[] args) {
CustomerView custView = new CustomerView();
custView.enterMainMenu();
}
}