1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
| import java.io.*; import java.util.*; import java.text.*;
public class GourmetCoffee {
private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true);
private static final NumberFormat CURRENCY = NumberFormat.getCurrencyInstance();
private Catalog catalog; private Order currentOrder; private Sales sales; public static void main(String[] args) throws IOException {
GourmetCoffee application = new GourmetCoffee(); application.run();
} private GourmetCoffee() {
this.catalog = loadCatalog(); this.sales = loadSales(this.catalog); this.currentOrder = new Order(); } private Catalog loadCatalog() {
Catalog catalog = new Catalog();
catalog.addProduct(new Coffee( "C001", "Colombia, Whole, 1 lb", 17.99, "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium", "Full")); catalog.addProduct(new Coffee( "C002", "Colombia, Ground, 1 lb", 18.75, "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium","Full")); catalog.addProduct(new Coffee( "C003", "Italian Roasts, Whole, 1 lb", 16.80, "Latin American Blend", "Italian Roast", "Dark and heavy", "Intense", "Low", "Medium")); catalog.addProduct(new Coffee( "C004", "Italian Roasts, Ground, 1 lb", 17.55, "Latin American Blend", "Italian Roast", "Dark and heavy", "Intense", "Low", "Medium")); catalog.addProduct(new Coffee( "C005", "French Roasts, Whole, 1 lb", 16.80, "Latin American Blend", "French Roast", "Bittersweet, full intense", "Intense, full", "None", "Medium")); catalog.addProduct(new CoffeeBrewer( "B001", "Home Coffee Brewer", 150.00, "Brewer 100", "Pourover", 6)); catalog.addProduct(new CoffeeBrewer( "B002", "Coffee Brewer, 2 Warmers", 200.00, "Brewer 200", "Pourover", 12)); catalog.addProduct(new CoffeeBrewer( "B003", "Coffee Brewer, 3 Warmers", 280.00, "Brewer 210", "Pourover", 12));
catalog.addProduct( new Product("A001", "Almond Flavored Syrup", 9.00)); catalog.addProduct( new Product("A002", "Irish Creme Flavored Syrup", 9.00)); catalog.addProduct( new Product("A003", "Mint Flavored syrup", 9.00)); catalog.addProduct( new Product("A004", "Caramel Flavored Syrup", 9.00)); catalog.addProduct( new Product("A005", "Gourmet Coffee Cookies", 12.00));
return catalog; } private Sales loadSales(Catalog catalog) {
Sales sales = new Sales(); Order[] orders = new Order[6];
orders[0] = new Order(); orders[0].addItem(new OrderItem(catalog.getProduct("C001"), 5)); sales.addOrder(orders[0]);
orders[1] = new Order(); orders[1].addItem(new OrderItem(catalog.getProduct("C002"), 2)); orders[1].addItem(new OrderItem(catalog.getProduct("A001"), 2)); orders[1].addItem(new OrderItem(catalog.getProduct("A003"), 2)); sales.addOrder(orders[1]);
orders[2] = new Order(); orders[2].addItem(new OrderItem(catalog.getProduct("B002"), 1)); orders[2].addItem(new OrderItem(catalog.getProduct("A003"), 3)); sales.addOrder(orders[2]);
orders[3] = new Order(); orders[3].addItem(new OrderItem(catalog.getProduct("B003"), 2)); orders[3].addItem(new OrderItem(catalog.getProduct("C001"), 3)); orders[3].addItem(new OrderItem(catalog.getProduct("C003"), 3)); orders[3].addItem(new OrderItem(catalog.getProduct("C005"), 3)); orders[3].addItem(new OrderItem(catalog.getProduct("A001"), 3)); orders[3].addItem(new OrderItem(catalog.getProduct("A004"), 2)); sales.addOrder(orders[3]); orders[4] = new Order(); orders[4].addItem(new OrderItem(catalog.getProduct("B001"), 1)); orders[4].addItem(new OrderItem(catalog.getProduct("C002"), 2)); orders[4].addItem(new OrderItem(catalog.getProduct("C003"), 2)); orders[4].addItem(new OrderItem(catalog.getProduct("A001"), 2)); orders[4].addItem(new OrderItem(catalog.getProduct("A002"), 6)); sales.addOrder(orders[4]); orders[5] = new Order(); orders[5].addItem(new OrderItem(catalog.getProduct("B001"), 1)); orders[5].addItem(new OrderItem(catalog.getProduct("C001"), 1)); orders[5].addItem(new OrderItem(catalog.getProduct("C005"), 5)); orders[5].addItem(new OrderItem(catalog.getProduct("A001"), 5)); orders[5].addItem(new OrderItem(catalog.getProduct("A004"), 4)); sales.addOrder(orders[5]); return sales; } private void run() throws IOException {
int choice = getChoice();
while (choice != 0) {
if (choice == 1) { displayCatalog(); } else if (choice == 2) { displayProductInfo(); } else if (choice == 3) { displayOrder(); } else if (choice == 4) { addModifyProduct(); } else if (choice == 5) { removeProduct(); } else if (choice == 6) { saleOrder(); } else if (choice == 7) { displayOrdersSold(); } else if (choice == 8) { displayNumberOfOrders(readProduct()); } else if (choice == 9) { displayTotalQuantityOfProducts(); }
choice = getChoice(); } } private int getChoice() throws IOException {
int input;
do { try { stdErr.println(); stdErr.print( "[0] Quit\n" + "[1] Display catalog\n" + "[2] Display product\n" + "[3] Display current order\n" + "[4] Add|modify product to|in current order\n" + "[5] Remove product from current order\n" + "[6] Register sale of current order\n" + "[7] Display sales\n" + "[8] Display number of orders with a specific product\n" + "[9] Display the total quantity sold for each product\n" + "choice> "); stdErr.flush();
input = Integer.parseInt(stdIn.readLine());
stdErr.println();
if (0 <= input && 9 >= input) { break; } else { stdErr.println("Invalid choice: " + input); } } catch (NumberFormatException nfe) { stdErr.println(nfe); } } while (true);
return input; } public void displayCatalog() {
int size = this.catalog.getNumberOfProducts();
if (size == 0) { stdErr.println("The catalog is empty"); } else { for (Product product : this.catalog) {
stdOut.println(product.getCode() + " " + product.getDescription()); } } } public void displayProductInfo() throws IOException {
Product product = readProduct();
stdOut.println(" Description: " + product.getDescription()); stdOut.println(" Price: " + product.getPrice()); if (product instanceof Coffee) {
Coffee coffee = (Coffee) product;
stdOut.println(" Origin: " + coffee.getOrigin()); stdOut.println(" Roast: " + coffee.getRoast()); stdOut.println(" Flavor: " + coffee.getFlavor()); stdOut.println(" Aroma: " + coffee.getAroma()); stdOut.println(" Acidity: " + coffee.getAcidity()); stdOut.println(" Body: " + coffee.getBody()); } else if (product instanceof CoffeeBrewer) {
CoffeeBrewer brewer = (CoffeeBrewer) product;
stdOut.println(" Model: " + brewer.getModel()); stdOut.println(" Water Supply: " + brewer.getWaterSupply()); stdOut.println(" Number of Cups: " + brewer.getNumberOfCups()); } } public void displayOrder() {
int size = this.currentOrder.getNumberOfItems();
if (size == 0) { stdErr.println("The current order is empty"); } else { for (OrderItem orderItem : this.currentOrder) { stdOut.println(orderItem.toString()); } stdOut.println("Total: " + CURRENCY.format(this.currentOrder.getTotalCost())); } } public void addModifyProduct() throws IOException {
Product product = readProduct(); int quantity = readQuantity(); OrderItem orderItem = this.currentOrder.getItem(product);
if (orderItem == null) { this.currentOrder.addItem(new OrderItem(product, quantity)); stdOut.println("The product " + product.getCode() + " has been added"); } else { orderItem.setQuantity(quantity); stdOut.println("The quantity of the product " + product.getCode() + " has been modified"); } } public void removeProduct() throws IOException {
Product product = readProduct(); OrderItem orderItem = this.currentOrder.getItem(product);
if (orderItem != null) { this.currentOrder.removeItem(orderItem); stdOut.println("The product " + product.getCode() + " has been removed from the current order"); } else { stdErr.println( "There are no products in the current order with that code"); } } public void saleOrder() {
if (this.currentOrder.getNumberOfItems() > 0) { this.sales.addOrder(this.currentOrder); this.currentOrder = new Order(); stdOut.println("The sale of the order has been registered"); } else { stdErr.println("The current order is empty"); } } public void displayOrdersSold() {
int numOrders = this.sales.getNumberOfOrders();
if (numOrders != 0) { int orderNumber = 1; for (Order order : this.sales) {
stdOut.println("Order " + orderNumber++);
for (OrderItem orderItem : order) { stdOut.println(" " + orderItem.toString()); } stdOut.println(" Total: " + CURRENCY.format(order.getTotalCost())); } } else { stdErr.println("There are no sales"); } } public void displayNumberOfOrders(Product product) { int total = 0; for(Order order:sales) { for(OrderItem orderItem:order) { if(orderItem.getProduct().equals(product)) { total++; break; } } } System.out.println("Number of orders that contains the product "+product.getCode()+": "+total); } public void displayTotalQuantityOfProducts() {
/* PLACE YOUR CODE HERE */ for(Product product:catalog) { int total = 0; for(Order order:sales) { for(OrderItem orderItem:order) { if(orderItem.getProduct().equals(product)) { total+=orderItem.getQuantity(); } } } System.out.println(product.getCode()+": "+total); } } private Product readProduct() throws IOException {
do { stdErr.print("Product code> "); stdErr.flush(); Product product = this.catalog.getProduct(stdIn.readLine()); if (product != null) {
return product; } else { stdErr.println("There are no products with that code"); } } while (true); } private int readQuantity() throws IOException {
int quantity;
do { try {
stdErr.print("Quantity> "); stdErr.flush(); quantity = Integer.parseInt(stdIn.readLine()); if (quantity > 0) {
return quantity;
} else { stdErr.println( "Invalid input. Please enter a positive integer"); } } catch (NumberFormatException nfe) { stdErr.println(nfe); } } while (true); } }
|