如何显示arraylist中的元素?

iqjalb3h  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(354)

我正在写一个出纳排队系统的程序。程序应该添加、编辑和删除客户信息: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));
              }
           }
        }
    }
}

问题是在我运行并输入所有元素后,它不会显示语句,程序将立即退出。

qc6wkl3g

qc6wkl3g1#

程序的逻辑似乎非常复杂,不太清楚为什么要复制客户数据数组 cusInfo 对于多个列表,将输入数据存储在数组中,之后不要在循环中使用它 "VIEW CUSTOMER DETAILS" 信息。
当你使用 ci 变量,列表中填充了空字符串和0。
为使现有代码正常工作而对其进行的简单修复可以是:

System.out.println("VIEW CUSTOMER DETAILS");
for (int i = 0; i < customernum; i++) {
    ci = cusInfo[i];              //  use the data stored in the array!
    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));
        agesSenior.add(ages.get(i));              // fixed typo, should be agesSenior
        countSenior++;
    }
}

但是,如果实际任务是按年龄对客户进行分类,并将高级人员移到最高层,那么 Comparator 应在调用时实现和使用 Arrays.sort (例如,作为lambda)。
排序的逻辑可以是这样的:如果只有一个客户是高级的,就把它放在第一位;否则按年龄对比客户:60->65->70->35->45->59
此外,填充不必要列表的冗余代码可能会被完全删除,从而导致以下结果:

Scanner input = new Scanner(System.in);
CustomerInfo[] cusInfo = new CustomerInfo[5];

System.out.println("ADD CUSTOMER DETAILS");

for (int i = 0; i < cusInfo.length; 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[i] = c;
   System.out.println();
}

System.out.println("VIEW CUSTOMER DETAILS");
Arrays.sort(cusInfo, (c1, c2) -> c1.age >= 60 ^ c2.age >= 60 // check if _only one_ is senior
        ? (c1.age >= 60 ? -1 : 1) // put senior first
        : Integer.compare(c1.age, c2.age) // compare by age
);

for (CustomerInfo ci : cusInfo) {
    System.out.printf("Customer ID         : %d\n", ci.custID);
    System.out.printf("Customer Last Name  : %s\n", ci.lastname);
    System.out.printf("Customer First Name : %s\n", ci.firstname);
    System.out.printf("Customer Age        : %s\n", ci.age);
}

样本输出:

VIEW CUSTOMER DETAILS
Customer ID         : 1
Customer Last Name  : aa
Customer First Name : bb
Customer Age        : 60
Customer ID         : 4
Customer Last Name  : eg
Customer First Name : jg
Customer Age        : 72
Customer ID         : 5
Customer Last Name  : ww
Customer First Name : nh
Customer Age        : 39
Customer ID         : 3
Customer Last Name  : gg
Customer First Name : qq
Customer Age        : 48
Customer ID         : 2
Customer Last Name  : rr
Customer First Name : ww
Customer Age        : 58
zaq34kh6

zaq34kh62#

我很高兴你改进了你的问题,这表明你已经尽力了。这就是我刚才说的。使用比较器,将以下内容添加到代码中:
添加此类

public class SortByAge implements Comparator<CustomerInfo> {

    @Override
    public Comparator<CustomerInfo> reversed() {
        return Comparator.super.reversed();
    }

    @Override
    public int compare(CustomerInfo o1, CustomerInfo o2) {
        return o1.getAge() - o2.getAge();
    }

}

那么主要是

Scanner input = new Scanner(System.in);
        ArrayList<CustomerInfo> customerInfo = new ArrayList<>();

        System.out.println("ADD CUSTOMER DETAILS");
        for(int i = 0; i < 2; 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);
           customerInfo.add(c);
           System.out.println();
        }

        SortByAge sba = new SortByAge();
        Collections.sort(customerInfo,sba.reversed());

        for(CustomerInfo ci : customerInfo){
            System.out.printf("Customer ID         : %d\n", ci.getCustID());
            System.out.printf("Customer Last Name  : %s\n", ci.getFirstName());
            System.out.printf("Customer First Name : %s\n", ci.getLastName());
            System.out.printf("Customer Age        : %s\n", ci.getAge());
        }

相关问题