Java 简单控制台项目之客户信息管理软件 --- 凌宸1642
项目二:客户信息管理软件
模拟实现一个基于文本界面的《客户信息管理软件》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
- 类结构的使用:属性、方法及构造器
- 对象的创建与使用
- 类的封装性
- 声明和使用数组
- 数组的插入、删除和替换
- 关键字的使用:this
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。
项目采用分级菜单方式。主菜单如下:
/*-----------------客户信息管理软件-----------------1 添 加 客 户2 修 改 客 户3 删 除 客 户4 客 户 列 表5 退 出请选择(1-5): */
需求说明
每个客户的信息被保存在
Customer
对象中。以一个
Customer
类型的数组来记录当前所有的客户。每次“添加客户”(菜单1)后,客户
Customer
对象被添加到数组中。每次“修改客户”(菜单2)后,修改后的客户
Customer
对象替换数组中原对象。每次“删除客户”(菜单3)后,客户
Customer
对象被从数组中清除。执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。
"添加客户"的界面及操作过程如下所示:
/*……请选择(1-5):1---------------------添加客户--------------------- 姓名:佟刚 性别:男 年龄:35 电话:010-56253825 邮箱:tongtong@atguigu.com ---------------------添加完成--------------------- */
"修改客户"的界面及操作过程如下所示:
/*……请选择(1-5):2---------------------修改客户--------------------- 请选择待修改客户编号(-1退出):1 姓名(佟刚):<直接回车表示不修改> 性别(男): 年龄(35): 电话(010-56253825): 邮箱(tongtong@atguigu.com):tongg@atguigu.com ---------------------修改完成--------------------- */
"删除客户"的界面及操作过程如下所示:
/*……请选择(1-5):3---------------------删除客户--------------------- 请选择待删除客户编号(-1退出):1 确认是否删除(Y/N):y ---------------------删除完成--------------------- */
"客户列表"的界面及操作过程如下所示:
/*……请选择(1-5):4---------------------------客户列表--------------------------- 编号 姓名 性别 年龄 电话 邮箱1 佟刚 男 45 010-56253825 tong@abc.com2 封捷 女 36 010-56253825 fengjie@ibm.com3 雷丰阳 男 32 010-56253825 leify@163.com -------------------------客户列表完成------------------------- */
/*项目包结构> pojo> customermanager> bean> Customer> service> CustomerList> util> CMUtility> view> CustomerView
*/
package com.lingchen.pojo.customermanager.bean;/*** @Author 凌宸* @Date 2021/10/12* @Version 1.0*//*** Customer为实体类,用来封装客户信息* 该类封装客户的以下信息:* String name :客户姓名* char gender :性别* int age :年龄* String phone:电话号码* String email :电子邮箱* 提供各属性的get/set方法* 提供所需的构造器(可自行确定)*/
public class Customer {String name; // 客户姓名char gender; // 性别int age; // 年龄String phone; // 电话号码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;}
}
package com.lingchen.pojo.customermanager.service;/*** @Author 凌宸* @Date 2021/10/12* @Version 1.0*/import com.lingchen.pojo.customermanager.bean.Customer;/*** CustomerList为Customer对象的管理模块,内部使用数组管理一组Customer对象* 本类封装以下信息:* Customer[] customers:用来保存客户对象的数组* int total = 0 :记录已保存客户对象的数量* 该类至少提供以下构造器和方法:* public CustomerList(int totalCustomer)* public boolean addCustomer(Customer customer)* public boolean replaceCustomer(int index, Customer cust)* public boolean deleteCustomer(int index)* public Customer[] getAllCustomers()* public Customer getCustomer(int index)* public int getTotal()*/
public class CustomerList {Customer[] customers; // 用来保存客户对象的数组int total = 0; // 记录已保存客户对象的数量/*** @Description 构造器,用来初始化customers数组* @param totalCustomer:指定customers数组的最大空间*/public CustomerList(int totalCustomer){customers = new Customer[totalCustomer];}/** @Description 将参数customer添加到数组中最后一个客户对象记录之后* @param customer 指定要添加的客户对象* @return 添加成功返回true;false表示数组已满,无法添加*/public boolean addCustomer(Customer customer){if(total > customers.length){return false;}customers[total ++] = customer;return true;}/*** @Description 用参数customer替换数组中由index指定的对象* @param index index指定所替换对象在数组中的位置,从0开始* @param customer customer指定替换的新客户对象* @return 替换成功返回true;false表示索引无效,无法替换*/public boolean replaceCustomer(int index, Customer customer){if(index < 0 || index >= total){return false;}customers[index] = customer;return true;}/*** @Description 从数组中删除参数index指定索引位置的客户对象记录* @param index index指定所删除对象在数组中的索引位置,从0开始* @return 删除成功返回true;false表示索引无效,无法删除*/public boolean deleteCustomer(int index){if(index < 0 || index >= total){return false;}for(int i = index; i < total - 1; i ++){customers[i] = customers[i + 1];}// 最后一个有数据的元素需要置空customers[-- total] = null;return true;}/*** @Description 返回数组中记录的所有客户对象* @return Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。*/public Customer[] getAllCustomers(){Customer[] custs = new Customer[total];for(int i = 0 ; i < total; i ++){custs[i] = customers[i];}return custs;}/*** @Description 返回参数index指定索引位置的客户对象记录* @param index index指定所要获取的客户在数组中的索引位置,从0开始* @return 封装了客户信息的Customer对象*/public Customer getCustomer(int index){if(index < 0 || index >= total){return null;}return customers[index];}/*** @Description 获取存储的客户数量* @return 存储的客户数量*/public int getTotal(){return total;}}
package com.lingchen.pojo.customermanager.util;/*** @Author 凌宸* @Date 2021/10/12* @Version 1.0*/
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;}
}
package com.lingchen.pojo.customermanager.view;/*** @Author 凌宸* @Date 2021/10/12* @Version 1.0*/import com.lingchen.pojo.customermanager.bean.Customer;
import com.lingchen.pojo.customermanager.service.CustomerList;
import com.lingchen.pojo.customermanager.util.CMUtility;/*** CustomerView为主模块,负责菜单的显示和处理用户操作* 本类封装以下信息:* CustomerList customerList = new CustomerList(10);* 创建最大包含10个客户对象的CustomerList 对象,供以下各成员方法使用。* 该类至少提供以下方法:* public void enterMainMenu()* private void addNewCustomer()* private void modifyCustomer()* private void deleteCustomer()* private void listAllCustomers()* public static void main(String[] args)*/
public class CustomerView {CustomerList customerList = new CustomerList(10);public CustomerView() {Customer customer = new Customer("凌宸",'男',21,"18897993582","lc@qq.com");customerList.addCustomer(customer);}/*** @Description 显示《客户信息管理软件》界面的方法*/public void enterMainMenu(){boolean isFlag = true;do{System.out.println("\n-----------------客户信息管理软件-----------------\n");System.out.println(" 1 添 加 客 户");System.out.println(" 2 修 改 客 户");System.out.println(" 3 删 除 客 户");System.out.println(" 4 客 户 列 表");System.out.println(" 5 退 出\n");System.out.print(" 请选择(1-5):");char menuSelection = CMUtility.readMenuSelection();switch (menuSelection){case '1':addNewCustomer(); break;case '2':modifyCustomer(); break;case '3':deleteCustomer(); break;case '4':listAllCustomers(); break;case '5':System.out.print("是否确认退出(Y/N):");char isExit = CMUtility.readConfirmSelection();if(isExit == 'Y'){System.out.println("退出成功!");isFlag = false;}}}while(isFlag);}/*** @Description 添加用户的操作*/private void addNewCustomer(){System.out.println("\n---------------------添加客户---------------------");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(13);System.out.print("邮箱:");String email = CMUtility.readString(32);Customer customer = new Customer(name, gender, age, phone, email);boolean isSuccess = customerList.addCustomer(customer);if(isSuccess){System.out.println("---------------------添加完成---------------------");}else{System.out.println("---------------------添加失败---------------------");}}/*** @Description 查找用户的操作*/private void modifyCustomer(){System.out.println("\n---------------------修改客户---------------------");Customer customer;int selection;for(; ;){System.out.print("请选择待修改客户编号(-1退出):");selection = CMUtility.readInt();if(selection == -1){return ;}customer = customerList.getCustomer(selection - 1);if(customer == null){System.out.println("无法找到指定课户!");}else{break;}}// 修改客户信息System.out.print("姓名(" + customer.getName() +"):");String name = CMUtility.readString(10,customer.getName());System.out.print("性别(" + customer.getGender() +"):");char gender = CMUtility.readChar(customer.getGender());System.out.print("年龄(" + customer.getAge() +"):");int age = CMUtility.readInt(customer.getAge());System.out.print("电话(" + customer.getPhone() +"):");String phone = CMUtility.readString(13,customer.getPhone());System.out.print("邮箱(" + customer.getEmail() +"):");String email = CMUtility.readString(32,customer.getEmail());Customer newCustomer = new Customer(name, gender, age, phone, email);boolean isRepalaced = customerList.replaceCustomer(selection - 1, newCustomer);if(isRepalaced){System.out.println("---------------------修改完成---------------------");}else{System.out.println("---------------------修改失败---------------------");}}/*** @Description 删除用户的操作*/private void deleteCustomer(){System.out.println("\n---------------------删除客户---------------------");Customer customer;int selection;for(; ;){System.out.print("请选择待删除客户编号(-1退出):");selection = CMUtility.readInt();if(selection == -1){return ;}customer = customerList.getCustomer(selection - 1);if(customer == null){System.out.println("无法找到指定课户!");}else{break;}}// 找到了指定用户System.out.print("确认是否删除(Y/N):");char isDelete = CMUtility.readConfirmSelection();if(isDelete == 'Y'){customerList.deleteCustomer(selection - 1);System.out.println("---------------------删除完成---------------------");}}/*** @Description 展示所有用户的操作*/private 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年龄\t\t电话\t\t\t\t邮箱");Customer[] allCustomers = customerList.getAllCustomers();for (int i = 0; i < allCustomers.length; i++) {Customer c = allCustomers[i];System.out.println((i + 1) + "\t\t" + c.getName()+ "\t\t" + c.getGender() + "\t\t" + c.getAge()+ "\t\t" + c.getPhone()+ "\t\t" + c.getEmail());}}System.out.println("-------------------------客户列表完成-------------------------");}public static void main(String[] args){CustomerView view = new CustomerView();view.enterMainMenu();}
}
Java 简单控制台项目之客户信息管理软件 --- 凌宸1642相关推荐
- Java项目二——客户信息管理软件
目录 1.CMUtility工具类 2.客户类:Customer 3.客户信息管理类:CustomerList 4.CustomerView为主模块 二.程序的运行结果展示 1.添加客户 2.修改客户 ...
- Java学习路线:day11 客户信息管理软件
文章目录 转载自atguigu.com视频 客户信息管理软件 需求说明书 软件设计结构 第1步:封装CMUtility工具类 第2步:Customer类的设计 第3步:CustomerList类的设计 ...
- Java基础项目——客户信息管理软件
目录 前言 本项目目标 一.需求及软件设计结构说明 1.需求说明 1)主菜单 2)添加客户 3)修改客户 4)删除客户 5)客户列表 2.软件设计结构 1)Customer类的设计 2)Custome ...
- 零基础Java学习之初级项目实践(客户信息管理软件-附源码)
项目涉及知识点 基础的面向对象编程项目. 类和对象(属性.方法及构造器) 类的封装 引用数组 数组的插入.删除和替换 对象的聚集处理 多对象协同工作 需求说明 总体说明 模拟实现基于文本界面的< ...
- Java小项目—客户信息管理软件(二)
CustomerView类的设计 CustomerView为主模块,负责菜单的显示和处理用户操作. 本类封装以下信息: 创建最大包含10个客户对象的CustomerList对象,供以下各成员方法使用. ...
- java se 学习之项目二:客户信息管理软件
一.需求说明 模拟实现基于文本界面的<客户信息管理软件>. 该软件能够实现对客户对象的插入.修改和删除(用数组实现),并能够打印客户明细表. 项目采用分级菜单方式.主菜单如下: ----- ...
- Java实现客户信息管理软件
资源分享: 尚硅谷 链接:https://pan.baidu.com/s/1B_liAbYxos9voDuFLx-Ldg 提取码:1if5 --来自百度网盘超级会员V6的分享 「Project2( ...
- Scala --简单项目设计--客户信息管理软件
项目需求分析 模拟实现基于文本界面的<客户信息管理软件>. 该软件scala能够实现对客户对象的插入,修改,删除,显示,查询(用ArrayBuffer或者ListBuffer实现),并能够 ...
- P001【项目一】客户信息管理软件_Customer类(2)
客户信息管理软件_问题描述汇总 Customer 类的详细代码 CustomerList 类的详细代码 CustomerView 类的详细代码 CMutility 类的详细代码 实体对象Custome ...
最新文章
- 如何找回由于IO设备错误移动磁盘的文件
- 光纤通信是如何接入网络的?
- 统计、可视化两不误,多达19种可视化技能你一定要掌握~~
- K-序列求和 (逆元)
- 海量数据处理:如何从10亿个数中,找出最大的10000个数?(top K问题)
- chromebook刷机_如何在Chromebook上拍照
- 【翻译】使用Ext JS设计响应式应用程序
- centos7.6使用Mariadb官方二进制安装
- edui 富文本编辑_ueditor集成秀米编辑器
- mysql 拼接字符串查询
- 经典散文集锦:读者杂志卷首语大荟萃
- PS-怎么使用参考线?
- 关于智能码控门禁系统项目的二维码验证问题
- 你还在纠结用什么库写 Python 命令行程序?看这一篇就够了
- CocosEditor For JS (Cocos2d-JS) 教程聚合和代码下载
- centos下ftp安装及添加账户
- android 上传nexus_上传 Android aar 到 nexus 上
- 春夜宴从弟桃花园序 ——李白
- ValueError: Unknown activation function: ReLU
- 【PBR系列六】基于物理的环境光照(上):漫反射辐照度(Diffuse irradiance)
热门文章
- SQL日期与时间戳转换unix_timestamp() 与 from_unixtime()
- db后缀的文件导出为excel
- matlab从1到n怎么循环,8 Matlab 循环操作
- 赫伯特•亚历山大•西蒙(1916年6月15日--2001年2月9日 Herbert Alexander Simon )
- 《梦幻厨房》一起做美食之项目结构介绍
- 云端青柚UZ:一颗横跨农业与区块链的明日之星冉冉升起
- 中国 SaaS 企业如何突围?这几点是关键!
- 如何从光盘本地安装CentOS 7图形界面(Gnome GUI)
- Cesium-popup点击弹窗功能
- DICOM:DICOM3.0网络通信协议之“开源库实现剖析”