Customer: java Bean (包含属性 get set 构造器)
/**
* @Classname Customer
* @Description TODO
* @Date 2022/1/2 6:32
* @Created by asus
*/
public class Customer {
private String Name;
private char Gender;
private int Age ;
private String Phone;
private String Email;
public void setName(String Name){
this.Name = Name;
}
public String getName(){
return Name;
}
public void setGender(char Ggender){
this.Gender = Ggender;
}
public char getGender(){
return Gender;
}
public void setAge(int Age){
this.Age = Age;
}
public int getAge(){
return Age;
}
public void setPhone(String phone){
this.Phone = phone;
}
public String getPhone(){
return Phone;
}
public void setEmail(String Email){
this.Email = Email;
}
public String getEmail(){
return Email;
}
public Customer(){
}
public Customer(String name ,char Gender,int Age,String Phone,String Email){
this.Name = name;
this.Age = Age;
this.Gender = Gender;
this.Phone = Phone;
this.Email = Email;
}
}
CustomerList:一些对CustomerList处理的方法
/**
* @Classname CustomerList
* @Description TODO
* @Date 2022/1/2 6:35
* @Created by asus
*/
public class CustomerList {
private Customer[] customers; //客户对象数组
int total = 0; //计数
public CustomerList(int totalCustomer) {
/**
* 功能描述:初始化用户数组的构造器
* @param totalCustomer 用户数组最大长度
* @return:
* @date: 2022/1/2
*/
customers = new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer) {
/**
* 功能描述:
* @param customer
* @return: boolean 成功:true 失败 :false
* @date: 2022/1/2
*/
if (total >= customers.length) {
return false;
}
else{
customers[total] = customer;
total++;
return true;
}
}
public boolean replaceCustomer(int index ,Customer customer){
/**
* 功能描述: 修改指定位置的客户信息
* @param index 索引
* @param customer 客户信息
* @return: boolean 成功:true 失败 :false
* @date: 2022/1/2
*/
if(index<0 || index >=total){
return false;
}
customers[index] = customer;
return true;
}
public boolean deleteCustomer(int index){
/***
* 功能描述: 删除指定位置客户信息
* @param index
* @return: boolean 成功:true 失败 :false
* @date: 2022/1/2
*/
if(index<0 || index>=total){
return false;
}
for(int i=index;i<=total-1;i++){ //将index到最后一个往前移一位,此时倒数第一 第二相同
customers[i] = customers[i+1];
}
customers[total-1] = null; //将最后一个客户信息置为空
total--;
return true;
}
public Customer[] getAllCustomers(){
/**
* 功能描述: 获取所有客户信息
* @param
* @return: bean.Customer[]
* @date: 2022/1/2
*/
Customer[] customers1 = new Customer[total];
for(int i = 0;i< total;i++){
customers1[i] = customers[i];
}
return customers1;
}
public Customer getCustomer(int index){
/**
* 功能描述: 获取指定位置的客户信息
* @param index
* @return: bean.Customer
* @date: 2022/1/2
*/
if(index<0 || index>=total){
return null;
}
return customers[index];
}
public int getTotal(){
return total;
}
}
CustomerView: 交互界面
import java.util.Scanner;
/**
* @Classname CustomerView
* @Description TODO
* @Date 2022/1/2 6:36
* @Created by asus
*/
public class CustomerView {
private CustomerList customerList= new CustomerList(10);
Scanner scan = new Scanner(System.in);
public void enterMainMenu(){
/**
* 功能描述: 显示界面
* @param
* @return: void
* @date: 2022/1/2
*/
boolean flag = true;
while(flag) {
int i = 0;
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.println(" 选则(1~5):");
System.out.println("--------------------------客户信息管理--------------------------");
i = scan.nextInt();
switch (i) {
case 1:
addNewCustomer();
break;
case 2:
modifyCustomer();
break;
case 3:
deleteCustomer();
break;
case 4:
listAllCustomers();
break;
case 5:
String char1 ;
System.out.println("是否退出(Y/N):");
char1 = scan.next();
if(char1.equals("Y")){
flag = false;
}
break;
}
}
}
private void addNewCustomer(){
System.out.println("--------------------------添加客户--------------------------");
System.out.print("姓名:");
String name = scan.next();
System.out.print("性别:");
char Gender = scan.next().charAt(0);
System.out.print("年龄:");
int age = scan.nextInt();
System.out.print("电话:");
String Phone = scan.next();
System.out.print("邮箱:");
String Email = scan.next();
Customer customer = new Customer(name,Gender,age,Phone,Email);// 将客户信息封装在一个对象中
boolean isSeccess = customerList.addCustomer(customer);
if (isSeccess){
System.out.println("--------------------------添加成功--------------------------\n");
}
else{
System.out.println("--------------------------添加失败--------------------------\n");
}
}
private void modifyCustomer() {
listAllCustomers();
System.out.println("修改客户对应编号:");
int num;
for (; ; ) { //类似于while(1)
System.out.println("-1退出");
num = scan.nextInt();
if (num == -1) {
return;
}
Customer cust = customerList.getCustomer(num - 1);
if (cust == null) {
System.out.println("不存在");
} else {
break;
}
}
System.out.print("姓名:");
String name = scan.next();
System.out.print("性别:");
char Gender = scan.next().charAt(0);
System.out.print("年龄:");
int age = scan.nextInt();
System.out.print("电话:");
String Phone = scan.next();
System.out.print("邮箱:");
String Email = scan.next();
Customer newcustomer = new Customer(name,Gender,age,Phone,Email);// 将客户信息封装在一个对象中
boolean isReplace = customerList.replaceCustomer(num-1,newcustomer);
if(isReplace){
System.out.println("--------------------------修改成功--------------------------\n");
}else{
System.out.println("--------------------------修改失败--------------------------\n");
}
}
private void deleteCustomer(){
listAllCustomers();
System.out.println("删除客户对应编号:");
int num;
for (; ; ) {
System.out.println("-1退出");
num = scan.nextInt();
if (num == -1) {
return;
}
Customer cust = customerList.getCustomer(num - 1);
if (cust == null) {
System.out.println("不存在");
} else {
break;
}
}
boolean isDelete = customerList.deleteCustomer(num-1);
if(isDelete){
System.out.println("--------------------------删除成功--------------------------\n");
}else{
System.out.println("--------------------------删除失败--------------------------\n");
}
}
public CustomerView() { //构造一个初始用户
Customer customer = new Customer("陈封",'男',23,"13402951750","1162196987@qq.com");
customerList.addCustomer(customer);
}
private void listAllCustomers(){
int j = customerList.getTotal();
if(j==0){
System.out.println("没有客户信息!");
}
else{
System.out.println("--------------------------客户列表--------------------------");
System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t 邮箱");
Customer[] customers1 = customerList.getAllCustomers();
for (int i = 0; i < customers1.length; i++) {
System.out.println((i+1) + "\t\t"+customers1[i].getName() + "\t\t"
+customers1[i].getGender()+"\t\t"+customers1[i].getAge()+"\t\t"
+customers1[i].getPhone()+"\t\t"+customers1[i].getEmail());
}
System.out.println("--------------------------客户列表--------------------------");
}
}
public static void main(String[] args) {
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}