Java项目二(案例):客户信息管理软件
项目概述
软件功能
记录客户的个人信息,并能够打印客户列表。
软件说明
每个客户的信息被保存在Customer对象中。
以一个Customer类型的数组来记录当前所有的客户。
每次“添加客户”(菜单1)后,客户(Customer)对象被添加到数组中。
每次“修改客户”(菜单2)后,修改后的客户(Customer)对象替换数组中原对象。
每次“删除客户”(菜单3)后,客户(Customer)对象被从数组中清除。
执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。
涉及Java知识点
面向对象编程
类结构的使用:属性、方法及构造器
对象的创建与使用
类的封装性
声明和使用数组
数组的插入、删除和替换
关键字的使用:this
程序代码示例
程序共有四个类文件,分别保存在四个包下,分别是:
com.kaho.java.bean ----- Customer.java
com.kaho.java.service ----- CustomerList.java
com.kaho.java.util ----- CMUtility.java
com.kaho.java.ui ----- CustomerView
详情请看代码注释
Customer类
package com.kaho.java.bean;
/**
Customer为实体类,用来封装客户信息
该类封装客户的以下信息:
String name :客户姓名
char gender :性别
int age :年龄
String phone:电话号码
String email :电子邮箱
提供各属性的get/set方法
提供所需的构造器(可自行确定)
这是一个JavaBean类型文件
这个类中定义的方法用于对具体的Customer对象内部的个人信息(属性)进行操作
*/
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;
}
//getter、setter方法
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 String getDetails(){
return name + "\t\t" + gender + "\t\t" + age + "\t\t" + phone + "\t\t" + email;
}
}
CustomerList类
package com.kaho.java.service;
import com.kaho.java.bean.Customer; //该类用到了bean包下的Customer类
/**
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()
这个类中定义的各个方法用于对客户对象整体进行操作而不对具体对象的内部细节(即个人信息属性)进行操作
*/
public class CustomerList {
private Customer[] customers; //声明一个Customer类型的customers数组(未初始化)来保存多个客户"对象"
private int total = 0; //用来记录已保存客户对象的数量
/**
用途:构造器,用来初始化customers数组
参数:totalCustomer:指定customers数组的最大空间
*/
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer]; //初始化数组,预先设定数组的总长度
}
//方法(增、删、改、查等)
/**
用途:将参数customer添加到数组中最后一个客户对象记录之后
参数:customer指定要添加的客户对象
返回:添加成功返回true;false表示数组已满,无法添加
*/
public boolean addCustomer(Customer customer){
if (total >= customers.length) return false;
//total初始值为0,而后每添加一个客户对象会对total进行一次自增
customers[total] = customer;
total++;
return true;
}
/**
用途:用参数cust(Customer类型)替换数组中由索引index指定的对象
参数:cust指定替换的新客户对象
index指定所替换对象在数组中的位置,从0开始
返回:替换成功返回true;false表示索引无效,无法替换
*/
public boolean replaceCustomer(int index, Customer cust){
if (index > total - 1 || index < 0) return false;
customers[index] = cust; //将customers数组在索引index的元素赋为一个新的客户对象cust
return true;
}
/**
用途:从数组中删除参数index指定索引位置的客户对象记录
参数: index指定所删除对象在数组中的索引位置,从0开始
返回:删除成功返回true;false表示索引无效,无法删除
*/
public boolean deleteCustomer(int index){
if (index > total - 1 || index < 0) return false;
for (int i = index;i < total - 1;i++){
customers[i] = customers[i + 1]; //类似于顺序表的删除,从前往后,将后一位数组元素赋给前一位
}
customers[total - 1] = null; //将新数组已赋值部分(0 ~ total-1)的最后一个多出来的重复元素抹去,设置为空指针(引用型数据类型)
total--; //删除一个客户对象,数组已赋值部分长度减一
return true;
}
/**
用途:返回数组中记录的所有客户对象
返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
注意:不能直接return customers,因为实际上customers数组的长度是初始化时的totalCustomer(即 10),而非已赋了值的前 total个
*/
public Customer[] getAllCustomers(){
Customer[] cust = new Customer[total];
for (int i = 0;i < total;i++){
cust[i] = customers[i]; //这里是将对象的地址赋给新数组,两个数组保存的都是这几个已记录的客户对象
}
return cust;
}
/**
用途:返回参数index指定索引位置的客户对象记录
参数: index指定所要获取的客户在数组中的索引位置,从0开始
返回:封装了客户信息的Customer对象,索引不合法则返回null
*/
public Customer getCustomer(int index) {
if (index < 0 || index >= total) return null;
return customers[index];
}
/**
用途:获取当前已记录的客户数量
返回:当前customers[]数组的长度
*/
public int getTotal(){
return total;
}
}
Utility类
package com.kaho.java.util;
import java.util.*; //导入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); //charAt()返回字符串中指定索引的字符,这里读取键盘输入的一串字符串中的第一个字符
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); //charAt()返回字符串中指定索引的字符
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以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;
}
}
CustomerView类(包含main方法)
package com.kaho.java.ui;
//分别导入位于其他包内的几个需要使用到的类
import com.kaho.java.bean.Customer;
import com.kaho.java.util.CMUtility;
import com.kaho.java.service.CustomerList;
/**
* @Description CustomerView为主模块,负责菜单的显示和处理用户操作
* @author Kaho
* @version
* @date 2020.10.16
*
*/
public class CustomerView {
CustomerList customerList = new CustomerList(10);
//创建CustomerList的对象,供以下各成员方法使用。同时该对象通过构造器初始化了一个最大能包含十个客户对象的Customer类型数组customers
/**
用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法,以完成客户信息处理。
*/
public void enterMainMenu(){
boolean isFlag = true; //定义一个boolean类型的标记作为是否终止循环的判断条件
do {
System.out.println("-------------------客户信息管理软件--------------------\n");
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 key = CMUtility.readMenuSelection(); //读取用户菜单选择
switch (key) {
case '1':
addNewCustomer(); //添加客户
break;
case '2':
modifyCustomer(); //修改客户信息
break;
case '3':
deleteCustomer(); //删除客户
break;
case '4':
listAllCustomers();//显示客户列表
break;
case '5':
System.out.print("是否确定退出?(Y/N):"); //退出软件
//再次读取用户是否确认退出
char exit = CMUtility.readConfirmSelection();
if (exit == 'Y'){
isFlag = false;
}
break;
}
}while(isFlag);
}
/**
* 用途:调用CustomerList类和CMUtility类中的方法读取新客户的信息并将封装好的客户对象添加到customers数组中
*/
private void addNewCustomer(){
System.out.println("-------------------添加客户--------------------\n");
//读取新客户各项信息并保存到相应变量中
System.out.print("姓名:");
String name = CMUtility.readString(5);
System.out.print("性别:");
char gender = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(13);
System.out.print("邮箱:");
String email = CMUtility.readString(20);
//将读取到的信息全部导入一个新的对象中,即封装成一个新的客户对象
Customer customer = new Customer(name,gender,age,phone,email);
//调用CustomerList中的方法将此客户对象添加到customerList对象属性的customers数组中
boolean isFlag = customerList.addCustomer(customer);
if (isFlag == true){
System.out.println("-------------------添加完成--------------------\n");
}else{
System.out.println("-------------人数已达上限,添加失败!-------------\n");
}
}
/**
* 用途:修改指定索引的客户信息
*/
private void modifyCustomer(){
System.out.println("-------------------修改客户--------------------\n");
Customer customer; //用于存放单个对象,作为中转站
int index; //存放用户输入的编号(从1开始)
//用来找到指定对象的模块
for ( ; ; ){
System.out.print("请选择待修改客户编号(-1退出):");
//客户编号index从1开始
index = CMUtility.readInt();
if (index == -1){
return;
}
/*
将合法编号(长度不超过2的整数)上的那个客户对象赋给customer,这样下面修改信息的模块
就可直接用customer调用相应get方法得到该对象的各个属性
*/
customer = customerList.getCustomer(index - 1);
if (customer == null){
System.out.println("无法找到指定客户!");
}else{
break; //若customer存在(即指定位置上客户对象存在:非空指针),则跳出循环,进行下面的修改信息模块
}
}
//用来修改客户信息的模块
System.out.print("姓名(" + customer.getName() + "):");
//如果用户输入了姓名,则返回用户输入的信息,如果用户没有输入,直接回车,则返回customer.getName()。以下的几个方法也一样
String name = CMUtility.readString(5,customer.getName());
System.out.print("性别(" + customer.getGender() + "):");
char gender = CMUtility.readChar(customer.getGender());
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(20,customer.getEmail());
// 获取用户输入的新的信息以后,用新的信息封装成一个最新的Customer对象,再赋给customer
customer = new Customer(name,gender,age,phone,email);
//用修改后得到的新的客户对象customer替换掉原来对象数组中相应位置未修改的客户对象
//注意:新的客户对象的地址与原本的customers[index - 1]上的元素(未修改信息的对象)的地址不同,因为是新new的对象
boolean isFlag = customerList.replaceCustomer(index - 1,customer);
if (isFlag == true){
System.out.println("-------------------修改完成--------------------\n");
}else{
System.out.println("-------------------修改失败--------------------\n");
}
}
/**
* 用途:删除客户
*/
private void deleteCustomer(){
System.out.println("-------------------删除客户--------------------\n");
Customer customer; //定义一个Customer类型的变量用于存储客户对象,并没有new一个新的对象
int index;
for ( ; ; ) {
System.out.print("请选择待删除客户编号(-1退出):");
//客户编号index从1开始
index = CMUtility.readInt(); //读取一个客户编号
//如果读取到用户输入-1,则退出删除客户模式
if (index == -1) {
return;
}
//将合法编号(长度不超过2的整数)上的那个客户对象赋给customer
customer = customerList.getCustomer(index - 1);
//如果该编号上有客户对象存在则跳出循环,否则输出提示并重新循环
if (customer == null) {
System.out.println("无法找到指定客户!");
} else {
break;
}
}
// 一旦找到相应的索引位置的customer以后,让用户决定是否确认删除
System.out.print("确认是否删除(Y/N):");
char deleteOrNot = CMUtility.readConfirmSelection();
System.out.println();
if (deleteOrNot == 'Y'){
boolean isFlag = customerList.deleteCustomer(index - 1);
/*
用customerList对象调用deleteCustomer()操作,其删除的是customerList对象属性的数组customers[]中的相应位置元素对象。
若删除成功返回true;false表示索引无效,无法删除
*/
if (isFlag == true){
System.out.println("-------------------删除完成--------------------\n");
}else{
System.out.println("-------------------删除失败--------------------\n");
}
}else{
return;
}
}
/**
* 用途:显示客户列表
*/
private void listAllCustomers(){
System.out.println("-------------------客户列表--------------------\n");
//定义一个Customer类型数组customers用于存放通过customerList调用方法获取到的属性数组中的所有已记录的客户对象
Customer[] customers = customerList.getAllCustomers();
if (customers.length == 0){
System.out.println("没有任何客户记录!\n");
}else {
System.out.println("编号\t\t姓名\t\t性别\t\t电话号码\t\t电子邮箱");
for (int i = 0; i < customers.length; i++) {
Customer cust = customers[i];
/*
把数组内的每一个元素(即一个Customer客户对象)赋给Customer类型的变量cust。
由于cust的定义在for循环中,故每进行一次循环赋一个新的对象,每个对象只被下面这条语句
调用一次用于一次性得到客户所有信息。
*/
System.out.println( (i+1) +"\t\t"+ cust.getDetails());
}
}
System.out.println("-----------------客户列表完成------------------\n");
}
/**
* main()方法部分
* @param args
*/
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}
程序运行示例
添加客户
显示客户列表
修改客户信息
(键入回车即不修改这项信息)
修改后:
删除用户
删除后:
退出程序