简体   繁体   中英

Setting Up a Constructor and Array List to Accept Multiple Parameters (Java)

I'm trying to set up a constructor so that it asks how many items I have on a grocery list. Based on the number that's entered, a loop will run that many times. With each iteration of the loop, it will ask for the name, price, and taxable status of the item. I've set up my code below, but I keep getting the following error:

Constructor in class cannot be applied to given type. Required: int; found:java.lang.String,int,java.lang.String; Reason: actual and formal argument lists differ in length.

Is there a way to set up the constructor and array list so that they accept both int and String types?

public class Item
{
// instance variables - replace the example below with your own
private ArrayList<Item> items;
private int numberofItems;
private int count;
private String itemName;
private int cost;
private String taxable;


/**
 * Constructor for objects of class Item
 */
public Item(int numberofItems)
{
   items=new ArrayList<>();
   this.numberofItems = numberofItems;
   
    
}

/**
 * adds items to the arrayList
 */
public Item addItems(String itemName, int cost, String taxable)
{
   for (count =0; count <= numberofItems; count++); {
       items.add(new Item(itemName,cost, taxable));
    }
}
// /**
 // * Adds an item to the ItemList
 // */
// public void addItem(String itemName, int cost, String taxable)
// {
    // items.add(new ItemList(itemName, cost, taxable));

}

Add an additional constructor with three fields - itemName, cost, taxable in you class. This will resolve the compilation issue

public Item(String itemName, int cost, String taxable) {
    this.itemName = itemName;
    this.cost = cost;
    this.taxable = taxable;
}

Note: It will be worth in splitting the class. One can be Item and another could be ItemFactory (which creates and populates the list of item).

I don't know exactly what you are trying to achieve but this could be of help. A few points which can be improved by the way.

  • If count represents the number of Items to have, no need to define count as a variable in the Item class.
  • The list of Items could be separated from the Item class itself, and if you use say, a List,java has built-in functions, .size() , to get the size of the list.

You can end up creating two classes. One defines the Item class you want to use.

public class Item
{
  private String itemName;
  private int cost;
  private String taxable;


  public Item(String itemName, int cost, String taxable) {
    this.itemName = itemName;
    this.cost = cost;
    this.taxable = taxable;
  }
}

and another class can create objects of Item.

public class Application {

  private static ArrayList<Item> listItems = new ArrayList<>();
  public static void main(String[] args) {

    System.out.println("How many items are present on the grocery list?: ");
    Scanner sc = new Scanner(System.in);
    int count = sc.nextInt();

    if(count > 0 ){
      for (; count > 0 ; count--){
        // request user for itemName, cost and taxable
        addItems(itemName, cost, taxable);
      }
    }
    //get the size of items with, listItems.size()
  }

  private static void addItems(String itemName, int cost, String taxable){
    listItems.add(new Item(itemName, cost, taxable));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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