1.工具类
import java.util.Scanner;
public class CMUtity {
public static Scanner scanner = new Scanner(System.in);
public static String readKeyBoard(int limit, boolean broken) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (broken) {
return line;
} else {
continue;
}
}
if (line.length() < 1 || line.length() > limit) {
System.out.println("输入的数据错误,请重新输入");
continue;
}
break;
}
return line;
}
public static char readNumber() {
char c;
for (; ; ) {
String arr = readKeyBoard(1, false);
c = arr.charAt(0);
if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
System.out.println("输入错误,请重新输入");
} else {
break;
}
}
return c;
}
public static char comfirmChar() {
char c;
for (; ; ) {
String arr = readKeyBoard(1, false).toUpperCase();
c = arr.charAt(0);
if (c != 'Y' && c != 'N') {
System.out.println("输入错误,请重新输入");
} else {
break;
}
}
return c;
}
public static String readString(int limit) {
String arr = readKeyBoard(limit, false);
return arr;
}
public static String readString(int limit, String broken) {
String arr = readKeyBoard(limit, true);
return (arr.length() == 0) ? broken : arr;
}
public static int readInt() {
int c;
for (; ; ) {
try {
String arr = readKeyBoard(2, false);
c = Integer.parseInt(arr);
break;
} catch (NumberFormatException e) {
System.out.println("输入错误");
}
}
return c;
}
public static int readInt(int broken) {
int n;
for (; ; ) {
String arr = readKeyBoard(2, true);
if (arr.equals("")) {
return broken;
}
try {
n = Integer.parseInt(arr);
break;
} catch (NumberFormatException e) {
System.out.println("输入的数据有错,重新输入");
}
}
return n;
}
public static char readChar() {
String arr = readKeyBoard(1, false);
return arr.charAt(0);
}
public static char readChar(char broken)
{
String arr = readKeyBoard(1,true);
return (arr.length() == 0)?broken:arr.charAt(0);
}
}
2.bean类
public class Customer
{
String name;
char gender;
int age;
String phone;
String 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 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;
}
}
3.处理业务层
public class CustomerList
{
Customer[] customers;
int total = 0;
public CustomerList(int totalNumber)
{
customers = new Customer[totalNumber];
}
public boolean addCustomer(Customer customer)
{
if (total >= customers.length)
{
return false;
}
customers[total++] = customer;
return true;
}
public boolean deleteCustomer(int index)
{
if (index < 0 || index > total)
{
return false;
}
for (int i = index;i < total;i++)
{
customers[index] = customers[index++];
}
customers[--total] = null;
return true;
}
public boolean reapCustomer(int index,Customer cust)
{
if (index < 1 || index > total)
{
return false;
}
customers[index] = cust;
return true;
}
public Customer[] ListAllCustomer()
{
Customer customer[];
customer = customers;
return customer;
}
public Customer getCustomer(int index)
{
return customers[index];
}
public int getTotal()
{
return total;
}
}
4.页面层
import java.nio.charset.StandardCharsets;
public class CustomerView
{
public static CustomerList customerList = new CustomerList(100);
public static void main(String[] args) {
CustomerView customerView = new CustomerView();
customerView.enterMenu();
}
private void enterMenu()
{
boolean isFlsg = true;
while (isFlsg)
{
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.print("输入操作编号:");
int num = CMUtity.readNumber();
switch (num)
{
case '1':
addCustomer();
break;
case '2':
deleteCuotomer();
break;
case '3':
reapCustomer();
break;
case '4':
ListAllCustomer();
break;
case '5':
System.out.print("是否选择退出(Y/N)");
char isExit = CMUtity.comfirmChar();
if (isExit == 'Y')
{
break;
}
}
}
}
private void addCustomer()
{
System.out.println("*********增加客户************");
System.out.print("姓名:");
String name = CMUtity.readString(5);
System.out.print("性别:");
char gender = CMUtity.readChar();
System.out.print("年龄:");
int age = CMUtity.readInt();
System.out.print("电话:");
String phone = CMUtity.readString(30);
System.out.print("邮箱:");
String email = CMUtity.readString(40);
Customer customer = new Customer(name,gender,age,phone,email);
boolean isFlag = customerList.addCustomer(customer);
if (isFlag)
{
System.out.println("添加成功");
}else
{
System.out.println("添加失败");
}
}
private void deleteCuotomer() {
System.out.println("删除客户");
int total = customerList.getTotal();
int index;
for (; ; ) {
System.out.print("请选择编号(-1退出):");
index = CMUtity.readInt();
if (index-1 < 0 || index -1>= total) {
System.out.println("输入编号错误");
} else if (index == -1) {
return;
} else {
break;
}
}
System.out.print("是否选择删除(Y/N):");
char a = CMUtity.comfirmChar();
if (a == 'Y') {
{
boolean isSucced = customerList.deleteCustomer(index - 1);
if (isSucced) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
}}
private void reapCustomer()
{
System.out.println("**********修改客户信息**********");
Customer customer;
int num;
int total = customerList.getTotal();
for (;;)
{
System.out.print("输入客户编号(-1退出)");
num = CMUtity.readInt();
if (num == -1)
{
return;
}
customer = customerList.getCustomer(num - 1);
if (customer == null)
{
System.out.println("无法找到该用户");
}else
{
break;
}
}
System.out.print("姓名:");
String name = CMUtity.readString(5,customer.getName());
System.out.print("性别:");
char gender = CMUtity.readChar(customer.getGender());
System.out.print("年龄:");
int age = CMUtity.readInt(customer.getAge());
System.out.print("电话:");
String phone = CMUtity.readString(30,customer.getPhone());
System.out.print("邮箱:");
String email = CMUtity.readString(30,customer.getEmail());
Customer customer1 = new Customer(name,gender,age,phone,email);
boolean isSucced = customerList.reapCustomer(num,customer1);
if (isSucced)
{
System.out.println("修改成功");
}else
{
System.out.println("修改失败");
}
}
private void ListAllCustomer ()
{
System.out.println("*********客户列表*********");
int n = customerList.getTotal();
if (n == 0)
{
System.out.println("没有客户信息");
}else
{
Customer[] cust = customerList.ListAllCustomer();
Customer customer;
System.out.println("姓名\t\t性别\t年龄\t电话\t\t\t邮箱");
for (int i = 0;i < n;i++)
{
customer = cust[i];
System.out.println(customer.getName() + "\t" + customer.getGender() + "\t" + customer.getAge()+ "\t" + customer.getPhone() + "\t" + customer.getEmail());
}
System.out.println("客户列表完成");
}
}
}