简介:
简单的Java项目,实现记录用户的个人信息,并提供增删改查的功能
项目结构图:
效果展示:
1.增加用户
2.修改用户信息
3.删除用户
4.查找所有用户
代码实现:
1.CustomerUtils工具类
package projectUtils;
import java.util.*;
/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CustomerUtils {
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;
}
}
2.Customer类
package ProjectMVC_M;
import projectMVC_C.CustomerList;
/**
*
* @Description Model层,用来封装Customer的信息
* @author Dionysus_xu Email:2405483019@qq.com
* @version
* @Date 2022年1月23日下午4:02:48
*
*/
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public Customer() {
super();
}
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 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;
}
}
3.CustomerList类
package projectMVC_C;
import ProjectMVC_M.Customer;
/**
*
* @Description Controller层,用来控制用户的增删改查
* @author Dionysus_xu Email:2405483019@qq.com
* @version
* @Date 2022年1月23日下午4:05:08
*
*/
public class CustomerList {
private Customer[] customer;//存放多个客户的信息
int total = 0;//用来记录当前用户数量
/**
* 用来初始化Customer数组的构造器
* @param totalCustomer
*/
public CustomerList(int totalCustomer) {
customer = new Customer[totalCustomer];
}
/**
*
* @Description 将指定的客户添加到数组中
* @author Dionysus_xu
* @date 2022年1月24日下午3:51:57
* @param customer
* @return true 添加成功 false 添加失败
*/
public boolean addCustomer(Customer cust) {
if(total >= customer.length) {
return false;
}
customer[total] = cust;
total++;
return true;
}
/**
*
* @Description 修改指定索引位置的用户信息
* @author Dionysus_xu
* @date 2022年1月24日下午3:56:49
* @param index
* @param cust
* @return true 修改成功 false 修改失败
*/
public boolean replaceCustomer(int index, Customer cust) {
if(index<0||index>=total) {
return false;
}
this.customer[index] = cust;
return true;
}
/**
*
* @Description 删除指定位置的客户信息
* @author Dionysus_xu
* @date 2022年1月24日下午4:06:13
* @param index
* @return true 删除成功 false 删除失败
*/
public boolean deleteCustomer(int index) {
if(index <0|| index>total) {
return false;
}
for(int i = index;i<total-1;i++) {//用i+1位置的用户信息替换i位置的用户信息
this.customer[i] = this.customer[i+1];
if(i == total -1) {//最后一位置空
this.customer[i] = null;
}
}
total --;
return true;
}
public Customer[] getAllCustomers() {
Customer[] cus = new Customer[total];
for(int i=0;i<total;i++) {
cus[i]= this.customer[i];
}
return cus;
}
/**
*
* @Description 找到指定位置的用户信息
* @author Dionysus_xu
* @date 2022年1月24日下午4:19:55
* @param index
* @return 如果找到元素 返回用户信息 如果没找到 返回 null
*/
public Customer getCustomer(int index) {
if(index<0|| index>=total)
return null;
return customer[index];
}
public int getTotal() {
return this.total;
}
}
4.CustomerView类
package projectMVC_V;
import java.util.Scanner;
import ProjectMVC_M.Customer;
import projectMVC_C.CustomerList;
import projectUtils.CustomerUtils;
/**
*
* @Description View层,负责和用户进行交互
* @author Dionysus_xu Email:2405483019@qq.com
* @version
* @Date 2022年1月23日下午4:07:21
*
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
private String readString;
Scanner scan = new Scanner(System.in);
public CustomerView() {
Customer cust = new Customer("张三", '男', 30, "010-56253825", "abc@email.com");
customerList.addCustomer(cust);
}
/**
*
* @Description 呈现主页面信息
* @author Dionysus_xu
* @date 2022年1月24日下午4:28:26
*/
public void enterMainMenu() {
boolean isFlag = true;
while (isFlag) {
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 menu = CustomerUtils.readMenuSelection();
switch (menu) {
case '1':
this.addNewCustomer();
break;
case '2':
this.modifyCustomer();
break;
case '3':
this.deleteCustomer();
break;
case '4':
this.listAllCustomer();
break;
case '5':
// 退出功能
System.out.print("是否退出?(Y/N):");
char isExit = CustomerUtils.readConfirmSelection();
if (isExit == 'Y')
isFlag = false;
}
}
}
/**
*
* @Description 添加用户的操作
* @author Dionysus_xu
* @date 2022年1月24日下午4:23:39
*/
public void addNewCustomer() {
System.out.println("---------------------添加客户---------------------");
System.out.print("用户姓名:");
String name = CustomerUtils.readString(6);
System.out.print("用户性别(男/女):");
char gender = CustomerUtils.readChar();
System.out.print("用户年龄:");
int age = CustomerUtils.readInt(2);
System.out.print("用户电话:");
String phone = CustomerUtils.readString(13);
System.out.print("用户邮箱:");
String email = CustomerUtils.readString(30);
Customer cus = new Customer(name, gender, age, phone, email);
boolean isSuccess = customerList.addCustomer(cus);
if (!isSuccess) {
System.out.println("---------------------用户数量已满,添加失败---------------------");
} else {
System.out.println("---------------------添加成功---------------------");
}
}
/**
*
* @Description 修改指定用户的操作
* @author Dionysus_xu
* @date 2022年1月24日下午4:43:59
*/
public void modifyCustomer() {
System.out.println("---------------------修改客户---------------------");
for (;;) {
int index = 0;
System.out.print("请输入想要修改的用户编号(-1表示退出):");
index = scan.nextInt();
if (index == -1) {//退出
return;
} else {//正常处理,非退出
index -= 1;
if (index >= 0 && index < customerList.getTotal()) {//下标正常
Customer cust = customerList.getCustomer(index);
System.out.print("用户姓名(" + cust.getName() + "):");
String name = CustomerUtils.readString(6,cust.getName());
System.out.print("用户性别(" + cust.getGender() + "):");
char gender = CustomerUtils.readChar(cust.getGender() );
System.out.print("用户年龄(" + cust.getAge() + "):");
int age = CustomerUtils.readInt(cust.getAge());
System.out.print("用户电话(" + cust.getPhone() + "):");
String phone = CustomerUtils.readString(13,cust.getPhone());
System.out.print("用户邮箱(" + cust.getEmail() + "):");
String email = CustomerUtils.readString(30,cust.getEmail());
Customer cus = new Customer(name, gender, age, phone, email);
customerList.replaceCustomer(index, cus);
System.out.println("---------------------修改成功---------------------");
return;
} else {
System.out.println("---------------------序号输入错误,修改失败---------------------");
}
}
}
}
/**
*
* @Description 删除指定的操作
* @author Dionysus_xu
* @date 2022年1月24日下午4:44:10
*/
public void deleteCustomer() {
System.out.println("---------------------删除客户---------------------");
for(;;) {
System.out.print("请输入想要删除的客户编号:");
int index =scan.nextInt();
if(index<1||index>customerList.getTotal()) {
System.out.println("---------------------未找到该客户---------------------");
}else {
customerList.deleteCustomer(index-1);
System.out.println("---------------------删除客户成功---------------------");
return;
}
}
}
/**
*
* @Description 显示用户列表的操作
* @author Dionysus_xu
* @date 2022年1月24日下午4:44:24
*/
public void listAllCustomer() {
System.out.println("---------------------------客户列表---------------------------");
if (customerList.getTotal() == 0) {
System.out.println("没有客户信息");
} else {
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
Customer[] cus = customerList.getAllCustomers();
for (int i = 0; i < cus.length; i++) {
Customer custs = cus[i];
System.out.println((i + 1) + "\t" + custs.getName() + "\t" + custs.getGender() + "\t" + custs.getAge()
+ "\t" + custs.getPhone() + "\t" + custs.getEmail());
}
}
System.out.println("-------------------------客户列表完成-------------------------");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}