A classic application program is the shopping list a way of
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:
will display something similar to the following:
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.
a3a.txt
Solution
Item Class :-
-----------------------------
public class Item {
private String name;
private int count;
Item(String name , int quantity){
this.name = name;
this.count = quantity;
}
void setName(String name){
this.name = name;
}
void setQuantity(int quantity){
this.count = quantity;
}
String getName(){
return this.name;
}
int getQuantity(){
return this.count;
}
}
ShoppingDemo Class (includes Main method):-
----------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ShoppingDemo {
List<Item> shoppingList= new ArrayList<Item>();
List<Item> purchaseList= new ArrayList<Item>();
public void addShoppingItem(String name, int count){
//check for item already exist
for(int i = 0 ; i < shoppingList.size() ; i++){
//Get Item at current Index
Item curItem = (Item) shoppingList.get(i) ;
//check if given name matches with current item name
boolean found = curItem.getName().equalsIgnoreCase(name);
if(found){
//increase item quantity , no need to add new Item
curItem.setQuantity(curItem.getQuantity()+count);
return;
}
}
//Item not already available so create new Item and add to list
Item it = new Item(name,count);
shoppingList.add(it);
}
public void addToPurchaseList(String name, int count){
//check for item already exist
for(int i = 0 ; i < purchaseList.size() ; i++){
//Get Item at current Index
Item curItem = (Item) purchaseList.get(i) ;
//check if given name matches with current item name
boolean found = curItem.getName().equalsIgnoreCase(name);
if(found){
//increase item quantity , no need to add new Item
curItem.setQuantity(curItem.getQuantity()+count);
return;
}
}
//Item not already available so create new Item and add to list
Item it = new Item(name,count);
purchaseList.add(it);
//Decrease Quantity of given item from Shopping List
//check for item already exist
for(int i = 0 ; i < shoppingList.size() ; i++){
//Get Item at current Index
Item curItem = (Item) shoppingList.get(i) ;
//check if given name matches with current item name
boolean found = curItem.getName().equalsIgnoreCase(name);
if(found){
//increase item quantity , no need to add new Item
curItem.setQuantity(curItem.getQuantity()-count);
//Remove item if Quantity is < = 0 , so already purchased reqired quantity
if(curItem.getQuantity() <= 0){
shoppingList.remove(i);
}
return;
}
}
}
public void printLists(){
System.out.println(\"Shopping List :\");
System.out.println(\"=======================\");
for(int i = 0 ; i < shoppingList.size() ; i++){
//Get Item at current Index
Item curItem = (Item) shoppingList.get(i) ;
System.out.println(curItem.getQuantity() + \"- \" + curItem.getName());
}
System.out.println(\"Purchase List :\");
System.out.println(\"=======================\");
for(int i = 0 ; i < purchaseList.size() ; i++){
//Get Item at current Index
Item curItem = (Item) purchaseList.get(i) ;
System.out.println(curItem.getQuantity() + \"- \" + curItem.getName());
}
}
public static void main(String args[]){
ShoppingDemo demo = new ShoppingDemo();
//Read from file
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(\"/home/ramkumar/Study/Chegg/a3a.txt\"));
String line = br.readLine();
while (line != null) {
if(line.equals(\"list\")){
demo.printLists();
}
else{
String [] items = line.split(\",\");
if(items.length > 0 && items[0].equalsIgnoreCase(\"add\")){
demo.addShoppingItem(items[2], Integer.parseInt(items[1]));
}
else{
demo.addToPurchaseList(items[2], Integer.parseInt(items[1]));
}
}
line = br.readLine();
}
}
catch(Exception e){
}
finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


