银行信息管理系统(JAVA)
class Customer
package com.atguigu.p2.bean;
/**
* @Description
* @author hxrstart Email:huangxr026081@163.com
* @version
* @date 2022年3月10日下午4:32:23
*
*/
public class Customer {
private String name;//姓名
private char gender;//性别
private int age;//年龄
private String phone;//电话
private String 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;
}
public Customer() {
}
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;
}
}
class CustomerList
*
*/
package com.atguigu.p2.service;
import com.atguigu.p2.bean.Customer;
/**
* @Description
* @author hxrstart Email:huangxr026081@163.com
* @version
* @date 2022年3月10日下午4:32:46
*
*/
public class CustomerList {
private Customer[] customers;
private int total = 0;
/*
* 用来初始化customer数组的构造器
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
*
* @Description:将指定的客户添加到数组中
* @author hxrstart
* @date 2022年3月10日下午4:57:54
* @param customer
* @return true:添加成功 false:添加失败
*
*/
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;
}
customers[total++] = customer;
return true;
}
/**
*
* @Description:修改指定索引处的用户
* @author hxrstart
* @date 2022年3月10日下午5:02:10
* @param index
* @param cust
* @return 修改成功:true 修改失败:false
*
*/
public boolean replaceCustomer(int index, Customer cust) {
if (index < 0 || index >= total) {
return false;
}
customers[index] = cust;
return true;
}
/**
*
* @Description:删除指定索引位置上的用户
* @author hxrstart
* @date 2022年3月10日下午5:11:10
* @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++) {
customers[i] = customers[i+1];
}
customers[total -1] = null;
total--;
return true;
}
/**
*
* @Description:获取所有用户信息
* @author hxrstart
* @date 2022年3月10日下午5:20:46
* @return
*
*/
public Customer[] getAllCustomers() {
Customer[] custs = new Customer[total];
for(int i = 0;i < total;i++) {
custs[i] = customers[i];
}
return custs;
}
/**
*
* @Description:获取指定索引位置上的用户
* @author hxrstart
* @date 2022年3月10日下午5:24:26
* @param index
* @return 如果找到
*
*/
public Customer getCustomer(int index) {
if(index < 0 || index > total -1) {
return null;
}
return customers[index];
}
/**
*
* @Description:获取存储的客户数量
* @author hxrstart
* @date 2022年3月10日下午5:29:10
* @return
*
*/
public int getTotal() {
return total;
}
}
class CustomerView
*
*/
package com.atguigu.p2.ui;
import com.atguigu.p2.bean.Customer;
import com.atguigu.p2.service.CustomerList;
import com.atguigu.p2.util.CMUtility;
/**
* @Description
* @author hxrstart Email:huangxr026081@163.com
* @version
* @date 2022年3月10日下午4:33:04
*
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer costomer = new Customer("王涛", '男', 23, "131313131", "wangtao.gmail.com");
customerList.addCustomer(costomer);
}
/**
*
* @Description:显示《客户信息管理软件》界面的方法
* @author hxrstart
* @date 2022年3月10日下午8:09:30
*
*/
public void entermenu() {
boolean isFlag = true;
while (isFlag) {
System.out.println("\n--------------客户信息管理软件--------------");
System.out.println(" 1.添加客户");
System.out.println(" 2.修改客户");
System.out.println(" 3.删除客户");
System.out.println(" 4.客户列表");
System.out.println(" 5.退 出");
System.out.println(" 请选择(1-5):");
char menu = CMUtility.readMenuSelection();
switch (menu) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.out.println("确认是否退出:(Y/N):");
char isExit = CMUtility.readConfirmSelection();
if (isExit == 'Y') {
isFlag = false;
}
break;
}
}
}
/**
*
* @Description:添加客户的操作
* @author hxrstart
* @date 2022年3月10日下午8:09:49
*
*/
private void addNewCustomer() {
System.out.println("------------------添加客户-----------------");
System.out.println("姓名:");
String name = CMUtility.readString(10);
System.out.println("性别:");
char gender = CMUtility.readChar();
System.out.println("年龄:");
int age = CMUtility.readInt();
System.out.println("电话:");
String phone = CMUtility.readString(13);
System.out.println("邮箱:");
String email = CMUtility.readString(30);
Customer customer = new Customer(name,gender,age,phone,email);
boolean isSuccess = customerList.addCustomer(customer);
if(isSuccess) {
System.out.println("-----------------添加完成!---------------------");
}else {
System.out.println("-----------------目录已满,添加失败!--------------");
}
}
/**
*
* @Description:修改客户的操作
* @author hxrstart
* @date 2022年3月10日下午8:10:41
*
*/
private void modifyCustomer() {
System.out.println("-----------------修改客户--------------");
Customer customer;
int number;
for(;;) {
System.out.println("请选择待修改的客户(-1退出):");
number = CMUtility.readInt();
if(number == -1) {
return;
}
customer = customerList.getCustomer(number-1);
if(customer == null) {
System.out.println("无法找到指定客户!");
}else {
break;
}
}
//修改客户信息
System.out.println("姓名(" + customer.getName() + "):");
String name = CMUtility.readString(10, customer.getName());
System.out.println("性别(" + customer.getAge() + "):");
char gender = CMUtility.readChar(customer.getGender());
System.out.println("年龄(" + customer.getAge() + "):");
int age = CMUtility.readInt(customer.getAge());
System.out.println("电话(" + customer.getPhone() + "):");
String phone = CMUtility.readString(13, customer.getPhone());
System.out.println("邮箱(" + customer.getEmail() + "):");
String email = CMUtility.readString(30, customer.getEmail());
Customer newCust = new Customer(name,gender,age,phone,email);
boolean isReplaced = customerList.replaceCustomer(number - 1, newCust);
if(isReplaced) {
System.out.println("----------------修改成功!----------------");
}else {
System.out.println("----------------修改失败!----------------");
}
}
//删除客户
private void deleteCustomer() {
System.out.println("----------------删除客户--------------");
int number;
Customer customer;
for(;;) {
System.out.println("请选择待删除的编号(-1退出):");
number = CMUtility.readInt();
if(number == -1) {
return;
}
customer = customerList.getCustomer(number-1);
if(customer == null) {
System.out.println("无法找到指定客户!");
}else {
break;
}
}
//刪除客戶
boolean isDelete = customerList.deleteCustomer(number - 1);
if(isDelete) {
System.out.println("刪除成功!");
}else {
System.out.println("刪除失败!");
}
}
//客户列表
private void listAllCustomers() {
System.out.println("----------------客户列表-------------------");
int total = customerList.getTotal();
if (total == 0) {
System.out.println("没有用户信息!");
} else {
System.out.println("编号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "电话" + "\t" + "邮箱");
Customer[] custs = customerList.getAllCustomers();
for (int i = 0; i < custs.length; i++) {
Customer cust = custs[i];
System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + cust.getAge()
+ "\t" + cust.getPhone() + "\t" + cust.getEmail());
}
}
System.out.println("------------------客户列表完成--------------");
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.entermenu();
}
}
CMUtility//工具包
import java.util.*;
public class CMUtility {
public static void main(String[] args) {
//System.out.println(readMenuSelection());
}
private static Scanner scanner = new Scanner(System.in);
private static String readKeyBoard(int limit,boolean blank){
for (;;) {
String str = scanner.nextLine();
if (str.length()>0 && str.length()<=limit) {
return str;
} else if (blank) {
return str;
}else{
System.out.println("请输入长度不超过"+limit+"的指令");
}
}
}
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' ) {
return c;
} else {
System.out.println("选择错误,请重新输入。");
}
}
}
public static char readChar(){
//获取性别
String str = readKeyBoard(1,false);
return str.charAt(0);
}
public static char readChar(char defaultValue){
//修改性别信息时,不输入信息直接回车
String str = readKeyBoard(1,true);
return (str.length()==0)? defaultValue : str.charAt(0);
}
public static int readInt(){
//获取年龄
int n;
for(;;){
String str = readKeyBoard(2,false);
try{
n = Integer.parseInt(str);
return n;
}catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入。");
}
}
}
public static int readInt(int defaultValue){
//修改年龄信息时,不输入信息直接回车
int n;
for(;;){
String str = readKeyBoard(2,true);
if (str.equals("")) {
return defaultValue;
}
try{
n = Integer.parseInt(str);
return n;
}catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入。");
}
}
}
public static String readString(int limit){
//姓名、电话、邮箱的输入
return readKeyBoard(limit,false);
}
public static String readString(int limit,String defaultValue){
//修改姓名、电话、邮箱时,不输入信息直接回车
String str = readKeyBoard(limit,true);
return str.equals("") ? defaultValue : str;
}
public static char readConfirmSelection(){
//获取确认的输入
for(;;){
String str = readKeyBoard(1,false).toUpperCase();
char c = str.charAt(0);
if (c=='Y' || c=='N') {
return c;
} else {
System.out.println("选择错误,请输入Y/N");
}
}
}
}