Question: Convert *** the code below*** to include an ArrayList, try/catch blocks, and a new exception class for error handling that you define. You will need
Convert ***the code below*** to include an ArrayList, try/catch blocks, and a new exception class for error handling that you define. You will need to the use throw in your program
Include an interface in the application that handles printing of output.
Replace the priority logic that will include multiple instances of the same priority (i.e. multiple priority #1, #2, etc.)
Support multiple quantities of an item (i.e. 2 loafs of bread)
Read/Write the shopping list to a file (*.txt)
Update your test plan, class diagram, and pseudo code to reflect the changes made.
The test plan must include tests for the different types of error handling
Classes must now make sense, including the derived classes. No stub classes.
Program Processes:
Get the input from the user
Input shopping list
Multiple priorities #1, #2s etc.
Ask for the budget and call it bankAccount
Persons Name (no #'s in the name)
Ask for the quantity of an item (i.e. 3 qts of milk)
Display what was in the bank account before shopping
Display the list before shopping and after shopping
Display what was purchased
Display what is left in the bank account
Have an exit criteria clearly defined
public abstract class Item { protected int ItemQty; public int getItemQty() { return ItemQty; } public void setItemQty(int ItemQty) { this.ItemQty = ItemQty; } }
public class ShoppingItem extends Item { private String ItemName; private double ItemPrice;
public ShoppingItem() { ItemName = "Apple"; ItemPrice = 100; ItemQty = 1; } public ShoppingItem(String ItemName, int ItemQty ) { this.ItemName = ItemName; this.ItemQty = ItemQty; this.ItemPrice = 0; // intial value set to zero } public String getItemName() // getting the ItemName { return ItemName; } public double getItemPrice() // getting the ItemPrice { return ItemPrice; } public double getItemTotalPrice() // getting the TotalPrice { return ItemPrice * ItemQty; } public void setItemName(String ItemName) // set method { this.ItemName = ItemName; }
public void setItempPrice(double ItemPrice) { this.ItemPrice = ItemPrice; } public String toString() { String state = ItemName + " - $" + ItemPrice + " x " + ItemQty; return state; } }
import java.util.ArrayList; import java.util.Scanner;
public class ShoppingList implements SortableList { ShoppingItem[] items; boolean[] isItemPurchased; public ShoppingList(int size) { items = new ShoppingItem[size]; isItemPurchased = new boolean[size]; } public void addItem(int index) { Scanner sc = new Scanner(System.in); System.out.println(); String ItemName; boolean isDuplicate; // catch duplicate if entered do { isDuplicate = false; System.out.println("Enter the name of item"); ItemName = sc.next(); for (int i = 0; i < items.length; i++) { ShoppingItem item = items[i]; if (item != null && item.getItemName().equals(ItemName)) { isDuplicate = true; } } if (isDuplicate) { System.out.println("Do not enter a duplicate"); } } while (isDuplicate); System.out.println("Enter the Priority of your item"); int ItemQty = sc.nextInt(); ShoppingItem Item = new ShoppingItem(ItemName,ItemQty); items[index] = Item; } public void updateItem() { System.out.println(); System.out.println("Enter the number of Item to modify"); Scanner sc = new Scanner(System.in); int index= sc.nextInt(); System.out.println("Enter the Priority of your item"); int ItemQty = sc.nextInt(); ShoppingItem Item = items[index]; Item.setItemQty(ItemQty); } public void displayItem() { System.out.println( items.length + " items. "); for (int i = 0; i < items.length; i++) { System.out.println(i + ": " + items[i].toString()); }
} public void setPrices() { items[0].setItempPrice(19.99); items[1].setItempPrice(4.55); items[2].setItempPrice(7.00); items[3].setItempPrice(19.99); items[4].setItempPrice(26.98); items[5].setItempPrice(36.78); items[6].setItempPrice(1.98); } public void shop(double initialMoney) { double money = initialMoney; for (int i = 0; i < items.length; i++) { double price = items[i].getItemPrice(); if (money - price >= 0) { isItemPurchased[i] = true; money = money - price; } } } public void showPurchases(boolean purchased) { for (int i = 0; i < items.length; i++) { if (isItemPurchased[i] == purchased) { ShoppingItem item = items[i]; System.out.printf("%s: %.2f%n", item.getItemName(), item.getItemPrice()); } } } // bubble-sort @Override public void sort() { int n = items.length; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (items[j].getItemQty() > items[j+1].getItemQty()) { // swap temp and items[i] ShoppingItem temp = items[j]; items[j] = items[j+1]; items[j+1] = temp; } } }
import java.util.ArrayList; import java.util.Scanner;
public class ShoppingListTest {
private static final int SIZE = 7; private static final double INITIAL_MONEY = 59.00;
public static void main(String[] args) { Scanner sc = new Scanner(System.in); ShoppingList List = new ShoppingList(SIZE); for (int i = 0; i < SIZE; i++) { List.addItem(i); } int Opt = 0; while (Opt != 2) { List.sort(); List.displayItem(); System.out.println("--------------------------"); System.out.println("----- Shopping List------"); System.out.println("1.Modify the list. "); System.out.println("2.Continue. "); Opt = sc.nextInt(); if (Opt == 1) { List.updateItem(); } } sc.close(); List.setPrices(); List.displayItem(); System.out.println("Shopping..."); List.shop(INITIAL_MONEY); System.out.println("Purchased items:"); List.showPurchases(true); System.out.println(); System.out.println("Non-purchased items:"); List.showPurchases(false); } }
public interface SortableList { public void sort(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
