Customer类:
1.属性的封装
2.get和set的使用
(1.)void setxxx(参数){this.xxx=xxx}没有返回值
(2.) int getxxx(){return xxx;} 有返回值
3.构造函数
public 类名(){}
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
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 int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public char getGender(){
return gender;
}
public void setGender(char gender){
this.gender=gender;
}
public Customer(){
}
public Customer(String name,int age,char gender,String phone,String email){
this.gender=gender;
this.name=name;
this.age=age;
this.phone=phone;
this.email=email;
}
}
CustomerList类
1.
// CustomerList是Customer对象的管理模块,
// 内部用数组管理一组Customer对象,并提供
// 相应的添加,修改,删除,遍历方法,供CustomerView 调用。
public class CustomerList {
private static int total;
private static Customer[] customers;//客户的对象数组
//记录以保存客户对象的数量
public CustomerList(int totalCustomer){
customers=new Customer[totalCustomer];
}
//增
public boolean addCustomer(Customer customer){
if(total<0||total>=customers.length){
return false;
}else{
customers[total]=customer;
total++;
}
return true;
}
//改
//修改索引位置的客户信息
//cust是修改后的数值
public boolean replaceCustomer(int index, Customer cust) {
if(index<0||index>=total){
return false;
}
customers[index]=cust;
return true;
}
//删
//删除指定位置上的客户
public boolean deleateCustomer(int index){
if(0<index||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 static Customer[] getallCustomers(){
Customer[] custs=new Customer[total];
for(int i=0;i<total;i++){
custs[i]=customers[i];
}
return custs;
}
//查(查询单个)
public Customer getCustomers(int index){
if(index<0||index>=total){
return null;
}
return customers[index];
}
//获取存储的客户的数量
public int getTotal(){
return total;
}
}
CustomerView类
//主模块,负责菜单的显示和处理用户的操作
public class CustomerView {
//创建10个客户对象的CustomerList的对象
private CustomerList customerlist=new CustomerList(10);
public CustomerView(){
Customer customer=new Customer("欧文",34,'男',"12334556","123@qq.com");
customerlist.addCustomer(customer);
}
//显示《客户信息管理软件》界面的方法
public void enterMainMenu() {
//while循环 列表需要重复出现在页面当中。
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("请选择(1-5):");
char menu=CMUtility.readMenuSelection();
switch (menu){
case '1':{
addNewCustomer();
break;}
case '2':{
modifyCustomer();
break;}
case '3':{
deleteCustomer();
break;}
case '4':{
listAllCustomers();
break;}
case '5': {
System.out.println("退出");
System.out.println("确认是否退出(Y/N):");
char isExit = CMUtility.readComfirmSelection();
if (isExit == 'Y') {
isFlag = false;
System.out.println("退出成功");
}
break;
}
}
}
}
//1.添加客户
public void addNewCustomer(){
System.out.println("#####显示添加客户#####");
System.out.println("姓名");
String name=CMUtility.readString(10);
System.out.println("性别");
char gender=CMUtility.readChar();
System.out.println("年龄");
int age=CMUtility.readInt();
System.out.println("手机号");
String phone=CMUtility.readString(13);
System.out.println("邮箱");
String email=CMUtility.readString(23);
Customer c=new Customer(name,age,gender,phone,email);
customerlist.addCustomer(c);
}
//2.修改客户
public void modifyCustomer() {
System.out.println("#####显示修改客户#####");
int index=0;
while(true) {
System.out.println("请选择要修改的客户编号(-1退出)");
index=CMUtility.readInt();
if(index>customerlist.getTotal()||index==0) {
System.out.println("找不到客户");
}else if(index==-1) {
return;
}else break;
}
System.out.print("姓名("+customerlist.getCustomers(index-1).getName()+"):");
String name = CMUtility.readString(10, customerlist.getCustomers(index-1).getName());
System.out.print("性别("+customerlist.getCustomers(index-1).getGender()+"):");
char gender = CMUtility.readChar(customerlist.getCustomers(index-1).getGender());
System.out.print("年龄("+customerlist.getCustomers(index-1).getAge()+"):");
int age = CMUtility.readInt(customerlist.getCustomers(index-1).getAge());
System.out.print("手机号("+customerlist.getCustomers(index-1).getPhone()+"):");
String phone = CMUtility.readString(13, customerlist.getCustomers(index-1).getPhone());
System.out.print("电子邮箱("+customerlist.getCustomers(index-1).getEmail()+"):");
String email = CMUtility.readString(30, customerlist.getCustomers(index-1).getEmail());
Customer customer = new Customer(name,age,gender, phone, email);
boolean isTrue = customerlist.replaceCustomer(index-1, customer);
System.out.println("修改成功");
}
//3.删除客户
public void deleteCustomer(){
System.out.println("#####显示删除客户#####");
int number=0;
for(;;){
System.out.println("请选择修改客户编号(-1):");
number=CMUtility.readInt();
if(number==-1) {
return;
}
Customer customer= customerlist.getCustomers(number-1);
if(customer==null){
System.out.println("无法找到指定用户");
}
break;
}
System.out.println("确认是否删除(Y/N):");
char isDelete=CMUtility.readComfirmSelection();
if(isDelete=='Y'){
boolean deleteSuccess= customerlist.deleateCustomer(number-1);
if(deleteSuccess){
System.out.println("--------成功");
}else {
System.out.println("失败");
}
}else {
return;
}
}
//4.显示客户列表
public void listAllCustomers(){
System.out.println("-----------------------显示客户列表----------------------");
int total=customerlist.getTotal();
if(total==0){
System.out.println("没有客户记录!");
}
else {
System.out.println("a\tb\tc\t\td\t\te\t\t\tf");
Customer[] custs=CustomerList.getallCustomers();
for(int i=0;i<custs.length;i++){
Customer cust=custs[i];
System.out.println((i+1)+"\t"+cust.getAge()+"\t"+cust.getGender()+"\t"+cust.getEmail()
+"\t"+cust.getName()+"\t"+cust.getPhone());
}
}
System.out.println("-----------------------客户列表完成----------------------");
}
public static void main(String[] args) {
CustomerView view =new CustomerView();
view.enterMainMenu();
}
}
CMUtility类
//CMUtility 工具类
//将不同的功能封装为方法
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);//方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引为1
if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6') {
System.out.println("选择错误,请重新输入");
} else {
break;
}
}
return c;
}
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入:");
}
}
return n;
}
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.println("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("") ? defaultValue : str;
}
public static char readComfirmSelection() {
char c;
for (;;) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.println("选择错误,请重新输入");
}
}
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.println("输入长度小于1或大于" + limit + ",错误,请重写输入");
continue;
}
break;
}
return line;
}
}