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 IN C++ Create a class named Coord in C++ Class has 3 private data items...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items               int xCoord;               int yCoord;               int zCoord; write the setters and getters. They should be inline functions               void setXCoord(int)             void setYCoord(int)            void setZCoord(int)               int getXCoord()                     int getYCoord()                   int getZCoord() write a member function named void display() that displays the data items in the following format      blank line      xCoord is                          ????????      yCoord is                          ????????      zCoord...
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....
Define and implement a class named Coach, which represents a special kind of person. It is...
Define and implement a class named Coach, which represents a special kind of person. It is to be defined by inheriting from the Person class. The Coach class has the following constructor: Coach(string n, int sl) // creates a person with name n, whose occupation // is “coach” and service length is sl The class must have a private static attribute static int nextID ; which is the unique personID of the next Coach object to be created. This is...
Create a List Create a class that holds an ordered list of items. This list should...
Create a List Create a class that holds an ordered list of items. This list should have a variable size, most importantly this class should implement the interface SimpleArrayList. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other words, you have to write the code for each of the functions specified in the SimpleArrayList interface. You are not allowed to use any 3rd party data structures or libraries...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final fields of type double to store the real and imaginary parts. Try to use constructor chaining to implement 2 of the 3 required constructors. If you cannot complete one or more of the methods, at least make sure that it returns some value of the correct type; this will allow the tester to run, and it will make it much easier to evaluate your...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
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++
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT