0
点赞
收藏
分享

微信扫一扫

尚硅谷项目二(加了一个功能)

胡桑_b06e 2022-02-12 阅读 60
eclipse
package Customer;

public class Customer {
    private String name; //客户姓名
    private char gender; //性别
    private int age; //客户年龄
    private String phone;//客户电话号码
    private String email;//客户电子邮箱
    
    
	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.*;
public class CustomerList {
	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;
		total++;
		return true;
	}
  
    public boolean replaceCustomer(int index,Customer customer){
    	if(index<0||index>= total){
    		return false;
    	}
    	
    	customers[index] = customer;
    	return true; 
    	 
    }
    public boolean deleteCustomer(int index,Customer customer){
    	if(index <0||index >=total){
    		return false;
    	}
    	for(int i = index;i<total-1;i++){
    		customers[index] = customers[i+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’-‘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'&&c!='6') {
				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 customer = new Customer("张三",'男',20,"12345678","emai@qq.com");
		Customer customer1 = new Customer("李四",'女',19,"12345678","emai@qq.com");
		customerList.addCustomer(customer);
		customerList.addCustomer(customer1);
	}
	
	public void addNewCustomer(){
		 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);
		 boolean isSuccess = customerList.addCustomer( new Customer(name, gender, age, phone, email));
		 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{
				//修改客户信息
				System.out.print("姓名:"+cust.getName());
				String name = CustomerTool.readString(10,cust.getName());
				System.out.print("性别:"+cust.getGender());
				char gender = CustomerTool.readChar(cust.getGender());
				System.out.print("年龄:"+cust.getAge());
				int age = CustomerTool.readInt(cust.getAge());
				System.out.print("电话:"+cust.getPhone());
				String phone = CustomerTool.readString(15,cust.getPhone());
				System.out.print("邮箱:"+cust.getEmail());
				String email = CustomerTool.readString(20, cust.getEmail());
				customerList.replaceCustomer(choice-1, new Customer(name, gender, age, phone, email));
				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, cust);
				System.out.println("删除成功!");
			}
		
		 
	}
	
	public void listAllCustomers(){
		System.out.println("--------------------客户列表-----------------------");
		int total = customerList.getTotal();
		if(total == 0){
			System.out.println("没有客户记录");
		}else{
			System.out.println("编号\t"+"姓名\t"+"性别\t"+"年龄\t"+"电话号码\t\t"+"电子邮箱\t");
		    Customer[] customer = customerList.getAllCustomer();
		    for(int i = 0;i<customer.length;i++){
		    	System.out.println((i+1)+"\t"+customer[i].getName()+"\t"+customer[i].getGender()+"\t"+customer[i].getAge()+"\t"+customer[i].getPhone()+"\t"+customer[i].getEmail());
		    }
		}	
	}
	public void FindInformation(){
		boolean isFlag = true;
		while(isFlag){
		System.out.println("------------查找客户信息------------");
		System.out.println("请输入你要查找的信息:");
		System.out.println("1.姓名\n"+"2.电话\n"+"3.邮箱\n"+"4.退出");
		char selection = CustomerTool.readMenuSelection();
		switch(selection){
		case '1':
			FindName();
			break;
		case '2':
			FindPhone();
			break;
		case '3':
			FindEmail();
			break;
		case '4':
			System.out.println("您已退出查询!");
			isFlag = false;
			break;
		}
		
	}
}
	public void FindName(){
		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("编号\t"+"姓名\t"+"性别\t"+"年龄\t"+"电话号码\t\t"+"电子邮箱\t");
	    		 System.out.println(customer1.getName()+"\t"+customer1.getGender()+"\t"+customer1.getAge()+"\t"+customer1.getPhone()+"\t"+customer1.getEmail());
	    		 count = 1;
	    	 }
	     }
	     if(count<1){
	    	 System.out.println("对不起!未找到对应信息!"); 
	     }
	     return;
	   }
	public void FindPhone(){
		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("编号\t"+"姓名\t"+"性别\t"+"年龄\t"+"电话号码\t\t"+"电子邮箱\t");
	    		 System.out.println(customer1.getName()+"\t"+customer1.getGender()+"\t"+customer1.getAge()+"\t"+customer1.getPhone()+"\t"+customer1.getEmail());
	    		 return;
	    	 }
	     }
	     System.out.println("对不起!未找到对应信息!");  
	}
	public void FindEmail(){
		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("编号\t"+"姓名\t"+"性别\t"+"年龄\t"+"电话号码\t\t"+"电子邮箱\t");
	    		 System.out.println(customer1.getName()+"\t"+customer1.getGender()+"\t"+customer1.getAge()+"\t"+customer1.getPhone()+"\t"+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("       请选择(1-6): ");
			
			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':
				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();
		}
		
		
	}


举报

相关推荐

0 条评论