Customer类 : 客户属性以及配套的get set方法
CustomerList类: customer对象的管理类,封装了对customer的操作方法
CustomerView类: 主界面类,封装了界面操作
CMUtility类: 工具类,给用户提供操作入口
import java.util.*;
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/***
* 用于菜单界面的选择。该方法读取键盘,如果用户输入1-5之间的任意字符则方法返回
* 返回值为用户的选择
* @return
*/
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;
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值
* @return
*/
public static char readChar() {
String string =readKeyBoard(1,false);
return string.charAt(0);
}
/**
* 从键盘读入一个字符,并将其作为方法的返回值
* 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
* @return
*/
public static char readChar(char defaultValue) {
String string =readKeyBoard(1,false);
return(string.length()==0)?defaultValue:string.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 string =readKeyBoard(limit,true);
return string.equals("")?defaultValue:string;
}
/**
*用于确认选择的输入,该方法从键盘读取Y或N,将其作为方法的返回值
*/
public static char readConfirmSelection() {
char c;
for(;;) {
String string= readKeyBoard(1,false).toUpperCase();
c=string.charAt(0);
if(c=='Y'||c=='N') {
break;
}else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
//从键盘读取值
public 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.println("输入长度(不大于"+limit+")错误,请重新输入");
continue;
}
break;
}
return line;
}
}
//Customer类 封装用户的信息
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
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 class CustomerList {
private Customer customers[];
private int total;
public CustomerList(int totalNumber) {
customers=new Customer[10];
}
public boolean addcustomer(Customer customer) {
if(total>customers.length) {
return false;
}else {
customers[total]=customer;
total++;
return true;
}
}
public boolean replaceCustomer(int index , Customer cust) {
if(index<0||index>=total) {
return false;
}
customers[index]=cust;
return true;
}
public boolean deleteCustomer(int index) {
if(index<0||index>=total){
return false;
}
for(int i =index;i<total-1;i++) {
customers[i]=customers[i+1];
}
customers[total-1]=null;
total--;
return true;
}
public Customer[] getAllCustomers() {
Customer[] cust = new Customer[total];
for(int i =0;i<total;i++) {
cust[i]=customers[i];
}
return cust;
}
public Customer getCustomer(int index) {
if(index<0||index>=total) {
return null;
}
return customers[index];
}
public int getTotal() {
return total;
}
}
//视图类,给管理者直接操作
public class CustomerView {
CustomerList customerlist = new CustomerList(10);
public CustomerView() {
}
public void enterMenu() {
boolean isFlag=true;
while(isFlag) {
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();
System.out.print(" 请选择(1-5):");
char MenuChoose = cmutility.chooseMenu();
switch(MenuChoose) {
case '1'://添加客户
addCustomer();
break;
case '2'://修改客户
modifyCustomer();
break;
case '3'://删除客户
delCustomer();
break;
case '4'://客户列表
listCustomer();
break;
case '5'://退出
System.out.println("请确认是否退出(Y/N)");
char YNchoose =cmutility.chooseYN();
if(YNchoose=='Y') {
isFlag=false;
}
}
}
}
public void addCustomer() {
System.out.println("---添加客户---");
System.out.print("姓名: ");
String nameString = 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(13);
Customer customer=new Customer(nameString, gender, age, phone, email);
boolean isSuccess =customerlist.addcustomer(customer);
if(isSuccess) {
System.out.println("添加成功!");
}else {
System.out.println("客户目录已满,添加失败");
}
}
public void modifyCustomer() {
Customer customer;
int readInt;
for(;;) {
System.out.println("请输入待修改的客户编号(输入-1退出): ");
readInt = cmutility.readInt();
if(readInt==-1) {
return;
}
customer =customerlist .getCustomer(readInt-1);
if(customer==null) {
System.out.println("无法找到此客户");
}else {
break;
}
}
// System.out.println("姓名:("+customer.getName()+"): ");
// customer.setName(cmutility.readString(5, customer.getName()));
// System.out.println("性别:("+customer.getGender()+"): ");
// customer.setGender(cmutility.readChar(customer.getGender()));
// System.out.println("年龄:("+customer.getAge()+"): ");
// customer.setAge(cmutility.readInt());
// System.out.println("电话:("+customer.getPhone()+"): ");
// customer.setPhone(cmutility.readString(13, customer.getPhone()));
// System.out.println("邮箱:("+customer.getEmail()+"): ");
// customer.setEmail(cmutility.readString(13, customer.getEmail()));
//
System.out.print("姓名:("+customer.getName()+"): ");
String nameString =cmutility.readString(4, 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 phoneString =cmutility.readString(13, customer.getPhone());
System.out.print("邮箱:("+customer.getEmail()+"): ");
String emailString=cmutility.readString(13, customer.getEmail());
Customer newCustomer = new Customer(nameString, gender, age, phoneString, emailString);
boolean isReplace =customerlist.replaceCustomer(readInt-1, newCustomer);
if(isReplace) {
System.out.println("修改完成");
}else {
System.out.println("---修改失败---");
}
}
public void delCustomer() {
int readInt;
System.out.println("---删除客户---");
for(;;) {
System.out.println("请输入删除客户的编号(-1退出): ");
readInt = cmutility.readInt();
if(readInt==-1) {
return;
}
Customer customer =customerlist.getCustomer(readInt-1);
if(customer == null) {
System.out.println("无此客户");
}else {
break;
}
}
System.out.println("是否确认删除(Y/N): ");
char chooseYN = cmutility.chooseYN();
if(chooseYN=='Y') {
boolean isDelete = customerlist.deleteCustomer(readInt-1);
if(isDelete) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
return;
}
}
public void listCustomer() {
System.out.println("---客户列表---");
int total =customerlist .getTotal();
if(total==0) {
System.out.println("无客户!");
}else {
Customer[] custs=customerlist .getAllCustomers();
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
for(int i=0;i<total;i++) {
Customer cust=custs[i];
System.out.println((i+1)+"\t"+cust.getName()+"\t"+cust.getGender()+
"\t"+cust.getAge()+"\t"+cust.getPhone()+"\t"+cust.getEmail());
}
}
System.out.println("---显示完毕---");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.enterMenu();
}
}