Need to be solved in Java, thanks a lot. (1) The GroceryItem Class Implement a c
ID: 3784971 • Letter: N
Question
Need to be solved in Java, thanks a lot.
(1) The GroceryItem Class Implement a class called GroceryItem that represents grocery items that one would pick up when doing your supermarket shopping. The class has 4 private instance variables: name of type String representing the name of the item (e.g., "Cascade") price of type float representing the cost of the item (e.g., $4.79) weight of type float representing the weight of the item in Kilograms (e.g., 2.4 kg) perishable of type boolean. A value of true indicates that the item needs to be refrigerated or frozen and false otherwise. Implement the following instance methods: public get methods for the attributes. A zero-argument constructor A constructor that accepts as arguments the item's name, price and weight. Note that the perishable variable is not indicated here. It should be set to false by default. Make another constructor to accept all 4 parameters, perishable being one of them. A toString() method that returns a string representation of the item like this: "Cascade weighing 2.4kg with price $4.79" (2) The GroceryBag Class Implement a class called GroceryBag that represents a bag which will hold GroceryItem objects. The class has the following private static constants: MAX_WEIGHT of type double which indicates the maximum weight that the bag can hold (set it to 5 Kg). Make use of this value properly in the code below. MAX_ITEMS of type integer that indicates the maximum number of items that can be placed in the bag (set it to 25). Make use of this value properly in the code below. The class has the following private instance variables: items which is an array that will hold GroceryItem objects. numItems which is the number of items in the bag. weight of type float which returns the total weight of all items currently in the bag.
Create the following instance methods:
Appropriate public get methods
A zero-argument constructor.
A toString() method that shows the number of items in the bag and the total weight of the bag
or outputs "An empty grocery bag ".
A method called addItem() which adds a given GroceryItem to the bag only if the total weight
of the bag is not exceeded by this item.
A method called removeItem(GroceryItem item) which removes the given item from the bag.
A heaviestItem() method that returns the heaviest item in the cart. Return null if cart empty.
A has(GroceryItem item) method that returns a boolean indicating whether or not the given
item is currently in the cart.
Test your code with the following program and ensure that the output is correct:
public class GroceryBagTestProgram {
public static void main(String args[]) {
GroceryItem g1, g2, g3, g4, g5, g6;
GroceryBag b1, b2;
g1 = new GroceryItem("Jumbo Cherries", 6.59f, 1.0f);
g2 = new GroceryItem("Smart-Ones Frozen Entrees", 1.99f, 0.311f, true);
g3 = new GroceryItem("SnackPack Pudding", 0.99f, 0.396f);
g4 = new GroceryItem("Nabob Coffee", 3.99f, 0.326f);
g5 = new GroceryItem("Fresh Salmon", 4.99f, 0.413f, true);
g6 = new GroceryItem("Coca-Cola 12-pack", 3.49f, 5);
b1 = new GroceryBag();
b2 = new GroceryBag();
b1.addItem(g1);
b1.addItem(g2);
b1.addItem(g3);
b1.addItem(g4);
b1.addItem(g5);
b1.addItem(g5);
b1.addItem(g5);
System.out.println("BAG 1: " + b1);
for(int i=0; i<b1.getNumItems(); i++)
System.out.println(" " + b1.getItems()[i]);
System.out.println("BAG 2: " + b2);
System.out.println(" Heaviest item in BAG 2: " + b2.heaviestItem());
b1.addItem(g6);
b2.addItem(g6);
System.out.println(" BAG 1: " + b1);
System.out.println("BAG 2: " + b2);
System.out.println(" BAG 1 contains Nabob Coffee: " + b1.has(g4));
System.out.println("BAG 1 contains a case of coke: " + b1.has(g6));
System.out.println("BAG 2 contains a case of coke: " + b2.has(g6));
System.out.println(" Heaviest item in BAG 1: " + b1.heaviestItem());
System.out.println("Heaviest item in BAG 2: " + b2.heaviestItem());
}
}
The output should be as follows:
BAG 1: A 3.2720003kg grocery bag with 7 items
Jumbo Cherries weighing 1.0kg with price $6.59
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99
SnackPack Pudding weighing 0.396kg with price $0.99
Nabob Coffee weighing 0.326kg with price $3.99 Fresh Salmon weighing 0.413kg with price $4.99 Fresh Salmon weighing 0.413kg with price $4.99 Fresh Salmon weighing 0.413kg with price $4.99 BAG 2: An empty grocery bag Heaviest item in BAG 2: null BAG 1: A 3.2720003kg grocery bag with 7 items BAG 2: A 5.0kg grocery bag with 1 items BAG 1 contains Nabob Coffee: true BAG 1 contains a case of coke: false BAG 2 contains a case of coke: true Heaviest item in BAG 1: Jumbo Cherries weighing 1.0kg with price $6.59 Heaviest item in BAG 2: Coca-Cola 12-pack weighing 5.0kg with price $3.49 (3) The Shopper Class Implement a class called Shopper which represents a person that buys grocery items. The Shopper should maintain an array (called cart) that contains all the loose GroceryItem objects that the shopper is planning to purchase. You should create (and make use of) a MAX_CART_ITEMS static constant to represent the maximum number of items that can be in the cart at any time (set to 100). Create the following methods: public get methods for the instance variables. A zero-argument constructor. A toString() method that returns a string representation of the shopper in the form: "Shopper with shopping cart containing 10 items". addItem(): adds a given GroceryItem to the shopping cart. removeItem(GroceryItem item): removes the given item from the shopping cart. Make sure that the array does not have gaps in it (i.e., move the last item in the cart into the removed item's position). A packBags() method which returns an array of packed GroceryBag objects and possibly some unpacked GroceryItem objects (e.g., a case of coke). The method should use the following packing algorithm: Fill as many bags as are necessary one at a time, each to just below its weight limit. That is take items in order from the cart and place them into an initially empty bag and stop when the next item will cause the bag to exceed its weight limit. Then make a new bag and pack that one until it is full. The items are taken sequentially from the cart, although ... as each item is packed it should be removed from the cart (use the removeItem() method). Therefore, the use of the removeItem() method will affect the order of the items as they are packed. If an item is too heavy (i.e., exceeds the MAX_WEIGHT) to be placed in a bag (e.g., a case of coke), then it should be left in the cart. In theory, all items in the cart may be too heavy, which means no bags may be packed. Or, all items in the cart are exactly the bag limit in weight, which means the number of bags will equal the number of items initially in the cart. Either way, your method should return an array with a size equal to the number of packed bags. Make use of the following test program:
public class ShopperTestProgram {
public static void main(String args[]) {
GroceryItem g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;
g1 = new GroceryItem("Smart-Ones Frozen Entrees",1.99f,0.311f, true);
g2 = new GroceryItem("SnackPack Pudding",0.99f,0.396f);
g3 = new GroceryItem("Breyers Chocolate Icecream",2.99f,2.27f, true);
g4 = new GroceryItem("Nabob Coffee",3.99f,0.326f);
g5 = new GroceryItem("Gold Seal Salmon",1.99f,0.213f);
g6 = new GroceryItem("Ocean Spray Cranberry Cocktail",2.99f,2.26f);
g7 = new GroceryItem("Heinz Beans Original",0.79f,0.477f);
g8 = new GroceryItem("Lean Ground Beef",4.94f,0.75f, true);
g9 = new GroceryItem("5-Alive Frozen Juice",0.75f,0.426f, true);
g10 = new GroceryItem("Coca-Cola 12-pack",3.49f,5.112f);
g11 = new GroceryItem("Toilet Paper - 48 pack",40.96f,10.89f);
// Make a new customer and add some items to his/her shopping cart
Shopper c = new Shopper();
c.addItem(g1); c.addItem(g2); c.addItem(g3); c.addItem(g4);
c.addItem(g5); c.addItem(g6); c.addItem(g7); c.addItem(g8);
c.addItem(g9); c.addItem(g10); c.addItem(g1); c.addItem(g6);
c.addItem(g2); c.addItem(g2); c.addItem(g3); c.addItem(g3);
c.addItem(g3); c.addItem(g3); c.addItem(g3); c.addItem(g10);
c.addItem(g11); c.addItem(g9); c.addItem(g5); c.addItem(g6);
c.addItem(g7); c.addItem(g8); c.addItem(g8); c.addItem(g8);
c.addItem(g5);
System.out.println(" INITIAL CART CONTENTS:");
for (int i=0; i<c.getNumItems(); i++) {
System.out.println(" " + c.getCart()[i]);
}
// Pack the bags and show the contents
GroceryBag[] packedBags = c.packBags();
for (int i=0; i<packedBags.length; i++) {
System.out.println(" BAG " + (i+1) + " (Total Weight = " +
packedBags[i].getWeight() + "kg) CONTENTS:");
for (int j=0; j<packedBags[i].getNumItems(); j++) {
System.out.println(" " + packedBags[i].getItems()[j]);
}
}
System.out.println(" REMAINING CART CONTENTS:");
for (int i=0; i<c.getNumItems(); i++) {
System.out.println(" " + c.getCart()[i]);
}
}
}
Here is the expected result (although the order of the items being packed may vary according to your
algorithm).
INITIAL CART CONTENTS:
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99
SnackPack Pudding weighing 0.396kg with price $0.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Nabob Coffee weighing 0.326kg with price $3.99
Gold Seal Salmon weighing 0.213kg with price $1.99
Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99
Heinz Beans Original weighing 0.477kg with price $0.79
Lean Ground Beef weighing 0.75kg with price $4.94
5-Alive Frozen Juice weighing 0.426kg with price $0.75
Coca-Cola 12-pack weighing 5.112kg with price $3.49
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99
Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99
SnackPack Pudding weighing 0.396kg with price $0.99
SnackPack Pudding weighing 0.396kg with price $0.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99 Breyers Chocolate Icecream weighing 2.27kg with price $2.99 Coca-Cola 12-pack weighing 5.112kg with price $3.49 Toilet Paper - 48 pack weighing 10.89kg with price $40.96 5-Alive Frozen Juice weighing 0.426kg with price $0.75 Gold Seal Salmon weighing 0.213kg with price $1.99 Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99 Heinz Beans Original weighing 0.477kg with price $0.79 Lean Ground Beef weighing 0.75kg with price $4.94 Lean Ground Beef weighing 0.75kg with price $4.94 Lean Ground Beef weighing 0.75kg with price $4.94 Gold Seal Salmon weighing 0.213kg with price $1.99 BAG 1 (Total Weight = 3.251kg) CONTENTS: Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99 Gold Seal Salmon weighing 0.213kg with price $1.99 Lean Ground Beef weighing 0.75kg with price $4.94 Lean Ground Beef weighing 0.75kg with price $4.94 Lean Ground Beef weighing 0.75kg with price $4.94 Heinz Beans Original weighing 0.477kg with price $0.79 BAG 2 (Total Weight = 3.295kg) CONTENTS: Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99 Gold Seal Salmon weighing 0.213kg with price $1.99 5-Alive Frozen Juice weighing 0.426kg with price $0.75 SnackPack Pudding weighing 0.396kg with price $0.99 BAG 3 (Total Weight = 4.54kg) CONTENTS: Breyers Chocolate Icecream weighing 2.27kg with price $2.99 Breyers Chocolate Icecream weighing 2.27kg with price $2.99 BAG 4 (Total Weight = 4.54kg) CONTENTS: Breyers Chocolate Icecream weighing 2.27kg with price $2.99 Breyers Chocolate Icecream weighing 2.27kg with price $2.99 BAG 5 (Total Weight = 4.936kg) CONTENTS: Breyers Chocolate Icecream weighing 2.27kg with price $2.99 Breyers Chocolate Icecream weighing 2.27kg with price $2.99 SnackPack Pudding weighing 0.396kg with price $0.99 BAG 6 (Total Weight = 4.946kg) CONTENTS: SnackPack Pudding weighing 0.396kg with price $0.99 Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99 Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99 Nabob Coffee weighing 0.326kg with price $3.99 5-Alive Frozen Juice weighing 0.426kg with price $0.75 Lean Ground Beef weighing 0.75kg with price $4.94 Heinz Beans Original weighing 0.477kg with price $0.79 BAG 7 (Total Weight = 2.473kg) CONTENTS: Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99 Gold Seal Salmon weighing 0.213kg with price $1.99 REMAINING CART CONTENTS: Toilet Paper - 48 pack weighing 10.89kg with price $40.96 Coca-Cola 12-pack weighing 5.112kg with price $3.49 Coca-Cola 12-pack weighing 5.112kg with price $3.49 Finally, write an unpackPerishables() method in the GroceryBag class that removes all the perishable items from the bag. Note that the GroceryBag should be modified (changed) by this method in that it will likely have less in it afterwards. The method should not print anything out (that belongs in the testing code) but instead should return an array of perishable GroceryItem objects. Add the following to the ShopperTestProgram so that it tests your method:
System.out.println(" UNPACKING PERISHABLES:");
for (int i=0; i<packedBags.length; i++) {
GroceryItem[] perishables = packedBags[i].unpackPerishables();
for (int j=0; j<perishables.length; j++) {
System.out.println(" " + perishables[j]);
}
}
System.out.println(" REMAINING BAG CONTENTS:");
for (int i=0; i<packedBags.length; i++) {
System.out.println(" BAG " + (i+1) + " (Total Weight = " +
packedBags[i].getWeight() + "kg) CONTENTS:");
for (int j=0; j<packedBags[i].getNumItems(); j++) {
System.out.println(" " + packedBags[i].getItems()[j]);
}
}
Here is the result which should appear at the end of your output (although this may vary according to
how you packed the bags earlier):
UNPACKING PERISHABLES:
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99
Lean Ground Beef weighing 0.75kg with price $4.94
Lean Ground Beef weighing 0.75kg with price $4.94
Lean Ground Beef weighing 0.75kg with price $4.94
5-Alive Frozen Juice weighing 0.426kg with price $0.75
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Breyers Chocolate Icecream weighing 2.27kg with price $2.99
Smart-Ones Frozen Entrees weighing 0.311kg with price $1.99
5-Alive Frozen Juice weighing 0.426kg with price $0.75
Lean Ground Beef weighing 0.75kg with price $4.94
REMAINING BAG CONTENTS:
BAG 1 (Total Weight = 0.69000006kg) CONTENTS:
Heinz Beans Original weighing 0.477kg with price $0.79
Gold Seal Salmon weighing 0.213kg with price $1.99
BAG 2 (Total Weight = 2.869kg) CONTENTS:
Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99
Gold Seal Salmon weighing 0.213kg with price $1.99
SnackPack Pudding weighing 0.396kg with price $0.99
BAG 3 (Total Weight = 0.0kg) CONTENTS:
BAG 4 (Total Weight = 0.0kg) CONTENTS:
BAG 5 (Total Weight = 0.3959999kg) CONTENTS:
SnackPack Pudding weighing 0.396kg with price $0.99
BAG 6 (Total Weight = 3.459kg) CONTENTS:
SnackPack Pudding weighing 0.396kg with price $0.99
Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99
Heinz Beans Original weighing 0.477kg with price $0.79
Nabob Coffee weighing 0.326kg with price $3.99
BAG 7 (Total Weight = 2.473kg) CONTENTS:
Ocean Spray Cranberry Cocktail weighing 2.26kg with price $2.99
Gold Seal Salmon weighing 0.213kg with price $1.99
Explanation / Answer
import java.util.Scanner; import java.util.InputMismatchException; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; /** * Implements a Grocery List Version 4: Has code for all methods (final version) * * @author William McDaniel Albritton */ public class GroceryListProgram4 { /** * The main() Method Starts The Program. * * @param commandlineArguments 1st argument is INPUT File, 2nd argument is OUTPUT File */ public static void main(String[] commandlineArguments) { // Error Checking For 2 Command Line Arguments.. if (commandlineArguments.length != 2) { System.out.println("Please enter the INPUT file name as " + "the 1st commandline argument."); System.out.println("Please enter the OUTPUT file name as " + "the 2nd commandline argument."); System.out.println("Please enter exactly " + "two (2) commandline arguments."); }// end of if // if no commandline argument errors, continue program else { // Declare and instantiate array of 100 Strings final Integer MAX_SIZE = new Integer(100); String groceryList[] = new String[MAX_SIZE]; // Set size of grocery list to zero (0) items Integer size = new Integer(0); // read grocery items from file & store in array for grocery list try { size = GroceryListProgram4.readFromFile(commandlineArguments[0], groceryList, size); } catch (ArrayIndexOutOfBoundsException exception) { System.out.print("ERROR: Too many items in input file. "); System.out.println("Please limit to " + MAX_SIZE + " items."); // Immediately terminates program System.exit(1); } // user's choice for Menu Integer choice = new Integer(0); // choice for ending program final Integer QUIT = new Integer(4); // if the user does not want to QUIT, keep looping while (!choice.equals(QUIT)) { // get the user's choice choice = GroceryListProgram4.displayMenu(); // add grocery item if (choice.equals(1)) { size = GroceryListProgram4.add(groceryList, size); } // delete grocery item else if (choice.equals(2)) { size = GroceryListProgram4.delete(groceryList, size); } // display grocery item else if (choice.equals(3)) { GroceryListProgram4.display(groceryList, size); } // error message else if (!choice.equals(QUIT)) { System.out.println("ERROR: Please enter an integer in the range from 1 to 4"); } }// end of "while" // write to from grocery list array to OUTPUT file GroceryListProgram4.writeToFile(commandlineArguments[1], groceryList, size); }// end of "else" }// end of main() method /** * Displays the menu for the program and returns user's choice * * @return the choice of the user (if error, returns 0) */ public static Integer displayMenu() { // display menu System.out.println(); System.out.println(" GROCERY LIST MENU"); System.out.println(" Enter 1 to Add"); System.out.println(" Enter 2 to Delete"); System.out.println(" Enter 3 to Display"); System.out.println(" Enter 4 to Quit"); System.out.print(" Enter your choice: "); // get input from user Scanner keyboardInput = new Scanner(System.in); Integer choiceOfUser = new Integer(0); try { // non-integer input will throw InputMismatchException choiceOfUser = keyboardInput.nextInt(); } catch (InputMismatchException exception) { // Already have error message in main() method, // as choiceOfUser = 0 } System.out.println(); return choiceOfUser; } /** * Reads grocery items from a file and stores items in an array * * @param inputFile is the INPUT File * @param array is the Grocery List array * @param size is the number of items in Grocery List * @return the new size of the grocery list * @throws ArrayIndexOutOfBoundsException if array size is less than number of * items in input file */ public static Integer readFromFile(String inputFile, String[] array, Integer size) throws ArrayIndexOutOfBoundsException { // connect to file (does NOT create new file) File file = new File(inputFile); Scanner scanFile = null; try { scanFile = new Scanner(file); } catch (FileNotFoundException exception) { // Print error message. // In order to print double quotes("), // the escape sequence for double quotes (") must be used. System.out.print("ERROR: File not found for ""); System.out.println(inputFile + """); } // if made connection to file, read from file if (scanFile != null) { // keeps looping if file has more lines.. while (scanFile.hasNextLine()) { // get a line of text.. String line = scanFile.nextLine(); // display a line of text to screen.. // System.out.println(line); // array[0] stores the first row (headings) to table array[size] = line; // increment size ++size; } // In order to print double quotes("), // the escape sequence for double quotes (") must be used. System.out.println("Read from file "" + inputFile + """); }// end of "if" for connecting to file return size; } /** * Adds a grocery item to an array * * @param list is the grocery list * @param listSize is the size of the grocery list * @return new size of the grocery list */ public static Integer add(String[] list, Integer listSize) { // get item from user Scanner keyboard = new Scanner(System.in); System.out.print("Enter name of item: "); String name = keyboard.nextLine(); System.out.print("Enter number of items: "); String number = keyboard.nextLine(); // add to the end of the array list[listSize] = name + ", " + number; // add one to the size (one item to end of list) return listSize + 1; } /** * Deletes a grocery item from an array * * @param list is the grocery list * @param listSize is the size of the grocery list * @return new size of the grocery list */ public static Integer delete(String[] list, Integer listSize) { // get user input System.out.print("Enter the row number of the item you wish to delete: "); Scanner keyboard = new Scanner(System.in); try { // throws an exception if not an integer Integer row = keyboard.nextInt(); // check for negative integers if (row listSize - 1) { System.out.println("ERROR: The integer is too big for the list."); } else { // delete item by shifting items on the right of the item to the left for (int i = row; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.