最近用面向对象的思想做了一个账户管理软件的练习。
这个软件主要分为三个模块:
CustomerView为主模块,负责菜单的显示和处理用户操作。
CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历方法,供CustomerView调用。
Customer为实体对象,用来封装客户信息。
Customer的设计
public class Customer {
String name;
char gender;
int age;
String phone;
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的设计
public class CustomerList {
Customer[] customers;
int total = 0;
public CustomerList(int lengthCust) {
customers = new Customer[lengthCust];
}
// 添加客户
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;
}else {
customers[total] = customer;
total++;
return true;
}
}
//替换客户
public boolean replaceCustomer(int index,Customer cust) {
if (index >= total || index < 0) {
return false;
}else {
customers[index] = cust;
return true;
}
}
//删除客户
public boolean deleteCustomer(int index) {
if (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[] getAllCustomers() {
Customer[] allCustomers = new Customer[total];
for (int i = 0; i < total; i++) {
allCustomers[i] = customers[i];
}
return allCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
public int getTotal() {
return total;
}
// 以下是测试用的
// public static void main(String[] args) {
// CustomerList custl = new CustomerList(10);
// Customer cust = new Customer("wantao",'男',45,"123123","12123");
// custl.addCustomer(cust);
// System.out.println(custl.getCustomer(0).name);
//
// }
}
CustomerView的设计
public class CustomerView {
CustomerList customerList = new CustomerList(10);
// 以下被注释掉的是测试。。。。
// public CustomerView() {
// Customer customer = new Customer("王",'男',23,"123","123");
// customerList.addCustomer(customer);
// }
public void enterMainMenu() {
boolean isFlag = true;
while(isFlag) {
System.out.println("------------客户信息管理软件----------\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 sele = CMUtility.readMenuSelection();
switch(sele) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.out.println("确认是否退出(Y/N)");
char isExit = CMUtility.readConfirmSelection();
if (isExit == 'Y') {
System.out.println("退出成功");
isFlag = false;
}
}
}
}
private 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 phone = CMUtility.readString(11);
System.out.println("邮箱:");
String email = CMUtility.readString(30);
Customer cust = new Customer(name,gender,age,phone,email);
boolean exer = customerList.addCustomer(cust);
if (exer) {
System.out.println("------------添加完成-----------");
}else {
System.out.println("------------添加失败-----------");
}
System.out.println(customerList.getCustomer(0).getAge());
}
private void modifyCustomer() {
System.out.println("------------修改客户-----------");
System.out.println("请选择待修改客户编号(-1)退出:");
int number = CMUtility.readInt();
if (number == -1) {
return;
}
Customer cust = customerList.getCustomer(number-1);
if (cust == null) {
System.out.println("无此号人");
return;
}
System.out.println("姓名("+cust.name+"):");
String name = CMUtility.readString(10, cust.name);
System.out.println("性别("+cust.gender+"):");
char gender = CMUtility.readChar(cust.gender);
System.out.println("年龄("+cust.age+"):");
int age = CMUtility.readInt(cust.age);
System.out.println("电话("+cust.phone+"):");
String phone = CMUtility.readString(13, cust.phone);
System.out.println("邮箱("+cust.email+"):");
String email = CMUtility.readString(30, cust.email);
Customer custReplace = new Customer(name,gender,age,phone,email);
boolean isWork = customerList.replaceCustomer(number-1, custReplace);
// customerList.getCustomer(number-1) = custReplace;
if (isWork) {
System.out.println("替换成功");
}else {
System.out.println("替换失败,无此用户");
}
}
private void deleteCustomer() {
System.out.println("------------删除客户-----------");
System.out.println("请选择待删除客户编号(-1)退出:");
int number = CMUtility.readInt();
if (number == -1) {
return;
}
System.out.print("确认是否删除(Y/N):");
char isDele = CMUtility.readConfirmSelection();
if (isDele == 'Y') {
boolean isWork = customerList.deleteCustomer(number - 1);
if (isWork) {
System.out.println("-----------删除完成----------");
}else {
System.out.println("-----------删除失败,无此客户---");
}
}
}
private void listAllCustomers() {
System.out.println("------------客户列表-----------");
System.out.println("编号"+"\t姓名"+"\t性别"+
"\t年龄"+"\t电话"+"\t\t邮箱");
Customer[] allc = customerList.getAllCustomers();
for (int i = 0; i < allc.length; i++) {
// System.out.println(allc.length);
System.out.println((i+1)+"\t"+allc[i].getName()
+"\t"+allc[i].getGender()+"\t"+
allc[i].getAge()+"\t"+allc[i].getPhone()
+"\t"+allc[i].getEmail());
}
System.out.println("------------客户列表完成-----------");
}
public static void main(String[] args) {
CustomerView customerView = new CustomerView();
customerView.enterMainMenu();
}
}
工具类:CMUtility
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;
}
}