Fix everything
This commit is contained in:
@@ -11,6 +11,8 @@ import java.text.NumberFormat;
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
private static final double DELTA = 0.001;
|
||||
|
||||
private static final int INITIAL_SIZE = 5;
|
||||
private static final int LABEL_WIDTH = 20;
|
||||
private static final int VALUE_WIDTH = 10;
|
||||
@@ -34,7 +36,7 @@ public class Order {
|
||||
/**
|
||||
* Copy a given order.
|
||||
*
|
||||
* @param other
|
||||
* @param other The order to copy
|
||||
*/
|
||||
public Order(Order other) {
|
||||
orderNum = other.orderNum;
|
||||
@@ -153,7 +155,7 @@ public class Order {
|
||||
builder.append("Order # " + orderNum);
|
||||
builder.append("\n");
|
||||
|
||||
//
|
||||
// Items
|
||||
for (int i = 0; i < count; i++) {
|
||||
MenuItem item = items[i];
|
||||
builder.append(formatLabel(LABEL_WIDTH, VALUE_WIDTH, item.getCost(),
|
||||
@@ -194,8 +196,13 @@ public class Order {
|
||||
boolean substituted = false;
|
||||
for (int i = 0; i < count; i++) {
|
||||
MenuItem item = items[i];
|
||||
if (item.getCost() == subItem.getCost()
|
||||
&& item.getName().equals(subItem.getName())) {
|
||||
// Here I use a comparison against delta to ensure an item will be
|
||||
// replaced even if the JVM gives slightly different values to each
|
||||
// cost, which would occur if the same MenuItem object is not
|
||||
// re-used.
|
||||
if ((Math.abs(item.getCost() - subItem.getCost()) < DELTA)
|
||||
&& item.getName().equals(subItem.getName())
|
||||
&& item.getType() == subItem.getType()) {
|
||||
items[i] = new MenuItem(replacementItem, item.getCost(),
|
||||
item.getType());
|
||||
substituted = true;
|
||||
|
||||
@@ -110,6 +110,10 @@ public class OrderProcessor {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (types == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
@@ -148,6 +152,11 @@ public class OrderProcessor {
|
||||
oList.addOrder(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the item command
|
||||
*
|
||||
* @param sc The scanner
|
||||
*/
|
||||
private void itemCommand(Scanner sc) {
|
||||
int orderNumber = sc.nextInt();
|
||||
Order order = oList.findOrder(orderNumber);
|
||||
@@ -157,6 +166,11 @@ public class OrderProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the sub command
|
||||
*
|
||||
* @param sc The scanner
|
||||
*/
|
||||
private void subCommand(Scanner sc) {
|
||||
MenuItem subItem = readItem(sc);
|
||||
String replacementItem = sc.next();
|
||||
@@ -164,12 +178,28 @@ public class OrderProcessor {
|
||||
oList.substituteItems(subItem, replacementItem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read in the next 3 values as a MenuItem. If any of the values can't be
|
||||
* determined, will return null.
|
||||
*
|
||||
* @param sc The scanner
|
||||
* @return The MenuItem read in
|
||||
*/
|
||||
private MenuItem readItem(Scanner sc) {
|
||||
if (sc.hasNext()) {
|
||||
String itemName = sc.next();
|
||||
|
||||
if (sc.hasNextDouble()) {
|
||||
double itemCost = sc.nextDouble();
|
||||
|
||||
if (sc.hasNext()) {
|
||||
MenuItemType itemType = MenuItemType.valueOf(sc.next());
|
||||
|
||||
return new MenuItem(itemName, itemCost, itemType);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -228,6 +228,9 @@ public class OrderProcessorTest {
|
||||
// Test to make sure it returns nothing if oList is null
|
||||
assertEquals("",
|
||||
new OrderProcessor(null).getMenuItemSummary(typesInReport));
|
||||
|
||||
// Test to make sure it returns nothing if types list is null
|
||||
assertEquals("", new OrderProcessor(list).getMenuItemSummary(null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,7 +20,7 @@ public class OrderTest {
|
||||
private static final double DELTA = 0.01;
|
||||
|
||||
/**
|
||||
* Some standard menu items to be used in tests. TODO add more if needed
|
||||
* Some standard menu items to be used in tests.
|
||||
*/
|
||||
private static final MenuItem[] MENU_ITEMS = {
|
||||
new MenuItem("Steak", 25.45, MenuItemType.ENTREE),
|
||||
@@ -73,6 +73,20 @@ public class OrderTest {
|
||||
assertEquals(97645, order2.getOrderNumber());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the Order(Order) const
|
||||
*/
|
||||
@Test
|
||||
public void testOrderOrder() {
|
||||
Order o1 = new Order(order1);
|
||||
assertTrue(checkOrder(o1, 531, new MenuItem[] { MENU_ITEMS[0],
|
||||
MENU_ITEMS[8], MENU_ITEMS[14] }));
|
||||
|
||||
Order o2 = new Order(order2);
|
||||
assertTrue(checkOrder(o2, 97645, new MenuItem[] { MENU_ITEMS[3],
|
||||
MENU_ITEMS[6], MENU_ITEMS[11], MENU_ITEMS[12] }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the {@link Order#getItemCount()} method
|
||||
*/
|
||||
@@ -249,6 +263,36 @@ public class OrderTest {
|
||||
assertNotEquals("25% Tip missing", -1, order2Str.indexOf("6.40"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the substitute item method
|
||||
*/
|
||||
@Test
|
||||
public void testSubstituteItem() {
|
||||
MenuItem[] items1 = { MENU_ITEMS[3],
|
||||
// item 6 with name changed
|
||||
new MenuItem("Boiling Water", 3.50, MenuItemType.DRINK),
|
||||
MENU_ITEMS[11], MENU_ITEMS[12] };
|
||||
|
||||
// Should substitute Water
|
||||
// do this after creating the array of menu items
|
||||
assertTrue("Wrong value returned",
|
||||
order2.substituteItem(MENU_ITEMS[6], "Boiling Water"));
|
||||
|
||||
assertTrue("SubstituteItem failed", checkOrder(order2, 97645, items1));
|
||||
|
||||
// Since it has already been replaced, the type & cost will match, but
|
||||
// not the name, therefore returning false.
|
||||
assertFalse("Wrong value returned",
|
||||
order2.substituteItem(MENU_ITEMS[6], "Boiling Water"));
|
||||
|
||||
// Since it has already been replaced, the Name & Cost will match, but
|
||||
// not the type, therefore returning false.
|
||||
assertFalse("Wrong value returned",
|
||||
order2.substituteItem(
|
||||
new MenuItem("Boiling Water", 3.50, MenuItemType.SOUP),
|
||||
"Chicken Noodle"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two MenuItems
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user