涉及知识点:
1、对象的创建与使用
2、类结构的使用:属性,构造器,方法
3、类的封装
4、数组声明和使用数组的插入,替换,删除
5、关键字的使用this
CMUtility 工具类:用于读取键盘输入信息
import java.util.*;
public class CMUtility {
int tmp;
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 作为返回值。
*/
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位的整数,将其作为方法的范围值
如果用户不输入字符而直接回车,方法以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的字符串, 并将其作为方法的返回值。
*/
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;
}
}
Customer :用于创建用户数据
public class Customer {
private String name;
private char sex;
private int age;
private String telephone;
private String email;
public Customer(String name, char sex, int age, String telephone, String email) {
this.name = name;
this.sex = sex;
this.age = age;
this.telephone = telephone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
CustomerList :用于存储用户数据,对用户数据进行增删改查操作
public class CustomerList {
private Customer[] customers;
private int total;
public CustomerList(){
}
public CustomerList(int number){
customers = new Customer[number];
}
public boolean addCustomer(Customer customer){
if(customer == null){
System.out.println("添加失败");
return false;
}
customers[total++] = customer;
return true;
}
public boolean replaceCustomer(int index,Customer newCustomer){
if(index < 0 || index >= total){
System.out.println("找不到指定客户");
return false;
}
customers[index] = newCustomer;
return true;
}
public boolean deleteCustomer(int index,Customer customer){
if(index < 0 || index >= total){
System.out.println("找不到指定客户");
return false;
}
for(;index < total - 1;index++){
customers[index] = customers[index + 1];
}
customers[total - 1] = null;
total --;
return true;
}
public int getTotal(){
return total;
}
public Customer[] getAllCustomers(){
return customers;
}
public Customer getCustomer(int index){
return customers[index];
}
}
CustomerView :用于展示功能界面(配合CustomerList完成增删改查)
public class CustomerView {
CustomerList customerList = new CustomerList(10);
public CustomerView(){
// Customer customer = new Customer("李四",'男',20,"15685660482","123@qq.com");
// customerList.addCustomer(customer);
}
public void menu() {
while (true) {
System.out.println("-----------客户管理软件-----------");
System.out.println("\t\t1 添加客户\t\t");
System.out.println("\t\t2 修改客户\t\t");
System.out.println("\t\t3 删除客户\t\t");
System.out.println("\t\t4 客户列表\t\t");
System.out.println("\t\t5 退 出\t\t");
System.out.println();
System.out.println("请选择(1-5):");
char menuSelection = CMUtility.readMenuSelection();
switch (menuSelection) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listCustomer();
break;
//退出功能
case '5':
System.out.println("确认是否退出y/n");
char isExit = CMUtility.readConfirmSelection();
if (isExit == 'Y') {
System.out.println("已退出");
return;
}
}
}
}
//添加用户
public void addNewCustomer(){
System.out.println("-----------添加用户-----------");
System.out.println("姓名:");
String name = CMUtility.readString(4);
System.out.println("性别:");
char sex = CMUtility.readChar();
System.out.println("年龄:");
int age = CMUtility.readInt();
System.out.println("电话:");
String telephone = CMUtility.readString(14);
System.out.println("邮箱:");
String email = CMUtility.readString(20);
Customer newcustomer = new Customer(name,sex,age,telephone,email);
boolean isSuccess = customerList.addCustomer(newcustomer);
if(isSuccess){
System.out.println("添加完成");
}else{
System.out.println("添加失败,客户目录已满");
}
}
//修改用户
public void modifyCustomer() {
System.out.println("-----------修改用户-----------");
int number;
Customer cust;
for (; ; ) {
System.out.println("输入用户编号(-1退出)");
number = CMUtility.readInt();
cust = customerList.getCustomer(number - 1);
if (number == -1) {
return;
}
if (cust == null) {
System.out.println("找不到指定用户");
} else break;
}
//Customer cust = customerList.getCustomer(number - 1);
System.out.println("姓名(" + cust.getName() + "):");
String name = CMUtility.readString(4, cust.getName());
System.out.println("性别(" + cust.getSex() + "):");
char sex = CMUtility.readChar(cust.getSex());
System.out.println("年龄(" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.println("电话(" + cust.getTelephone() + "):");
String telephone = CMUtility.readString(20, cust.getTelephone());
System.out.println("邮箱(" + cust.getEmail() + "):");
String email = CMUtility.readString(20, cust.getEmail());
Customer newCust = new Customer(name, sex, age, telephone, email);
boolean isSuccess = customerList.replaceCustomer(number - 1, newCust);
if (isSuccess) {
System.out.println("修改完成");
} else {
System.out.println("修改失败");
}
}
public void deleteCustomer() {
System.out.println("-----------删除用户-----------");
int number;
Customer cust;
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;
}
boolean isDeleteSuccess = customerList.deleteCustomer(number - 1,cust);
if(isDeleteSuccess){
System.out.println("删除成功");
}
}
//列出所有客户
public void listCustomer(){
System.out.println("-----------客户列表-----------");
int total = customerList.getTotal();
if(total == 0){
System.out.println("没有客户记录");
}else {
System.out.println("编号\t\t姓名\t\t\t性别\t\t年龄\t\t电话\t\t\t邮箱");
Customer[] custs = customerList.getAllCustomers();
for (int i = 0; i < total; i++) {
System.out.println((i + 1) + "\t\t" + custs[i].getName() + " \t\t" + custs[i].getSex() + "\t\t" + custs[i].getAge()
+ "\t\t" + custs[i].getTelephone() + "\t\t\t" + custs[i].getEmail());
}
}
}
public static void main(String[] args) {
CustomerView cv = new CustomerView();
cv.menu();
}
}