In: Computer Science
public class GroceryCart {
private static final int DEFAULT_CAPACITY = 10;
/*
* Default constructor with zero arguments. This constructs a grocery
* cart with a default capacity of ten items.
*/
public GroceryCart() {
}
/*
* Alternate constructor which takes in a maxCapacity. This maxCapacity
* determines how many items can fit inside of this groceryCart.
*/
public GroceryCart(int maxCapacity) {
}
/*
* Adds an item to the grocery cart. Returns true if the item was added
* successfully (i.e. there was still room in the cart to add it.
*/
public boolean addItem(String item, Double cost) {
return false;
}
/*
* Removes the specified item from the cart. Returns true if the item was
* successfully removed (i.e. the item existed in the cart).
*/
public boolean removeItem(String item) {
return false;
}
/*
* Empties the cart of all contents.
*/
public void emptyCart() {
}
/*
* Returns a string representation of the carts contents. The contents
* should be printed out in alphabetical order. It should be of
* the form, "item:price, item:price, item:price". An example:
* "Green Beans:2.99, Milk:41.99, Rolled Oats:1.99, Zucchini:2.67"
*/
public String toString() {
return "";
}
}
NOTE: Since you have not enclosed any description of the problem, I am providing the definitions of all the methods as per my understanding.
CODE
class Grocery {
private String item;
private double cost;
public Grocery(String item, double cost) {
this.item = item;
this.cost = cost;
}
public String getItem() {
return item;
}
public double getCost() {
return cost;
}
}
public class GroceryCart {
private static final int DEFAULT_CAPACITY = 10;
private Grocery[] groceries;
private int size;
/*
* Default constructor with zero arguments. This constructs a grocery
* cart with a default capacity of ten items.
*/
public GroceryCart() {
groceries = new Grocery[DEFAULT_CAPACITY];
size = 0;
}
/*
* Alternate constructor which takes in a maxCapacity. This maxCapacity
* determines how many items can fit inside of this groceryCart.
*/
public GroceryCart(int maxCapacity) {
groceries = new Grocery[maxCapacity];
size = 0;
}
/*
* Adds an item to the grocery cart. Returns true if the item was added
* successfully (i.e. there was still room in the cart to add it.
*/
public boolean addItem(String item, Double cost) {
if (size >= groceries.length) {
return false;
}
groceries[size ++] = new Grocery(item, cost);
}
/*
* Removes the specified item from the cart. Returns true if the item was
* successfully removed (i.e. the item existed in the cart).
*/
public boolean removeItem(String item) {
for (int i=0; i<groceries.length; i++) {
if (groceries[i].getItem().equals(item)) {
for (int j=i; j<groceries.length; j++) {
groceries[j] = groceries[j+1];
}
size --;
return true;
}
}
return false;
}
/*
* Empties the cart of all contents.
*/
public void emptyCart() {
groceries = new Grocery[DEFAULT_CAPACITY];
}
/*
* Returns a string representation of the carts contents. The contents
* should be printed out in alphabetical order. It should be of
* the form, "item:price, item:price, item:price". An example:
* "Green Beans:2.99, Milk:41.99, Rolled Oats:1.99, Zucchini:2.67"
*/
public String toString() {
String res = "";
for(int i=0; i<groceries.length; i++) {
res += groceries[i].getItem() + ":" + groceries[i].getCost() + ", ";
}
return res.substring(0, res.length() - 2);
}
}