【尚硅谷_java基础】Project 2:客户信息管理软件

本文档介绍了如何实现一个基于Java的客户管理软件,涉及键盘访问控制、菜单设计、Customer类、CustomerList类和CustomerView类的详细实现,包括读取用户输入、数据验证和操作管理功能。
摘要由CSDN通过智能技术生成


项目来自于: 尚硅谷Java入门视频教程(在线答疑+Java面试真题)

1. 目标

在这里插入图片描述

2. 需求说明

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.软件设计结构

在这里插入图片描述
在这里插入图片描述

4. 设计

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

5. 键盘访问的实现

在这里插入图片描述

在这里插入图片描述

6. 代码实现

6.1 键盘访问实现类

package pers.chh3213.project2;


import java.util.*;
/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
    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') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
	/**
	从键盘读取一个字符,并将其作为方法的返回值。
	*/
    public static char readChar() {
        String str = readKeyBoard(1, 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;
    }
	/**
	从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
	如果用户不输入字符而直接回车,方法将以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的字符串,并将其作为方法的返回值。
	如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
	*/
    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.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

6.2 Customer类

package pers.chh3213.project2;

public class Customer {
	private String name;
	private char gender;
	private int age;
	private String phone;
	private String email;
	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 int getAge() {
		return age;
	}
	public String getEmail() {
		return email;
	}
	public char getGender() {
		return gender;
	}
	public String getName() {
		return name;
	}
	public String getPhone() {
		return phone;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}

}

6.3 CustomerList类

package pers.chh3213.project2;
/**
 *
* CustomerList.java
* @Description 为Customer对象的管理模块,内部使用数组管理一组Customer对象
* @author chh3213
* @version
* @date 2021年12月27日下午11:08:09
 */
public class CustomerList {
	private Customer[] customers;//用来保存客户对象的数组
	private int total;//记录已保存客户对象的数量
	public static void main(String[] args) {
		CustomerList cuList = new CustomerList(10);

	}
	public  CustomerList(int totalCustomer) {
		// 构造器,用来初始化customers数组
		this.customers = new Customer[totalCustomer];
	}
	public boolean addCustomer(Customer customer){
		//将参数customer添加到数组中最后一个客户对象记录之后
		if(this.customers.length<=this.total){
			System.out.println("添加失败,客户数量已满");
			return false;
		}
		else {
			this.customers[this.total]=customer;
			this.total++;
		}
//		System.out.println(this.total);
		return true;
	}
	public boolean replaceCustomer(int index, Customer cust) {
		//用参数customer替换数组中由index指定的对象
		for (int i = 0; i < this.total; i++) {
			if(i==index) {
				this.customers[i]=cust;
				return true;
			}
		}
		System.out.println("索引无效,无法替换");
		return false;

	}
	public boolean deleteCustomer(int index) {
		//从数组中删除参数index指定索引位置的客户对象记录
		for (int i = 0; i < this.total; i++) {
			if(i==index) {
				for (int j = i; j < this.total; j++) {
					if(j+1!=this.customers.length) {
						this.customers[j]=this.customers[j+1];
					}
					else {
						this.customers[j]=null;
					}
				}
				this.total--;
				return true;
			}
		}
		System.out.println("索引无效,无法删除");
		return false;
	}
	public Customer[] getAllCustomers() {
		//返回数组中记录的所有客户对象
		Customer[] customers = new Customer[this.total];
		for (int i = 0; i < this.total; i++) {
			customers[i]= this.customers[i];
		}
		return customers;
	}
	public Customer getCustomer(int index) {
		if(index<this.total)return this.customers[index];
		else {
			System.out.println("该索引位置无客户信息");
			return null;
		}
	}
	public int getTotal() {
		return total;
	}
}

6.4 CustomerView类

package pers.chh3213.project2;

import java.util.Iterator;

/**
 *
* CustomerView.java
* @Description 为主模块,负责菜单的显示和处理用户操作
* @author chh3213
* @version
* @date 2021年12月28日上午8:41:00
 */
public class CustomerView {
	private CustomerList customerList;
	public CustomerView() {
		customerList = new CustomerList(10);
	}
	public static void main(String[] args) {
		CustomerView view = new CustomerView();
		view.enterMainMenu();
	}
	public void enterMainMenu() {
		/*显示主菜单,响应用户输入,
		 * 根据用户操作分别调用其他相应的成员方法(如addNewCustomer),
		 * 以完成客户信息处理。
		 */
		while (true) {
			System.out.print("-------客户信息管理软件-------\r\n"
					+ "\r\n"
					+ "       1 添 加 客 户\r\n"
					+ "       2 修 改 客 户\r\n"
					+ "       3 删 除 客 户\r\n"
					+ "       4 客 户 列 表\r\n"
					+ "       5 退   出\r\n"
					+ "\r\n"
					+ "       请选择(1-5):");
			char choose = CMUtility.readMenuSelection();
			switch (choose) {
			case '1': {
				this.addNewCustomer();
				break;
			}
			case '2': {
				this.modifyCustomer();
				break;
			}
			case '3': {
				this.deleteCustomer();
				break;
			}
			case '4': {
				this.listAllCustomers();
				break;
			}
			default:
				System.out.println("是否退出(Y/N)?");
				char c= CMUtility.readConfirmSelection();
				if(c== 'Y')System.exit(0);
				else {
					break;
				}
			}
		}

	}
	private void addNewCustomer() {
		//添加客户
		System.out.println("--------添加客户----------");
		System.out.print("姓名:");
		String name = CMUtility.readString(10);
		System.out.println();
		System.out.print("性别:");
		char gender = CMUtility.readChar();
		System.out.println();
		System.out.print("年龄:");
		int age = CMUtility.readInt();
		System.out.println();
		System.out.print("电话:");
		String phone = CMUtility.readString(12);
		System.out.println();
		System.out.print("邮箱:");
		String email = CMUtility.readString(20);
		boolean addCustomerSuccess = this.customerList.addCustomer(new Customer(name, gender, age, phone, email));
		if(addCustomerSuccess) {
			System.out.println("---------------------添加完成---------------------\r\n");
		}
	}
	private void modifyCustomer() {
		//修改客户
		System.out.println("--------修改客户----------");
		System.out.print("请选择待修改客户编号(-1退出):");
		int choose = CMUtility.readInt(-1);
		if(choose!=-1 && choose<this.customerList.getTotal()) {
			System.out.println();
			System.out.print("姓名:("+this.customerList.getCustomer(choose).getName()+")");
			String name = CMUtility.readString(10,this.customerList.getCustomer(choose).getName());
			System.out.println();
			System.out.print("性别:("+this.customerList.getCustomer(choose).getGender()+")");
			char gender = CMUtility.readChar(this.customerList.getCustomer(choose).getGender());
			System.out.println();
			System.out.print("年龄:("+this.customerList.getCustomer(choose).getAge()+")");
			int age = CMUtility.readInt(this.customerList.getCustomer(choose).getAge());
			System.out.println();
			System.out.print("电话:("+this.customerList.getCustomer(choose).getPhone()+")");
			String phone = CMUtility.readString(12,this.customerList.getCustomer(choose).getPhone());
			System.out.println();
			System.out.print("邮箱:("+this.customerList.getCustomer(choose).getEmail()+")");
			String email = CMUtility.readString(20,this.customerList.getCustomer(choose).getEmail());
			this.customerList.replaceCustomer(choose, new Customer(name, gender, age, phone, email));
			System.out.println("---------------------修改完成---------------------\r\n");
		}
		if(choose>=this.customerList.getTotal()) {
			System.out.println("此索引无客户信息");
		}
	}
	private void deleteCustomer() {
		//删除客户
		System.out.println("---------------------删除客户---------------------");
		System.out.print("请选择待删除客户编号(-1退出):");
		int choose = CMUtility.readInt(-1);
		if(choose!=-1&& choose<this.customerList.getTotal()) {
			System.out.println();
			System.out.print("确认是否删除(Y/N):");
			char c= CMUtility.readConfirmSelection();
			if(c=='Y') {
				this.customerList.deleteCustomer(choose);
			}
		}
		if(choose>=this.customerList.getTotal()) {
			System.out.println("此索引无客户信息");
		}
	}
	private void listAllCustomers() {
		//客户列表
		System.out.println("-------------------客户列表-------------------");
		System.out.println("编号  姓名   性别   年龄          电话                   邮箱\r\n");
		Customer[] customers = this.customerList.getAllCustomers();
		for (int i = 0; i < customers.length; i++) {
			System.out.println(i+"\t"+customers[i].getName()+"\t"+customers[i].getGender()+"\t"+customers[i].getAge()+"\t"+customers[i].getPhone()+"\t"+customers[i].getEmail());
		}
		System.out.println("-------------------客户列表完成--------------");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CHH3213

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值