0

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

}
5
  • Can you share your ItemList class? Commented Oct 23, 2020 at 4:20
  • 1
    You're invoking a constructor new Item(String, int, String ) in addItems() method and it is not defined Commented Oct 23, 2020 at 4:30
  • I don't have an ItemList class. It should say "Adds an item to the Item Array List". Commented Oct 23, 2020 at 4:37
  • why don't you just make another constructor accepting String, int, String ? A class may have more than one if the signature is different Commented Oct 23, 2020 at 5:11
  • Yes. I ended up making another constructor. Thank you for your help! Commented Oct 23, 2020 at 21:49

2 Answers 2

1

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));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.