目标
模拟实现一个基于文本界面的《客户信息管理软件》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
- 类结构的使用(属性、方法及构造器)
- 对象的创建与使用
- 类的封装
- 声明和使用数组
- 数组的插入、删除和替换
- 关键字的使用this
代码:
Customer.java
package com.lhc.project2;
/*
* 设置客户类
*/
public class Customer {
private int id;
private String name;
private char gender;
private int age;
private String phone;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
private static int num = 1;
//定义一个静态变量,用于存放保存客户的编号
public static int getNum() {
return num;
}
public static void setNum(int num) {
Customer.num = num;
}
public Customer( String name, char gender, int age, String phone, String email) {
super();
this.id = num++;
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.email = email;
}
public Customer() {
this.id = num++;
}
}
CustomerList.java
package com.lhc.project2;
import java.util.Arrays;
public class CustomerList {
//用来保存客户对象的数组
Customer[] cs;
//记录已保存客户对象的数量
int total = 0;
//用来初始化cs数组
//参数: totalCustomer:指定customers数组的最大空间
public CustomerList(int totalCustomer) {
cs = new Customer[totalCustomer];
}
/* 用途:将参数customer添加组中最后一个客户记录对象之后
* 参数:customer指定要添加的客户对象
* 返回:添加成功返回true;false表示数组已满,无法添加
*/
public boolean addCustomer(Customer customer) {
//添加失败的情况
//1.customer客户对象是null
if(customer == null) {
return false;
}
//2.数组已满,添加失败
if(cs.length <= total) {
return false;
}
//[客户1,客户2,客户3,null,null] total = 3
cs[total++] = customer;
return true;
}
/*用途:用参数customer替换数组中由index指定的对象
* 参数:customer指定替换的新客户对象
* index指定所替换对象在数组中的位置
* 返回:替换成功返回true;false表示索引替换无效,无法替换
*/
public boolean replaceCustomer(int index,Customer cust) {
//返回false的情况
//判断索引是否为非法索引
if(index < 0 || index >= total) {
return false;
}
//customer客户对象是null
if(cust == null) {
return false;
}
cs[index] = cust;
return true;
}
public boolean deleteCustomer(int index) {
//判断索引是否为非法索引
if(index < 0 || index >= total) {
return false;
}
/*
* [客户1,客户2,客户3,客户4,null] total = 4 index = 1 删除客户2
*
* cs[1] = cs[2];
* [客户1,客户2,客户3,客户4,null] [客户1,客户3,客户3,客户4,null]
*
* cs[2] = cs[3];
* [客户1,客户3,客户3,客户4,null] [客户1,客户3,客户4,客户4,null]
*
* cs[3] = null; 这一步应该放在循环外面操作。有可能数组已经满了
*/
for (int i = index; i < total - 1; i++) {
cs[i] = cs[i+1];
}
//[客户1,客户3,客户3,客户4,null] [客户1,客户3,客户4,null,null]
cs[total - 1] = null;
//成功删除了一个客户
total--;
return true;
}
/*
*用途:返回数组中记录的所有客户对象
* 返回:Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同
*/
public Customer[] getAllCustomers() { // [客户1,客户2,客户3,客户4,null]
return Arrays.copyOf(cs, total);
}
/*
* 用途:返回参数index指定索引位置的客户对象记录
* 参数:index指定索要获取的客户对象在数组中的索引位置
* 返回:封装了客户信息的Customer对象
*/
public Customer getCustomer(int index) {
//判断索引是否为非法索引
if(index < 0 || index >= total) {
return null;
}
return cs[index];
}
}
CustomerView.java
package com.lhc.project2;
public class CustomerView {
//创建最大包含10客户对象的CustomerList对象
CustomerList list = new CustomerList(10);
//用途:创建CustomerView实例,并调用enterMainMenu()方法以执行程序
public static void main(String[] args) {
CustomerView cv = new CustomerView();
cv.enterMainMenu();
}
//用途:显示主菜单,响应用户输入
//根据用户操作分别调用其他相应的成员方法(如addNewCustomer),以完成客户信息处理
public void enterMainMenu() {
//定义开关(标记)
boolean flag = true;
while(flag) {
System.out.println("---------------客户信息管理软件---------------");
System.out.println();
System.out.println("\t\t1.添加客户");
System.out.println("\t\t2.修改客户");
System.out.println("\t\t3.删除客户");
System.out.println("\t\t4.客户列表");
System.out.println("\t\t5.退 出");
System.out.println();
System.out.print("\t\t请选择(1-5):");
char key = CMUtility.readMenuSelection();
switch(key) {
//添加客户
case '1':
addNewCustomer();
break;
//修改客户
case '2':
modifyCustomer();
break;
//删除客户
case '3':
deleteCustomer();
break;
//客户列表
case '4':
listAllCustomer();
break;
//退出
case '5':
flag = exit();
break;
}
}
}
private boolean exit() {
return false;
}
private void listAllCustomer() {
System.out.println("-----------------客户列表-----------------");
System.out.println("编号\t姓名\t性别\t年龄\t手机\t邮箱");
//获取所有客户对象的数组
Customer[] cs = list.getAllCustomers();
if(cs.length == 0 ) {
System.out.println("系统中还没有客户信息,赶快添加吧......");
}
for (int i = 0; i < cs.length; i++) {
Customer c = cs[i];
System.out.println(c.getId() +"\t"+ c.getName()+"\t" + c.getGender() +"\t"+c.getAge()+"\t"+ c.getPhone()+"\t" + c.getEmail());
}
System.out.println("-----------------客户列表完成-----------------");
}
private void modifyCustomer() {
System.out.println("-----------------修改客户-----------------");
System.out.println("请选择待修改客户编号(-1退出):");
int id = CMUtility.readInt();
if(id == -1) {
return;
}
//根据客户编号获取客户的索引
int index = getIndex(id);
//根据索引获取客户对象
Customer c = list.getCustomer(index);
if(c == null) {
System.out.println("-----------------修改失败-----------------");
return;
}
System.out.println("姓名"+"("+c.getName()+"):");
String name = CMUtility.readString(4,c.getName());
System.out.println("性别"+"("+c.getGender()+"):");
char gender = CMUtility.readChar(c.getGender());
System.out.println("年龄"+"("+c.getAge()+"):");
int age = CMUtility.readInt(c.getAge());
System.out.println("手机"+"("+c.getPhone()+"):");
String phone = CMUtility.readString(11,c.getPhone());
System.out.println("邮箱"+"("+c.getEmail()+"):");
String email = CMUtility.readString(25,c.getEmail());
Customer cust = new Customer(name,gender,age,phone,email);
//将原来的客户编号存入新的客户中
cust.setId(c.getId());
//将客户标准类中num - 1
Customer.setNum(Customer.getNum() - 1);
boolean result = list.replaceCustomer(index, cust);
if(result) {
System.out.println("-----------------修改成功-----------------");
}else {
System.out.println("-----------------修改失败-----------------");
}
}
private void deleteCustomer() {
System.out.println("-----------------删除客户-----------------");
System.out.println("请选择待删除客户编号(-1退出):");
int id = CMUtility.readInt();
if(id == -1) {
return;
}
System.out.println("确认是否决定删除Y/N");
//获取是否删除的命令
char key = CMUtility.readConfirmSelection();
if (key == 'Y' || key == 'y') {
//根据客户编号获取索引
int index = getIndex(id);
//根据索引进行删除
boolean result = list.deleteCustomer(index);
if(result) {
System.out.println("-----------------删除成功-----------------");
}else {
System.out.println("-----------------删除失败-----------------");
}
}
}
private int getIndex(int id) {
//获取所有客户对象数组
Customer[] cs = list.getAllCustomers();
//遍历数组中所有的客户对象
for(int i = 0; i < cs.length; i++) {
//获取每个客户对象
Customer c = cs[i];
if(id == c.getId()) {
return i;
}
}
return -1;
}
private void addNewCustomer() {
System.out.println("-----------------添加客户-----------------");
System.out.println("姓名");
String name = CMUtility.readString(4);
System.out.println("性别");
char gender = CMUtility.readChar();
System.out.println("年龄");
int age = CMUtility.readInt();
System.out.println("手机");
String phone = CMUtility.readString(11);
System.out.println("邮箱");
String email = CMUtility.readString(25);
//通过上述五个参数,创建客户对象
Customer customer = new Customer(name,gender,age,phone,email);
//将创建好的客户对象添加到list添加客户的方法
boolean result = list.addCustomer(customer);
if(result) {
System.out.println("-----------------添加成功-----------------");
}else {
System.out.println("-----------------添加失败-----------------");
}
}
}
CMUtility.java
package com.lhc.project2;
import java.util.*;
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
* 读取菜单项
* @return char类型的数组
*/
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;
}
/**
* 读取一个字符
* @return
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
* 读取一个字符,如果用户没有输入,读取默认值defaultValue
* @param defaultValue
* @return
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
* 读取一个int类型数据
* @return
*/
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;
}
/**
* 读取一个Int类型的数据,如果用户没有输入,读取默认值 defaultValue
* @param defaultValue
* @return
*/
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;
}
/**
* 读取一个字符串
* @param limit 限制用户输入的字符串的长度
* @return
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
* 读取一个字符串,如果用户没有输入,读取默认值defaultValue
* @param limit 限制用户输入的字符串的长度
* @param defaultValue
* @return
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
* 读取是否确认 只有Y / N
* @return
*/
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;
}
}
运行结果