Customer
package com.atguigu.p2.bean;
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String 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;
}
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;
}
}
CustomerList
package com.atguigu.p2.service;
import com.atguigu.p2.bean.Customer;
public class CustomerList {
private Customer[] customers;
private int total = 0;
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer){
if (total<customers.length) {
customers[total] = customer;
total++;
return true;
}else return false;
}
public boolean replaceCustomer(int index,Customer customer){
if (index>=0 && index<total) {
customers[index] = customer;
return true;
}else return false;
}
public boolean deleteCustomer(int index){
if (index>=0 && index<total) {
for (int i = index; i < total-1; i++) {
customers[i] = customers[i+1];
}
customers[total-1] = null;
total--;
return true;
}else return false;
}
public Customer[] getAllCustomers(){
Customer[] custs = new Customer[total];
for (int i = 0; i < total; i++) {
custs[i] = customers[i];
}
return customers;
}
public Customer getCustomer(int index){
if (index>=0 && index<total) {
return customers[index];
}else return null;
}
public int getTotal() {
return total;
}
}
CustomerView
package com.atguigu.p2.ui;
import com.atguigu.p2.bean.Customer;
import com.atguigu.p2.service.CustomerList;
import com.atguigu.p2.util.CMUtility;
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer customer = new Customer("Tom",'男',23,"123","tom@gmail.com");
customerList.addCustomer(customer);
}
public void enterMainMenu(){
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 select = CMUtility.readMenuSelection();
switch (select) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.out.println("确认退出(Y/N)?");
char confirmExit = CMUtility.readConfirmSelection();
if (confirmExit=='Y') {
isFlag=false;
}
}
}
}
public void addNewCustomer(){
int total = customerList.getTotal();
if (total<customerList.getAllCustomers().length) {
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 tel = CMUtility.readString(11);
System.out.println("客户邮箱:");
String email = CMUtility.readString(15);
Customer cust = new Customer(name,gender,age,tel,email);
customerList.addCustomer(cust);
System.out.println("-----------------添加成功-----------------\t");
}else{
System.out.println("-----------客户列表已满,添加失败-------------\t");
}
}
public void modifyCustomer(){
System.out.println("-----------------修改客户-----------------");
int index;
Customer cust;
for (;;) {
System.out.println("请输入要修改的客户序号(输入-1退出)");
index = CMUtility.readInt();
if (index==-1) {
return;
}
index--;
cust = customerList.getCustomer(index);
if (cust == null) {
System.out.println("无法找到客户!");
} else {
break;
}
}
System.out.println("姓名("+cust.getName()+"):");
String name = CMUtility.readString(10, cust.getName());
System.out.println("性别("+cust.getGender()+"):");
char gender = CMUtility.readChar( cust.getGender());
System.out.println("年龄("+cust.getAge()+"):");
int age = CMUtility.readInt(cust.getAge());
System.out.println("电话("+cust.getPhone()+"):");
String tel = CMUtility.readString(11, cust.getPhone());
System.out.println("邮箱("+cust.getEmail()+"):");
String email = CMUtility.readString(15, cust.getEmail());
Customer newCust = new Customer(name,gender,age,tel,email);
boolean isReplaced = customerList.replaceCustomer(index, newCust);
if (isReplaced) {
System.out.println("-----------------修 改 成 功-----------------\t");
} else {
System.out.println("-----------------修 改 失 败-----------------\t");
}
}
public void deleteCustomer(){
System.out.println("-----------------删除客户-----------------");
Customer cust;
int index;
for (;;) {
System.out.println("请输入要删除的客户序号(输入-1退出)");
index = CMUtility.readInt();
if (index == -1) {
return;
}
index--;
cust = customerList.getCustomer(index);
if (cust == null) {
System.out.println("无法找到客户!");
} else {
break;
}
}
System.out.println("是否确认删除(Y/N)");
char isDelete = CMUtility.readConfirmSelection();
if (isDelete == 'Y') {
boolean deleteSuccess = customerList.deleteCustomer(index);
if (deleteSuccess) {
System.out.println("-----------------删 除 成 功-----------------\t");
} else {
System.out.println("删除失败,请输入正确序号:1->"+customerList.getTotal());
}
}
}
public void listAllCustomers(){
System.out.println("-------------------客 户 列 表------------------\n");
int total = customerList.getTotal();
if (total == 0) {
System.out.println("没有客户记录!");
}else {
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
Customer[] custList = customerList.getAllCustomers();
for (int i = 0; i < total; i++) {
System.out.println(i+1 + "\t" + custList[i].getName()+"\t" +
custList[i].getGender()+"\t" + custList[i].getAge()+"\t" +
custList[i].getPhone()+"\t\t" + custList[i].getEmail()+"\t");
}
}
System.out.println("-----------------客户列表完成-----------------\n");
}
public static void main(String[] args) {
CustomerView CV = new CustomerView();
CV.enterMainMenu();
}
}
CMUtility
package com.atguigu.p2.util;
import java.util.*;
public class CMUtility {
public static void main(String[] args) {
}
private static Scanner scanner = new Scanner(System.in);
private static String readKeyBoard(int limit,boolean blank){
for (;;) {
String str = scanner.nextLine();
if (str.length()>0 && str.length()<=limit) {
return str;
} else if (blank) {
return str;
}else{
System.out.println("请输入长度不超过"+limit+"的指令");
}
}
}
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' ) {
return c;
} else {
System.out.println("选择错误,请重新输入。");
}
}
}
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);
return n;
}catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入。");
}
}
}
public static int readInt(int defaultValue){
int n;
for(;;){
String str = readKeyBoard(2,true);
if (str.equals("")) {
return defaultValue;
}
try{
n = Integer.parseInt(str);
return n;
}catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入。");
}
}
}
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 readConfirmSelection(){
for(;;){
String str = readKeyBoard(1,false).toUpperCase();
char c = str.charAt(0);
if (c=='Y' || c=='N') {
return c;
} else {
System.out.println("选择错误,请输入Y/N");
}
}
}
}