本篇文章小编给大家分享一下Java使用集合来实现一个客户信息管理系统代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
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 array = new ArrayList();
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(ArrayListarray ){ 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(ArrayListarray ,String cid){ boolean flag = false; for (int i = 0; i (3)修改客户信息
public static void modifyCustomer(ArrayListarray ){ 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 (4)删除客户
public static void deleteCustomer(ArrayListarray ){ 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(ArrayListarray ){ if (array.size()==0){ System.out.println("无信息,请添加信息在再查询"); return;//为了程序不再往下执行 } System.out.println("编号t姓名t性别t年龄tt电话tt邮箱"); for (int i = 0; i (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错误如下:
首先 if (customers.getCid().equals( ))这一步比较的是遍历后的集合与输入的cid是否相同,所以应该是与cid比较而不是i;其次, array.set( );这一步是修改指定索引处的元素,返回被修改的元素;这里是用上面存储新的客户信息的c来修改索引处元素,所以修改后的代码为
for (int i = 0; i
模拟极限越野
模拟极限越野开着大巴在各种奇葩路况里送乘客。山路弯道多得让人
模拟火车
模拟火车让你当回真正的火车司机,得先学会看信号灯、操作控制杆
可口的披萨美味的披萨
可口的披萨美味的披萨带您体验经营一家披萨店的乐趣。游戏画风温
定制虚拟偶像最新版
定制虚拟偶像最新版是一款超级经典有趣的日本模拟装扮类型的手游
病娇模拟器手机版正版(yandere simulator)
病娇模拟器最新版手游是一款萌妹二次元模拟游戏,玩家在游戏中可