目录
需求说明
模拟实现一个基于文本界面的
《
客户信息管理软件
》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
Ø
类结构的使用:属性、方法及构造器
Ø
对象的创建与使用
Ø
类的封装性
Ø
声明和使用数组
Ø
数组的插入、删除和替换
Ø
关键字的使用:
this
![](https://i-blog.csdnimg.cn/blog_migrate/c592b72462132d5a8f9a03552f03f985.png)
前提准备:一个工具包 ,用于接受键盘交互
package Unility;
import java.util.Scanner;
public class CMUtility {
/**
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类,用于封装客户信息
public class Customer {
// 属性声明
private String name;
private String gender;
private int age;
private String tel;
private String email;
// 构造器声明 用于初始化Customer
public Customer(String name, String gender, int age, String tel, String email) {
this.name = name;
this.gender = gender;
this.age = age;
this.tel = tel;
this.email = email;
}
// get/set方法声明
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
构建CustomerList类,用于处理客户名单(增删查改和遍历)
import Bean.Customer;
public class CustomerList {
private int total = 10;
private int i = 0;
private Customer[] customers = new Customer[total]; // 初始化为一个含10个Customers的数组
// 构建构造器时只需要修改total数值
public CustomerList(int total) {
this.total = total;
}
// 构建方法,对数组进行增删改查,被添加的客户信息作为实参传递
public boolean addCustomer(Customer customer){ // 这里类似于形参传递的 int a
if (i < total){ //保证不会超过设置数组长度
customers[i] = customer;
i++;
return true;
}else{
return false;
}
}
// 删除
public boolean deletCustomer(int index){ // 这里类似于形参传递的 int a
if(index < 0 || index >= i){
return false;
}else{
for(int j = index ; j < i ; j++){
customers[j] = customers[j+1];
}
customers[i] = null;
i--; // 这里不能忘了
return true;
}
}
// 修改
public boolean replaceCustomer(int index, Customer cust_replace){ // 这里类似于形参传递的 int a
if(index < 0 || index >= i){
return false;
}else{
customers[index] = cust_replace;
return true;
}
}
// 遍历
public Customer[] getAllCustomer(){ //返回一个customer的数组,这里用复制的方式
Customer[] custs = new Customer[i]; // 新建一个数组用于存储信息
for (int j = 0; j < i; j++) {
custs[j] = customers[j];
}
return custs; // 返回新建的数组
}
// 查找
public Customer getCustomer(int index){
if (index < 0 || index > i){
return null;
}else{
return customers[index];
}
}
// 获取整个数组的长度
public int getI() {
return i;
}
}
构建CustomerView类,用于与客户的交互
import list.CustomerList;
import Bean.Customer;
import Unility.CMUnility.CMUtility;
public class CustomerView {
CustomerList customers = new CustomerList(10);
public static void main(String[] args){
// 这里是主程序
CustomerView view = new CustomerView();
view.enterMainMenu();
}
public void enterMainMenu(){
boolean isFlag = true;
while(isFlag){
// 打印初始界面
System.out.println("\n-----------------客户信息管理软件-----------------\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 c = CMUtility.readMenuSelection();
switch (c){
case '1': // 这里是char型,需要加单引号
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4': // 遍历客户列表
listAllCustomers();
break;
case '5': // 退出功能的实现
System.out.println("确认退出(Y/N)");
char isExit = CMUtility.readConfirmSelection();
if(isExit == 'Y'){
isFlag = false;
}else{
break;
}
}
}
}
private void addNewCustomer(){
System.out.println("---------------------------添加客户---------------------------");
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(15);
System.out.print("邮箱:");
String email = CMUtility.readString(15);
Customer cust_new = new Customer(name, gender, age, phone, email);
int i = customers.getI();
boolean addFlag = customers.addCustomer(cust_new);
if(addFlag == false){
System.out.println("用户已满,添加失败");
}else{
System.out.println("添加成功");
}
}
private void modifyCustomer(){
System.out.println("---------------------修改客户---------------------");
int index = 0;
Customer cust = null;
for (;;){
System.out.println("请输入要修改的客户序号(-1退出)");
index = CMUtility.readInt();
if (index == -1){
break;
}
cust = customers.getCustomer(index - 1);
if(cust == null){
System.out.println("找不到客户");
}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.getTel() + "):");
String phone = CMUtility.readString(15, cust.getTel());
System.out.print("邮箱(" + cust.getEmail() + "):");
String email = CMUtility.readString(15, cust.getEmail());
Customer cust_modufied = new Customer(name, gender,age,phone,email);
customers.replaceCustomer(index -1, cust_modufied);
}
private void deleteCustomer(){
System.out.println("---------------------删除客户---------------------");
int index = 0;
Customer cust = null;
for (;;){
System.out.println("请输入要删除的客户序号(-1退出)");
index = CMUtility.readInt();
if (index == -1){
break;
}
cust = customers.getCustomer(index - 1);
if(cust == null){
System.out.println("找不到客户");
}else{
break;
}
}
// 跳出循环说明找到了, 进行数组删除操作
boolean deletFlag = customers.deletCustomer( index -1);
if(deletFlag == false){
System.out.println("删除失败");
}else{
System.out.println("删除成功");
}
}
private void listAllCustomers(){
System.out.println("---------------------------客户列表---------------------------");
Customer[] custs = customers.getAllCustomer();
if(custs.length == 0){
System.out.println("当前没有客户");
}else {
System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
for (int i = 0; i < custs.length; i++) {
System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" +
custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" +
custs[i].getTel() + "\t" + custs[i].getEmail());
}
}
System.out.println("-------------------------客户列表完成-------------------------");
}
}
总结
- 编写项目前要构建框架
- 返回一个遍历数组时,需要首先构建一个新的数组用来存放数据(customerlist getAllCustomer)
- 引用型变量为空时,数据为null,也可以依据这个进行条件判断
- 删除和查找数组中的数据方法的形参一般为数组的索引,而添加数据的形参一般为要添加的数据