1.介绍
v:客户端,c:服务端
如下代码写到view文件夹里
Customer对象(每个客户信息的保存)也就是Javabean实体类,用数组存放Customer对象实现增删改(修改Customer对象再替换数组中)查。
如下括号里如张三是原来的信息
CM…java在utils包里,见文章:https://blog.csdn.net/weixin_43435675/article/details/107279257
下面View调用Service里方法
2.M
(Test.java先运行main方法)CustomerView.java-
Customer.java -
CustomerService.java
package com.atguigu.bean;
public class Customer {
private String name; // 客户姓名
private char gender; // 性别
private int age; // 年龄
private String phone; // 电话号码
private String email; // 电子邮箱
public Customer() {
super();
}
public Customer(String name, char gender, int age, String phone, String email) {
super();
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;
}
public String getInfo(){
return name + "\t" + gender +"\t"+ age + "\t" + phone + "\t" + email;
}
}
3.V
package com.atguigu.view;
import com.atguigu.bean.Customer;
import com.atguigu.service.CustomerService;
import com.atguigu.utils.CMUtility;
public class CustomerView {
private CustomerService cs = new CustomerService();
public void menu(){
while(true){
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.print("请选择(1-5):");
char select = CMUtility.readMenuSelection();
switch(select){
case '1':
add();
break;
case '2':
update();
break;
case '3':
delete();
break;
case '4':
list();
break;
case '5':
System.out.println("确定退出吗?Y/N");
char confirm = CMUtility.readConfirmSelection();
if(confirm == 'Y'){
return;
}
}
}
}
//1111111111111111111111111111111111111111111111111111111111111111111111111111111
private void list() {
//(1)调用CustomerService类的getAll方法获取已经存储的所有客户对象
Customer[] all = cs.getAll();
//(2)遍历显示
System.out.println("---------------------------客户列表---------------------------");
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
for (int i = 0; i < all.length; i++) {
System.out.println( (i+1) + "\t" +all[i].getInfo()); //打印客户对象的信息
}
System.out.println("-------------------------客户列表完成-------------------------");
}
//1111111111111111111111111111111111111111111111111111111111111111111111111111111
private void delete() {
System.out.println("---------------------删除客户---------------------");
System.out.print("请选择待删除客户编号(-1退出):");
int id = CMUtility.readInt(); //用户输入
if(id == -1){
//如果是-1就结束
return;
}
System.out.print("确认是否删除(Y/N):");
char confirm = CMUtility.readConfirmSelection();
if(confirm == 'N'){
//如果不删除,就结束
return;
}
//调用CustomerService类的removeById(int id)删除
cs.removeById(id);
System.out.println("---------------------删除完成---------------------");
}
//11111111111111111111111111111111111111111111111111111111111111111111111111111111
private void update() {
System.out.println("---------------------修改客户---------------------");
System.out.print("请选择待修改客户编号(-1退出):");
int id = CMUtility.readInt();
if(id == -1){
//-1退出
return;
}
//调用CustomerSerivce的getById(id)获取该客户对象的原来即old的信息
Customer old = cs.getById(id);
//输入新的数据
System.out.print("姓名(" + old.getName() + "):");
//如果用户直接输入回车,没有输入新的姓名,那么就用old.getName()代替,即保持不变
String name = CMUtility.readString(20, old.getName());
System.out.print("性别(" + old.getGender() +"):");
char gender = CMUtility.readChar(old.getGender());
System.out.print("年龄(" + old.getAge() +"):");
int age = CMUtility.readInt(old.getAge());
System.out.print("电话(" + old.getPhone() +"):");
String phone = CMUtility.readString(11, old.getPhone());
System.out.print("邮箱(" + old.getEmail() +"):");
String email = CMUtility.readString(32, old.getEmail());
//创建一个新的客户对象
Customer newCustomer = new Customer(name, gender, age, phone, email);
//替换数组中原来的客户对象 //调用CustomerService的replace(id,newCustomer)
cs.replace(id, newCustomer);
System.out.println("---------------------修改完成---------------------");
}
//11111111111111111111111111111111111111111111111111111111111111111111111111111111
private void add() {
System.out.println("---------------------添加客户---------------------");
//(1)键盘输入客户信息
System.out.print("姓名:");
String name = CMUtility.readString(20);
System.out.print("性别:");
char gender = CMUtility.readChar();
// char gender = CMUtility.readChar('男');//如果用户不输入,用'男'代替
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(11);
System.out.print("邮箱:");
String email = CMUtility.readString(32);
//(2)创建Customer对象
Customer c = new Customer(name, gender, age, phone, email);
//(3)调用CustomerService的
//addCustomer(Customer c)
// CustomerService cs = new CustomerService();//不应该是局部变量
cs.addCustomer(c);
System.out.println("---------------------添加完成---------------------");
}
}
4.C
package com.atguigu.service;
import java.util.Arrays;
import com.atguigu.bean.Customer;
public class CustomerService {
private Customer[] all ;//用来存储客户对象
private int total;//记录实际存储的客户的数量
public CustomerService(){
all = new Customer[2];
}
public CustomerService(int initSize){
all = new Customer[initSize];
}
//1111111111111111111111111111111111111111111111111.添加一个客户对象到当前的数组中
public void addCustomer(Customer c){
//(1)数组是否已满
if(total >= all.length){
System.out.println("数组已满");
return;
}
//(2)把c存储all数组中
all[total++] = c;
}
//111111111111111111111111111111111111111111111112.返回所有已经存储的客户对象
public Customer[] getAll(){
// return all;//如果返回all,里面可能有空null。返回total长度的all对象数组
/* Customer[] result = new Customer[total];
for (int i = 0; i < total; i++) {
result[i] = all[i];
}
return result;*/
//下面一行=上面
return Arrays.copyOf(all, total);//复制一个新数组all,长度为total
}
//1111111111111111111111111111111111111111113.根据客户的编号进行删除客户对象的操作
public void removeById(int id){
//(1)确定下标
int index = id-1;
//(2)检查下标的合理性
if(index<0 || index>=total){
System.out.println(id + "对应的客户不存在");//以后这个可以抛异常解决
return;
}
//(3)把[index]后面的元素往前移动
/*
* 假设:total = 5
* index = 1 移动all[2]->all[1], all[3]-->all[2],all[4]-->all[3]
* 移动3个 length = total - index - 1;
*/ //把all的index+1移动到all的index
System.arraycopy(all, index+1, all, index, total-index-1);
// //(4)把最后一个置为null
// all[total-1] = null;
// //(5)人数减少
// total--;
all[--total] = null; //结合(4)(5)
}
//111111111111111111111111111111111111111111111114.根据客户编号查询一个客户对象的方法
public Customer getById(int id){
//(1)确定下标
int index = id -1;
//(2)考虑下标的合理性
if(index<0 || index>=total){
System.out.println(id+"客户不存在");
return null; //因为getById不是void,所以要写null
}
//(3)返回[index]位置的客户对象
return all[index];
}
//111111111111111111111111111111111111111115.根据客户编号,替换原来的客户对象
public void replace(int id, Customer newCustomer){
//(1)先确定index下标
int index = id -1;
//(2)检查下标
if(index<0 || index>=total){
System.out.println(id + "客户不存在");
return;
}
//(3)替换
all[index] = newCustomer;
}
}
5.main函数
package com.atguigu.test;
import com.atguigu.view.CustomerView;
public class Test {
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.menu();
}
}
B站/知乎/微信公众号:码农编程录