【尚硅谷-Java学习】【项目2源码】客户信息管理软件

Customer

package com.atguigu.p2.bean;

public class Customer {
	private String name;//姓名
	private char gender;//性别
	private int age;//年龄
	private String phone;//电话
	private String email;//邮箱
	
	public Customer() {
		
	}
	public Customer(String name, char gender, int age, String phone, String email) {
		
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = 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;
	}
	
	
}

CustomerList

package com.atguigu.p2.service;

import com.atguigu.p2.bean.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) {
			customers[total] = customer;
			total++;
			return true;
		}else return false;
		
	}
	public boolean replaceCustomer(int index,Customer customer){
		if (index>=0 && index<total) {
			customers[index] = customer;
			return true;
		}else return false;
	}
	public boolean deleteCustomer(int index){
		if (index>=0 && index<total) {
			for (int i = index; i < total-1; i++) {
				customers[i] = customers[i+1];
			}
			customers[total-1] = null; 
			total--;
			return true;
		}else return false;
		
	}
	public Customer[] getAllCustomers(){
		//null的部分不返回
		Customer[] custs = new Customer[total];
		for (int i = 0; i < total; i++) {
			custs[i] = customers[i];
		}
		return customers;
	}
	public Customer getCustomer(int index){
		if (index>=0 && index<total) {
		return customers[index];
		}else return null;
	}
	public int getTotal() {
		return total;
	}
	
}

CustomerView

package com.atguigu.p2.ui;

import com.atguigu.p2.bean.Customer;
import com.atguigu.p2.service.CustomerList;
import com.atguigu.p2.util.CMUtility;

public class CustomerView {
	private CustomerList  customerList = new CustomerList(10);
	

	public CustomerView() {
		Customer customer = new Customer("Tom",'男',23,"123","tom@gmail.com");
		customerList.addCustomer(customer);
	}

	public void enterMainMenu(){
		//显示主页面
		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("       请选择(1-5): ");
			
			char select = CMUtility.readMenuSelection();
			switch (select) {
			case '1':
				addNewCustomer();
				break;

			case '2':
				modifyCustomer();
				break;
			case '3':
				
				deleteCustomer();
				break;
			case '4':
				listAllCustomers();
				break;
			case '5':
				System.out.println("确认退出(Y/N)?");
				char confirmExit = CMUtility.readConfirmSelection();
				if (confirmExit=='Y') {
					isFlag=false;
				}
			}
			
			
		}
		
	}
	
	public void addNewCustomer(){
		int total = customerList.getTotal();
		if (total<customerList.getAllCustomers().length) {
			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 tel = CMUtility.readString(11);
			System.out.println("客户邮箱:");
			String email = CMUtility.readString(15);
			Customer cust = new Customer(name,gender,age,tel,email);
			customerList.addCustomer(cust);
			System.out.println("-----------------添加成功-----------------\t");
		}else{
			System.out.println("-----------客户列表已满,添加失败-------------\t");
		}
		
	}
	
	public void modifyCustomer(){
		System.out.println("-----------------修改客户-----------------");
		int index;
		Customer cust;
		for (;;) {
			System.out.println("请输入要修改的客户序号(输入-1退出)");
			index = CMUtility.readInt();
			if (index==-1) {
				return;
			}
			index--;
			cust = customerList.getCustomer(index);
			if (cust == null) {
				System.out.println("无法找到客户!");
			} else {
				break;
			}
		}
		
		System.out.println("姓名("+cust.getName()+"):");
		String name = CMUtility.readString(10, cust.getName());
		System.out.println("性别("+cust.getGender()+"):");
		char gender = CMUtility.readChar( cust.getGender());
		System.out.println("年龄("+cust.getAge()+"):");
		int age = CMUtility.readInt(cust.getAge());
		System.out.println("电话("+cust.getPhone()+"):");
		String tel = CMUtility.readString(11, cust.getPhone());
		System.out.println("邮箱("+cust.getEmail()+"):");
		String email = CMUtility.readString(15, cust.getEmail());
		
		Customer newCust = new Customer(name,gender,age,tel,email);
		boolean isReplaced = customerList.replaceCustomer(index, newCust);
		if (isReplaced) {
			System.out.println("-----------------修   改  成  功-----------------\t");
		} else {
			System.out.println("-----------------修   改  失  败-----------------\t");
		}
		
		
	}
	
	public void deleteCustomer(){
		System.out.println("-----------------删除客户-----------------");
		Customer cust;
		int index;
		for (;;) {
			System.out.println("请输入要删除的客户序号(输入-1退出)");
			index = CMUtility.readInt();
			if (index == -1) {
				return;
			}
			index--;
			cust = customerList.getCustomer(index);
			if (cust == null) {
				System.out.println("无法找到客户!");
			} else {
				break;
			}
		}
		System.out.println("是否确认删除(Y/N)");
		char isDelete = CMUtility.readConfirmSelection();
		if (isDelete == 'Y') {
			boolean deleteSuccess = customerList.deleteCustomer(index);
			if (deleteSuccess) {
				System.out.println("-----------------删  除  成  功-----------------\t");
			} else {
				System.out.println("删除失败,请输入正确序号:1->"+customerList.getTotal());
			}
		}
		
		
		
	}
	public void listAllCustomers(){
		System.out.println("-------------------客  户  列  表------------------\n");
		int total = customerList.getTotal();
		if (total == 0) {
			System.out.println("没有客户记录!");
		}else {
			System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
			Customer[] custList = customerList.getAllCustomers();
			for (int i = 0; i < total; i++) {
				System.out.println(i+1 + "\t" + custList[i].getName()+"\t" + 
			custList[i].getGender()+"\t" + custList[i].getAge()+"\t" + 
						custList[i].getPhone()+"\t\t" + custList[i].getEmail()+"\t");
			}
		}
		System.out.println("-----------------客户列表完成-----------------\n");
	}
	public static void main(String[] args) {
		CustomerView CV = new CustomerView();
		CV.enterMainMenu();
	}
	
	
}

CMUtility

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

  • 28
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值