• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use an array instead of arrayList  RSS feed

 
Greenhorn
Posts: 4
Eclipse IDE Firefox Browser Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a program for my class's chapter on classes. I need to make a program that makes a grocery list using an array. Here's the assignment from the book.
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 (example: four boxes
of cookies). The GroceryList class should use an array field to store the grocery items and to keep 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 has fewer than 10 items.
public double getTotalCost()
Returns the total sum cost of all grocery item orders 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.

I have the assignment mostly done, but the JUnit test case our teacher gave us to test it tests add() an array instead of an array list. For this reason, I need to change my program to use an array. Can anybody help me with this? my code is below.


 
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like making an int[], you can have a GroceryItemOrder[]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget that in your add() method you need to check if you have fewer than 10 items before adding a new item.
public void add(GroceryItemOrder item)
Adds the given item order to this list if the list has fewer than 10 items

I know you said you weren't finished but just in case this had slipped past you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!