Question

In: Computer Science

Write a class named GroceryList that represents a list of items to buy from the market,...

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). It also has client class called GroceryClient which creates objects of GroceryList and GroceryItemOrder. (For this assignment, you will have to submit 3 .java files: one for each class and 3 .class files associated with these .java files. So in total you will be submitting 6 files for this problem.) 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. The GroceryClient class has static main(..) method and initializes objects for GroceryItemOrder class by adding different items along with their unit item price and available quantity and then creates a GroceryList class object and adds the required grocery list using add() method and determines the total cost using getTotalCost() method and displays it on the console.

Solutions

Expert Solution

/**
 * @fileName GroceryList.java
 * @author ravi
 * @since 1/2/17
 */



public class GroceryList {
    GroceryItemOrder[] groceryList;
    public static int count;

    /**
     * This is default constructor 
     */
    public GroceryList() {
        this.groceryList = new GroceryItemOrder[10];
    }

    /**
     * Adding item to the groceryList
     * @param item
     */
    public void add(GroceryItemOrder item) {
        if (count < 10) {
            groceryList[count++] = item;
        }
    }

    /**
     * Give total cast for all item for 
     * @return
     */

    public double getTotalCost() {
        double total = 0;
        for (GroceryItemOrder item : this.groceryList) {
            if (item != null) {
                total += item.getCost();
            }
        }
        return total;
    }

}
/**
 * @fileName GroceryItemOrder.java
 * @author
 * @since 1/2/17
 */



public class GroceryItemOrder {

    String name;
    int quantity;
    double pricePerUnit;

    /**
     * This is parametrized constructor this will initialize the instance of  GroceryItemOrder class
     * @param name
     * @param quantity
     * @param pricePerUnit
     */
    public GroceryItemOrder(String name, int quantity, double pricePerUnit) {
        this.name = name;
        this.quantity = quantity;
        this.pricePerUnit = pricePerUnit;
    }

    /**
     * This will return the coast fro all quantity for item
     * @return
     */
    public double getCost() {
        return this.quantity * this.pricePerUnit;
    }

    /**
     * This will set the quantity of item
     * @param quantity
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}
/**
 * @fileName GroceryClient.java
 * @author
 * @since 1/2/17
 */


package groceryList;

public class GroceryClient {
    public static void main(String[] args) {
        GroceryItemOrder item1 = new GroceryItemOrder("cookies",4,2.30);
        GroceryItemOrder item2 = new GroceryItemOrder("chocolate",15,15.30);
        GroceryItemOrder item3 = new GroceryItemOrder("icecream",10,40.30);
        GroceryItemOrder item4 = new GroceryItemOrder("Apple",20,30.30);
        GroceryItemOrder item5 = new GroceryItemOrder("orange",20,20.30);
        GroceryItemOrder item6 = new GroceryItemOrder("kivy",4,45.30);
        GroceryItemOrder item7 = new GroceryItemOrder("banana",50,5.30);
        GroceryItemOrder item8 = new GroceryItemOrder("salt",4,8.30);
        GroceryItemOrder item9 = new GroceryItemOrder("lemon",4,3.30);

        GroceryList groceryList = new GroceryList();
        groceryList.add(item1);
        groceryList.add(item2);
        groceryList.add(item3);
        groceryList.add(item4);
        groceryList.add(item5);
        groceryList.add(item6);
        groceryList.add(item7);
        groceryList.add(item8);
        groceryList.add(item9);


        System.out.println("Total Cost :"+groceryList.getTotalCost());
    }
}

output:


Related Solutions

Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler. iterator: returns an...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
Modify the linked list code from class to work with strings. Insert the following food items...
Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list. c++
Write a class named GasTank containing: An instance variable named amount of type double, initialized to...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is...
In an eclipse or blues compatible code, please create a class named Items including the described...
In an eclipse or blues compatible code, please create a class named Items including the described methods (replace and delete0 Write the following static method: /** Replaces each occurrence of an oldItem in aList with newItem */ public static void replace(ArrayList<String>aList, String oldItem, String newItem) Write the following static method: /**Deletes the first occurrence of target in aList*/ public static void delete(ArrayList<String>aList, String target)
a. Design a class named ItemForSale that holds data about items placed for sale on Carlos's...
a. Design a class named ItemForSale that holds data about items placed for sale on Carlos's List, a classified advertising website. Fields include an ad number, item description, asking price, and phone number. Include get and set methods for each field. Include a static method that displays the website's motto ("Sell Stuff Locally!"). Include two overloaded constructors as follows: A default constructor that sets the ad number to 101, the asking price to $1, and the item description and phone...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT