面向对象综合项目(客户管理系统)
完成功能
- 添加客户
- 修改客户
- 删除客户
- 查询客户
代码注释很全 可以直接看代码理解
项目效果看最后
package com.zjx.domain;
/**
* 客户的实体类
* @author zjx
*
*/
public class Customer {
private int id; //客户编号
private String name; //客户姓名
private char gender; //客户性别
private int age; //客户年龄
private String phone; //客户电话
private String email; //客户邮箱
public Customer(){}
/**
* 初始化赋值
* @param id
* @param name
* @param gender
* @param age
* @param phone
* @param email
*/
public Customer(int id, String name, char gender, int age, String phone, String email) {
this(name, gender, age, phone, email);
this.setId(id);
}
/**
* 初始化赋值
* @param name
* @param gender
* @param age
* @param phone
* @param email
*/
public Customer( String name, char gender, int age, String phone, String email) {
this.setName(name);
this.setGender(gender);
this.setAge(age);
this.setPhone(phone);
this.setEmail(email);
}
/**
* 返回各户对象的各个属性信息
* @return
*/
public String getInfo(){
return this.getId() + "\t" + this.getName() + "\t" + this.getAge()
+ "\t" + this.getPhone() + "\t"+this.getEmail();
}
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) {
if(gender != '男' || gender != '女')
this.gender = '男';
else
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age <= 0 || age >= 110)
this.age = 18;
else
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.zjx.service;
/**
* 功能:业务模块
* 主要作用:用于管理一组客户对象
* 实现客户对象的增删改查
* 属性:
* 客户数组
* 记录已保存客户对象的数量
* 方法:
* 增删改查
* @author zjx
*
*/
import java.util.Arrays;
import com.zjx.domain.Customer;
public class CustomerList {
//成员属性
private Customer[] customers;//保存客户对象的数组
int total = 1; //记录待添加的位置 也是存储的客户实际个数
/**
* 初始化数组
* @param totalCustomer 传入的数组长度
*/
public CustomerList(int totalCustomer){
//开辟空间
customers = new Customer[totalCustomer];
//刚开始默认有一个客户
customers[0] = new Customer(1, "星星星", '男', 19, "010-123456", "[email protected]");
}
/**
* 添加新客户
* @param newCustomer 要添加的客户对象
* @return 是否添加成功
*/
public boolean addCustomer(Customer newCustomer){
if (total >= customers.length) {//如果总个数大于等于数组元素个数
return false; // 添加失败
}
//为新客户赋予编号 total 1 id 2 total 2 id 3
newCustomer.setId(total+1);
customers[total++] = newCustomer; //赋值并且后移
return true;
}
/**
* 修改替换用户
* @param index 下标
* @param newCustomer 新客户
* @return 是否成功
*/
public boolean replaceCustomer(int index,Customer newCustomer){
if (index < 0 || index >= total) {//如果不在合法范围
return false;
}
customers[index] = newCustomer; //替换用户
return true;
}
/**
* 删除客户
* @param index 要删除的客户所在的数组下标
* @return 是否删除成功
*/
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];
//为原本的id 再减去1
customers[i].setId(customers[i].getId()-1);
}
//总用户-1 就是最后一个位置的下标
//因为total是1开始的
//并且总用户-1
//--再强 先 减减再运算 减完1后才说最后一个位置
customers[--total]=null;
return true;
}
/**
* 查询所有客户
* @return 返回所有客户数组
*/
public Customer[] getAllCustomers(){
// Customer[] newCustomers =new Customer[total];
// for(int i = 0; i < total; i++){
// newCustomers[i] = customers[i];
// }
//返回实际客户元素数组 total 相当于 实际数组的长度
return Arrays.copyOf(customers, total);
}
/**
* 查询指定的客户
* @param index 指定的客户下标
* @return 返回指定客户对象
*/
public Customer getCustomer(int index){
if (index < 0 || index >= total) {//如果不在合法范围
return null;
}
return customers[index];
}
}
package com.zjx.util;
import java.util.*;
/**
* 工具类
* @author zjx
*
*/
public class CMUtility {
//类加载的时候就创建
private static Scanner scanner = new Scanner(System.in);
/**
* 读取菜单选项
* 返回一个有效的字符
* @return 1 2 3 4 5
*/
public static char readMenuSelection() {
char c;
for (; ; ) {
//字符串最大长度是1 不要空字符串
String str = readKeyBoard(1, false);
c = str.charAt(0); // 把String 转为char
//如果不满足要求 继续下次循环
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break; //如果输入的满足要求 就退出循环
}
return c;
}
/**
* 用于接收一个非空字符的任意单个字符
* @return 任意单个字符
*/
public static char readChar() {
//字符串最大长度是1 不要空字符串
String str = readKeyBoard(1, false);
return str.charAt(0); //返回转为字符类型的一个字符串
}
/**
* 用于接收一个任意单个字符 可以为空字符串
* @param defaultValue 如果为空字符串 返回的默认字符
* @return 返回默认字符或者录入的一个单个字符
*/
public static char readChar(char defaultValue) {
//字符串最大长度是1 可以为空字符串
String str = readKeyBoard(1, true);
//如果长度为0 返回 defaultValue 否则返回输入的哪一个字符串
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
* 接收最大长度为2的任意整形 不能为空字符串
* @return 返回最大长度为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的任意整形 可以为空字符
* 如果是空字符串,返回默认的整形
* @param defaultValue 默认的整形
* @return 默认两个长度的整形 或者 输入最大长度为2的任意整形
*/
public static int readInt(int defaultValue) {
int n;
for (; ; ) {
//循环读取长度为2的字符串
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;
}
/**
* 读取键盘确定与否的方法
* @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') { // 如果等于Y 或者 N就退出循环
break;
} else { //否则提示 并且继续下次循环
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
/**
* 用于接收指定不超过最大长度的字符串
* @param limit 最大长度
* @param blankReturn 标记
* @return 有效的字符串
*/
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
//循环接收键盘输入
while (scanner.hasNextLine()) {//如果有输入
line = scanner.nextLine();
if (line.length() == 0) { //如果字符串长度为0 回车
if (blankReturn) return line; //根据标记判断是否返回空字符串
else continue;//不要空字符串 希望可以继续循环键盘输入 直到有有效数字
}
//如果输入的字符串的长度小于1 或者 大于最大长度限制
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
package com.zjx.view;
import com.zjx.domain.Customer;
import com.zjx.service.CustomerList;
import static com.zjx.util.CMUtility.*;
/**
* 实现羡慕的界面展示
* @author zjx
*
*/
public class CustomerView {
CustomerList list = new CustomerList(100);
/**
* 展示主菜单
*/
public void enterMainMenu(){
boolean flag = true;
do{
//打印
System.out.println("\n---------------客户信息管理软件---------------");
System.out.println(" 1.添加客户");
System.out.println(" 2.修改客户");
System.out.println(" 3.删除客户");
System.out.println(" 4.客户列表");
System.out.println(" 5.退出\n");
System.out.println(" 请选择(1-5):");
//接收键盘的输入 这个方法最后的结果只会是1-5
char key = readMenuSelection();
switch (key) {
case '1'://添加客户
add();
break;
case '2'://修改客户
update();
break;
case '3':
delete();//删除客户
break;
case '4':
showAllCustomers();//展示所有客户
break;
case '5':
flag = exit();//退出
// flag = false;
break;
}
}while(flag);
}
/**
* 增加客户
*/
public void add(){
System.out.println("\n--------------添加客户----------------");
System.out.print("姓名: ");
String name = readString(8);
System.out.print("性别: ");
char gender = readChar();
System.out.print("年龄: ");
int age = readInt();
System.out.print("电话: ");
String phone = readString(11);
System.out.print("邮箱: ");
String email = readString(18);
//核心的添加功能
Customer newCustomer = new Customer(name, gender, age, phone, email);
boolean addCustomer = list.addCustomer(newCustomer);
if (addCustomer) {
System.out.println("----------------------添加完成---------------------");
}else{
System.out.println("----------------------添加失败---------------------");
}
}
/**
* 修改客户
*/
public void update(){
Customer customer = null;
int no;
System.out.println("\n--------------修改客户----------------");
//循环接收输入
for(;;){
System.out.println("请选择待修改客户编号(-1退出):");
no = readInt();//接收最大长度为2的整形编号
//判断输入的no是否有效
//索引 等于 编号 -1
customer = list.getCustomer(no - 1);
if (customer != null) {//如果没有这个用户
break;//不为空跳出循环
}
System.out.println("无法找到指定用户!");
}
//找到了当前输入索引的用户
System.out.print("姓名("+customer.getName()+"):");
//接收输入 如果是回车表示不修改
//最大八个长度的名字 直接回车 默认是原来的姓名
String name = readString(8, customer.getName());
System.out.print("性别("+customer.getGender()+"):");
char gender = readChar(customer.getGender());
System.out.print("年龄("+customer.getAge()+"):");
int age = readInt(customer.getAge());
System.out.print("电话("+customer.getPhone()+"):");
String phone = readString(11, customer.getPhone());
System.out.print("邮箱("+customer.getEmail()+"):");
String email = readString(18, customer.getEmail());
//调用CustomerList 真正的修改
//no-1 是真正的索引 no是编号
Customer newCustomer = new Customer(no, name, gender, age, phone, email);
boolean replaceCustomer = list.replaceCustomer(no-1, newCustomer);
if (replaceCustomer) {
System.out.println("----------------------修改完成---------------------");
}else{
System.out.println("----------------------修改失败---------------------");
}
}
/**
* 删除客户
*/
public void delete(){
Customer customer = null;
int no;
System.out.println("\n--------------删除客户----------------");
//循环接收输入
for(;;){
System.out.println("请选择待删除客户编号(-1退出):");
no = readInt();//接收最大长度为2的整形编号
//判断输入的no是否有效
//索引 等于 编号 -1
customer = list.getCustomer(no - 1);
if (customer != null) {//如果没有这个用户
break;//不为空跳出循环
}
System.out.println("无法找到指定用户!");
}
System.out.print("确定要删除吗(Y/N): ");
char key = readConfirmSelection();
if (key == 'Y') {
boolean deleteCustomer = list.deleteCustomer(no - 1);
if (deleteCustomer) {
System.out.println("----------------------删除完成---------------------");
}else{
System.out.println("----------------------删除失败---------------------");
}
}
}
/**
* 展示所有客户
*/
public void showAllCustomers(){
Customer[] allCustomers = list.getAllCustomers();
for (int i = 0; i < allCustomers.length; i++) {
System.out.println(allCustomers[i].getInfo());
}
}
/**
* 退出
*/
public boolean exit(){
//接收键盘输入 是否真正的退出
System.out.println("确认是否要退出吗?Y/N:");
char key = readConfirmSelection();
// if (key=='Y') {
// //确认退出 返回false 让flag接收
// return false;
// }else{
// return true;
// }
return key=='N'; // 如果key为N就是不退出 为true
}
public static void main(String[] args) {
CustomerView v = new CustomerView();
v.enterMainMenu();
}
}