尚硅谷_宋红康_Project2.pptx
package com.atguigu.project;
/*
该类封装客户的以下信息:
String name :客户姓名
char gender :性别
int age :年龄
String phone:电话号码
String email :电子邮箱
*/
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public Customer() {
}
public Customer(String name, char gender, int age, String phone, String email) {
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;
}
}
package com.atguigu.project;
public class CustomerList {
private Customer[] customers;
private int total = 0;
public CustomerList() {
}
/**
* 用途:构造器,用来初始化customers数组
* 参数:totalCustomer:指定customers数组的最大空间
*
* @param totalCustomer 指定customers数组的最大空间
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
* 用途:将参数customer添加到数组中最后一个客户对象记录之后
* 参数:customer指定要添加的客户对象
* 返回:添加成功返回true;false表示数组已满,无法添加
*
* @param customer 指定要添加的客户对象
* @return 添加成功返回true;false表示数组已满,无法添加
*/
public boolean addCustomer(Customer customer) {
if (total < customers.length) {
customers[total++] = customer;
return true;
} else {
return false;
}
}
/**
* 用途:用参数customer替换数组中由index指定的对象
* 参数:customer指定替换的新客户对象
* index指定所替换对象在数组中的位置,从0开始
* 返回:替换成功返回true;false表示索引无效,无法替换
*
* @param index 指定所替换对象在数组中的位置,从0开始
* @param cust customer指定替换的新客户对象
* @return 替换成功返回true;false表示索引无效,无法替换
*/
public boolean replaceCustomer(int index, Customer cust) {
if (index >= 0 && index < customers.length) {
customers[index] = cust;
return true;
} else {
return false;
}
}
/**
* 用途:从数组中删除参数index指定索引位置的客户对象记录
* 参数: index指定所删除对象在数组中的索引位置,从0开始
* 返回:删除成功返回true;false表示索引无效,无法删除
*
* @param index index指定所删除对象在数组中的索引位置,从0开始
* @return 删除成功返回true;false表示索引无效,无法删除
*/
public boolean deleteCustomer(int index) {
if (index >= 0 && index < customers.length) {
customers[index] = null;
return true;
} else {
return false;
}
}
/**
* 用途:返回数组中记录的所有客户对象
* 返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
*
* @return Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
*/
public Customer[] getAllCustomers() {
return customers;
}
/**
* 用途:返回参数index指定索引位置的客户对象记录
* 参数: index指定所要获取的客户在数组中的索引位置,从0开始
* 返回:封装了客户信息的Customer对象
*
* @param index 指定所要获取的客户在数组中的索引位置,从0开始
* @return 封装了客户信息的Customer对象
*/
public Customer getCustomer(int index) {
if (index >= 0 && index < customers.length) {
return customers[index];
} else {
System.out.println("索引有问题,反回第一个客户信息");
return customers[0];
}
}
public int getTotal() {
return this.total;
}
}
package com.atguigu.project;
import java.util.Scanner;
public class CustomerView {
CustomerList customerList = new CustomerList(10);
Scanner sc = new Scanner(System.in);
/**
* 用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法(如addNewCustomer),以完成客户信息处理。
*/
public void enterMainMenu() {
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.print(" 请选择(1-5):");
}
/**
*
*/
private void addNewCustomer() {
System.out.println("---------------------添加客户---------------------");
Customer customer = new Customer();
System.out.print("姓名:");
customer.setName(sc.next());
System.out.print("性别:");
customer.setGender(sc.next().charAt(0));
System.out.print("年龄:");
customer.setAge(sc.nextInt());
System.out.print("电话:");
customer.setPhone(sc.next());
System.out.print("邮箱:");
customer.setEmail(sc.next());
customerList.addCustomer(customer);
System.out.println("---------------------添加完成---------------------");
}
private void modifyCustomer() {
System.out.println("---------------------修改客户---------------------");
System.out.print("请选择待修改客户编号(-1退出):");
int choice = sc.nextInt()-1;
//返回目标类customerList.getCustomer(choice);
Customer customer = new Customer();
System.out.print("姓名(" + customerList.getCustomer(choice).getName() + "):");
customer.setName(sc.next());
System.out.print("性别(" + customerList.getCustomer(choice).getGender() + "):");
customer.setGender(sc.next().charAt(0));
System.out.print("年龄(" + customerList.getCustomer(choice).getAge() + "):");
customer.setAge(sc.nextInt());
System.out.print("电话(" + customerList.getCustomer(choice).getPhone() + "):");
customer.setPhone(sc.next());
System.out.print("邮箱(" + customerList.getCustomer(choice).getEmail() + "):");
customer.setEmail(sc.next());
customerList.replaceCustomer(choice, customer);
System.out.println("---------------------添加完成---------------------");
}
private void deleteCustomer() {
System.out.println("---------------------删除客户---------------------");
System.out.print("请选择待删除客户编号(-1退出):");
int choice = sc.nextInt();
System.out.println("确认是否删除(Y/N):");
String flag = sc.next();
if (flag.toLowerCase().equals("y")) {
customerList.deleteCustomer(choice);
} else {
System.out.println("删除失败!!!");
}
System.out.println("---------------------删除完成---------------------");
}
private void listAllCustomers() {
System.out.println("---------------------------客户列表---------------------------");
System.out.println("编号 姓名 性别 年龄 电话 邮箱");
//得到客户数组customerList.getAllCustomers();
for (int i = 0; i < customerList.getTotal(); i++) {
if (customerList.getCustomer(i) != null) {
System.out.println(i + 1 + "\t\t" + customerList.getCustomer(i).getName() + "\t\t" + customerList.getCustomer(i).getGender()
+"\t\t"+customerList.getCustomer(i).getAge()
+ "\t\t" + customerList.getCustomer(i).getPhone() + "\t\t\t\t" + customerList.getCustomer(i).getEmail());
} else {
System.out.println(i + 1 + "\t" + "什么都没有");
}
}
System.out.println("-------------------------客户列表完成-------------------------\n");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CustomerView cv = new CustomerView();
label:
while (true){
cv.enterMainMenu();
int yourChoice=sc.nextInt();
switch (yourChoice){
case 1:
cv.addNewCustomer();
break;
case 2:
cv.modifyCustomer();
break;
case 3:
cv.deleteCustomer();
break;
case 4:
cv.listAllCustomers();
break;
case 5:
break label;
default:
System.out.println("输入有问题,请重新输入");
break;
}
}
}
}