package com.atguigu.bean;
public class Customer {
private String name;
private String sex;
private int age;
private String phone;
private String email;
public Customer() {
super();
}
public Customer(String name, String sex, int age, String phone, String email) {
super();
this.name=name;
this.sex=sex;
this.age=age;
this.phone=phone;
this.email=email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
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 String getInfo() {
return name+"\t"+sex+"\t"+age+"\t"+phone+"\t"+email;
}
}
package com.atguigu.view;
import java.util.Scanner;
import com.atguigu.bean.Customer;
import com.atguigu.service.CustomerService;
public class CustomerView {
private CustomerService cs=new CustomerService();
java.util.Scanner input=new java.util.Scanner(System.in);
public void menu() {
boolean flag=true;
while(flag) {
System.out.println("-----------------------客户信息管理软件--------------------------------------------");
System.out.println("\t\t\t1添加客户");
System.out.println("\t\t\t2修改客户");
System.out.println("\t\t\t3删除客户");
System.out.println("\t\t\t4客户列表");
System.out.println("\t\t\t5退 出");
System.out.print("\t\t\t请选择(1-5):");
int select=input.nextInt();
switch(select) {
case 1:
add();
break;
case 2:
upadte();
break;
case 3:
delete();
break;
case 4:
list();
break;
case 5:
System.out.println("确认是否输出y/n:");
char confirm=input.next().charAt(0);
if(confirm=='Y'||confirm=='y')
{
flag=false;
}
}
}
}
private void list() {
Customer[] all=cs.getAll();
System.out.println("-----------------------客户信息--------------------------------------------");
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
for(int i=0;i<all.length;i++) {
System.out.println((i+1)+"\t"+all[i].getInfo());
}
System.out.println("-----------------------客户信息列表完成--------------------------------------------");
}
private void delete() {
System.out.println("-----------------------刪除客户信息--------------------------------------------");
System.out.println("\t选择删除客户的编号:");
int id=input.nextInt();
if(id==-1) {
return;
}
System.out.println("\t确认是否删除:(y/n)");
char confirm=input.next().charAt(0);
if(confirm=='n'||confirm=='N') {
return;
}
cs.removeById(id);
System.out.println("-----------------------删除完成---------------------------------------------------");
}
private void upadte() {
System.out.println("-----------------------更新客户信息--------------------------------------------");
System.out.println("请选择待修改客户编号(-1退出):");
int id=input.nextInt();
if(id==-1) {
return ;
}
Customer old=cs.getById(id);
System.out.println("姓名("+old.getName()+"):");
String name = input.next();
System.out.println("性别("+old.getSex()+"):");
String sex=input.next();
System.out.println("年龄("+old.getAge()+"):");
int age=input.nextInt();
System.out.println("电话("+old.getPhone()+"):");
String phone=input.next();
System.out.println("邮箱("+old.getEmail()+"):");
String email=input.next();
Customer newCustomer=new Customer(name,sex,age,phone,email);
System.out.println("-----------------------修改完成----------------------------------------------------");
}
private void add() {
System.out.println("-----------------------添加客户信息--------------------------------------------");
System.out.print("姓名:");
String name=input.next();
System.out.print("性別:");
String sex=input.next();
System.out.print("年齡:");
int age=input.nextInt();
System.out.print("電話:");
String phone=input.next();
System.out.print("郵箱:");
String email=input.next();
Customer c=new Customer(name,sex,age,phone,email);
cs.addCustomer(c);
System.out.println("-----------------------添加完成--------------------------------------------");
}
}
package com.atguigu.service;
import com.atguigu.bean.Customer;
public class CustomerService {
private Customer[] all;
private int total;
public CustomerService() {
all=new Customer[10];
}
public CustomerService(int initSize) {
all=new Customer[initSize];
}
public void addCustomer(Customer c) {
if(total>=all.length) {
System.out.println("数组已满");
return;
}
all[total++]=c;
}
public Customer[] getAll() {
Customer [] result=new Customer[total];
for(int i=0;i<total;i++) {
result[i]=all[i];
}
return result;
}
public void removeById(int id) {
int index=id-1;
if(index<0||index>=total) {
System.out.println(id+"对应的客户不存在");
return ;
}
System.arraycopy(all, index+1, all, index,total-index-1);
all[--total]=null;
}
public Customer getById(int id) {
int index=id-1;
if(index<0||index>=total) {
System.out.println(id+"对应的客户不存在");
return null;
}
return all[index];
}
public void replace(int id , Customer newCustomer){
int index=id-1;
if(index<0||index>=total) {
System.out.println(id+"对应的客户不存在");
return;
}
all[index]=newCustomer;
}
}
package com.atguigu.test;
import com.atguigu.view.CustomerView;
public class Test {
public static void main(String[] args) {
CustomerView view=new CustomerView();
view.menu();
}
}