实体类Customer用来封装客户信息。
控制类CustomerController用来操控实体对象。
View类用来显示信息与用户交互。
Text类用来测试系统。
运行效果:
public class Customer {
/*
*
* @ 客户信息类
*
* @ 重写toString()
*
*/
private String name; // 姓名
private String sex; // 性别
private int age; // 年龄
private String addr; // 地址
private String phone;// 联系方式
// 不允许创建空对象
// public Customer() {
// }
// 允许地址、联系方式为空构建对象
public Customer(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public Customer(String name, String sex, int age, String addr,
String phone) {
this(name, sex, age);
this.addr = addr;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return String.format("姓名:%s,性别:%s,年龄:%d,电话:%s,地址:%s", name, sex,
age, phone, addr);
}
}
import java.util.Arrays;
public class CustomerController {
private Customer[] customer;
private int index; // 索引
public CustomerController() {
customer = new Customer[10];
}
public CustomerController(int length) {
customer = new Customer[length];
}
/*
* @ 添加成功返回索引号
*
* @ 添加失败返回-1
*/
public int add(Customer customer) {
if (contains(customer.getPhone()) > 0) {
return -1;// 已经存在
}
if (index < this.customer.length) {
this.customer[index] = customer;
index++;
return index - 1;
} else {
return -1;
}
}
/*
* @ int contains(String phone) 是否已经包含某个手机号
*
* @ 包含返回索引号
*
* @ 不包含返回-1
*/
public int contains(String phone) {
for (int i = 0; i < index; i++) {
if (customer[i].getPhone().equals(phone)) {
return i;
}
}
return -1;
}
/*
* 删除成功返回true,删除失败返回false
*/
public boolean del(String phone) {
int index = contains(phone);
if (index < 0) {// 不包含该联系人
return false;
}
for (int i = index; i < this.index - 1; i++) {
customer[i] = customer[i + 1];
// 要删除的对象后面的对象前移
}
this.index--;
customer[this.index] = null;// 原来末尾的对象清空
return true;
}
/*
* 利用手机号查找
*/
public Customer query(String phone) {
int index = contains(phone);
if (index < 0) {
return null;
}
return customer[index];// 下标从0开始
}
/*
* 得到指定索引处的对象
*/
public Customer getCustomer(int index) {
return customer[index];
}
/*
* 设置指定索引处的对象
*/
public void setCustomer(Customer customer, int index) {
this.customer[index] = customer;
}
/*
* 得到整个对象组中已经存入对象的部分
*/
public Customer[] getCustomers() {
return Arrays.copyOf(customer, index);
}
public int getCapacity() {
return customer.length;
}
}
import java.util.Scanner;
public class View {
private Scanner input;
private CustomerController cc;
private static final View view = new View();
private View() {
input = new Scanner(System.in);
cc = new CustomerController(20);
}
public static View getInstance() {
return view;
}
public void init() {
Customer c4 = new Customer("小名", "女", 17, "广州", "891349");
cc.add(new Customer("小华", "男", 18, "北京", "123456"));
cc.add(new Customer("小李", "男", 20, "成都", "678923"));
cc.add(new Customer("王二", "男", 19, "杭州", "673524"));
cc.add(c4);
cc.add(c4);
System.out.println("客户信息列表:");
showAll();
System.out.println("以上为初始化客户信息列表,方便测试用.\n\n");
}
public void run() {
boolean flag = true;
exit: while (flag) {
menu();
int i = input.nextInt();
input.nextLine();
switch (i) {
case 1:
addAcc();
break;
case 2:
delAcc();
break;
case 3:
reviseAcc();
break;
case 4:
quaryAcc();
break;
case 5:
showAll();
break;
case 6:
break exit;
default:
break;
}
} // while
input.close();
System.out.println("系统退出");
}// run
public void menu() {
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(" 6. 退出系统");
System.out.println("==========================");
System.out.print(" 请选择1~6: ");
}
public Customer newAcc(Customer c, int flag) {
String string = "";
if (flag == 0 || flag == 1) {
System.out.print("\t姓名: ");
string = input.nextLine();
c.setName(string);
}
if (flag == 0 || flag == 2) {
System.out.print("\t性别: ");
string = input.nextLine();
c.setSex(string);
}
if (flag == 0 || flag == 3) {
System.out.print("\t年龄: ");
int age = input.nextInt();
input.nextLine();
c.setAge(age);
}
if (flag == 0 || flag == 4) {
System.out.print("\t电话: ");
string = input.nextLine();
c.setPhone(string);
}
if (flag == 0 || flag == 5) {
System.out.print("\t地址: ");
string = input.nextLine();
c.setAddr(string);
}
return c;
}
public void addAcc() {
System.out.println("请输入要添加账户 的信息:");
Customer newCustomer = new Customer("", "", 0);
newAcc(newCustomer, 0);
int index = cc.add(newCustomer);
if (index == -1) {
System.out.println("添加失败!");
} else {
System.out.println("添加成功!");
System.out.println(newCustomer);
index++;
System.out.println(String.format("已添加用户%d人,剩余可添加%d人", index,
cc.getCapacity() - index));
}
}
public void delAcc() {
System.out.print("请输入要删除账户 的手机号:");
String phone = input.nextLine();
int index = cc.contains(phone);
if (index < 0) {
System.out.println("不存在该联系人!");
} else {
System.out.println(cc.getCustomer(index).toString());
cc.del(phone);
System.out.println("客户删除成功!");
}
showAll();
}
public void reviseAcc() {
System.out.print("请输入要修改账户 的手机号:");
String phone = input.nextLine();
int index = cc.contains(phone);
Customer c1 = cc.getCustomer(index);
if (c1 == null) {
System.out.println("不存在该联系人");
return;
}
System.out.println(c1);
boolean flag = true;
while (flag) {
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(" 6. 结束修改");
System.out.println("==========================");
System.out.print(" 请选择1~6: ");
int i = input.nextInt();
input.nextLine();
if (i == 6) {
break;
}
newAcc(c1, i);
} // while
System.out.println("修改成功!");
cc.setCustomer(c1, index);
System.out.println(c1);
}
public void quaryAcc() {
System.out.print("请输入要查看账户 的手机号:");
String phone = input.nextLine();
Customer c1 = cc.query(phone);
if (c1 == null) {
System.out.println("不存在该联系人");
} else {
System.out.println(c1);
}
}
public void showAll() {
Customer[] c1 = cc.getCustomers();
System.out.println("==========================");
System.out.println("总共" + c1.length + "条联系人:");
for (Customer customer : c1) {
if (customer != null)
System.out.println(customer);
}
System.out.println();
}
}
public class Test {
public static void main(String[] args) {
View view = View.getInstance();
view.init();
view.run();
}
}
代码不难,有问题留言。