//主界面代码:
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException { //还要加上throws IOException这段话
Scanner scan = new Scanner(System.in);
CustomerArrays add = new CustomerArrays();
boolean isFlag = true;
label1:while(isFlag){
System.out.println("-----------------客户信息管理软件-----------------" + "\n");
System.out.println(" 1.添 加 客 户 ");
System.out.println(" 2.修 改 客 户 ");
System.out.println(" 3.删 除 客 户 ");
System.out.println(" 4.客 户 列 表 ");
System.out.println(" 5.退 出 " + "\n");
System.out.print(" 请选择 (1-5): ");
int num = scan.nextInt();
if(1 <= num && num <= 5){
switch(num){
case 1:
add.AddCustomer();
continue label1;
case 2:
System.out.println("---------------------修改客户---------------------");
System.out.print("请输入需要修改客户的编号(-1退出)");
int num1 = scan.nextInt();
if(num1 == -1){
continue label1;
}else if(0 < num1 && num1 <= add.NumOfCustomers){
add.EditCustomer(num1);
System.out.println("---------------------修改完成---------------------");
continue label1;
}else{
System.out.println("没有当前编号的客户,为您返回初始界面");
continue label1;
}
case 3:
System.out.println("---------------------删除客户---------------------");
System.out.print("请输入需要删除客户的编号(-1退出)");
int num2 = scan.nextInt();
if(num2 == -1){
continue label1;
}else if(0 < num2 && num2 <= add.NumOfCustomers){
System.out.print("确认是否删除?(Y/N)");
String str = scan.next();
if(str.equals("Y") || str.equals("y")){
add.DeleteCustomer(num2);
System.out.println("---------------------删除完成---------------------");
continue label1;
}else if(str.equals("N") || str.equals("n")){
continue label1;
}else{
System.out.println("输入错误,为您返回初始界面");
continue label1;
}
}else{
System.out.println("没有当前编号的客户,为您返回初始界面");
continue label1;
}
case 4:
System.out.println("---------------------------客户列表---------------------------");
System.out.println("编号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "电话" + "\t" + "邮箱");
for(int i = 0;i < add.NumOfCustomers;i++){
System.out.println(i + 1 + "\t" + add.Customers[i].getName() + "\t" + add.Customers[i].getSex() + "\t" + add.Customers[i].getAge() + "\t" + add.Customers[i].getTel() + "\t" + add.Customers[i].getMail() );
}
System.out.println("--------------------------客户列表完成---------------------------");
continue label1;
case 5:
isFlag = false;
}
}else{
System.out.println("您输入的数字有误,请重新输入");
}
}
}
}
//客户信息代码:
package Project2;
public class Customer {
private String name;
private String sex;
private String age;
private String tel;
private String mail;
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 getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
//客户信息数组添加、修改、删除代码:
import java.io.*;
public class CustomerArrays {
Customer[] Customers = new Customer[100];//创造一个Customer数组,能容纳100个客户,如果不是Customer数组,最后cust不能赋给Customers,因为类型会不一样
int NumOfCustomers = 0;//客户数
Scanner scan = new Scanner(System.in);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));//判断回车
public void AddCustomer(){
Customer cust = new Customer();
System.out.println("---------------------添加客户---------------------");
System.out.print("姓名:");
String name = scan.next();
cust.setName(name);
System.out.println();
System.out.print("性别:");
String sex = scan.next();
cust.setSex(sex);
System.out.println();
System.out.print("年龄:");
String age = scan.next();
cust.setAge(age);
System.out.println();
System.out.print("电话:");
String tel = scan.next();
cust.setTel(tel);
System.out.println();
System.out.print("邮箱:");
String mail = scan.next();
cust.setMail(mail);
System.out.println();
System.out.println("---------------------添加完成---------------------");
Customers[NumOfCustomers] = cust;
NumOfCustomers++;
}
public void EditCustomer(int num) throws IOException{ //还要加上throws IOException这段话
System.out.print("姓名(" + Customers[num - 1].getName() + "):");
String name = bf.readLine(); //读取字符串name的长度
if(name.length() == 0){ //如果name长度是0,则代表输入的是回车,不进行替换操作
Customers[num - 1].setName(Customers[num - 1].getName());
}else{
Customers[num - 1].setName(name);
}
System.out.println();
System.out.print("性别(" + Customers[num - 1].getSex() + "):");
String sex = bf.readLine();
if(sex.length() == 0){
Customers[num - 1].setSex(Customers[num - 1].getSex());
}else{
Customers[num - 1].setSex(sex);
}
System.out.println();
System.out.print("年龄(" + Customers[num - 1].getAge() + "):");
String age = bf.readLine();
if(age.length() == 0){
Customers[num - 1].setAge(Customers[num - 1].getAge());
}else{
Customers[num - 1].setAge(age);
}
System.out.println();
System.out.print("电话(" + Customers[num - 1].getTel() + "):");
String Tel = bf.readLine();
if(Tel.length() == 0){
Customers[num - 1].setTel(Customers[num - 1].getTel());
}else{
Customers[num - 1].setTel(Tel);
}
System.out.println();
System.out.print("邮箱(" + Customers[num - 1].getMail() + "):");
String Mail = bf.readLine();
if(Mail.length() == 0){
Customers[num - 1].setMail(Customers[num - 1].getMail());
}else{
Customers[num - 1].setMail(Mail);
}
System.out.println();
}
public void DeleteCustomer(int num){
if(num - 1 < NumOfCustomers - 1){ //删除一个客户后客户总数NumOfCustomers应该减1,如果此时想删除的编号行是在中间而不是最后一行,
//则从该行开始,上一行复制下一行的数据。
for(int i = num - 1;i < NumOfCustomers - 1;i++){
//用下一行的Customer属性替换掉本行的Customer属性(即完成本行属性对下一行属性的复制)
Customers[i].setAge(Customers[i + 1].getAge());
Customers[i].setSex(Customers[i + 1].getSex());
Customers[i].setName(Customers[i + 1].getName());
Customers[i].setMail(Customers[i + 1].getMail());
Customers[i].setTel(Customers[i + 1].getTel());
}//在进行完for循环操作后,最后两行的数据是一模一样的,因为类里面实际上的的NumOfCustomers还未自减1,它仍有原来那么多的行数据。
}
NumOfCustomers--; //在完成NumOfCustomers自减1后,Test里面case 4的输出行数减少了最后一行
//但是减少的最后一行的Customer属性元素其实还在,只不过系统输出的时候没把他输出出来,看起来就好像被删除了一样。
//等到用户再继续添加客户的时候,减少的最后一行的Customer属性元素的属性被所赋的新的属性覆盖掉,而NumOfCustomers也再次加一,这时候
//就会把最后一行输出出来,只不过这时候输出的最后一行的属性元素已经被新的属性所替代,看起来就好像是新添加的一样。
}
}
项目二练习---客户信息管理系统(本人答案)--java.io.*获取回车功能
最新推荐文章于 2022-08-08 19:56:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)