我正在写一个出纳排队系统的程序。程序应该添加、编辑和删除客户信息:id、lastname、firstname和age。该计划应优先考虑或显示年龄超过60岁的客户,然后其他客户将跟随。
这是我的 CustomerInfo
班级。
public class CustomerInfo {
//instance variables
public int custID;
public String lastname, firstname;
public int age;
//overloading constructors
public CustomerInfo(int custID, String lastname, String firstname, int age) {
this.custID = custID;
this.lastname = lastname;
this.firstname = firstname;
this.age = age;
}
public CustomerInfo() {
custID = 0;
lastname = "";
firstname = "";
age = 0;
}
//setters or mutator methods
public void setCustID(int custID) {
this.custID = custID;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setAge(int age) {
this.age = age;
}
//getters or accessor methods
public int getCustID() {
return custID;
}
public String getLastName() {
return lastname;
}
public String getFirstName() {
return firstname;
}
public int getAge() {
return age;
}
}
这是我的添加和查看代码:
import java.util.*;
import javax.swing.*;
public class FinalProject {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner input = new Scanner(System.in);
CustomerInfo[] cusInfo = new CustomerInfo[5];
ArrayList<Integer> cusId = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<Integer> ages = new ArrayList<>();
ArrayList<Integer> cusIdSenior = new ArrayList<>();
ArrayList<String> lastNamesSenior = new ArrayList<>();
ArrayList<String> firstNamesSenior = new ArrayList<>();
ArrayList<Integer> agesSenior = new ArrayList<>();
int count = 0, countSenior = 0;
CustomerInfo ci = new CustomerInfo();
int customernum = 0;
System.out.println("ADD CUSTOMER DETAILS");
for(int i = 0; i < 5; i++) {
System.out.print("Customer's ID : ");
int custID = input.nextInt();
System.out.print("Customer's Last Name : ");
String lastname = input.next();
System.out.print("Customer's First Name : ");
String firstname = input.next();
System.out.print("Customer's Age : ");
int age = input.nextInt();
CustomerInfo c = new CustomerInfo(custID, lastname, firstname, age);
cusInfo[customernum] = c;
customernum++;
System.out.println();
}
//VIEW CUSTOMER DETAILS
if(customernum == 0) {
JOptionPane.showMessageDialog(null, "QUEUE IS EMPTY. NOTHING TO VIEW.");
}
else{
System.out.println("VIEW CUSTOMER DETAILS");
for(int i = 0; i < customernum; i++) {
cusId.add(ci.custID);
lastNames.add(ci.lastname);
firstNames.add(ci.firstname);
ages.add(ci.age);
count++;
}
for(int i = 0; i < count; i++) {
if(ages.get(i) >= 60) {
cusIdSenior.add(cusId.get(i));
lastNamesSenior.add(lastNames.get(i));
firstNamesSenior.add(firstNames.get(i));
ages.add(ages.get(i));
countSenior++;
}
}
for(int i = 0; i < countSenior; i++) {
System.out.printf("Customer ID : %d\n", cusIdSenior.get(i));
System.out.printf("Customer Last Name : %s\n", lastNamesSenior.get(i));
System.out.printf("Customer First Name : %s\n", firstNamesSenior.get(i));
System.out.printf("Customer Age : %s\n", agesSenior.get(i));
}
for(int i = 0; i < count; i ++) {
if(ages.get(i) < 60 && ages.get(i) > 0) {
System.out.printf("Customer ID : %d\n", cusId.get(i));
System.out.printf("Customer Last Name : %s\n", lastNames.get(i));
System.out.printf("Customer First Name : %s\n", firstNames.get(i));
System.out.printf("Customer Age : %s\n", ages.get(i));
}
}
}
}
}
问题是在我运行并输入所有元素后,它不会显示语句,程序将立即退出。
2条答案
按热度按时间qc6wkl3g1#
程序的逻辑似乎非常复杂,不太清楚为什么要复制客户数据数组
cusInfo
对于多个列表,将输入数据存储在数组中,之后不要在循环中使用它"VIEW CUSTOMER DETAILS"
信息。当你使用
ci
变量,列表中填充了空字符串和0。为使现有代码正常工作而对其进行的简单修复可以是:
但是,如果实际任务是按年龄对客户进行分类,并将高级人员移到最高层,那么
Comparator
应在调用时实现和使用Arrays.sort
(例如,作为lambda)。排序的逻辑可以是这样的:如果只有一个客户是高级的,就把它放在第一位;否则按年龄对比客户:60->65->70->35->45->59
此外,填充不必要列表的冗余代码可能会被完全删除,从而导致以下结果:
样本输出:
zaq34kh62#
我很高兴你改进了你的问题,这表明你已经尽力了。这就是我刚才说的。使用比较器,将以下内容添加到代码中:
添加此类
那么主要是