Question
(PLEASE ONLY USE ARRAYLIST TO STORE A COLLECTION, ONLY IMPORT JAVA.IO.* AND JAVA.UTIL.ARRAYLIST( NO SCANNER OR SOMETHING ELSE, WRITE IN JAVA PROGRAMMING LANGUAGE) A classic
(PLEASE ONLY USE ARRAYLIST TO STORE A COLLECTION, ONLY IMPORT JAVA.IO.* AND JAVA.UTIL.ARRAYLIST( NO SCANNER OR SOMETHING ELSE, WRITE IN JAVA PROGRAMMING LANGUAGE)
A classic application program is the shopping list: a way of keeping track of which groceries or other items need to be bought, and which of them have already been purchased. Your answer to this question will implement such a shopping list, and simulate a user's changes to the list with data read in from a text file.
First, you will need a class to represent items in the list. Objects of this class will contain two instance variables, a String nameof the desired item, and an int quantity that needs to be purchased.
Secondly, you will need two ArrayLists in your program. The first list will keep track of items that still need to be bought (call this the shopping list), and the quantity needed. The second will keep track of the items purchased, and the quantity that has been purchased (call this the purchase list).
The data file used as input to the program will consist of lines containing one of three commands. The first of these is add, which is followed by a quantity and an item name, separated by commas. This will add to the shopping list; if an item with that name already exists, increase the quantity desired by the given number. Otherwise, add a new item to the list with that name and quantity. The second command is buy, which is also followed by a quantity and an item name. This will add to the purchase list in a similar fashion. Also, if an item with that name is already in the shopping list, reduce the quantity desired by that number. If the quantity of that item reaches zero (or less), remove it from the shopping list. Finally, the list command should print out both lists, showing both the quantity and the item name, one item per line. For example, the data file:
add,3,loaf of bread add,2,jug of milk list buy,2,loaf of bread add,4,loaf of bread buy,3,jug of milk buy,1,stick of butter list
will display something similar to the following:
============== Shopping List: 3 - loaf of bread 2 - jug of milk Purchase List: ============== Shopping List: 5 - loaf of bread Purchase List: 2 - loaf of bread 3 - jug of milk 1 - stick of butter
Use the data file a3a.txt to test your program. Hand in your Java source code and a copy of the output produced by running your program on that file.
INPUT FILE BELOW:
list add,3,loaf of bread add,2,jug of milk list buy,2,loaf of bread add,4,loaf of bread buy,3,jug of milk buy,1,stick of butter list buy,5,loaf of bread buy,2,banana list add,1,apple add,2,banana add,3,cauliflower add,4,durian buy,5,kumquat add,5,kumquat buy,4,kumquat buy,1,kumquat 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