尚硅谷项目二

在项目二的基础上添加了一些功能

 

 

 

客户类:

package Customer;

public class Customer {
	private int number;   //客户编号
    private String name; //客户姓名
    private char gender; //性别
    private int age; //客户年龄
    private String phone;//客户电话号码
    private String email;//客户电子邮箱'
    
    
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	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;
	}
	
	public Customer(){}
	public Customer(String name, char gender, int age, String phone, String email) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = email;
	}
    
}

客户数组类:

package CustomerList;
import Customer.*;
import CustomerTool.CustomerTool;
public class CustomerList {
	private int numberof = 1;
	private  Customer[] customers;
	private int total = 0;
	//初始化数组的长度
	public CustomerList(int totalCustomer) {
	     customers = new Customer[totalCustomer];
	} 
	
	public boolean addCustomer(Customer customer){
		if(total>=customers.length){
			return false;
		}	
		customers[total] = customer;
		customers[total++].setNumber(numberof++);		
		return true;
	}
  
    public boolean replaceCustomer(int index,Customer customer){
    	if(index<0||index>= total){
    		return false;
    	}
    	System.out.print("是否需要修改姓名:"+customers[index-1].getName()+" 如果需要请在冒号后面修改,不需要则按回车跳过"+"      :");
		String name = CustomerTool.readString(10,customers[index-1].getName());
		System.out.print("是否需要修改性别:"+customers[index-1].getGender()+" 如果需要请在冒号后面修改,不需要则按回车跳过"+"      :");
		char gender = CustomerTool.readChar(customers[index-1].getGender());
		System.out.print("是否需要修改年龄:"+customers[index-1].getAge()+" 如果需要请在冒号后面修改,不需要则按回车跳过"+"      :");
		int age = CustomerTool.readInt(customers[index-1].getAge());
		System.out.print("是否需要修改电话号码"+customers[index-1].getPhone()+ "如果需要请在冒号后面修改,不需要则按回车跳过"+"      :");
		String phone = CustomerTool.readString(15,customers[index-1].getPhone());
		System.out.print("是否需要修改邮箱:"+customers[index-1].getEmail()+" 如果需要请在冒号后面修改,不需要则按回车跳过"+"      :");
		String email = CustomerTool.readString(20, customers[index-1].getEmail());
		customers[index-1].setName(name);
		customers[index-1].setGender(gender);
		customers[index-1].setAge(age);
		customers[index-1].setPhone(phone);
		customers[index-1].setEmail(email);
    	return true;  
    }
    public boolean InsertInformation(int index,Customer customer){
    	if(index < 0||index >= total){
    		System.out.println("插入失败!");
    		return false;
    	}
    	total++;
    	numberof ++;
    	for(int i = total-2;i>=index;i--){
    		customers[i+1] = customers[i];
    		customers[i+1].setNumber(customers[i+1].getNumber()+1);	
    	}  
    		customers[index] = customer; 
    		customers[index].setNumber(index+1);
    		return true;
    	
    }
    public boolean deleteCustomer(int index){
    	if(index <0||index >=total){
    		return false;
    	}
    	for(int i = index;i<total-1;i++){
    		customers[index] = customers[i+1];
    		customers[index].setNumber(customers[index].getNumber()-1);
    	}
    	customers[--total] = null;
    	
    	return true;
    }
    public Customer[] getAllCustomer(){
    	Customer[] cust = new Customer[total];
    	for(int i = 0;i<total;i++){
    		cust[i] = customers[i];
    	}
    	return cust;	
    }
    public Customer  getCustomer(int index){
    	if(index <0||index >=total){
    		return null;
    	}
    	return customers[index];
    }
    public int getTotal(){
    	return total;
    }
  
}

输入工具类:

package CustomerTool;
import CustomerList.*;import java.util.*;

public class CustomerTool {
	public static void main(String[] args) {
		//System.out.println(readMenuSelection());
	}
	private static Scanner scanner = new Scanner(System.in);
	/**
	 * 用于界面菜单的选择。该方法读取键盘用户键入的‘1’-‘7’的任意字符,方法返回。
	 * 
	 */
	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'&&c!='6'&&c!='7') {
				System.out.println("选择错误,请重新输入:");
			} else break;
		}
		return c;
	}
	
	/**
	 * 从键盘读取一个字符,并将其作为方法的返回值。
	 * 获取性别
	 */
	public static char readChar(){
		String str = readKeyBoard(10,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;
	}
	/**
	 *从键盘读取一个字符,并将其作为方法的返回值。
	 *如果用户不输入字符而回车,方法将以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的字符串,并将其作为方法的返回值。
	 * 如果用户不输入字符而直接回车,方法将以defaultVaue作为返回值。
	 */
	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.println("输入长度(不大于" + limit + ")错误,请重新输入");
				continue;
			}
			break;
		}
		return line;
		
	}

}

视图类:

package CustomerView;
import Customer.Customer;
import CustomerList.*;
import CustomerTool.*;

public class CustomerView {
	private CustomerList customerList = new CustomerList(10);
	
	public CustomerView(){
		Customer customer0 = new Customer("张三",'男',20,"123456","123456@qq.com");
		Customer customer1 = new Customer("李四",'女',20,"1234567","1234567@qq.com");
		Customer customer2 = new Customer("王五",'男',21,"12345678","12345678@qq.com");
		Customer customer3 = new Customer("Eason",'男',21,"123456789","123456789@qq.com");
		customerList.addCustomer(customer0);
		customerList.addCustomer(customer1);
		customerList.addCustomer(customer2);
		customerList.addCustomer(customer3);
	}
	
	public void addNewCustomer(){
		 System.out.println("--------------添加客户------------");
		 System.out.println("如果不想添加,可输入(-1)退出,继续添加即可输入任意数字");
		 int choice = CustomerTool.readInt();
		 if(choice == -1){
			 return;
		 }
		 System.out.println("请在冒号后面输入!!");
		 System.out.print("姓名:");
		 String name = CustomerTool.readString(10);
		 System.out.print("性别:");
		 char gender = CustomerTool.readChar();
		 System.out.print("年龄:");
		 int age =  CustomerTool.readInt();
		 System.out.print("电话:");
		 String phone = CustomerTool.readString(20);
		 System.out.print("邮箱:");
		 String email = CustomerTool.readString(20);
//		 Customer customer = new Customer(name, gender, age, phone, email);
//		 customerList.addCustomer(customer);
		 String phone1 = phone;
		 String email1 = email;
		 for(int i = 0;i<customerList.getTotal();i++){
				 if(customerList.getCustomer(i).getPhone().equals(phone)){
					 System.out.println("您的电话号码输入错误,与已有客户:"+customerList.getCustomer(i).getName()+"的电话号码"+customerList.getCustomer(i).getPhone()+"重复,请您重新输入");
					 System.out.print("电话");
					 phone1 = CustomerTool.readString(20);
				 }
				 if(customerList.getCustomer(i).getEmail().equals(email)){
					 System.out.println("您的电话号码输入错误,与已有客户:"+customerList.getCustomer(i).getName()+"的邮箱"+customerList.getCustomer(i).getEmail()+"重复,请您重新输入");
					 System.out.print("邮箱");
					 email1 = CustomerTool.readString(20);
				 }	 
		 }
		 boolean isSuccess = customerList.addCustomer( new Customer(name, gender, age, phone1, email1));
		 if(isSuccess){
			 System.out.println("添加成功!");
		 }else{
			 System.out.println("添加失败!");
		 }
	}
	//修改客户
	public void modifyCustomer(){
		System.out.println("-----------------修改客户----------------");
		
		for(;;){
			System.out.println("请选择要修改的客户编号(-1)退出");
			int choice = CustomerTool.readInt();
			if(choice == -1){
				return;
			}
			Customer cust = customerList.getCustomer(choice-1);
			if(cust == null){
				System.out.println("无法找到改客户");
			}else{
				//修改客户信息
                customerList.replaceCustomer(choice, cust);
				System.out.println("修改成功!!");
			}
		}
	}
	public void deleteCustomer(){
		 System.out.println("-----------------删除客户--------------");
		 System.out.println("请选择要删除的客户编号(-1)退出");
			int choice = CustomerTool.readInt();

			if(choice == -1){
				return;
			}
			Customer cust = customerList.getCustomer(choice-1);
			if(cust == null){
				System.out.println("删除失败!,无法找到客户信息");
			}else{
				customerList.deleteCustomer(choice-1);
				System.out.println("删除成功!");
			}
		
		 
	}
	
	public void listAllCustomers(){
		System.out.println("--------------------客户列表-----------------------");
		int total = customerList.getTotal();
		if(total == 0){
			System.out.println("没有客户记录");
		}else{
			System.out.println("编号	"+"姓名	"+"性别	"+"年龄	"+"电话号码		"+"电子邮箱	");
		    Customer[] customer = customerList.getAllCustomer();
		    for(int i = 0;i<customer.length;i++){
		    	System.out.println(customer[i].getNumber()+"	"+customer[i].getName()+"	"+customer[i].getGender()+"	"+customer[i].getAge()+"	"+customer[i].getPhone()+"	"+customer[i].getEmail());
		    }
		}	
	}
	public void InsertInformation(){
		System.out.println("------------插入客户信息------------");
		System.out.println("请选择要要在哪位客户编号后插入 (-1)退出");
		int choice = CustomerTool.readInt();

		if(choice == -1){
			return;
		}		
		Customer cust = customerList.getCustomer(choice-1);
		Customer cust1 = customerList.getCustomer(choice);
		if(cust == null){
		   System.out.println("插入失败");
		   return;
		}else if(cust1 == null){
			System.out.println("插入失败,该客户后无信息,请使用添加功能!");
		}
		else{
			 System.out.println("请在冒号后面输入!!");
			 System.out.print("姓名:");
			 String name = CustomerTool.readString(10);
			 System.out.print("性别:");
			 char gender = CustomerTool.readChar();
			 System.out.print("年龄:");
			 int age =  CustomerTool.readInt();
			 System.out.print("电话:");
			 String phone = CustomerTool.readString(20);
			 System.out.print("邮箱:");
			 String email = CustomerTool.readString(20);
			 String phone1 = phone;
			 String email1 = email;
			 for(int i = 0;i<customerList.getTotal();i++){
					 if(customerList.getCustomer(i).getPhone().equals(phone)){
						 System.out.println("您的电话号码输入错误,与已有客户:"+customerList.getCustomer(i).getName()+"的电话号码"+customerList.getCustomer(i).getPhone()+"重复,请您重新输入");
						 System.out.print("电话");
						 phone1 = CustomerTool.readString(20);
					 }
					 if(customerList.getCustomer(i).getEmail().equals(email)){
						 System.out.println("您的电话号码输入错误,与已有客户:"+customerList.getCustomer(i).getName()+"的邮箱"+customerList.getCustomer(i).getEmail()+"重复,请您重新输入");
						 System.out.print("邮箱");
						 email1 = CustomerTool.readString(20);
					 }	 
			 }
			 customerList.InsertInformation(choice, new Customer(name, gender, age, phone1, email1));	
		}
		
	}
	public void FindInformation(){
		boolean isFlag = true;
		while(isFlag){
		System.out.println("------------查找客户信息------------");
		System.out.println("请输入你要查找的信息:");
		System.out.println("1.姓名
"+"2.电话
"+"3.邮箱
"+"4.编号
"+"5.退出");
		char selection = CustomerTool.readMenuSelection();
		switch(selection){
		case '1':
			FindName();
			break;
		case '2':
			FindPhone();
			break;
		case '3':
			FindEmail();
			break;
		case '4':
			FindNumber();
			break;
		case '5':
			System.out.println("您已退出查询!");
			isFlag = false;
			break;
		}
		
	}
}
	public void FindNumber(){
		System.out.println("请在冒号后面输入!!");
		System.out.print("请输入你要查找的用户编号:");
		int number = CustomerTool.readInt(100);
		int count = 0;
	     for(int i = 0;i<customerList.getTotal();i++)
	     {
	    	 Customer customer1 = customerList.getCustomer(i);
	 
	    	 if(customer1.getNumber() == number){
	    		 System.out.println("成功找到!信息如下:");
	    		 System.out.println("原有编号:	"+"姓名	"+"性别	"+"年龄	"+"电话号码		"+"电子邮箱	");
	    		 System.out.println(customer1.getNumber()+"	"+customer1.getName()+"	"+customer1.getGender()+"	"+customer1.getAge()+"	"+customer1.getPhone()+"	"+customer1.getEmail());
	    		 count = 1;
	    	 }
	     }
	     if(count<1){
	    	 System.out.println("对不起!未找到对应信息!"); 
	     }
	     return;
	}
	public void FindName(){
		System.out.println("请在冒号后面输入!!");
		System.out.print("请输入你要查找的用户姓名:");
		String name = CustomerTool.readString(100);
		int count = 0;
	     for(int i = 0;i<customerList.getTotal();i++)
	     {
	    	 Customer customer1 = customerList.getCustomer(i);
	 
	    	 if(customer1.getName().equals(name)){
	    		 System.out.println("成功找到!信息如下:");
	    		 System.out.println("原有编号:	"+"姓名	"+"性别	"+"年龄	"+"电话号码		"+"电子邮箱	");
	    		 System.out.println(customer1.getNumber()+"	"+customer1.getName()+"	"+customer1.getGender()+"	"+customer1.getAge()+"	"+customer1.getPhone()+"	"+customer1.getEmail());
	    		 count = 1;
	    	 }
	     }
	     if(count<1){
	    	 System.out.println("对不起!未找到对应信息!"); 
	     }
	     return;
	   }
	public void FindPhone(){
		System.out.println("请在冒号后面输入!!");
		System.out.print("请输入你要查找的用户的电话号码:");
		String phone = CustomerTool.readString(100);
	     for(int i = 0;i<customerList.getTotal();i++)
	     {
	    	 Customer customer1 = customerList.getCustomer(i);
	 
	    	 if(customer1.getPhone().equals(phone)){
	    		 System.out.println("成功找到!信息如下:");
	    		 System.out.println("原有编号:	"+"姓名	"+"性别	"+"年龄	"+"电话号码		"+"电子邮箱	");
	    		 System.out.println(customer1.getName()+"	"+customer1.getGender()+"	"+customer1.getAge()+"	"+customer1.getPhone()+"	"+customer1.getEmail());
	    		 return;
	    	 }
	     }
	     System.out.println("对不起!未找到对应信息!");  
	}
	public void FindEmail(){
		System.out.println("请在冒号后面输入!!");
		System.out.print("请输入你要查找的用户的邮箱:");
		String email = CustomerTool.readString(100);
	     for(int i = 0;i<customerList.getTotal();i++)
	     {
	    	 Customer customer1 = customerList.getCustomer(i);
	 
	    	 if(customer1.getEmail().equals(email)){
	    		 System.out.println("成功找到!信息如下:");
	    		 System.out.println("原有编号:	"+"姓名	"+"性别	"+"年龄	"+"电话号码		"+"电子邮箱	");
	    		 System.out.println(customer1.getName()+"	"+customer1.getGender()+"	"+customer1.getAge()+"	"+customer1.getPhone()+"	"+customer1.getEmail());
	    		 return;
	    	 }
	     }
	     System.out.println("对不起!未找到对应信息!");
		
	}
	
	public void PrintMenu(){
		//显示主页面
		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    查找客户信息 ");
			System.out.println("                  6    插入客户信息 ");
			System.out.println("                  7    退        出 ");
			System.out.println("       请选择(1-7): ");
			
			char selection = CustomerTool.readMenuSelection();
			switch (selection) {
			case '1':
				addNewCustomer();
				break;
			case '2':
				modifyCustomer();
				break;
			case '3':
				deleteCustomer();
				break;
			case '4':
				listAllCustomers();
				break;
			case '5':
				FindInformation();
				break;
			case '6':
				InsertInformation();
				break;
			case '7':
				System.out.print("是否确认退出(Y/N):");
				char isExit = CustomerTool.readConfirmSelection();
				if (isExit == 'Y') {
					System.out.println("您已安全退出!");
					isFlag = false;
				}	
			}
		}
	}
	 public static void main(String[] args){
              CustomerView view = new CustomerView();
              view.PrintMenu();
		}
		
		
	}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值