import java.util.Scanner; import java.util.ArrayList; import java.io.*; /** * Shop.java * * Uses the Item class to create items and add them to a shopping * cart stored in an ArrayList. * * ACIT1515, Fall 2011, Lab 7 * David Tran a00801942 * */ public class Shop { public static void main(String[] args) throws IOException{ Item item; ArrayList cart = new ArrayList(); Scanner scan = new Scanner(System.in); Scanner fileScan, lineScan; String fileName, itemName; double cartTotal = 0; System.out.println("=================================================="); System.out.println("||---------Smart Shoppers Shopping System-------||"); System.out.println("=================================================="); System.out.println(); System.out.print("Enter your shopping list: "); fileName = scan.nextLine(); fileScan = new Scanner(new File(fileName)); do { double itemPrice = 0; int itemQuantity = 0; String itemInfo = ""; itemInfo = fileScan.nextLine(); lineScan = new Scanner(itemInfo); lineScan.useDelimiter(","); // using the comma as the delimiter to seperate name, price and quantity. itemName = lineScan.next(); // lets the first set of string variables up to the first comma to // be the name of the item while(lineScan.hasNext()){ itemPrice = lineScan.nextDouble(); itemQuantity = lineScan.nextInt(); cartTotal += (itemPrice*itemQuantity); // accumulatively adds up the total amount of each item's price // multiplied by their quantity item = new Item(itemName, itemPrice, itemQuantity); cart.add(item.toString()); // calling the ArrayList function to add the toString method of // each item into the cells of the List. System.out.println(item.toString()); // Also prints out the toString of the item class. } } while (fileScan.hasNextLine()); double cutoff = 0; // let the cutoff equal zero, and do the following whenever the cutoff // is never equal to zero. If the cutoff is zero, there is no need to // remove any more items that cost more than the cutoff. do{ if (cutoff > 0){ // if it is greater than zero, do this. // No such thing is negative currency // unless we owe customers money. In this program, we do not // do refunds. Scanner fileScan2 = new Scanner(new File(fileName)); // do a new file scan of our shopping list cartTotal = 0; // reset the total amount owed to zero. cart.clear(); // clear all contents of the array. do { double itemPrice = 0; int itemQuantity = 0; String itemInfo = ""; itemInfo = fileScan2.nextLine(); lineScan = new Scanner(itemInfo); lineScan.useDelimiter(","); itemName = lineScan.next(); while(lineScan.hasNext()){ itemPrice = lineScan.nextDouble(); if (itemPrice > cutoff){ // if the price of the item is greater than the // cutoff point, terminate. Otherwise, continue. break; } else { itemQuantity = lineScan.nextInt(); cartTotal += (itemPrice*itemQuantity); item = new Item(itemName, itemPrice, itemQuantity); cart.add(item.toString()); System.out.println(item.toString()); } } } while (fileScan2.hasNextLine()); System.out.println(); System.out.println("Your Shopping Cart:"); System.out.println(); // System.out.println(cart.toString()); // Above looks ugly, trying a different method int index = 0; while (index < cart.size()){ System.out.println(cart.get(index)); index++; } System.out.println("-------------------------------------------------"); System.out.println("Your total: $" + cartTotal); System.out.println(); System.out.print("Cutoff Price for Cart: "); cutoff = scan.nextDouble(); System.out.println(); // continue doing this until the customer is satisfied with the // cutoff point of their shopping list. } else{ System.out.println(); System.out.println("Your Shopping Cart:"); System.out.println(); // System.out.println(cart.toString()); // Above looks ugly, trying a different method int index = 0; while (index < cart.size()){ System.out.println(cart.get(index)); index++; } System.out.println("-------------------------------------------------"); System.out.println("Your total: $" + cartTotal); System.out.println(); System.out.print("Cutoff Price for Cart: "); cutoff = scan.nextDouble(); System.out.println(); } } while (cutoff != 0); } }