Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Grocery List Program

$
0
0
Here is the assignment:

Here is the assignment:
Write a class named GroceryList that represents a person’s 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 (example: four boxes of cookies).

The GroceryList class should use an array field to store the grocery items, as well as keeping track of its size (number of items in the list so far). Assume that a grocery list will have no more than 10 items. A GroceryList object should have the following methods:

public GroceryList( )

Constructs a new empty grocery list.

public void add(GroceryItemOrder item)

Adds the given item order to this list, if the list is not full(i.e., has fewer than 10 items).

public double getTotalCost( )

Returns the total sum cost of all grocery item order in this list.

The GroceryItemOrder class should store an item quantity and a price per unit. A GroceryItemOrder object should have the following methods:

public GroceryItemOrder(String name, int quantity, double pricePerUnit)

Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.

public double getCost( )

Returns the total cost of this item in its given quantity. For example, four boxes of cookies that cost 2.30 per unit have a total cost of 9.20.

public void setQuantity(int quantity)

Sets this grocery item’s quantity to be the given value.
import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;

public class GroceryList
{
int totalcost = 0;
ArrayList<GroceryItemOder> List = new ArrayList <GroceryList> ();
//Initalizes a scanner for user input
Scanner input = new Scanner (System.in);
//Constructs a new empty grocery list.
public GroceryList( )
{

}
import java.lang.String;
import java.util.Scanner;
public class GroceryItemOrder
{

	public static void main (String [] args)
		{
			int totalcost = 0;
		//Initializes scanner for user input
		Scanner in = new Scanner (System.in);

	// Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
	public GroceryItemOrder(String name, int quantity, double pricePerUnit)
	{
		System.out.println ("Enter the grocery item that you want:");
		String name.nextLine ();

		System.out.println ("Enter the grocery item cost");
		int price.nextInt ();

		System.out.println ("Enter in the quantity of the items":)/>/>;
		int quantity.nextInt ();

		name2= name;
		quantity2 = quantity;
		price = pricePerUnit;

	}


	//Returns the total cost of this item in its given quantity.  For example, four boxes of cookies that cost 2.30 per unit have a total cost of 9.20.
	public double getCost( )
	{
		totalcost = price * quantity;
		return totalcost;
	}



	//Sets this grocery item’s quantity to be the given value.
	public void setQuantity(int quantity)
	{


	}
}
//Adds the given item order to this list, if the list is not full(i.e., has fewer than 10 items).
public void add(GroceryItemOrder item)
{
List.add(item);
}

//Returns the total sum cost of all grocery item order in this list.
public double getTotalCost( )
{
return totalcost;

}

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>