简体   繁体   English

Java:使用getter从arraylist检索和打印项目

[英]Java: retrieving and printing out items from an arraylist using getters

I am writing a program to simulate a system in a coffee shop where the person who takes the orders of the customers gives a customer a token number and enters into a system the token number of the customer along with the items s/he has ordered. 我正在编写一个程序来模拟一家咖啡店中的系统,在该处接受客户订单的人会给客户一个令牌号,并将客户的令牌号与他/他订购的物品一起输入系统。 The recordOrder function carries out this operation which allows order details to be input. recordOrder函数执行此操作,该操作允许输入订单详细信息。 An order is represented by a token ID, tID, and an arrayList of strings representing the items in the order. 订单由令牌ID,tID和代表该订单中各项的字符串arrayList表示。

Here is what I have done so far: 到目前为止,这是我所做的:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

 public class Ques4 {

   private static class Order{
     private int tID;
     private int orderId;
     private String itemName;

    public Order(int tID,String itemName){
        this.tID=tID;
        this.itemName=itemName;

    }

    public int gettID() {
        return tID;
    }

    public void settID(int tID) {
        this.tID = tID;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    /*public String toString(){
        return "Token num: "+gettID()+ " Item name: "+getItemName();
    }*/

}

public static void main(String[] args) {


    ArrayList<Order> listOfItems= new ArrayList<Order>();
    recordOrder(listOfItems);


}

    private static void recordOrder(ArrayList<Order> listOfItems){

        int n, tnum;
        int num_Orders;
        String item = null;

        Scanner sc= new Scanner(System.in);

        System.out.println("Enter the token number: ");
        tnum= sc.nextInt();


       System.out.println("Enter the number of items for token number: "+tnum);
       n=sc.nextInt();
       sc.nextLine();


    System.out.println("Enter the items: ");

    for(int i=0; i<n;i++){

        item=sc.nextLine();

       }
    listOfItems.add(new Order(tnum, item));


 for(Order list: listOfItems){
     System.out.println("Token num: "+list.gettID()+ " Item name: "+list.getItemName());}   


        }
  }

The problem is that only the last item which I enter is being printed out,that is, suppose I input token number as 9, number of items as 3 and the item names as cheesecake, burritos, tacos, only tacos is being printed out and the output will be like this: 问题是只有我输入的最后一个项目被打印出来,也就是说,假设我输入令牌编号为9,项目编号为3,项目名称为乳酪蛋糕,墨西哥卷饼,炸玉米饼,只打印了炸玉米饼和输出将是这样的:

Token num: 9 Item name: tacos 代币号:9产品名称:玉米饼

It should instead be: 相反,它应该是:

Token num: 9 Item name: cheesecake burritos tacos 代币号:9产品名称:芝士蛋糕卷饼炸玉米饼

for(int i=0; i<n;i++){
    item=sc.nextLine();
    listOfItems.add(new Order(tnum, item));
}

Your logic of adding the items should be inside the loop. 您添加项目的逻辑应该在循环内。 Since you are adding it outside the loop, only one element gets added(that is the last element) 由于要在循环外添加它,因此仅添加一个元素(即最后一个元素)

EDIT: to print the required way: 编辑:打印所需的方式:

System.out.print("Token num: "+list.gettID()+ " Item name:");
for(Order list: listOfItems){
     System.out.print(" "+list.getItemName());
}   
System.out.println();

I think you want this: 我想你想要这个:

for(int i=0; i<n;i++){
    item += sc.nextLine();
}

listOfItems.add(new Order(tnum, item));

This will append the items to the String you're building each time for the for loop, instead of overwriting the variable each time. 每次都会为for循环将这些项追加到您要构建的String上,而不是每次都覆盖变量。

If you have to add more items to one object, you will need to initialize itemName as an array of String. 如果必须向一个对象添加更多项,则需要将itemName初始化为String数组。

String[] itemName;

And in your setter method, add to the string instead of replacing the whole value with the new one. 然后在您的setter方法中,将其添加到字符串中,而不是将整个值替换为新值。 This is why you get only the latest input value. 这就是为什么您仅获得最新输入值的原因。

 public void setItemName(String itemName) {
        this.itemName.add(itemName);
    }

Similarly, you can handle the getter to output the String array. 同样,您可以处理getter以输出String数组。

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

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