这是我的第二个项目 一共分为四部分,分为上下两篇。上篇为CMUtility和customerList,下篇为cusomer和customerView。
/**
* CMUtility工具类:
* 将不同的功能封装为方法,就是可以直接通过调用方法来使用他的功能,而无需具体考虑功能实现细节
* @author free
*
*/
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);
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值
*/
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;
}
/**
*从键盘读取一个长度不超过2位数的整数,并将其作为方法的返回值
*/
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的字符串,并将其作为方法的返回值
*/
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.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;
}
}
public class CustomerList {
private Customer[] customers;
private int total=0;
/**
* 用来初始化Customer数组的构造器
* totalCustomer指定数组的长度
*/
public CustomerList(int totalCustomer) {
customers=new Customer[totalCustomer];
}
/**
* 将指定的客户添加到数组中
* @param customer
* @return true 添加成功
*/
public boolean addCustomer(Customer customer) {
if(total >=customers.length) {
return false;
}
customers[total]=customer;
total++;
return true;
}
/**
* 修改指定索引位置上的客户信息
* @param index
* @param cust
* @return true 修改成功
*/
public boolean replaceCustomer(int index,Customer cust) {
if(index<0||index>=total) {
return false;
}else {
customers[index]=cust;
return true;
}
}
/**
* 删除指定位置上的客户
* @param index
* @return
*/
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;
}
/**
* 获取所有客户信息
* @return
*/
public Customer[] getAllCustomers() {
Customer[] custs=new Customer[total];
for(int i=0;i<total;i++) {
custs[i]=customers[i];
}
return custs;
}
/**
* 获取指定位置上的客户
* @param index
* @return
*/
public Customer getCustomer(int index) {
if(index<0||index>=total) {
return null;
}
return customers[index];
}
/**
* 获取存储的客户的数量
* @return
*/
public int getTotal() {
return total;
}
}