import java.io.*; class Main { public static void main(String args[]) throws Exception { Keyboard keyb = new Keyboard(); // TEST GroceryItem CLASS testGroceryItem(); System.out.print("\n\n\n\n\n\n\nHit enter to continue"); keyb.getString(); // TEST EMPTY RECEIPT testEmpty(); System.out.print("\n\n\n\n\n\n\n\nHit enter to continue"); keyb.getString(); // TEST RECEIPT WITH ONE ITEM testOneItem(); System.out.print("\n\n\n\nHit enter to continue"); keyb.getString(); // TEST RECEIPT WITH MULTIPLE ITEMS testMultipleItems(); System.out.print("\nHit enter to continue"); keyb.getString(); // TEST ROUNDING testRounding(); System.out.print("\n\nHit enter to continue"); keyb.getString(); // TEST TAX CALCULATION testTax(); System.out.println("\n\n"); } /* * This method tests that the GroceryItem class is implemented * correctly. It compares the expected values of the name and * price with the values of the name and price that are returned * by getName() and getPrice(). * * See output for more information */ public static void testGroceryItem() { System.out.println("\n\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the GroceryItem class correctly"); System.out.println("creates objects with the name and price specified"); System.out.println(); // create a GroceryItem object and compare the expected // output with the actual output GroceryItem item1 = new GroceryItem("item1", 3.293f); System.out.println("The following two lines should be identical:"); System.out.println(" item1.....3.293"); System.out.println(" " + itemToString(item1)); System.out.println(); // create a GroceryItem object and compare the expected // output with the actual output GroceryItem item2 = new GroceryItem("item2", 10f); System.out.println("The following two lines should be identical:"); System.out.println(" item2.....10.0"); System.out.println(" " + itemToString(item2)); System.out.println(); System.out.println("If either pair of lines doesn't match, there is a"); System.out.println("problem with either the constructor, the getName()"); System.out.println("method, or the getPrice() method of the"); System.out.println("GroceryItem class"); } /* * This method tests the behavior of the print() method of the Receipt * class when run with no items in the receipt. The output should * either be that the subtotal, tax, and total are all 0.0, or there * should be some message to the user that the receipt was empty. * * See output for more information */ public static void testEmpty() { System.out.println("\n\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the print() function behaves in a"); System.out.println("reasonable way if run with no items in the receipt"); System.out.println(); System.out.println("It should either print that the subtotal, tax, and total"); System.out.println("are all 0.0, or give some indication that the receipt is"); System.out.println("empty"); System.out.println(); System.out.println("If it prints any number besides 0.0, or prints nothing at"); System.out.println("all, it is not a proper handling of an empty receipt"); System.out.println(); // create a new Receipt object, and print it before we have // entered any items Receipt emptyReceipt = new Receipt(1.0f); emptyReceipt.print(); } /* * This method tests the behavior of the print() method when there * is exactly one item in the receipt. If nothing appears in the * printed receipt, or if any of the values are incorrect, then * the problem is probably in the addItem() method. Rounding * should not affect this test. */ public static void testOneItem() { System.out.println("\n\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the print() function behaves in a"); System.out.println("reasonable way if run with one item in the receipt"); System.out.println(); System.out.println("* The following should be printed (ignore formatting):"); System.out.println(); System.out.println("item.....40.0"); System.out.println(); System.out.println("SUBTOTAL.....40.0"); System.out.println("TAX..........10.0"); System.out.println("TOTAL........50.0"); System.out.println(); System.out.println("* Their receipt:"); System.out.println(); // create the grocery item to be added to the receipt GroceryItem item = new GroceryItem("item", 40.0f); // create a new Receipt object, add one GroceryItem to it, and // then print it Receipt oneItemReceipt = new Receipt(0.25f); oneItemReceipt.addItem(item); oneItemReceipt.print(); } /* * This tests the behavior of the print method when there are * multiple items in the receipt. Specifically, this demonstrates * that no items are being overwritten, and that the running * subtotal is calculated correctly. Errors could be the result * of either the addItem() method or the print() method. */ public static void testMultipleItems() { System.out.println("\n\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the print() function behaves in a"); System.out.println("reasonable way if run with multiple items in the receipt"); System.out.println(); System.out.println("* The following should be printed (ignore formatting):"); System.out.println(); System.out.println("item1.....7.5"); System.out.println("item2.....26.9"); System.out.println("item3.....0.8"); System.out.println(); System.out.println("SUBTOTAL.....35.2"); System.out.println("TAX..........3.52"); System.out.println("TOTAL........38.72"); System.out.println(); System.out.println("* Their receipt:"); System.out.println(); // create three items to be added to the receipt GroceryItem item1 = new GroceryItem("item1", 7.5f); GroceryItem item2 = new GroceryItem("item2", 26.900f); GroceryItem item3 = new GroceryItem("item3", .80f); // create a new receipt object, add the three grocery items // to it, and then print it Receipt multiItemReceipt = new Receipt(0.1f); multiItemReceipt.addItem(item1); multiItemReceipt.addItem(item2); multiItemReceipt.addItem(item3); multiItemReceipt.print(); } /* * This tests that the round method works and/or is being used * correctly. If the items are not rounded to two decimal places, * then the round() method is probably not being called when the * items are being printed. The prices of all five items should * round to 2.25. If the last two prices round to 2.24, then they * are probably truncating instead of rounding. The subtotal should * be rounded to 11.25. If this is rounded to any other value there * is an error in the round() method. */ public static void testRounding() { System.out.println("\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the round() method works correctly"); System.out.println(); System.out.println("All 5 items should round to 2.25"); System.out.println(); System.out.println("(1) If their receipt shows more than 2 decimal places,"); System.out.println(" then either they are not rounding or their round()"); System.out.println(" method doesn't work/isn't being used correctly"); System.out.println(); System.out.println("(2) If the last two values are listed as 2.24, then"); System.out.println(" they are truncating instead of rounding"); System.out.println(); // create 5 items whose prices are within 0.005 of 2.25 GroceryItem item1 = new GroceryItem("item1", 2.25f); GroceryItem item2 = new GroceryItem("item2", 2.252136f); GroceryItem item3 = new GroceryItem("item3", 2.2549f); GroceryItem item4 = new GroceryItem("item4", 2.2451f); GroceryItem item5 = new GroceryItem("item5", 2.248f); // create a new receipt object, add the five items, and print Receipt roundingReceipt = new Receipt(0.0f); roundingReceipt.addItem(item1); roundingReceipt.addItem(item2); roundingReceipt.addItem(item3); roundingReceipt.addItem(item4); roundingReceipt.addItem(item5); roundingReceipt.print(); } /* * This method tests that the tax is calculated correctly. These * tests check that the value of the tax isn't being rounded * incorrectly, and that the correct tax rate is being used. */ public static void testTax() { System.out.println("\n\n\n\n****************************"); System.out.println(); System.out.println("This tests that the tax is calculated correctly"); System.out.println(); // create two grocery items, whose prices sum to 100.0 GroceryItem item1 = new GroceryItem("item", 41.25f); GroceryItem item2 = new GroceryItem("item", 58.75f); System.out.println("* The tax for test 1 should be 10.0"); System.out.println(); /* * test that the tax isn't being rounded too early. If the * tax is 10.01 instead of 10.0, they are probably summing * over rounded values in the addItem() method */ Receipt simpleTaxReceipt = new Receipt(0.10f); simpleTaxReceipt.addItem(item1); simpleTaxReceipt.addItem(item2); simpleTaxReceipt.print(); System.out.println(); System.out.println("* The tax for test 2 should be 23.46"); System.out.println(); /* * test that the correct tax rate is being used, and that the * value is not being rounded too early */ Receipt longTaxReceipt = new Receipt(0.23456f); longTaxReceipt.addItem(item1); longTaxReceipt.addItem(item2); longTaxReceipt.print(); } /* * this method creates a String representation of a GroceryItem * object in order to simplify printing */ public static String itemToString(GroceryItem item) { return (item.getName() + "....." + item.getPrice()); } }