public class CustomerView {
//主函数
public static void main(String[] args) {
CustomerView view=new CustomerView();
view.enterMainMeau();
}
private Customerlist customerlist =new Customerlist(10);//实例化对象,加载10个数组空间
public CustomerView() {
Customer a1=new Customer("靓仔", '男', 18, "15754642234","999555333.@gmial");
customerlist.addCustomer(a1);
}
//显示界面
private void enterMainMeau() {
boolean isFlag =true;
while(isFlag) {
System.out.println("\n-----------------客户信息管理软件-----------------\n");
System.out.println(" 1 添 加 客 户");
System.out.println(" 2 修 改 客 户");
System.out.println(" 3 删 除 客 户");
System.out.println(" 4 客 户 列 表");
System.out.println(" 5 退 出\n");
System.out.print(" 请选择(1-5):");
char c = CMUtility.readMenuSelection();
switch(c) {
case'1':
addNewCustomer();
break;
case'2':
modifyCustomer();
break;
case'3':
deleteCustomer();
break;
case'4':
listAllCustomers();
break;
case'5':
System.out.print("确认是否退出(Y/N)");
char isExit =CMUtility.readConfirmSelection();
if(isExit=='Y') {
isFlag=false;
}
}
}
}
//添加客户
private void addNewCustomer() {
System.out.println("---------------------------添加客户-----------------------------");
System.out.print("姓名");
String name=CMUtility.readString(10);
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(30);
//将上述的数据都封装到对象当中
Customer customer =new Customer(name, gender, age, phone, email);
boolean isSuccess =customerlist.addCustomer(customer);
if(isSuccess) {
System.out.print("***************************添加完成********************************");
}
else {
System.out.print("---------------------------添加失败,客户列表已满-----------------------------");
}
}
//修改客户
private void modifyCustomer() {
System.out.println("-------------------------修改客户信息----------------------------");
while(true) {
Customer cr;
System.out.print("请选择要修改的客户编号(-1退出)");
int number = CMUtility.readInt();
if(number==-1) {
return;
}
cr=customerlist.getCustomer(number-1);
if(customerlist.getCustomer(number-1)==null){
System.out.println("无法找到指定用户");
}
else {//找到相应编号的客户,//修改客户信息
System.out.print("姓名("+cr.getName()+")");
String name =CMUtility.readString(10, cr.getName());
System.out.print("性别("+cr.getGender()+")");
char gender =CMUtility.readChar(cr.getGender());
System.out.print("年龄("+cr.getAge()+")");
int age =CMUtility.readInt(cr.getAge());
System.out.print("电话("+cr.getPhone()+")");
String phone =CMUtility.readString(13, cr.getPhone());
System.out.print("邮箱("+cr.getEmail()+")");
String email =CMUtility.readString(20, cr.getEmail());
Customer newCr=new Customer(name, gender, age, phone, email);
customerlist.replaceCustomer(number-1, newCr);
System.out.print("-----------------修改完成----------------");
return;
}
}
}
//删除客户
private void deleteCustomer() {
while(true) {
System.out.print("请选择删除客户的编号,(-1退出)");
int number=CMUtility.readInt();
if(number==-1) {
return;
}
Customer cr=customerlist.getCustomer(number-1);
if(cr==null) {
System.out.print("无法找到指定客户");
}
else {
System.out.println(number +" 号客户: "+cr.getName());
System.out.print("是否确认删除(Y/N)");
if(CMUtility.readConfirmSelection()=='Y') {
customerlist.deleteCustomer(number-1);
System.out.print("--------------------删除完成----------------");
}
}
}
}
//客户列表
private void listAllCustomers() {
System.out.println("---------------------------客户列表-----------------------------");
int total=customerlist.getTotal();
if(total==0)
{
System.out.print("没有客户记录");
}
else {
System.out.println("编号\t姓名\t性别\t年龄 \t 电话\t\t邮箱");
Customer[] custs = customerlist.getAllCustomer();
for(int i=0;i<custs.length;i++) {
Customer a1=custs[i];
System.out.println((i+1)+"\t"+a1.getName()+"\t"+a1.getGender()+"\t"+a1.getAge()+"\t"+a1.getPhone()+"\t"+a1.getEmail());
}
}
System.out.println("---------------------------******-----------------------------");
}
//退出
}
public class Customer {//javabean
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(String name,char gender,int age, String phone ,String email) {
this.age=age;
this.email=email;
this.gender=gender;
this.name=name;
this.phone=phone;
}
}
import java.util.*;
/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
*/
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;
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.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 str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
*/
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
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.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
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){
return false;
}
else {
customers[total++]=customer;
return true;
}
}
//修改
public boolean replaceCustomer(int index,Customer customer) {
if(index<0||index>=total) {
return false;
}
else {
customers[index]=customer;
return true;
}
}
//删除
public boolean deleteCustomer(int index) {
if(index<0||index>=total) {
return false;
}
else {
for(int i=index;i <total-1;i++) {
customers[i]=customers[i+1];
}
customers[total-1]=null;
total--;
return true;
}
}
//展示客户信息
public Customer[] getAllCustomer() {
Customer[] cust;
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;
}
else
{
return customers[index];
}
}
//获取客户数目
public int getTotal() {
return total;
}
}