( USE ONLY ARRAYLIST TO STORE A COLLECTION, solve all problems with programs wri
ID: 3603565 • Letter: #
Question
( USE ONLY ARRAYLIST TO STORE A COLLECTION,solve all problems with programs written in the 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:
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 file below
Explanation / Answer
import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;
public class GroceryList
{
int totalcost = 0;
ArrayList<GroceryItemOder> List = new ArrayList <GroceryList> ();
//Initalizes a scanner for user input
Scanner input = new Scanner (System.in);
//Constructs a new empty grocery list.
public GroceryList( )
{
}
import java.lang.String;
import java.util.Scanner;
public class GroceryItemOrder
{
public static void main (String [] args)
{
int totalcost = 0;
//Initializes scanner for user input
Scanner in = new Scanner (System.in);
// Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
public GroceryItemOrder(String name, int quantity, double pricePerUnit)
{
System.out.println ("Enter the grocery item that you want:");
String name.nextLine ();
System.out.println ("Enter the grocery item cost");
int price.nextInt ();
System.out.println ("Enter in the quantity of the items":)/>/>;
int quantity.nextInt ();
name2= name;
quantity2 = quantity;
price = pricePerUnit;
}
//Returns the total cost of this item in its given quantity. For example, four boxes of cookies that cost 2.30 per unit have a total cost of 9.20.
public double getCost( )
{
totalcost = price * quantity;
return totalcost;
}
//Sets this grocery item’s quantity to be the given value.
public void setQuantity(int quantity)
{
}
}
//Adds the given item order to this list, if the list is not full(i.e., has fewer than 10 items).
public void add(GroceryItemOrder item)
{
List.add(item);
}
//Returns the total sum cost of all grocery item order in this list.
public double getTotalCost( )
{
return totalcost;
}
}
in another way of java program
public class ShoppingItem {
private String ItemName;
private double ItemPrice;
private int ItemQty;
public ShoppingItem()
{
ItemName = "Fruit";
ItemPrice = 100;
ItemQty = 1;
}
public ShoppingItem(String ItemName, double ItemPrice, int ItemQty )
{
this.ItemName = ItemName;
this.ItemPrice = ItemPrice;
this.ItemQty = ItemQty;
}
public String getItemName() {
return ItemName;
}
public double getItemPrice() {
return ItemPrice;
}
public double getItemTotalPrice() {
return ItemPrice * ItemQty;
}
public int getItemQty() {
return ItemQty;
}
public void setItemName(String ItemName)
{
this.ItemName = ItemName;
}
public void setItempPrice(double ItemPrice)
{
this.ItemPrice = ItemPrice;
}
public void setItemQty(int ItemQty)
{
this.ItemQty = ItemQty;
}
@Override
public String toString()
{
String state = ItemName + " - €" + ItemPrice + " x " + ItemQty;
return state;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingList {
ArrayList<ShoppingItem> list = new ArrayList<ShoppingItem>();
//Add a new ShoppingItem to the list
public void addItem()
{
System.out.println();
System.out.println("enter in the name of your item");
Scanner keyboard = new Scanner(System.in);
String ItemName = keyboard.nextLine();
System.out.println("enter in the price of your item");
double ItemPrice = keyboard.nextDouble();
System.out.println("enter in the Qty of your item");
int ItemQty = keyboard.nextInt();
ShoppingItem Item = new ShoppingItem(ItemName, ItemPrice,
ItemQty);
list.add(Item);
System.out.println("Item Added");
}
//Display list and total number of items.
public void displayItem(){
System.out.println( list.size()+ " items. ");
for (ShoppingItem x : list) {
System.out.println(x.toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.