基于文本界面的客户信息管理系统-纯面向对象

    很久没写博客了,从现在开始记录Java项目,从基础开始慢慢的向上延伸,纯粹作为本人的个人记录,也顺便拿出来分享一下,不喜勿喷,有错请指正

一:项目介绍

介绍:模拟实现一个基于文本界面的——客户信息管理系
  • 类和对象(属性、方法及构造器)
  • 类的封装
  • 引用数组
  • 数组的插入、删除和替换
  • 多对象协同工作

该简易系统能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表

项目采用分级菜单方式。

1、主菜单如下:

-----------------客户信息管理软件-----------------

                            1 添 加 客 户
                            2 修 改 客 户
                            3 删 除 客 户
                            4 客 户 列 表
                            5 退           出

                            请选择(1-5):_

2、添加功能:

……
                   请选择(1-5):1

---------------------添加客户---------------------
姓名:张三
性别:男
年龄:30
电话:010-56253825
邮箱:[email protected]
---------------------添加完成---------------------

3、修改功能:

……
                   请选择(1-5):2

---------------------修改客户---------------------
请选择待修改客户编号(-1退出):1
姓名(张三):<直接回车表示不修改>
性别(男):
年龄(30):
电话(010-56253825):
邮箱([email protected]):[email protected]
---------------------修改完成---------------------

4、删除功能:

                ……
                   请选择(1-5):3

---------------------删除客户---------------------
请选择待删除客户编号(-1退出):1
确认是否删除(Y/N):y
---------------------删除完成---------------------

5、查/显示全部功能:

……
                 请选择(1-5):4

---------------------------客户列表---------------------------
编号  姓名       性别    年龄   电话            邮箱
 1    张三       男      30     010-56253825   [email protected]
 2    李四       女      23     010-56253825    [email protected]
 3    王芳       女      26     010-56253825   [email protected]
-------------------------客户列表完成-------------------------

二:方法介绍

该项目主要涉及一下4个类:

1)、Customer.java

2)、CustomerView.java

3)、CustomerService.java

4)、CMUtility.java

1)、Customer为实体类,用来封装客户信息
            该类封装客户的以下信息:
                Int id:客户编号
                String name :客户姓名
                char gender  :性别
                int age          :年龄
                String phone:电话号码
                String email :电子邮箱

2)、CustomerService为Customer对象的管理模块,内部用数组管理一组Customer对象

  •     本类封装以下信息:
                Customer[] customers:用来保存客户对象的数组

                int total = 0                 :记录已保存客户对象的数量

  • 该类至少提供以下方法:
  1. public Customer[] list():返回客户的信息数组
  2. public boolean add(Customer customer):完成客户添加的功能
  3. public boolean delete(int id):完成客户删除功能
  4. public boolean replace(int id ,Customer customer):完成客户修改的功能
  5. public int findById(int id):先根据id找到要删除/修改的id对应的customer对象,如果找到就返回下标对应的id,如果找不到就返回-1

3)、CustomerView为主模块,负责菜单的显示和处理用户操作

  • 本类封装以下信息:

            CustomerService customerService = new CustomerService(10);
            创建最大包含10客户对象的CustomerList 对象,供以下各成员方法使用。

  • 该类至少提供以下方法:
  1. public void mainMenu() :负责菜单的显示
  2. public void add()
  3. public void delete()
  4. public void replace()

4)、CMUtility为工具类,作为基础项目野直接拿来用,也可以拿来学习

    1.public static char readMenuSelection()
    用途:该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
    2.public static char readChar() 和public static char readChar(char defaultValue)
    用途:这两个方法功能相同,均从键盘读取一个字符,并将其作为方法的返回值。
    参数: defaultValue — 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。(提示:此方法可在修改客户时调用)
    3.public static int readInt() 和public static int readInt(int defaultValue)
    用途:这两个方法功能相同,均从键盘读取一个长度不超过2位的   整数,并将其作为方法的返回值。
    参数: defaultValue — 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
   4.public static String readString(int limit)和public static String readString(int limit, String defaultValue)
    用途:这两个方法功能相同,均从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
    参数:limit — 指定字符串的最大长度
              defaultValue — 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
    5.public static char readConfirmSelection() :
    用途:从键盘读取‘Y’或’N’,并将其作为方法的返回值。

代码如下:

Customer.java

package com.basic.customermanage.bean;

public class Customer {

	private int id;
	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) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = email;
	}
	
	public Customer(int id, String name, char gender, int age, String phone, String email) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = email;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 String getInfo() {
		
		return this.id+"\t"+this.name+"\t"+this.gender+"\t"+this.age
				+"\t"+this.phone+"\t"+this.email;
	}
	
}

CustomerService.java

package com.basic.customermanage.bean.service;

import com.basic.customermanage.bean.Customer;

/**
 * 功能 : 完成对客户的管理操作(增删改查 c[create]r[read]u[update]d[delete])
 * @author Administrator
 *
 */
public class CustomerService {

	//1、定义一个客户数组
	private Customer[] customers ;
	//2、定义客户的实际个数,每次运行的时候先默认存在一个客户
	private int customerNum = 1;
	//构造一个长度len的数组
	public CustomerService(int len) {
		super();
		this.customers = new Customer[len];
		//先创建一个customer对象
		Customer customer = new Customer(1, "张三", '男', 34, "123455", "[email protected]");
		this.customers[0] = customer;

	}
		//返回客户的信息数组
	public Customer[] list(){
//		使用一个笨方法,就是将customers数组中,实际的人员拷贝到一个新数组,将这个新数组返回。
//		Customer [] newCustomer = new Customer[customerNum];
//		for (int i = 0; i < newCustomer.length; i++) {
//			newCustomer[i] = customers[i];
//		}
//		return newCustomer;
		
		return customers;
		
	}
	//完成客户添加的功能
	public boolean add(Customer customer){
		
		//判断是否数组满了
		if (customerNum >=customers.length) {
			return false;
		}
		//将初始化的客户数量依次递增
		customers[customerNum++] = customer;
		//同时将客户的id进行依次递增
		customer.setId(customerNum);
		return true;
		
	}
	
	//完成客户删除功能
	public boolean delete(int id){
		//先根据id去找到该客户对应的下标
		int index = findById(id);
		//先判断是否找到该id
		if (index == -1) {
			return false;
		}
		//将该id之后的元素依次往前移
		for (int i = index + 1; i < customers.length; i++) {
			customers[i-1] = customers[i];
			
		}
		//将最后一个元素置空
		customers[customerNum--] = null;
		return true;
	}
	//完成客户修改的功能
	public boolean replace(int id ,Customer customer){
		int index = findById(id);
		if (index == -1) {
			return false;
		}
		customers[index] = customer;
		return true;
	}
	
	//先根据id找到要删除的id对应的customer对象
	//如果找到就返回下标对应的id,如果找不到就返回-1
	public int findById(int id){
		int index = -1;
		for (int i = 0; i < customerNum; i++) {
			if (customers[i].getId() == id) {
				 index = id;
				 break;
			}
			
		}
		return index;
	}
}

CustomerView.java

package com.basic.customermanage.view;

import com.basic.customermanage.bean.Customer;
import com.basic.customermanage.bean.service.CustomerService;
import com.basic.customermanage.util.CMUtility;

/**
 * CustomerView为主模块,负责菜单的显示和处理用户操作
 * 
 * @author Administrator
 *
 */
public class CustomerView {
	// -----------------客户信息管理软件-----------------
	//
	// 1 添 加 客 户
	// 2 修 改 客 户
	// 3 删 除 客 户
	// 4 客 户 列 表
	// 5 退 出
	//
	// 请选择(1-5):_
	public static void main(String[] args) {
		new CustomerView().mainMenu();
	}

	// 定义一个属性 控制菜单显示是否退出
	private boolean loop = true;
	// 定义一个变量,接收用户输入
	private char key = ' ';
	// 使用CMUtility 工具,我们定义一个对象,当然,里面的方法都是静态的,可以通过类名.方法的形式替代
	// private CMUtility cmUtility = new CMUtility();
	// 定义一个customerService属性,主要完成对客户信息的各种操作
	// 因为此项目仅仅是使用面向对象的知识完成的,所以使用数组来完成,缺点是只能完成指定大小的客户信息
	// 当然也可以用集合,可以不用指定大小
	private CustomerService customerService = new CustomerService(10);

	public void mainMenu() {
		// TODO Auto-generated method stub

		do {
			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.print("请选择(1-5):_");
			key = CMUtility.readMenuSelection();
			switch (key) {
			case '1':
				this.add();
				break;
			case '2':
				this.replace();
				break;
			case '3':
				this.delete();
				break;
			case '4':
				this.list();
				break;
			case '5':
				this.loop = false;
				break;

			default:
				System.out.println("输入错误");
				break;
			}
		} while (loop);
		System.out.println("你已退出客户信息管理系统。。");
	}

	public void list() {
		Customer[] customerList = customerService.list();
		System.out.println("---------------------修改客户---------------------");
		System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
		for (int s = 0; s < customerList.length && customerList[s] != null; s++) {
			System.out.println(customerList[s].getInfo());
		}
		System.out.println("---------------------添加完成---------------------");
	}
	// ---------------------添加客户---------------------
	// 姓名:张三
	// 性别:男
	// 年龄:30
	// 电话:010-56253825
	// 邮箱:[email protected]
	// ---------------------添加完成---------------------

	public void add() {
		System.out.println("---------------------添加客户---------------------");
		System.out.print("姓名:");
		String name = CMUtility.readString(10);
		System.out.print("性别:");
		char gender = CMUtility.readChar();
		System.out.print("年龄:");
		int age = CMUtility.readInt();
		System.out.print("电话:");
		String phone = CMUtility.readString(18);
		System.out.print("邮箱:");
		String email = CMUtility.readString(20);
		// 根据用户的输入,创建一个Customer对象
		Customer customer = new Customer(name, gender, age, phone, email);
		// 判断创建的customer对象是否添加进数组
		if (customerService.add(customer)) {
			System.out.println("---------------------添加完成---------------------");
		} else {
			System.out.println("---------------------添加失败---------------------");
		}
	}

	//
	// ---------------------删除客户---------------------
	// 请选择待删除客户编号(-1退出):1
	// 确认是否删除(Y/N):y
	// ---------------------删除完成---------------------
	public void delete() {

		System.out.println("---------------------删除客户---------------------");
		System.out.println("请选择待删除客户编号(-1退出):");
		int id = CMUtility.readInt();
		if (id == -1) {
			return;
		}
		System.out.println("确认是否删除(Y/N):");

		char choice = CMUtility.readConfirmSelection();
		if (this.customerService.delete(id)) {
			System.out.println("---------------------删除完成---------------------");
		} else {
			System.out.println("---------------------删除失败---------------------");
		}

	}

	// ---------------------修改客户---------------------
	// 请选择待修改客户编号(-1退出):1
	// 姓名(张三):<直接回车表示不修改>
	// 性别(男):
	// 年龄(30):
	// 电话(010-56253825):
	// 邮箱([email protected]):[email protected]
	// ---------------------修改完成---------------------
	public void replace() {

		System.out.println("---------------------修改客户---------------------");
		System.out.println("请选择待修改客户编号(-1退出):");
		int index = CMUtility.readInt();
		Customer[] customeres = customerService.list();
		for (int i = 0; i < customeres.length && customeres[i] != null; i++) {
			System.out.println(customeres[i].getInfo());
			System.out.println("customeres[i].getId()"+customeres[i].getId());
			System.out.println("index"+index);
			if (index == customeres[i].getId()) {
				// String defaultValue=customeres[index].getName();
				System.out.println("姓名:");
				//String name = CMUtility.readString(10);
				String name = CMUtility.readString(10, customeres[i].getName());
				System.out.print("性别:");
				char gender = CMUtility.readChar(customeres[i].getGender());
				System.out.print("年龄:");
				int age = CMUtility.readInt(customeres[i].getAge());
				System.out.print("电话:");
				String phone = CMUtility.readString(18, customeres[i].getPhone());
				System.out.print("邮箱:");
				String email = CMUtility.readString(20, customeres[i].getEmail());
				// int id = CMUtility.readInt(defaultValue);
				Customer customer = new Customer(i+1,name, gender, age, phone, email);
				if (customerService.replace(index, customer)) {
					System.out.println("---------------------修改完成---------------------");
				} else {
					System.out.println("---------------------修改失败---------------------");
				}
			}
		}
	}
}

CMUtility.java

package com.basic.customermanage.util;

import java.util.*;
/**

	
*/
public class CMUtility {
    private static Scanner scanner = new Scanner(System.in);

    
    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }

	/**
	 * 功能:读取键盘输入的一个字符
	 * @return 一个字符
	 */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    /**
     * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
     * @param defaultValue 指定的默认值
     * @return 默认值或输入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
	
    /**
     * 功能:读取键盘输入的整型,长度小于2位
     * @return 整数
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);//一个整数,长度<=2位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    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;
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串
     * @param limit 限制的长度
     * @return 指定长度的字符串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */
	
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


	/**
	 * 功能:读取键盘输入的确认选项,Y或N
	 * 
	 * @return 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;
    }

    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。 
     * 			如果为false表示读取不大于limit长度的字符串。
     * 			如果输入为空,或者输入大于limit的长度,就会提示重新输入。
     * @return
     */
    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;
    }
}

效果图:


源码:https://download.csdn.net/download/mxcsdn/10350618

下篇面向对象的高级特性:模拟实现一个基于文本界面的《公司团队人员调度系统》
























....

猜你喜欢

转载自blog.csdn.net/mxcsdn/article/details/79949665