本帖最后由 sky123 于 2015-12-10 18:51 编辑
最近学习了java的面向对象,从同学那里看到了一个小作业,试着做了做,和大家分享一下,希望大家指正
需求:模拟实现基于文本界面的《客户信息管理软件》。
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。
话不多说,下面直接放代码:
首先,customer类:
package com.hei.customer;
/**
Customer为实体类,用来封装客户信息
•该类封装客户的以下信息:
–String name : 客户姓名
–char gender : 性别
–int age : 年龄
–String phone: 电话号码
–String email : 电子邮箱
•提供各属性的get/set方法
•提供所需的构造器(可自行确定)
*/
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public Customer() {
super();
}
public Customer(java.lang.String name, char gender, int age,
java.lang.String phone, java.lang.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;
}
}
customer类:
package com.hei.customer;
import java.util.ArrayList;
/**
•CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象
•本类封装以下信息:
–Customer[] customers:用来保存客户对象的数组
–int total = 0 :记录已保存客户对象的数量
•该类至少提供以下方法:
–public CustomerList(int totalCustomer)
–public boolean addCustomer(Customer customer)
–public boolean replaceCustomer(int index, Customer cust)
–public boolean deleteCustomer(Customer customer)
–public Customer[] getAllCustomers()
–public Customer getCustomer(int index)
*/
public class CustomerList {
private ArrayList<Customer> aL = new ArrayList<>();
// private int total;
public CustomerList() {
super();
}
public CustomerList(int totalCustomer) {
//this.total = totalCustomer;
}
public boolean addCustomer(Customer customer) {
// total++;
return aL.add(customer);
}
public void replaceCustomer(int index,int shuxing,String newshuxing) {
switch (shuxing) {
case 1:
aL.get(index).setName(newshuxing);
break;
case 2:
aL.get(index).setGender(newshuxing.charAt(0));
break;
case 3:
aL.get(index).setAge(Integer.parseInt(newshuxing));
break;
case 4:
aL.get(index).setPhone(newshuxing);
break;
case 5:
aL.get(index).setEmail(newshuxing);
break;
default:
break;
}
}
public boolean deleteCustomer(Customer customer) {
return aL.remove(customer);
}
public ArrayList<Customer> getAllCustomers() {
return this.aL;
}
public Customer getCustomer(int index) {
return aL.get(index);
}
}
CustomerView主界面:
package com.hei.customer;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author wudi
*
*/
public class CustomerView {
public static void main(String[] args) {
CustomerList cList = new CustomerList();
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();
System.out.println();
boolean bool = true;
while(bool) {
System.out.print("请选择(1-5):");
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
switch (key) {
case "1":
add(cList);
break;
case "2":
System.out.print("请输入要修改的客户编号:");
int m = Integer.parseInt(sc.nextLine());
System.out.print("请输入要修改的客户属性:");
System.out.print("1:姓名\t2:性别\t3:年龄\t4:电话\t5:邮箱");
int n = Integer.parseInt(sc.nextLine());
System.out.print("请做修改:");
String newshuxing = sc.nextLine();
replace(cList,m,n,newshuxing);
break;
case "3":
System.out.print("请输入要删除的客户编号:");
int i = Integer.parseInt(sc.nextLine());
delete(cList,i);
break;
case "4":
print(cList);
break;
case "5":
bool = false;
System.out.println("程序结束,谢谢");
break;
default:
break;
}
}
}
public static void add(CustomerList cList) {
Scanner sc = new Scanner(System.in);
System.out.println("---------------------添加客户---------------------");
System.out.print("姓名:");
String name = sc.nextLine();
System.out.print("性别:");
String gender = sc.nextLine();
System.out.print("年龄:");
String age = sc.nextLine();
System.out.print("电话:");
String phone = sc.nextLine();
System.out.print("邮箱:");
String email = sc.nextLine();
Customer cus = new Customer(name,gender.charAt(0),Integer.parseInt(age),phone,email);
cList.addCustomer(cus);
}
public static void print(CustomerList cList) {
System.out.println("---------------------客户列表---------------------");
System.out.println("编号"+"\t姓名"+"\t性别"+"\t年龄"+"\t电话"+"\t邮箱");
ArrayList<Customer> arrL = cList.getAllCustomers();
for (int i = 0; i < arrL.size(); i++) {
System.out.println(i+"\t"+arrL.get(i).getName()+"\t"+arrL.get(i).getGender()+"\t"
+arrL.get(i).getAge()+"\t"+arrL.get(i).getPhone()+"\t"+arrL.get(i).getEmail());
}
}
public static void delete(CustomerList cList,int index) {
cList.deleteCustomer(cList.getCustomer(index));
}
public static void replace(CustomerList cList,int index,int shuxing,String newshuxing) {
cList.replaceCustomer(index, shuxing, newshuxing);
}
}
下面贴出运行的结果:
希望各位前辈给提些意见
|
|