java assignment due this sunday
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Name of class or program (matches filename) * * COMP 1020 SECTION Axx * INSTRUCTOR Name of your instructor * ASSIGNMENT Assignment #, question # * @author your name, your student number * @version date of completion * * PURPOSE: what is the purpose of your program? */ public class CoffeeShop { private static final String DONUT = "Donut"; private static final String COFFEE = "Coffee"; private static final String SANDWICH = "Sandwich"; private static final String POP = "Pop"; private static ArrayList<Order> orders = new ArrayList<Order>() ; private static Storage data = new Storage(); public static void main(String[] args) { //Question 1 readOrders(); printOrders(); //Question 2 uses collections class instead of the original ArrayList readOrdersB(); printOrdersB(); } private static void printOrdersB() { String toString = ""; String sortedString = ""; int donutQuantity = 0; int sandwichQuantity = 0; int popQuantity = 0; int coffeeQuantity = 0; double total = 0.0; for(Order order: data.getOrders()) { toString += order.toString() +"\n"; toString += "Total price: " + order.totalPrice() +"\n"; total+= order.totalPrice(); if(order instanceof Donut) { donutQuantity+=order.getQuantity(); }else if(order instanceof Pop) { popQuantity+=order.getQuantity(); }else if(order instanceof Sandwich) { sandwichQuantity+=order.getQuantity(); }else if(order instanceof Coffee) { coffeeQuantity+=order.getQuantity(); } } data.sort(); for(Order order: data.getOrders()) { sortedString += order.toString() +"\n"; sortedString += "Total price: " + order.totalPrice() +"\n"; } System.out.println("Question B"); System.out.println("toString..."); System.out.println(toString); System.out.println("..."); System.out.println("Sorted"); System.out.println(sortedString); System.out.println("..."); System.out.println("Donut Quantity: " + donutQuantity); System.out.println("Pop Quantity: " + popQuantity); System.out.println("Sandwich Quantity: " + sandwichQuantity); System.out.println("Coffee Quantity: " + coffeeQuantity); System.out.println("Total Price: " + total); } private static void readOrdersB() { try { FileInputStream fstream = new FileInputStream("a4b.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; while ( (strLine = br.readLine() ) != null ) { String[]tokens = strLine.split(","); data.add(construct(tokens)); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void printOrders() { String toString = ""; int donutQuantity = 0; double total = 0.0; for(Order order: orders) { toString += order.toString() +"\n"; toString += "Total price: " + order.totalPrice() +"\n"; total+= order.totalPrice(); if(order instanceof Donut) { donutQuantity+=order.getQuantity(); } } System.out.println("Question A"); System.out.println(toString); System.out.println("Donut Quantity: " + donutQuantity); System.out.println("Total Price: " + total); System.out.println("..."); System.out.println(""); } private static void readOrders() { try { FileInputStream fstream = new FileInputStream("a4a.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; while ( (strLine = br.readLine() ) != null ) { String[]tokens = strLine.split(","); orders.add(construct(tokens)); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * PURPOSE: uses the tokens that were split on commas * as parameters to create the classes they were meant to represent */ private static Order construct(String[] tokens) { Order order = null; String orderType = tokens[0]; int quantity = Integer.parseInt(tokens[1]); double price; String size; String flavour; if (orderType.equals(DONUT)) { price = Double.parseDouble(tokens[2]); flavour = tokens[3]; order = new Donut(orderType, quantity, price, flavour); }else if (orderType.equals(COFFEE)) { size = tokens[2]; order = new Coffee(orderType, quantity, size); }else if (orderType.equals(SANDWICH)) { price = Double.parseDouble(tokens[2]); String filling = tokens[3]; String bread = tokens[4]; order = new Sandwich(orderType, quantity, price, bread, filling); }else if (orderType.equals(POP)) { size = tokens[2]; String brand = tokens[3]; order = new Pop(orderType, quantity, size, brand); } return order; } } abstract class Order{ protected static final String SMALL_SIZE = "small"; protected static final String MEDIUM_SIZE = "medium"; protected static final String LARGE_SIZE = "large"; private static final double TAX = .07; private int quantity; private String orderType; private double price; public Order(String orderType, int quantity) { this.orderType = orderType; this.setQuantity(quantity); } public double totalPrice() { return price*quantity; } public double addTax(double total) { total+= total*TAX; return total; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public String toString() { return orderType+ ", " + getQuantity() + ", "+ getPrice(); } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } abstract class Drinks extends Order{ private String size; public Drinks(String orderType, int quantity, String size) { super(orderType, quantity); this.setSize(size); setPriceBySize(); } protected abstract void setPriceBySize( ); public String getSize() { return size; } public void setSize(String size) { this.size = size; } @Override public String toString() { return super.toString() + ", "+ getSize(); } } abstract class Food extends Order{ public Food(String orderType, int quantity, double price) { super(orderType, quantity); this.setPrice(price); } } class Coffee extends Drinks{ private static final double SMALL_PRICE = 1.39; private static final double MEDIUM_PRICE = 1.69; private static final double LARGE_PRICE = 1.99; public Coffee(String orderType, int quantity, String size) { super(orderType, quantity, size); } @Override protected void setPriceBySize() { if(getSize().equals(SMALL_SIZE)) { this.setPrice(SMALL_PRICE); }else if(getSize().equals(MEDIUM_SIZE)) { this.setPrice(MEDIUM_PRICE); }else { this.setPrice(LARGE_PRICE); } } } class Donut extends Food{ private String flavour; public Donut (String orderType, int quantity, double price, String flavour) { super(orderType, quantity, price); this.flavour = flavour; } public double totalPrice() { double total = super.totalPrice(); if (getQuantity()<6) { total=addTax(total); } return total; } @Override public String toString() { return super.toString() + ", "+ flavour; } } class Pop extends Drinks{ private static final double SMALL_PRICE = 1.79; private static final double MEDIUM_PRICE = 2.09; private static final double LARGE_PRICE = 2.49; private String brand; public Pop(String orderType, int quantity, String size, String brand) { super(orderType, quantity, size); this.brand = brand; } @Override protected void setPriceBySize() { if(getSize().equals(SMALL_SIZE)) { this.setPrice(SMALL_PRICE); }else if(getSize().equals(MEDIUM_SIZE)) { this.setPrice(MEDIUM_PRICE); }else { this.setPrice(LARGE_PRICE); } } @Override public String toString() { return super.toString() + ", "+ brand; } } class Sandwich extends Food{ private String bread; private String filling; public Sandwich(String orderType, int quantity, double price, String bread, String filling) { super(orderType, quantity, price); this.bread = bread; this.filling = filling; } public double totalPrice() { return addTax(super.totalPrice()); } @Override public String toString() { return super.toString() + ", "+ filling+ ", "+ bread; } } //Collections class class Storage { private List<Order> orders; public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public Storage() { orders = new ArrayList<Order>(); } public void add (Order order) { if(order instanceof Pop || order instanceof Sandwich || order instanceof Coffee || order instanceof Donut) { orders.add(order); } } public ArrayList<Order> sort (){ Collections.sort(orders,new Comparator<Order>(){ public int compare(Order o1,Order o2){ //times 100 for added accuracy return (int) (o1.totalPrice()*100 - o2.totalPrice()*100); }}); return (ArrayList<Order>) orders; } }