Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

you will simulate a Shopper that goes to a grocery store and buys items. The Shopper will select GroceryItems and place them into an Array

you will simulate a Shopper that goes to a grocery store and buys items. The Shopper will select GroceryItems and place them into an Array representing a shopping cart and then when buying the items, he/she will pack them into GroceryBags which will also use Arrays.

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.

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.

I need help answering the packBags() part please.

image text in transcribed

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 Groceryltem objects that the shopper is planning to purchase. You should create (and make use of) a MAX_CARTJTEMS 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". adds a given Groceryltem to the shopping cart. 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 method which returns an array of packed GroceryBag objects and possibly some unpacked Groceryltem 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 removeltem() method). Therefore, the use of the removeltem() 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions