简体   繁体   English

如何显示 arraylist 中的元素?

[英]How do I display the elements from my arraylist?

I am writing a program for cashier queueing system.我正在为收银员排队系统编写程序。 The program should add, edit, and delete customers info: id, lastname, firstname, and age.该程序应添加、编辑和删除客户信息:id、lastname、firstname 和 age。 The program should prioritize or display first the customers that age 60 above and then the other customers will follow.该程序应优先显示或显示 60 岁以上的客户,然后其他客户将跟随。

Here is my CustomerInfo class.这是我的CustomerInfo class。

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;
  }
}

And this is my add and view code:这是我的添加和查看代码:

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));
              }
           }
        }
    }
}

The problem is after I run and input all the elements, the it won't display the statements and the program will exit immediately.问题是在我运行并输入所有元素后,它不会显示语句并且程序将立即退出。

I am glad you improved your question and it shows that you've tried.我很高兴你改进了你的问题,这表明你已经尝试过了。 So here is what I was saying earlier.所以这就是我之前所说的。 Using a comparator, add the following to your code:使用比较器,将以下内容添加到您的代码中:

Add this class添加此 class

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();
    }
  
}

Then in the main然后在主要

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());
        }

The logic of the program seems to be redudantly complicated, it is not quite clear why you duplicate the array of customer data cusInfo with the several lists, store the input data in the array and do not use it at all in the loop after "VIEW CUSTOMER DETAILS" message.程序的逻辑似乎非常复杂,不太清楚为什么你用几个列表复制客户数据cusInfo的数组,将输入数据存储在数组中,并且在"VIEW CUSTOMER DETAILS"之后的循环中根本不使用它"VIEW CUSTOMER DETAILS"消息。

When you use ci variable, the lists are populated with empty strings and 0 age.当您使用ci变量时,列表将填充空字符串和 0 年龄。

Simple fixes for the existing code just to make it work could be:对现有代码进行简单修复以使其正常工作可能是:

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++;
    }
}

However, if the actual task is to sort the customers by their age and move senior people to the top, a custom Comparator should be implemented and used when calling Arrays.sort (eg as a lambda).但是,如果实际任务是按年龄对客户进行排序并将高级人员移到顶部,则应在调用Arrays.sort时实现并使用自定义Comparator器(例如作为 lambda)。

The logic of sorting could be like this: if only one of the customers is senior, place it first;排序的逻辑可能是这样的:如果只有一个客户是资深的,则将其放在第一位; else compare the customers by age as is: 60 -> 65 -> 70 -> 35 -> 45 -> 59否则按年龄比较客户:60 -> 65 -> 70 -> 35 -> 45 -> 59

Also, the redundant code of populating unnecessary lists may be removed completely, thus resulting in the following:此外,填充不必要列表的冗余代码可能会被完全删除,从而导致以下结果:

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);
}

Sample output:样品 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM