Question
Java. Troop 1043 is having its annual popcorn fundraiser. Scout popcorn is known to be really yummy. Your friend's mom is great with popcorn and
Java.
Troop 1043 is having its annual popcorn fundraiser. Scout popcorn is known to be really yummy. Your friend's mom is great with popcorn and is struggling to find a way to keep track of orders. It's your job to give her a hand with this task.
Your to-do list:
keep a master list of the popcorn orders
find the sum of all popcorn orders
find the number of a particular variety
remove the orders of a particular variety as they are being filled
An ArrayList will be the perfect way to keep a master list of popcorn orders. As you brainstorm more, you determine that you will create a popcorn class and objects created from that class be stored in the master list.
The PopcornOrder class will be as follows:
Instance Variables:
variety - variety of the popcorn (Jalapeno Cheese, Caramel with Salt, etc.)
quantity - number of that particular variety that has been ordered.
Constructors
no default constructor is required.
a constructor to instantiate a PopcornOrder for a given variety type and quantity ordered
Methods:
getVariety() - a getter method for the variety field.
getNumOrdered() - returns the number of this variety ordered.
You will then design a MasterList class to keep track of orders that will be as follows:
Instance Variables:
orders - ArrayList of all current popcorn orders (holds PopcornOrder objects)
Constructors
a default constructor that initializes the orders field
no other constructors is required
Methods:
addOrder(PopcornOrder) - adds a PopcornOrder object to orders.
getTotalOrders() - returns the sum of all popcorn unit orders. If there are no popcorn orders in orders this method returns 0.
removeVariety(String) - removes all popcorn orders from orders that are the same variety as the parameter and returns the total number of popcorn units removed. If there are no popcorn orders of that variety this method returns 0. This method assumes that there is only one popcorn order per variety in the list.
toString() - returns a String that contains all popcorn orders in a nicely formatted manner. If there are no orders in orders returns a message stating such.
Your Tasks:
#Part 1 - Implement the classes
Complete the two classes to implement this design. Make sure to use the same identifiers (variables, parameters, method names,) given for testing purposes.
#Part 2 - Test the Implementation
In the main method of the Main class test your implementation:
Create a master list of popcorn orders name popcorn2023.
MasterList popcorn2023 = new MasterList();
Add popcorn orders received so far. It should contain the following PopcornOrders
Variety | Quantity |
Butter Microwave | 3 |
Classic Caramel Corn | 2 |
Sea Salt Splash | 5 |
3. Call the toString() and print what it returns to be sure the ArrayList is filled correctly
4. Test the other methods of the MasterList class by using print statements (print() or println()) to check the return values for the following:
popcorn2023.getTotalOrders() returns 10
popcorn2023.removeVariety("Butter Microwave") returns 3
popcorn2023.removeVariety("Cheddar Cheese") returns 0
popcorn2023.getTotalOrders() returns 7
5. Call the toString() again to verify the master list was updated and all Butter Microwave orders have been removed.
6. Create a new ArrayList called popcorn2024 to get ready for next year.
7. Call the following methods to be sure and empty ArrayList will not cause any errors for any of these statements.
popcorn2024.getTotalOrders(); returns 0
popcorn2024.removeVariety("Sea Salt Spalsh"); returns 0
popcorn2024.toString() returns a String stating there are no orders in the master list.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started