1 客户类
public class Customers {
private String cid;
private String name;
private String sex;
private String age;
private String call;
private String email;
public Customers() {
}
public Customers(String cid,String name, String sex, String age, String call, String email) {
this.cid=cid;
this.name = name;
this.sex = sex;
this.age = age;
this.call = call;
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCall() {
return call;
}
public void setCall(String call) {
this.call = call;
}
public String getEmail() {
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
}
2 主界面
public class Customermanager {
public static void main(String[] args) {
ArrayList<Customers> array = new ArrayList<Customers>();
while (true) {
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" );
Scanner sc = new Scanner( System.in );
String line = sc.nextLine();
switch (line) {
case "1":
//System.out.println( "1 添加客户" );
addCustomer( array );
break;
case "2":
//System.out.println( "2 修改客户" );
modifyCustomer( array );
break;
case "3":
//System.out.println( "3 删除客户" );
deleteCustomer( array );
break;
case "4":
//System.out.println( "4 查看所有客户" );
findCustomer( array );
break;
case "5":
System.out.print("确认是否退出(Y/N):");
String y = sc.nextLine();
if (y.equals( "Y" ))
{
System.exit( 0 );
}
}
}
}
3 方法
(1)添加客户
public static void addCustomer(ArrayList<Customers> array ){
Scanner sc = new Scanner( System.in );
String cid;
while (true){
System.out.println("请输入客户编号");
cid = sc.nextLine();
boolean flag =isUsed( array,cid );
if (flag){
System.out.println("编号被占用,请重新输入");
}else {
break;
}
}
System.out.println("请输入客户姓名");
String name = sc.nextLine();
System.out.println("请输入客户性别");
String sex = sc.nextLine();
System.out.println("请输入客户年龄");
String age = sc.nextLine();
System.out.println("请输入客户电话");
String call = sc.nextLine();
System.out.println("请输入客户邮箱");
String email = sc.nextLine();
Customers c = new Customers();
c.setCid( cid );
c.setName( name );
c.setSex( sex );
c.setAge( age );
c.setCall( call );
c.setEmail( email );
array.add( c);
System.out.println("添加成功");
}
(2)判断编号是否被占用
public static boolean isUsed(ArrayList<Customers>array ,String cid){
boolean flag = false;
for (int i = 0; i <array.size() ; i++) {
Customers s = array.get( i );
if (s.getCid().equals( cid )){
flag = true;
break;
}
}
return flag;
}
(3)修改客户信息
public static void modifyCustomer(ArrayList<Customers> array ){
Scanner sc= new Scanner( System.in );
System.out.println("请输入要修改的客户编号");
String cid = sc.nextLine();
System.out.println("请输入客户姓名");
String name = sc.nextLine();
System.out.println("请输入客户性别");
String sex = sc.nextLine();
System.out.println("请输入客户年龄");
String age = sc.nextLine();
System.out.println("请输入客户电话");
String call = sc.nextLine();
System.out.println("请输入客户邮箱");
String adress = sc.nextLine();
String email = sc.nextLine();
Customers c = new Customers();
c.setCid( cid );
c.setName( name );
c.setSex( sex );
c.setAge( age );
c.setCall( call );
c.setEmail( email );
for (int i = 0; i <array.size() ; i++) {
Customers customers = array.get( i );
if (customers.getCid().equals( cid )){
array.set( i,c );
break;
}
}
System.out.println("修改客户信息成功");
}
(4)删除客户
public static void deleteCustomer(ArrayList<Customers> array ){
Scanner sc = new Scanner( System.in );
System.out.println("请输入要删除的客户编号(-1退出)");
String cid = sc.nextLine();
if (cid.equals( "-1" )){
return;
}
int index = -1;
for (int i = 0; i < array.size(); i++) {
Customers s = array.get( i );
if (s.getCid().equals( cid )) {
index = i;
break;
}
}
if (index == -1) {
System.out.println( "该信息不存在,请重新输入" );
} else {
System.out.println("确认是否删除(Y/N):");
String s = sc.nextLine();
if (s.equals( "Y" )|s.equals( "y" )){
array.remove( index );
System.out.println( "删除成功" );
}
}
}
(5) 客户列表
public static void findCustomer(ArrayList<Customers> array ){
if (array.size()==0){
System.out.println("无信息,请添加信息在再查询");
return;//为了程序不再往下执行
}
System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
for (int i = 0; i <array.size() ; i++) {
Customers s = array.get( i );
System.out.println(s.getCid()+"\t\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAge()+"\t\t"+s.getCall()+"\t\t"+s.getEmail());
}
}
(6)退出
System.out.print("确认是否退出(Y/N):");
String y = sc.nextLine();
if (y.equals( "Y" ))
{
System.exit( 0 );
}
4 问题总结
(1)字符串比较问题
在遇到输入“-1”退出时碰到了问题,当时想着怎么比较String类型和int类型的数据,后面知道直接用equals方法直接比较String类型数据就行。
(2)修改客户不成功
这是错误代码片
for (int i = 0; i <array.size() ; i++) {
Customers customers = array.get( i );
if (customers.getCid().equals( i )){
array.set( i,customers );
break;
}
错误如下: 首先 if (customers.getCid().equals( ))这一步比较的是遍历后的集合与输入的cid是否相同,所以应该是与cid比较而不是i;其次, array.set( );这一步是修改指定索引处的元素,返回被修改的元素;这里是用上面存储新的客户信息的c来修改索引处元素,所以修改后的代码为
for (int i = 0; i <array.size() ; i++) {
Customers customers = array.get( i );
if (customers.getCid().equals( cid )){
array.set( i,c );
break;
}
(3)get和set方法使用时的疑惑 (为什么这里用set那里用get?)
本题中录入数据时候多用set方法,输出数据时用get方法。那么具体的应用场景在什么地方?
set是写入数据,get是得到数据
将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
成员变量private,提供对应的getXxx()/setXxx()方法 总结:明确试用场景需求,封装之后,写入数据用(输入)set方法,得到数据(输出)用get方法。
|