练习基本的MVC设计模式,创建customer类,提供基本属性和对应的存取方法
package bean;
public class Customer {
private String name;
private int age;
private char gender;
private String phone;
private String email;
public Customer() {
}
public Customer(String name, int age, char gender, String phone, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getemail() {
return email;
}
public void setemail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void getInfo() {
System.out.println("客户姓名: " + name);
System.out.println("客户性别: " + gender);
System.out.println("客户年龄: " + age);
System.out.println("客户电话: " + phone);
System.out.println("客户邮箱: " + email);
}
}
创建customerList类,提供对象数组的增删改查方法
package controller;
import bean.Customer;
public class CustomerList {
private int total = 0;
private int limite = 9999;
private Customer[] customers;
public CustomerList() {
}
public boolean addCustomer(Customer customer) {
total++;
Customer[] customersTemp = new Customer[total];
customersTemp[total - 1] = customer;
for (int i = 0; i < customersTemp.length - 1; i++) {
customersTemp[i] = customers[i];
}
customers = customersTemp;
if(total >= 0 && total <= limite) {
return true;
}else{
return false;
}
}
public boolean replaceCustomer(int index,Customer customer) {
if(index < 0 || index > total - 1) {
return false;
}else {
customers[index] = customer;
return true;
}
}
public boolean deleteCustomer(int index) {
if(index < 0 || index > total - 1) {
return false;
}else {
total--;
if(total >= 0) {
Customer[] customersTemp = new Customer[total];
for (int i = 0; i < total; i++) {
if(i < index) {
customersTemp[i] = customers[i];
}else {
customersTemp[i] = customers[i + 1];
}
}
customers = customersTemp;
return true;
}else {
return false;
}
}
}
public int getTotal() {
return total;
}
public Customer[] getAllcustomers() {
return customers;
}
public Customer getCustomer(int index) {
if(index < 0 || index > total - 1) {
return null;
}else {
return customers[index];
}
}
//临时测试
public static void main(String[] args) {
CustomerList c = new CustomerList();
Customer a = new Customer();
Customer b = new Customer();
Customer d = new Customer();
Customer e = new Customer();
a.setAge(10);
b.setAge(20);
d.setAge(30);
c.addCustomer(a);
c.addCustomer(b);
System.out.println(c.getTotal());
System.out.println(c.getAllcustomers()[0].getAge());
System.out.println(c.deleteCustomer(0));
System.out.println(c.getAllcustomers()[0].getAge());
System.out.println(c.deleteCustomer(0));
System.out.println(c.getTotal());
System.out.println(c.replaceCustomer(0, e));
c.addCustomer(d);
System.out.println(c.getCustomer(0).getAge());
System.out.println(c.getTotal() == c.getAllcustomers().length);
}
}
创建customerUtility类,提供键盘输入交互的方法
package util;
import java.util.*;
public class CustomerUtility {
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;
}
}
创建customerView类,提供用户交互界面
,并调用其他类实现基本功能
package view;
import bean.Customer;
import controller.CustomerList;
import util.CustomerUtility;
public class CustomerView {
private CustomerList customerList = new CustomerList();
public CustomerView() {
Customer customer1 = new Customer("赵四", 22, '男', "13366667788", "[email protected]");
Customer customer2 = new Customer("钱秀", 26, '女', "15544332345", "[email protected]");
Customer customer3 = new Customer("孙杨", 33, '男', "15623451678", "[email protected]");
customerList.addCustomer(customer1);
customerList.addCustomer(customer2);
customerList.addCustomer(customer3);
}
public void mainMenu() {
boolean mark = true;
do {
System.out.println("\n------------------客户信息管理系统------------------");
System.out.println("\t\t 1 :添加客户");
System.out.println("\t\t 2 :修改客户");
System.out.println("\t\t 3 :删除客户");
System.out.println("\t\t 4 :客户列表");
System.out.println("\t\t 5:退出程序");
System.out.println("\t\t 请选择:1 - 5");
char meunSelect = CustomerUtility.readMenuSelection();
switch (meunSelect) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
showAllCUstomers();
break;
case '5':
System.out.println("请确认是否退出“Y/N”");
char confirmation = CustomerUtility.readConfirmSelection();
if(confirmation == 'Y') {
mark = false;
System.out.println("程序退出");
return;
}
break;
}
}
while(mark);
}
public void addNewCustomer() {
while (true) {
Customer c = new Customer();
System.out.println("请输入客户姓名");
c.setName(CustomerUtility.readString(50));
System.out.println("请输入客户性别");
c.setGender(CustomerUtility.readChar());
System.out.println("请输入客户年龄");
c.setAge(CustomerUtility.readInt());
System.out.println("请输入客户电话");
c.setPhone(CustomerUtility.readString(11));
System.out.println("请输入客户邮箱");
c.setemail(CustomerUtility.readString(50));
System.out.println("客户信息录入完毕,请确认是否添加“Y/N”");
char confirmation = CustomerUtility.readConfirmSelection();
if(confirmation == 'Y') {
boolean mark = customerList.addCustomer(c);
if(mark) {
System.out.println("第" + customerList.getTotal() + "位客户添加成功");
}else {
System.out.println("添加失败,客户数量已满");
return;
}
}
System.out.println("是否继续添加客户(Y/N)");
char confirmation1 = CustomerUtility.readConfirmSelection();
if(confirmation1 == 'N') {
return;
}
}
}
public void modifyCustomer() {
int index;
while (true) {
for(;;) {
System.out.println("请输入想要修改的客户的编号");
index = CustomerUtility.readInt();
if(index < 1 || index > customerList.getTotal()) {
System.out.println("编号超出范围,请重新输入");
continue;
}else {
break;
}
}
Customer c = new Customer();
System.out.println("请输入客户姓名");
c.setName(CustomerUtility.readString(50, customerList.getCustomer(index - 1).getName()));
System.out.println("请输入客户性别");
c.setGender(CustomerUtility.readChar(customerList.getCustomer(index - 1).getGender()));
System.out.println("请输入客户年龄");
c.setAge(CustomerUtility.readInt(customerList.getCustomer(index - 1).getAge()));
System.out.println("请输入客户电话");
c.setPhone(CustomerUtility.readString(11, customerList.getCustomer(index - 1).getPhone()));
System.out.println("请输入客户邮箱");
c.setemail(CustomerUtility.readString(50, customerList.getCustomer(index - 1).getemail()));
System.out.println("客户信息录入完毕,请确认是否修改“Y/N”");
char confirmation = CustomerUtility.readConfirmSelection();
if(confirmation == 'Y') {
boolean mark = customerList.replaceCustomer(index - 1, c);
if(mark) {
System.out.println("第" + index + "号客户修改成功");
}else {
System.out.println("修改失败");
}
}
System.out.println("是否继续修改客户“Y/N”");
char confirmation1 = CustomerUtility.readConfirmSelection();
if(confirmation1 == 'N') {
return;
}
}
}
public void deleteCustomer() {
while(true) {
int index;
for(;;) {
System.out.println("请输入想要删除的客户的编号(0为退出删除操作)");
index = CustomerUtility.readInt();
if(index == 0){
return;
}else if(index < 1 || index > customerList.getTotal()) {
System.out.println("编号超出范围,请重新输入");
continue;
}else {
break;
}
}
String tempName = customerList.getCustomer(index - 1).getName();
System.out.println("请确认是否删除姓名为"+ tempName + "的第" + index + "号客户(Y/N)");
char confirmation = CustomerUtility.readConfirmSelection();
if(confirmation == 'Y') {
boolean mark = customerList.deleteCustomer(index - 1);
if(mark) {
System.out.println("姓名为" + tempName + "的第" + index + "号客户删除成功");
System.out.println("当前还有" + customerList.getTotal() + "个客户");
}else {
System.out.println("删除失败");
}
}
System.out.println("是否继续删除客户“Y/N”");
char confirmation1 = CustomerUtility.readConfirmSelection();
if(confirmation1 == 'N') {
return;
}
}
}
public void showAllCUstomers() {
System.out.println("------------------------客户信息列表 ------------------------");
if(customerList.getTotal() == 0) {
System.out.println("当前没有客户信息");
}
System.out.println("客户编号 \t姓名\t性别\t年龄\t 电话\t\t邮箱");
for (int i = 0; i < customerList.getAllcustomers().length; i++) {
System.out.println((i + 1) + "\t " + customerList.getAllcustomers()[i].getName() + "\t"
+customerList.getAllcustomers()[i].getGender() + "\t"
+customerList.getAllcustomers()[i].getAge() + "\t"
+customerList.getAllcustomers()[i].getPhone() + "\t"
+customerList.getAllcustomers()[i].getemail() + "\n");
}
System.out.println("------------------------列表完成------------------------");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.mainMenu();
}
}