I need help coding this in java:


Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity.
The GroceryList should use an ArrayList of type GroceryItemOrder to store its list of items to purchase.
GroceryList should have the following methods:
1. A constructor that constructs a new (initially empty) groceryList
2. A method: public void add( )
which add a new GroceryItemOrder to the GroceryList
Note: this method should request user input from the keyboard for each new GroceryItemOrder
that is created
3. A method: public double getTotalCost( )
which returns the total cost of all items in the grocery list
4. A method: public void printGroceryList( )
that prints the whole grocery list in the following format:
ITEM QUANTITY COST Each COST Tot.
<item name> _tab_ <item quantity> _tab_ <price per unit> _tab_ <total price>

…________________________________________________________________
Total Cost <total cost>

Have you created the GroceryItemOrder class yet?
I would do that first.

I am awful at java but this is what i have so far:

import java.util.*;
import java.io.*;
public class GroceryList{
    Scanner console = new Scanner(System.in);
    String productName;
    double price = 0;
    int quantity = 0;
    ArrayList<String> list = new ArrayList<String>();
    
    public void add(){
        System.out.println("Enter product name: ");
        productName = console.next();
    }
    
    public double getTotalCost(){
        System.out.println("Enter cost of item: ");
        double price = console.nextDouble();
        System.out.println("Enter quantity: ");
        int quantity = console.nextInt();
       
    }
    public void printGroceryList(){
        System.out.println(  productName + ""+ quantity+ "" + price);
    }

}

Can someone please help me?!?!

Can someone please help me?!?!

In what way can we help you

Is there an error?

I notice in your code that you don't have a constructor function in the code you posted above

except for the default constructor, which is automatically generated during compilation if no other constructor is provided.

the more interesting code to look at is: how do you use this class? where do you call it's methods?

Something has to be put into the GroceryList and that's the GroceryItemOrder.
If you build that class now, some things will become apparent.

It sounds like the GroceryItemOrder is not necessarily a single item (so it can contain a quantity).

I also would imagine the getTotal() in that class would return the quantity*price.

I do not understand what is meant by constructor function... should i use an arraylist as a parameter in the method add instead of attempting to get a user input?

for ex: [eggs,milk,bread,bacon]

No. You need a place to put the items when you get them.

So I made the ArrayList Grocery Item Order:

ArrayList<String> GroceryItemOrder= new ArrayList<String>();

But i do not know how to take the user input and put it into my ArrayList.
Then how would you add the quantity, price, total price to the ArrayList...or would I not do that?

GroceryItemOrder will be a class.

public class GroceryItemOrder
{
   private String itemName;
   private double price;
   private int quantity;

   //MORE CODE GOES HERE
}

GroceryList will be a class that either HAS an ArrayList<GroceryItemOrder>
...or *is* an ArrayList<GroceryItemOrder>

import java.io.*;
import java.util.*;

public class GroceryList extends ArrayList<GroceryItemOrder>
{
// more code goes here
}

You can then use the GroceryList class to manipulate itself or you can have another class containing "main" that will manipulate the GroceryList.

public class GroceryList{
    Scanner console = new Scanner(System.in);
    String productName;
    double price = 0;
    int quantity = 0;
    double totalPrice = 0;
    ArrayList<String> GroceryItemOrder= new ArrayList<String>();
    
    
    public void add(Scanner Input){
        System.out.println("Enter product name: ");
        productName = console.next();
        GroceryItemOrder.add(productName);
        
    }

This doesn't give me a syntax error, however I still cannot get it to work....

This was the second problem of the code that i forgot to add:


GroceryItemOrder class should store an item’s name, number of units, and price per unit and should have the following methods:
1. A constructor: public GroceryItemOrder(String name, int quantity, double pricePerUnit)
2. A method: public double getCost( )
which returns the total cost of this item for the given quantity. For example, 4 boxes of cookies that cost $2.30 per unit would return $9.20.
3. A method: public void setQuantity( int quantity)
sets this grocery item’s quantity to the given quantity
4. A method: public void print( )
prints the GroceryItemOrder in the following format:
<item name> _tab_ <item quantity> _tab_ <price per unit> _tab_ <total price>

GroceryItemOrder class

It won't be ArrayList<String>, it will be ArrayList<GroceryItemOrder>

It won't be ArrayList<String>, it will be ArrayList<GroceryItemOrder>

Why would it be Arraylist<GroceryItemOrder>?

I though inside the <> is the type of arraylist it is going to be?

could you please just help me edit my code, i do not understand you.

Disregard the last comment, I now can relate the two classes. However, I still cannot get my user input into the original ArrayList so I can use it in the GroceryItemOrder class

So in your add method of your GroceryList class, you are asking for input, right?
That input is going into a temporary GroceryItemOrder object (hint, hint), right?
...as you ask for each necessary element (like name and quantity)

Then, you're adding that temporary GroceryItemOrder to the class-level ArrayList<GroceryItemOrder> ... right? :)

So in your add method of your GroceryList class, you are asking for input, right?
That input is going into a temporary GroceryItemOrder object (hint, hint), right?
...as you ask for each necessary element (like name and quantity)

Then, you're adding that temporary GroceryItemOrder to the class-level ArrayList<GroceryItemOrder> ... right? :)

Correct!

So wouldn't this add the user input(item) into the ArrayList

public void add(Scanner Input){
        System.out.println("Enter product name: ");
        productName = console.next();
        Items.add(productName);

Well, it looks here like "productName" is just a string where you will need a string AND an integer for the amount. THEN you will add the GroceryItemOrder object to the GroceryList.

Also (on a slightly different note), if your Scanner is named Input, then wouldn't you call Input.next()? ...but still, you will be asking for a String then and integer.

After you have both of those items in a temp GroceryItemOrder, you will add them to the master list.

This is how i calculate the price and quantity of the list. However, I still do not know how to add it to the ArrayList

public double getTotalCost(){
        System.out.println("Enter cost of item: ");
        double price = console.nextDouble();
        System.out.println("Enter quantity: ");
        int quantity = console.nextInt();
        double totalPrice = price * quantity;
       return price;

Adding elements will be done in the add() method.

getTotalCost() will return to you the (sum) cost of all of the elements in the ArrayList.

I'm imagining something like this (quick-n-dirty):

import java.io.*;
import java.util.*;

public class GroceryList extends ArrayList<GroceryItemOrder>
{
   public void add()
   {
      Scanner inp = new Scanner(System.in);
      GroceryItemOrder tempGIO = new GroceryItemOrder();
      System.out.print("Enter item name: ");
      tempGIO.setName(inp.next());

      System.out.print("Enter item price (for 1): ");
      tempGIO.setPrice(inp.nextDouble());

      System.out.print("Enter item quantity: ");
      tempGIO.setQuantity(inp.nextInt());

      this.add(tempGIO);
   }

   public double getTotalCost()
   {
      double dblTotalCost = 0;

      for(int i=0; i< this.size(); i++)
      {
         dblTotalCost += this.get(i).getTotal();
      }

      return dblTotalCost;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.