这个案例可以实现增加客户信息,查找指定位置的客户信息,显示所有客户信息,删除客户信息,以及退出程序的5个基本操作。下面将分为4个类来完成。
一:Customer类用于初始化客户的信息,包括姓名,性别,年龄,电话,邮箱。所有属性都用Java的反射机制封装起来了。
public class Customer {
private String name;//客户姓名
private char gender ;//客户性别
private int age;//客户年龄
private String phonenumber;//客户号码
private String email;//客户邮箱
public Customer() {
//无参构造器“ALT+shift+s”
}
public Customer(String name, char gender, int age, String phonenumber, String email) {
//有参构造器
this.name = name;
this.gender = gender;
this.age = age;
this.phonenumber = phonenumber;
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 getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
二.CustomerList这个类,是用来完成客户信息的增删改查的基本功能
/**
* Description 当前类为Custoemr对象的管理模块,内部使用数组管理一组Customer对象
* @author 23147
*
*/
public class CustomerList {
private Customer[]customers;//声明Customer对象数组的名称
private int total=0;
public CustomerList(int totalCustomer) {
customers= new Customer[totalCustomer];//通过构造器初始化数组,告诉我们这个客户的数组有多长
}
public boolean addCustomer(Customer customer) {
if(total>=customers.length) {
return false;
}
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[] custs=new Customer[total];//为什么这里要新增一个数组,因为customer 中的有些元素时null;
for (int i = 0; i < total; i++) {
custs[i]=customers[i];
}
return custs;
}
public Customer getCustomer(int index) {
if(index<0||index>=total) {
return null;}
return customers[index];
}
public int getTotal() {
return total;
}
}
三.CMUtility类是与用户交互的类,获取用户的输入值
* 将不同的的功能封装为方法,就是可以直接通过调用方法使用它的功能,从而无需考虑具体的功能实现
* @author 23147
*
*/
import java.util.Scanner;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;
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.println("选择错误,请重新输入:");
} else break;
}return c;
}
/**
* 从键盘读取一个字符,将其作为方法的返回值。
*/
public static char readChar() {
String str =readkeyBoard(1,false);
return str.charAt(0);
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值,如果用户不输入字符直接回车,方法将以 defaultvalue 作为返回值。
* @param defaultValue
* @return
*/
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.println("数字输入错误,请重新输入:");
}
}return n;
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值,如果用户不输入字符直接回车,方法将以 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.println("数字输入错误,请重新输入:");
}
}return n;
}
/**
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法得返回值
* @param limit
* @return
*/
public static String readString(int limit) {
return readkeyBoard(limit,false);
}
/**
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法得返回值,如果用户不输入而直接回车,方法将以defaultValue作为返回值
* @param limit
* @param defaultValue
* @return
*/
public static String readString(int limit,String defaultValue) {
String str=readkeyBoard(limit,true);
return str.equals("") ? defaultValue :str;
}
/**
* 用于选择确认的输入。该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值。
* @return
*/
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.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("输入长度不大于"+limit+"错误请重新输入");
continue;
}break;
}return line;
}
}
四.CustomerView类为最终效果的展示以及调用
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer customer=new Customer("陈涛",'男',18,"1325555","2265");
customerList.addCustomer(customer);
}
public void setCustomerList(CustomerList customerList) {
this.customerList = customerList;
}
/**
* 显示用户管理软件界面
*
*/
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.退出 \n ");
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.print("确认是否退出Y/N");
char isExit = CMUtility.readConfirmSelection();
if (isExit == 'Y') {
isFlag = false;
}
}
}
}
/**
* 添加用户
*/
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 phonenumber=CMUtility.readString(12);
System.out.println("输入邮箱");
String email=CMUtility.readString(10);
Customer customer=new Customer(name, gender, age, phonenumber, email);
boolean isSuccess=customerList.addCustomer(customer);
if(isSuccess) {
System.out.println("------------添加完成------------");
}else {
System.out.println("--------------客户端目录已满---------");
}
}
/**
* 修改用户
*/
public void modifyCustomer() {
System.out.println("-----------------------修改客户----------------------");
Customer cust;
int number;
for(;;) {
System.out.println("请修改指定客户(-1)退出:");
number =CMUtility.readInt();
if(number==-1) {
return;
}
cust=customerList.getCustomer(number-1);
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.getPhonenumber()+"):");
String phonenumber=CMUtility.readString(12,cust.getPhonenumber());
System.out.println("邮箱("+cust.getEmail()+"):");
String email=CMUtility.readString(30, cust.getEmail());
Customer newcust=new Customer(name, gender, age, phonenumber, email);
customerList.replaceCustomer(number-1, newcust);
boolean isReplaced=customerList.replaceCustomer(number-1, newcust);
if(isReplaced) {
System.out.println("--------------修改成功-----------------");
}else {
System.out.println("---------------修改失败--------------------");
}
}
/**
* 删除用户
*/
public void deleteCustomer() {
int number;
for(;;) {
System.out.println("请选择指定位置删除客户编号(-1)退出");
number=CMUtility.readInt();
if(number==-1) {
return;
}
Customer customer=customerList.getCustomer(number-1);
if(customer==null) {
System.out.println("无法找到指定客户!");
}else {
break;
}}
//找到了指定位置的客户
System.out.println("确认是否删除(‘Y’,‘N’)");
char isdelete=CMUtility.readConfirmSelection();
if (isdelete=='Y') {
boolean deletesuccess=customerList.deleteCustomer(number-1);
if(deletesuccess) {
System.out.println("--------------删除成功----------");
}else {
System.out.println("---------刪除失敗--------------");
}
}else {
return;
}
}
/**
* 显示用户
*/
public void listAllCustomers() {
System.out.println("----------客户列表----------------");
int total = customerList.getTotal();
if (total == 0) {
System.out.println("没有客户记录");
} else {
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
Customer[] custs = customerList.getAllCustomers();
for (int i = 0; i < custs.length; i++) {
System.out.println((i + 1) + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t"
+ custs[i].getAge() + "\t" + custs[i].getPhonenumber() + "\t" + custs[i].getEmail());
}
}
System.out.println("--------客户列表完成---------------");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
//view.setCustomerList(new CustomerList(10));
view.enterMainmenu();
}
}
案例总结:通过这个案例进一步理解了面向对象的思想,以及方法的调用,数组的多种使用效果,我个人觉得通过这个案例学习收获还是很大的。希望在学习Java语言这条路,继续加油!!!