package com.atguigu.p2;
import java.util.*;/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);/**
用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
*/
public staticcharreadMenuSelection(){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("选择错误,请重新输入:");}elsebreak;}return c;}/**
从键盘读取一个字符,并将其作为方法的返回值。
*/
public staticcharreadChar(){
String str =readKeyBoard(1, false);return str.charAt(0);}/**
从键盘读取一个字符,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public staticcharreadChar(char defaultValue){
String str =readKeyBoard(1, true);return(str.length()==0)? defaultValue : str.charAt(0);}/**
从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
*/
public staticintreadInt(){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 staticintreadInt(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){returnreadKeyBoard(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 staticcharreadConfirmSelection(){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;elsecontinue;}if(line.length()<1|| line.length()> limit){
System.out.print("输入长度(不大于"+ limit +")错误,请重新输入:");continue;}break;}return line;}}
> 客户
package com.atguigu.p2;
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public Customer(String name,char gender,int age){this(name, gender, age,"","");}
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 voidsetName(String name){
this.name = name;}
public chargetGender(){return gender;}
public voidsetGender(char gender){
this.gender = gender;}
public intgetAge(){return age;}
public voidsetAge(int age){
this.age = age;}
public String getPhone(){return phone;}
public voidsetPhone(String phone){
this.phone = phone;}
public String getEmail(){return email;}
public voidsetEmail(String email){
this.email = email;}
public String getDetails(){return name +"\t"+ gender +"\t"+ age +"\t\t"+ phone +"\t"+ email;}}
> 客户列表
package com.atguigu.p2;
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;
customers[total++]= customer;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]= null;return true;}
public Customer[]getAllCustomers(){
Customer[] custs = new Customer[total];for(int i =0; i < total; i++){
custs[i]= customers[i];}return custs;}
public intgetTotal(){return total;}
public Customer getCustomer(int index){if(index <0|| index >= total)return null;return customers[index];}}