
- package com.atguigu.p2.util;
-
- import java.util.*;
-
- public class CMUtility {
- public static void main(String[] args) {
- //System.out.println(readMenuSelection());
- }
- private static Scanner scanner = new Scanner(System.in);
- private static String readKeyBoard(int limit,boolean blank){
- for (;;) {
- String str = scanner.nextLine();
- if (str.length()>0 && str.length()<=limit) {
- return str;
- } else if (blank) {
- return str;
- }else{
- System.out.println("请输入长度不超过"+limit+"的指令");
- }
- }
-
- }
-
- 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' ) {
- return c;
- } else {
- System.out.println("选择错误,请重新输入。");
- }
- }
- }
-
- 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);
- }
-
- public static int readInt(){
- //获取年龄
- int n;
- for(;;){
- String str = readKeyBoard(2,false);
- try{
- n = Integer.parseInt(str);
- return n;
- }catch (NumberFormatException e) {
- System.out.println("数字输入错误,请重新输入。");
- }
- }
- }
-
- public static int readInt(int defaultValue){
- //修改年龄信息时,不输入信息直接回车
- int n;
- for(;;){
- String str = readKeyBoard(2,true);
- if (str.equals("")) {
- return defaultValue;
- }
- try{
- n = Integer.parseInt(str);
- return n;
- }catch (NumberFormatException e) {
- System.out.println("数字输入错误,请重新输入。");
- }
- }
- }
-
- public static String readString(int limit){
- //姓名、电话、邮箱的输入
- return readKeyBoard(limit,false);
- }
-
- public static String readString(int limit,String defaultValue){
- //修改姓名、电话、邮箱时,不输入信息直接回车
- String str = readKeyBoard(limit,true);
- return str.equals("") ? defaultValue : str;
- }
-
- public static char readConfirmSelection(){
- //获取确认的输入
- for(;;){
- String str = readKeyBoard(1,false).toUpperCase();
- char c = str.charAt(0);
- if (c=='Y' || c=='N') {
- return c;
- } else {
- System.out.println("选择错误,请输入Y/N");
- }
- }
- }
- }
-
Customer模块
- package com.cms3;
-
- public class Customer {
- private String name;
- private char gender;
- private int age;
- private String tele;
- private String mail;
- public Customer() {
-
- }
- public Customer(String name,char gender, int age, String tele, String mail) {
- this.name = name;
- this.gender = gender;
- this.age = age;
- this.tele = tele;
- this.mail = mail;
- }
- 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 getTele() {
- return tele;
- }
- public void setTele(String tele) {
- this.tele = tele;
- }
- public String getMail() {
- return mail;
- }
- public void setMail(String mail) {
- this.mail = mail;
- }
-
-
- }
CustomerList模块
- package com.cms3;
-
- public class CustomerList {
- private int total = 0;
- Customer[] custList;
- private Customer customer = new Customer("Tom",'m',10,"17717777777","tom@gmail.com");
- public CustomerList() {
-
- }
- public CustomerList(int total) {
- custList = new Customer[total];
- addCustomer(customer);
- }
- public boolean addCustomer(Customer customer) {
- if(total < custList.length) {
- custList[total ++] = customer;
- return true;
- }else {
- return false;
- }
- }
-
- public boolean replaceCustomer(int index, Customer customer) {
- if(index < 0 || index >= total) {
- return false;
- }else {
- custList[index] = customer;
- return true;
- }
- }
-
- public boolean deleteCustomer(int index) {
- if(index < 0 || index >= total) {
- return false;
- }else {
- custList[index] = null;
- for(int i = index; i < total - 1; i ++) {
- custList[i] = custList[i + 1];
- }
- total --;
- return true;
- }
- }
- public Customer getCustomer(int index) {
- if(index >= 0 && index < total) {
- return custList[index];
- }else {
- System.out.println("获取失败");
- return null;
- }
- }
- public Customer[] getAllCustomer() {
- return custList;
- }
- public int getTotal() {
- return total;
- }
-
- }
CustomerView
- package com.cms3;
-
- import com.cms3.Customer;
- import com.cms3.CustomerList;
- import com.atguigu.p2.util.CMUtility;
-
- public class CustomerView {
- private CustomerList custList = new CustomerList(10);
- public static void main(String[] args) {
- CustomerView CV = new CustomerView();
- CV.showMenu();
-
- }
- public void showMenu() {
- boolean isFlag = true;
- while(isFlag) {
- System.out.println("客户关系管理软件");
- System.out.println("1: 添加客户");
- System.out.println("2: 修改客户");
- System.out.println("3: 删除客户");
- System.out.println("4: 显示客户列表");
- System.out.println("5: 退出");
- int choiceMenu = CMUtility.readMenuSelection();
- switch(choiceMenu) {
- case '1':
- addClient();
- break;
- case '2':
- replaceClient();
- break;
- case '3':
- deleteClient();
- break;
- case '4':
- getAllClient();
- break;
- case '5':
- System.out.println("确定要退出吗?(Y/N):");
- char choiceYN = CMUtility.readConfirmSelection();
- if(choiceYN == 'Y') {
- isFlag = false;
- }
- break;
- default:
- System.out.println("输入错误,请重新输入");
- }
- }
- }
- public void addClient() {
- 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 tele = CMUtility.readString(11);
- System.out.println("邮箱");
- String mail = CMUtility.readString(30);
- Customer customer = new Customer(name,gender,age,tele,mail);
- boolean addResult = custList.addCustomer(customer);
- if(addResult) {
- System.out.println("添加成功");
- }else {
- System.out.println("列表已满,添加失败");
- }
-
- }
- public void replaceClient() {
- System.out.println("修改客户");
- System.out.println("请输入要修改的客户编号(-1退出)");
- int index = CMUtility.readInt();
- if(index == -1) {
- return;
- }else {
- Customer customer = custList.getCustomer(index - 1);
- System.out.println("姓名(" + customer.getName() + "):");
- String name = CMUtility.readString(10,customer.getName());
- System.out.println("性别(" + customer.getGender() + "):");
- char gender = CMUtility.readChar(customer.getGender());
- System.out.println("年龄(" + customer.getAge() + "):");
- int age = CMUtility.readInt(customer.getAge());
- System.out.println("电话(" + customer.getTele() + "):");
- String tele = CMUtility.readString(11,customer.getTele());
- System.out.println("邮箱(" + customer.getMail() + "):");
- String mail = CMUtility.readString(20,customer.getMail());
- Customer newCustomer = new Customer(name,gender,age,tele, mail);
- boolean replaceResult = custList.replaceCustomer(index - 1, newCustomer);
- if(replaceResult) {
- System.out.println("修改成功");
- }else {
- System.out.println("修改失败");
- }
- }
-
- }
- public void deleteClient() {
- System.out.println("删除客户");
- System.out.println("请输入要删除的客户编号,(-1退出)");
- int index = CMUtility.readInt();
- if(index == -1) {
- return;
- }else {
- boolean deleteResult = custList.deleteCustomer(index - 1);
- if(deleteResult) {
- System.out.println("删除成功");
- }else {
- System.out.println("删除失败");
- }
- }
-
- }
- public void getAllClient() {
- System.out.println("客户列表");
- int total = custList.getTotal();
- Customer customer[] = custList.getAllCustomer();
- System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
- if(total == 0) {
- System.out.println("列表为空");
- return;
- }else {
- for(int i = 0; i < total; i ++) {
- System.out.println("" + (i + 1)
- + "\t" + customer[i].getName()
- + "\t" + customer[i].getGender()
- + "\t" + customer[i].getAge()
- + "\t" + customer[i].getTele()
- + "\t" + customer[i].getMail());
- }
- }
- }
-
- }