Java-CMS

 

Utility

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());
			}
		}
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值