Simulate \"shopping list\" software (can assume String items), by using an Array
ID: 3763935 • Letter: S
Question
Simulate "shopping list" software (can assume String items), by using an ArrayList. Your program must repeatedly output these menu options, until the user chooses "7" to Exit and stop the program: 1. Show shopping list. [If empty, say "No items".] 2. Add new item to list. [Ask user to input new item, then add it to back of list.] 3. Remove an item from the list. [Ask user to input the list number of the item.] 4. Insert an item into the list. [Ask user to input the new data, and the list number of the insertion point.] 5. Change an item in the list. [Ask user to input the list number, and the new data.] 6. Clear the list. [Empties all items, results in an empty list.] 7. Exit. [Exits the program.] Recall that ArrayList items start at position index 0. But, this electronic "shopping list" software must assume that the shopping list data, when processing the user's menu choices, start at position 1, as is typically the case with human-numbered lists. When you output the list, for menu option 1, each item in the list must show its position number, starting at 1. For example, if the user chooses menu option 1, the program's output is something like: 1. Chips 2. Sodas 3. More chips where the shopping list software numbers from index 1, although the data-tracking ArrayList is actually numbered from index 0.
Explanation / Answer
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");
}
public void insertItem()
{
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.insert(Item);
System.out.println("Item Added");
}
public void removeItem()
{
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.remove(Item);
System.out.println("Item removed");
}
//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());
}
}
public void clearItem()
{
System.out.println( list.size()+ " items. ");
list.clear(Item);
}
}
import java.util.Scanner;
public class Application {
public static void main( String [] args) {
Scanner input = new Scanner(System.in);
ShoppingList myList = new ShoppingList();
int userOpt = 0;
while (userOpt != 7) {
System.out.println("");
System.out.println("----- Shopping List------");
System.out.println("(1) Display the shopping list. ");
System.out.println("(2) Add an item to the list. ");
System.out.println("(3) Insert an item to the list. ");
System.out.println("(4) Remove an item to the list. ");
System.out.println("(5) Display list and total number of items. ");
System.out.println("(6) Clear ");
System.out.println("(7) Exit. ");
userOpt = input.nextInt();
if (userOpt == 1)
{
myList.displayItem();
if (userOpt == 2)
{
myList.addItem();
}
if (userOpt == 3)
{
myList.insertItem();
if (userOpt == 4)
{
myList.RemoveItem();
if (userOpt == 5)
{
myList.displayItem();
}
if (userOpt == 6) {
myList.clearItem();
}
}
}