Finish project 3
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
package uwstout.courses.cs144.projects.project3.orders;
|
package uwstout.courses.cs144.projects.project3.orders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a list of Orders
|
||||||
|
*
|
||||||
|
* @author Brett Bender
|
||||||
|
* @version 2022.12.12
|
||||||
|
*/
|
||||||
public class OrderList {
|
public class OrderList {
|
||||||
|
|
||||||
private static final int INITIAL_SIZE = 3;
|
private static final int INITIAL_SIZE = 3;
|
||||||
@@ -7,11 +13,19 @@ public class OrderList {
|
|||||||
private Order[] orders;
|
private Order[] orders;
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty order list
|
||||||
|
*/
|
||||||
public OrderList() {
|
public OrderList() {
|
||||||
orders = new Order[INITIAL_SIZE];
|
orders = new Order[INITIAL_SIZE];
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies an order list
|
||||||
|
*
|
||||||
|
* @param other The list to copy
|
||||||
|
*/
|
||||||
public OrderList(OrderList other) {
|
public OrderList(OrderList other) {
|
||||||
count = other.count;
|
count = other.count;
|
||||||
orders = new Order[other.orders.length];
|
orders = new Order[other.orders.length];
|
||||||
@@ -20,10 +34,21 @@ public class OrderList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of orders in the list.
|
||||||
|
*
|
||||||
|
* @return The number of orders
|
||||||
|
*/
|
||||||
public int getOrderCount() {
|
public int getOrderCount() {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific order in the list
|
||||||
|
*
|
||||||
|
* @param pos The position to get the order from
|
||||||
|
* @return The order in that position
|
||||||
|
*/
|
||||||
public Order getOrder(int pos) {
|
public Order getOrder(int pos) {
|
||||||
if (pos >= 0 && pos < count) {
|
if (pos >= 0 && pos < count) {
|
||||||
return orders[pos];
|
return orders[pos];
|
||||||
@@ -31,6 +56,11 @@ public class OrderList {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an order to the list (automatically grows the array if needed).
|
||||||
|
*
|
||||||
|
* @param other The order to add
|
||||||
|
*/
|
||||||
public void addOrder(Order other) {
|
public void addOrder(Order other) {
|
||||||
if (count == orders.length) {
|
if (count == orders.length) {
|
||||||
growArray();
|
growArray();
|
||||||
@@ -38,6 +68,12 @@ public class OrderList {
|
|||||||
orders[count++] = other;
|
orders[count++] = other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds an order in the list. Returns null if can't be found.
|
||||||
|
*
|
||||||
|
* @param orderNumber The order number to search for
|
||||||
|
* @return The order
|
||||||
|
*/
|
||||||
public Order findOrder(int orderNumber) {
|
public Order findOrder(int orderNumber) {
|
||||||
Order found = null;
|
Order found = null;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
@@ -50,6 +86,11 @@ public class OrderList {
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the grand total off all of the orders in the array.
|
||||||
|
*
|
||||||
|
* @return The grand total (with tax)
|
||||||
|
*/
|
||||||
public double getGrandTotal() {
|
public double getGrandTotal() {
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
@@ -60,10 +101,17 @@ public class OrderList {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitutes an item into the orders in the list.
|
||||||
|
*
|
||||||
|
* @param subItem The item to be subbed out
|
||||||
|
* @param replacementName The name of the item being subbed in
|
||||||
|
* @return The number of orders that had the item replaced
|
||||||
|
*/
|
||||||
public int substituteItems(MenuItem subItem, String replacementName) {
|
public int substituteItems(MenuItem subItem, String replacementName) {
|
||||||
int replaced = 0;
|
int replaced = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 0; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (orders[i].substituteItem(subItem, replacementName)) {
|
if (orders[i].substituteItem(subItem, replacementName)) {
|
||||||
replaced++;
|
replaced++;
|
||||||
}
|
}
|
||||||
@@ -72,6 +120,9 @@ public class OrderList {
|
|||||||
return replaced;
|
return replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the list
|
||||||
|
*/
|
||||||
public void clearList() {
|
public void clearList() {
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,360 @@
|
|||||||
|
package uwstout.courses.cs144.projects.project3.orders;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the OrderList class
|
||||||
|
*
|
||||||
|
* @author Brett Bender
|
||||||
|
* @version 2022.12.12
|
||||||
|
*/
|
||||||
|
public class OrderListTest {
|
||||||
|
|
||||||
|
private static final double DELTA = 0.01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some standard menu items to be used in tests.
|
||||||
|
*/
|
||||||
|
private static final MenuItem[] MENU_ITEMS = {
|
||||||
|
new MenuItem("Steak", 25.45, MenuItemType.ENTREE), // 0
|
||||||
|
new MenuItem("Tofu", 35.95, MenuItemType.ENTREE), // 1
|
||||||
|
new MenuItem("Chicken Noodle", 3.75, MenuItemType.SOUP), // 2
|
||||||
|
new MenuItem("Hot Seasoned Water", 10.10, MenuItemType.SOUP), // 3
|
||||||
|
new MenuItem("All Lettuce", 7.15, MenuItemType.SALAD), // 4
|
||||||
|
new MenuItem("Taco Salad", 5.65, MenuItemType.SALAD), // 5
|
||||||
|
new MenuItem("Water", 3.50, MenuItemType.DRINK), // 6
|
||||||
|
new MenuItem("Cold Water", 5.50, MenuItemType.DRINK), // 7
|
||||||
|
new MenuItem("Fries", 6.95, MenuItemType.SIDE), // 8
|
||||||
|
new MenuItem("Bacon Fries", 8.95, MenuItemType.SIDE), // 9
|
||||||
|
new MenuItem("Fried Bacon", 10.95, MenuItemType.SIDE), // 10
|
||||||
|
new MenuItem("Mixed Vegetables", 5.50, MenuItemType.SIDE), // 11
|
||||||
|
new MenuItem("Unmixed Vegetables", 6.50, MenuItemType.SIDE), // 12
|
||||||
|
new MenuItem("Pie", 7.25, MenuItemType.DESSERT), // 13
|
||||||
|
new MenuItem("Cake", 6.10, MenuItemType.DESSERT), }; // 14
|
||||||
|
|
||||||
|
private OrderList list1;
|
||||||
|
private OrderList list2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the class up for testing
|
||||||
|
*
|
||||||
|
* @throws Exception Not used
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
// List 1
|
||||||
|
list1 = new OrderList();
|
||||||
|
Order order;
|
||||||
|
// add some stuff
|
||||||
|
order = new Order(531);
|
||||||
|
order.addItem(MENU_ITEMS[0]);
|
||||||
|
order.addItem(MENU_ITEMS[8]);
|
||||||
|
order.addItem(MENU_ITEMS[14]);
|
||||||
|
list1.addOrder(order);
|
||||||
|
|
||||||
|
order = new Order(97645);
|
||||||
|
order.addItem(MENU_ITEMS[3]);
|
||||||
|
order.addItem(MENU_ITEMS[6]);
|
||||||
|
order.addItem(MENU_ITEMS[11]);
|
||||||
|
order.addItem(MENU_ITEMS[12]);
|
||||||
|
list1.addOrder(order);
|
||||||
|
|
||||||
|
order = new Order(2931);
|
||||||
|
order.addItem(MENU_ITEMS[6]);
|
||||||
|
list1.addOrder(order);
|
||||||
|
|
||||||
|
// List 2
|
||||||
|
list2 = new OrderList();
|
||||||
|
|
||||||
|
order = new Order(8675309);
|
||||||
|
order.addItem(MENU_ITEMS[1]);
|
||||||
|
order.addItem(MENU_ITEMS[5]);
|
||||||
|
order.addItem(MENU_ITEMS[6]);
|
||||||
|
list2.addOrder(order);
|
||||||
|
|
||||||
|
order = new Order(1337);
|
||||||
|
order.addItem(MENU_ITEMS[7]);
|
||||||
|
order.addItem(MENU_ITEMS[14]);
|
||||||
|
list2.addOrder(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the constructor
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOrderList() {
|
||||||
|
OrderList testList = new OrderList();
|
||||||
|
|
||||||
|
assertEquals(0, testList.getOrderCount());
|
||||||
|
assertNull(testList.getOrder(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the copy constructor
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOrderListOrderList() {
|
||||||
|
// Test copy constructor
|
||||||
|
OrderList copyList = new OrderList(list1);
|
||||||
|
|
||||||
|
assertEquals("List length is wrong", list1.getOrderCount(),
|
||||||
|
copyList.getOrderCount());
|
||||||
|
// check orders
|
||||||
|
for (int i = 0; i < list1.getOrderCount(); i++) {
|
||||||
|
assertTrue("The orders in the list were wrong",
|
||||||
|
checkOrder(list1.getOrder(i), copyList.getOrder(i)));
|
||||||
|
// check if a copy was made, instead of a reference
|
||||||
|
assertNotSame("The Orders were not copied", list1.getOrder(i),
|
||||||
|
copyList.getOrder(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderList copyList2 = new OrderList(list2);
|
||||||
|
|
||||||
|
assertEquals("List length is wrong", list2.getOrderCount(),
|
||||||
|
copyList2.getOrderCount());
|
||||||
|
// check orders
|
||||||
|
for (int i = 0; i < list2.getOrderCount(); i++) {
|
||||||
|
assertTrue("The orders in the list were wrong",
|
||||||
|
checkOrder(list2.getOrder(i), copyList2.getOrder(i)));
|
||||||
|
// check if a copy was made, instead of a reference
|
||||||
|
assertNotSame("The Orders were not copied", list2.getOrder(i),
|
||||||
|
copyList2.getOrder(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the getOrderCount method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrderCount() {
|
||||||
|
assertEquals(3, list1.getOrderCount());
|
||||||
|
assertEquals(2, list2.getOrderCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the getOrder method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrder() {
|
||||||
|
assertNull(list1.getOrder(-1));
|
||||||
|
assertTrue(checkOrder(list1.getOrder(0), 531, new MenuItem[] {
|
||||||
|
MENU_ITEMS[0], MENU_ITEMS[8], MENU_ITEMS[14] }));
|
||||||
|
assertTrue(checkOrder(list1.getOrder(1), 97645,
|
||||||
|
new MenuItem[] { MENU_ITEMS[3], MENU_ITEMS[6], MENU_ITEMS[11],
|
||||||
|
MENU_ITEMS[12] }));
|
||||||
|
assertTrue(checkOrder(list1.getOrder(2), 2931,
|
||||||
|
new MenuItem[] { MENU_ITEMS[6] }));
|
||||||
|
assertNull(list1.getOrder(3));
|
||||||
|
|
||||||
|
assertNull(list2.getOrder(-1));
|
||||||
|
assertTrue(checkOrder(list2.getOrder(0), 8675309, new MenuItem[] {
|
||||||
|
MENU_ITEMS[1], MENU_ITEMS[5], MENU_ITEMS[6] }));
|
||||||
|
assertTrue(checkOrder(list2.getOrder(1), 1337,
|
||||||
|
new MenuItem[] { MENU_ITEMS[7], MENU_ITEMS[14] }));
|
||||||
|
assertNull(list2.getOrder(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the addOrder method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testAddOrder() {
|
||||||
|
Order order;
|
||||||
|
|
||||||
|
// Generally test adding again
|
||||||
|
order = new Order(333);
|
||||||
|
order.addItem(MENU_ITEMS[1]);
|
||||||
|
list1.addOrder(order);
|
||||||
|
|
||||||
|
assertTrue(checkOrder(list1.getOrder(list1.getOrderCount() - 1), 333,
|
||||||
|
new MenuItem[] { MENU_ITEMS[1] }));
|
||||||
|
|
||||||
|
// Ensure we can go past the initial 5 limit
|
||||||
|
order = new Order(666);
|
||||||
|
order.addItem(MENU_ITEMS[2]);
|
||||||
|
order.addItem(MENU_ITEMS[10]);
|
||||||
|
order.addItem(MENU_ITEMS[13]);
|
||||||
|
list1.addOrder(order);
|
||||||
|
|
||||||
|
assertTrue(checkOrder(list1.getOrder(list1.getOrderCount() - 1), 666,
|
||||||
|
new MenuItem[] { MENU_ITEMS[2], MENU_ITEMS[10],
|
||||||
|
MENU_ITEMS[13] }));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the findOrder method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testFindOrder() {
|
||||||
|
assertNull(list1.findOrder(-1));
|
||||||
|
assertTrue(checkOrder(list1.findOrder(531), list1.getOrder(0)));
|
||||||
|
assertTrue(checkOrder(list1.findOrder(97645), list1.getOrder(1)));
|
||||||
|
assertTrue(checkOrder(list1.findOrder(2931), list1.getOrder(2)));
|
||||||
|
|
||||||
|
assertNull(list2.findOrder(-1));
|
||||||
|
assertTrue(checkOrder(list2.findOrder(8675309), list2.getOrder(0)));
|
||||||
|
assertTrue(checkOrder(list2.findOrder(1337), list2.getOrder(1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the getGrandTotal method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetGrandTotal() {
|
||||||
|
assertEquals(71.39, list1.getGrandTotal(), DELTA);
|
||||||
|
assertEquals(59.88, list2.getGrandTotal(), DELTA);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the substituteItem method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSubstituteItems() {
|
||||||
|
// Changing water to boiling water
|
||||||
|
// 2 Orders should change
|
||||||
|
MenuItem[] items1 = { MENU_ITEMS[3],
|
||||||
|
// item 6 with name changed
|
||||||
|
new MenuItem("Boiling Water", 3.50, MenuItemType.DRINK),
|
||||||
|
MENU_ITEMS[11], MENU_ITEMS[12] };
|
||||||
|
MenuItem[] items2 = {
|
||||||
|
new MenuItem("Boiling Water", 3.50, MenuItemType.DRINK),
|
||||||
|
|
||||||
|
};
|
||||||
|
// 1 should not, make copy
|
||||||
|
Order origOrder = new Order(list1.getOrder(0));
|
||||||
|
|
||||||
|
// Should substitute Water
|
||||||
|
// do this after creating the array of menu items
|
||||||
|
assertEquals("Wrong value returned", 2,
|
||||||
|
list1.substituteItems(MENU_ITEMS[6], "Boiling Water"));
|
||||||
|
|
||||||
|
assertTrue("substituteItems failed - order 0",
|
||||||
|
checkOrder(list1.getOrder(0), origOrder));
|
||||||
|
assertTrue("substituteItems failed - order 1",
|
||||||
|
checkOrder(list1.getOrder(1), 97645, items1));
|
||||||
|
assertTrue("substituteItems failed - order 2",
|
||||||
|
checkOrder(list1.getOrder(2), 2931, items2));
|
||||||
|
|
||||||
|
// Changing water to boiling water
|
||||||
|
// Only the first order should change
|
||||||
|
MenuItem[] list2Items1 = {
|
||||||
|
// item 6 with name changed
|
||||||
|
MENU_ITEMS[1], MENU_ITEMS[5],
|
||||||
|
new MenuItem("Mountain Moo", 3.50, MenuItemType.DRINK) };
|
||||||
|
origOrder = new Order(list2.getOrder(1));
|
||||||
|
|
||||||
|
// Should substitute Water
|
||||||
|
// do this after creating the array of menu items
|
||||||
|
assertEquals("Wrong value returned", 1,
|
||||||
|
list2.substituteItems(MENU_ITEMS[6], "Mountain Moo"));
|
||||||
|
|
||||||
|
assertTrue("substituteItems failed - order 0",
|
||||||
|
checkOrder(list2.getOrder(0), 8675309, list2Items1));
|
||||||
|
|
||||||
|
assertTrue("substituteItems failed - order 1",
|
||||||
|
checkOrder(list2.getOrder(1), origOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the clearList method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testClearList() {
|
||||||
|
assertEquals(3, list1.getOrderCount());
|
||||||
|
list1.clearList();
|
||||||
|
assertEquals(0, list1.getOrderCount());
|
||||||
|
assertNull(list1.getOrder(0));
|
||||||
|
|
||||||
|
assertEquals(2, list2.getOrderCount());
|
||||||
|
list2.clearList();
|
||||||
|
assertEquals(0, list2.getOrderCount());
|
||||||
|
assertNull(list2.getOrder(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two MenuItems
|
||||||
|
*
|
||||||
|
* @param expected The MenuItem you expect to have
|
||||||
|
* @param actual The actual MenuItem
|
||||||
|
* @return true if they are the same
|
||||||
|
*/
|
||||||
|
private boolean checkMenuItem(MenuItem expected, MenuItem actual) {
|
||||||
|
// if both are null or the same object
|
||||||
|
if (expected == actual) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// one of them is null
|
||||||
|
if (expected == null || actual == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// check if all of the values match
|
||||||
|
return expected.getName().equals(actual.getName())
|
||||||
|
&& Math.abs(expected.getCost() - actual.getCost()) < 0.01
|
||||||
|
&& expected.getType() == actual.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if an Order is as expected
|
||||||
|
*
|
||||||
|
* @param ord Order to check
|
||||||
|
* @param eOrderNumber Expected order number
|
||||||
|
* @param items Array of MenuItems that should be in the array.
|
||||||
|
* Should have the same length as the number of items in
|
||||||
|
* the order.
|
||||||
|
* @return True if it matches the order number and items.
|
||||||
|
*/
|
||||||
|
private boolean checkOrder(Order ord, int eOrderNumber, MenuItem[] items) {
|
||||||
|
// check if the order number of number of items
|
||||||
|
// is wrong
|
||||||
|
if (ord == null || ord.getOrderNumber() != eOrderNumber
|
||||||
|
|| ord.getItemCount() != items.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// check if each menu item is the same
|
||||||
|
for (int i = 0; i < ord.getItemCount(); i++) {
|
||||||
|
if (!checkMenuItem(items[i], ord.getItem(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// must be equal if we are here.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if an Order is as expected
|
||||||
|
*
|
||||||
|
* @param eOrder Expected value
|
||||||
|
* @param aOrder Actual order
|
||||||
|
*
|
||||||
|
* @return True if the orders are the same.
|
||||||
|
*/
|
||||||
|
private boolean checkOrder(Order eOrder, Order aOrder) {
|
||||||
|
// if both are null or the same object
|
||||||
|
if (eOrder == aOrder) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// one of them is null
|
||||||
|
// or the number of items does not match
|
||||||
|
if (eOrder == null || aOrder == null
|
||||||
|
|| eOrder.getItemCount() != aOrder.getItemCount()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// check if each menu item is the same
|
||||||
|
for (int i = 0; i < eOrder.getItemCount(); i++) {
|
||||||
|
if (!checkMenuItem(eOrder.getItem(i), aOrder.getItem(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// must be equal if we are here.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,27 @@
|
|||||||
package uwstout.courses.cs144.projects.project3.orders;
|
package uwstout.courses.cs144.projects.project3.orders;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.NumberFormat;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes orders from a file, as well as additional helpers for handling
|
||||||
|
* data.
|
||||||
|
*
|
||||||
|
* @author Brett Bender
|
||||||
|
* @version 2022.12.12
|
||||||
|
*/
|
||||||
public class OrderProcessor {
|
public class OrderProcessor {
|
||||||
|
|
||||||
|
private static final NumberFormat DF = DecimalFormat.getCurrencyInstance();
|
||||||
|
|
||||||
private OrderList oList;
|
private OrderList oList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an OrderProcessor with the given order list
|
||||||
|
*
|
||||||
|
* @param nOList The order list to be used
|
||||||
|
*/
|
||||||
public OrderProcessor(OrderList nOList) {
|
public OrderProcessor(OrderList nOList) {
|
||||||
oList = null;
|
oList = null;
|
||||||
if (nOList != null) {
|
if (nOList != null) {
|
||||||
@@ -13,10 +29,21 @@ public class OrderProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the OrderList associated with this processor
|
||||||
|
*
|
||||||
|
* @return The order list
|
||||||
|
*/
|
||||||
public OrderList getList() {
|
public OrderList getList() {
|
||||||
return oList;
|
return oList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process a scanner that contains commands that the OrderProcessor should
|
||||||
|
* handle.
|
||||||
|
*
|
||||||
|
* @param sc The input scanner
|
||||||
|
*/
|
||||||
public void processOrders(Scanner sc) {
|
public void processOrders(Scanner sc) {
|
||||||
if (oList == null) {
|
if (oList == null) {
|
||||||
return;
|
return;
|
||||||
@@ -34,7 +61,7 @@ public class OrderProcessor {
|
|||||||
orderCommand(lineSc);
|
orderCommand(lineSc);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "items":
|
case "item":
|
||||||
itemCommand(lineSc);
|
itemCommand(lineSc);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -49,6 +76,11 @@ public class OrderProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a summary of all of the orders in the OrderList.
|
||||||
|
*
|
||||||
|
* @return The summary
|
||||||
|
*/
|
||||||
public String getOrderSummary() {
|
public String getOrderSummary() {
|
||||||
if (oList == null) {
|
if (oList == null) {
|
||||||
return "";
|
return "";
|
||||||
@@ -58,15 +90,21 @@ public class OrderProcessor {
|
|||||||
|
|
||||||
for (int i = 0; i < oList.getOrderCount(); i++) {
|
for (int i = 0; i < oList.getOrderCount(); i++) {
|
||||||
Order order = oList.getOrder(i);
|
Order order = oList.getOrder(i);
|
||||||
builder.append(order.getOrderNumber() + " " + order.getTotalCost()
|
builder.append(order.getOrderNumber() + " "
|
||||||
+ order.getTax() + "\n");
|
+ DF.format(order.getTotalCost() + order.getTax()) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.append("\nGrand Total: " + oList.getGrandTotal());
|
builder.append("\nGrand Total: " + DF.format(oList.getGrandTotal()));
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a summary of the types that are contained within the orders
|
||||||
|
*
|
||||||
|
* @param types The types to get a summary of
|
||||||
|
* @return The summary
|
||||||
|
*/
|
||||||
public String getMenuItemSummary(MenuItemType[] types) {
|
public String getMenuItemSummary(MenuItemType[] types) {
|
||||||
if (oList == null) {
|
if (oList == null) {
|
||||||
return "";
|
return "";
|
||||||
@@ -90,6 +128,14 @@ public class OrderProcessor {
|
|||||||
return builder.toString().trim();
|
return builder.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process the order command:
|
||||||
|
*
|
||||||
|
* {@code order <order number> <item count> <item name 1> <item cost 1> <item type
|
||||||
|
* 1> <item name 2> ...}
|
||||||
|
*
|
||||||
|
* @param sc The scanner
|
||||||
|
*/
|
||||||
private void orderCommand(Scanner sc) {
|
private void orderCommand(Scanner sc) {
|
||||||
int orderNumber = sc.nextInt();
|
int orderNumber = sc.nextInt();
|
||||||
int itemCount = sc.nextInt();
|
int itemCount = sc.nextInt();
|
||||||
@@ -106,8 +152,10 @@ public class OrderProcessor {
|
|||||||
int orderNumber = sc.nextInt();
|
int orderNumber = sc.nextInt();
|
||||||
Order order = oList.findOrder(orderNumber);
|
Order order = oList.findOrder(orderNumber);
|
||||||
|
|
||||||
|
if (order != null) {
|
||||||
order.addItem(readItem(sc));
|
order.addItem(readItem(sc));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void subCommand(Scanner sc) {
|
private void subCommand(Scanner sc) {
|
||||||
MenuItem subItem = readItem(sc);
|
MenuItem subItem = readItem(sc);
|
||||||
|
|||||||
@@ -0,0 +1,346 @@
|
|||||||
|
package uwstout.courses.cs144.projects.project3.orders;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the OrderProcessor class
|
||||||
|
*
|
||||||
|
* @author Brett Bender
|
||||||
|
* @version 2022.12.12
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class OrderProcessorTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some standard menu items to be used in tests.
|
||||||
|
*/
|
||||||
|
private static final MenuItem[] ITEM_VALUES = {
|
||||||
|
new MenuItem("Pork", 19.15, MenuItemType.ENTREE),
|
||||||
|
new MenuItem("Tofu", 35.95, MenuItemType.ENTREE),
|
||||||
|
new MenuItem("Chicken Noodle", 3.75, MenuItemType.SOUP),
|
||||||
|
new MenuItem("Hot Seasoned Water", 10.10, MenuItemType.SOUP),
|
||||||
|
new MenuItem("All Lettuce", 7.15, MenuItemType.SALAD),
|
||||||
|
new MenuItem("Ceasar Salad", 4.50, MenuItemType.SALAD),
|
||||||
|
new MenuItem("Taco Salad", 5.65, MenuItemType.SALAD),
|
||||||
|
new MenuItem("Water", 3.50, MenuItemType.DRINK),
|
||||||
|
new MenuItem("Sprite", 4.15, MenuItemType.DRINK),
|
||||||
|
new MenuItem("Cold Water", 5.50, MenuItemType.DRINK),
|
||||||
|
new MenuItem("Bacon Fries", 8.95, MenuItemType.SIDE),
|
||||||
|
new MenuItem("Fried Bacon", 10.95, MenuItemType.SIDE),
|
||||||
|
new MenuItem("Mixed Vegetables", 5.50, MenuItemType.SIDE),
|
||||||
|
new MenuItem("Unmixed Vegetables", 6.50, MenuItemType.SIDE),
|
||||||
|
new MenuItem("Pie", 7.25, MenuItemType.DESSERT),
|
||||||
|
new MenuItem("Ice Cream", 5.10, MenuItemType.DESSERT), };
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A series of order commands for testing All fields are separated by \t and
|
||||||
|
* each line ends with a tab
|
||||||
|
*/
|
||||||
|
private static final String ORDER_INPUT = "order\t39515\t1\tFried Bacon\t10.95\tSIDE\n"
|
||||||
|
+ "order\t66618\t2\tAll Lettuce\t7.15\tSALAD\tPork\t19.15\tENTREE\n"
|
||||||
|
+ "order\t82328\t1\tCold Water\t5.5\tDRINK\n"
|
||||||
|
+ "subs\tCeasar Salad\t4.5\tSALAD\tNothin' But Lettuce\n"
|
||||||
|
+ "order\t11613\t1\tCeasar Salad\t4.5\tSALAD\n"
|
||||||
|
+ "item\t82328\tPork\t19.15\tENTREE\n"
|
||||||
|
+ "order\t74480\t2\tTofu\t35.95\tENTREE\tSprite\t4.15\tDRINK\n"
|
||||||
|
+ "order\t31423\t2\tUnmixed Vegetables\t6.5\tSIDE\tWater\t3.5\tDRINK\n"
|
||||||
|
+ "order\t55259\t3\tTaco Salad\t5.65\tSALAD\tHot Seasoned Water\t10.1\tSOUP\tTofu\t35.95\tENTREE\n"
|
||||||
|
+ "item\t00001\tUnmixed Vegetables\t6.5\tSIDE\n"
|
||||||
|
+ "subs\tTofu\t35.95\tENTREE\tBacon\n";
|
||||||
|
|
||||||
|
private OrderProcessor p2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the class up for testing
|
||||||
|
*
|
||||||
|
* @throws Exception Not used
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
p2 = new OrderProcessor(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the constructor
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOrderProcessor() {
|
||||||
|
OrderList expectedList = getExpectedList();
|
||||||
|
|
||||||
|
OrderProcessor o1 = new OrderProcessor(null);
|
||||||
|
OrderProcessor o2 = new OrderProcessor(getExpectedList());
|
||||||
|
|
||||||
|
assertNull(o1.getList());
|
||||||
|
|
||||||
|
OrderList testList = o2.getList();
|
||||||
|
|
||||||
|
// check orders
|
||||||
|
for (int i = 0; i < expectedList.getOrderCount(); i++) {
|
||||||
|
assertTrue("The Order at position " + i + " did not match",
|
||||||
|
checkOrder(expectedList.getOrder(i), testList.getOrder(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the processOrders method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testProcessOrders() {
|
||||||
|
OrderList list = new OrderList();
|
||||||
|
OrderProcessor processor = new OrderProcessor(list);
|
||||||
|
|
||||||
|
// Create Scanner with input
|
||||||
|
Scanner input = new Scanner(ORDER_INPUT);
|
||||||
|
// process it
|
||||||
|
processor.processOrders(input);
|
||||||
|
// Get expected list
|
||||||
|
OrderList expectedList = getExpectedList();
|
||||||
|
// Get list from processor
|
||||||
|
OrderList readInList = processor.getList();
|
||||||
|
|
||||||
|
assertEquals("Processed list is not the right length.",
|
||||||
|
expectedList.getOrderCount(), readInList.getOrderCount());
|
||||||
|
|
||||||
|
// check orders
|
||||||
|
for (int i = 0; i < expectedList.getOrderCount(); i++) {
|
||||||
|
assertTrue("The Order at position " + i + " did not match",
|
||||||
|
checkOrder(expectedList.getOrder(i),
|
||||||
|
readInList.getOrder(i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderProcessor processor2 = new OrderProcessor(null);
|
||||||
|
// This should do nothing.
|
||||||
|
// Create Scanner with input
|
||||||
|
Scanner input2 = new Scanner(ORDER_INPUT);
|
||||||
|
// process it
|
||||||
|
processor2.processOrders(input2);
|
||||||
|
// Get list from processor
|
||||||
|
OrderList readInList2 = processor2.getList();
|
||||||
|
|
||||||
|
assertNull(readInList2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the getOrderSummary method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetOrderSummary() {
|
||||||
|
OrderList list = new OrderList();
|
||||||
|
OrderProcessor processor = new OrderProcessor(list);
|
||||||
|
|
||||||
|
// Create Scanner with input
|
||||||
|
Scanner input = new Scanner(ORDER_INPUT);
|
||||||
|
// process it
|
||||||
|
processor.processOrders(input);
|
||||||
|
|
||||||
|
// get report
|
||||||
|
String summary = processor.getOrderSummary();
|
||||||
|
|
||||||
|
// for debugging
|
||||||
|
// System.out.println(summary);
|
||||||
|
|
||||||
|
// Check if the order numbers are in string
|
||||||
|
// indexOf returns the position in the String where
|
||||||
|
// the parameter is found. It returns -1 if
|
||||||
|
// it is not there.
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("39515"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("66618"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("82328"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("11613"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("74480"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("31423"));
|
||||||
|
assertNotEquals("No order number found", -1, summary.indexOf("55259"));
|
||||||
|
|
||||||
|
// find the costs + tax
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("11.56"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("27.77"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("26.03"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("4.75"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("42.35"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("10.56"));
|
||||||
|
assertNotEquals("No order total found", -1, summary.indexOf("54.60"));
|
||||||
|
|
||||||
|
// find grand total
|
||||||
|
assertNotEquals("No grand total found", -1, summary.indexOf("177.62"));
|
||||||
|
|
||||||
|
// Test to make sure it returns nothing if oList is null
|
||||||
|
assertEquals("", new OrderProcessor(null).getOrderSummary());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the getMenuItemSummary method
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetMenuItemSummary() {
|
||||||
|
MenuItemType[] typesInReport = { MenuItemType.DRINK, MenuItemType.SOUP,
|
||||||
|
MenuItemType.DESSERT };
|
||||||
|
|
||||||
|
MenuItemType[] typesNotInReport = { MenuItemType.ENTREE,
|
||||||
|
MenuItemType.SALAD, MenuItemType.SIDE };
|
||||||
|
|
||||||
|
OrderList list = new OrderList();
|
||||||
|
OrderProcessor processor = new OrderProcessor(list);
|
||||||
|
|
||||||
|
// Create Scanner with input
|
||||||
|
Scanner input = new Scanner(ORDER_INPUT);
|
||||||
|
// process it
|
||||||
|
processor.processOrders(input);
|
||||||
|
|
||||||
|
// get report
|
||||||
|
String summary = processor.getMenuItemSummary(typesInReport);
|
||||||
|
|
||||||
|
// for debugging
|
||||||
|
// System.out.println(summary);
|
||||||
|
|
||||||
|
// find the names of the types in the output
|
||||||
|
for (int i = 0; i < typesInReport.length; i++) {
|
||||||
|
// indexOf returns -1 if not found
|
||||||
|
assertNotEquals(typesInReport[i] + " was not in the summary.", -1,
|
||||||
|
summary.indexOf(typesInReport[i].toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the counts in the summary
|
||||||
|
// This is not the best way, but is good enough
|
||||||
|
// for the moment
|
||||||
|
assertNotEquals("Cannot find the Drink count", -1,
|
||||||
|
summary.indexOf("3"));
|
||||||
|
assertNotEquals("Cannot find the Soup count", -1, summary.indexOf("1"));
|
||||||
|
assertNotEquals("Cannot find the Dessert count", -1,
|
||||||
|
summary.indexOf("0"));
|
||||||
|
|
||||||
|
// check for other names not being there
|
||||||
|
for (int i = 0; i < typesNotInReport.length; i++) {
|
||||||
|
// indexOf returns -1 if not found
|
||||||
|
assertEquals(
|
||||||
|
typesNotInReport[i] + " was in the summary "
|
||||||
|
+ "when it should not be.",
|
||||||
|
-1, summary.indexOf(typesNotInReport[i].toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test to make sure it returns nothing if oList is null
|
||||||
|
assertEquals("",
|
||||||
|
new OrderProcessor(null).getMenuItemSummary(typesInReport));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two MenuItems
|
||||||
|
*
|
||||||
|
* @param expected The MenuItem you expect to have
|
||||||
|
* @param actual The actual MenuItem
|
||||||
|
* @return true if they are the same
|
||||||
|
*/
|
||||||
|
private boolean checkMenuItem(MenuItem expected, MenuItem actual) {
|
||||||
|
// if both are null or the same object
|
||||||
|
if (expected == actual) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// one of them is null
|
||||||
|
if (expected == null || actual == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// check if all of the values match
|
||||||
|
return expected.getName().equals(actual.getName())
|
||||||
|
&& Math.abs(expected.getCost() - actual.getCost()) < 0.01
|
||||||
|
&& expected.getType() == actual.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if an Order is as expected
|
||||||
|
*
|
||||||
|
* @param eOrder Expected value
|
||||||
|
* @param aOrder Actual order
|
||||||
|
*
|
||||||
|
* @return True if the orders are the same.
|
||||||
|
*/
|
||||||
|
private boolean checkOrder(Order eOrder, Order aOrder) {
|
||||||
|
// if both are null or the same object
|
||||||
|
if (eOrder == aOrder) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// one of them is null
|
||||||
|
// or the number of items does not match
|
||||||
|
if (eOrder == null || aOrder == null
|
||||||
|
|| eOrder.getItemCount() != aOrder.getItemCount()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// check if each menu item is the same
|
||||||
|
for (int i = 0; i < eOrder.getItemCount(); i++) {
|
||||||
|
if (!checkMenuItem(eOrder.getItem(i), aOrder.getItem(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// must be equal if we are here.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the expected output for ORDER_INPUT
|
||||||
|
*
|
||||||
|
* @return List with the expected output
|
||||||
|
*/
|
||||||
|
private OrderList getExpectedList() {
|
||||||
|
// substituted menu items
|
||||||
|
// "subs\tTofu\t35.95\tENTREE\tBacon"
|
||||||
|
MenuItem subs = new MenuItem("Bacon", 35.95, MenuItemType.ENTREE);
|
||||||
|
// does nothing since there isn't anything order in the list
|
||||||
|
// with a Ceasar Salad when this is run
|
||||||
|
// "subs\tCeasar Salad\t4.5\tSALAD\tNothin' But Lettuce\n"
|
||||||
|
|
||||||
|
// does nothing since 00001 is not an order number
|
||||||
|
// in the list
|
||||||
|
// "item\t00001\tUnmixed Vegetables\t6.5\tSIDE\n"
|
||||||
|
|
||||||
|
OrderList list = new OrderList();
|
||||||
|
Order order;
|
||||||
|
// "order\t39515\t1\tFried Bacon\t10.95\tSIDE\n"
|
||||||
|
order = new Order(39515);
|
||||||
|
order.addItem(ITEM_VALUES[11]);
|
||||||
|
list.addOrder(order);
|
||||||
|
// "order\t66618\t2\tAll Lettuce\t7.15\tSALAD\tPork\t19.15\tENTREE\n"
|
||||||
|
order = new Order(66618);
|
||||||
|
order.addItem(ITEM_VALUES[4]);
|
||||||
|
order.addItem(ITEM_VALUES[0]);
|
||||||
|
list.addOrder(order);
|
||||||
|
// "order\t82328\t1\tCold Water\t5.5\tDRINK\n"
|
||||||
|
// "item\t82328\tPork\t19.15\tENTREE\n" adds item
|
||||||
|
order = new Order(82328);
|
||||||
|
order.addItem(ITEM_VALUES[9]);
|
||||||
|
order.addItem(ITEM_VALUES[0]);
|
||||||
|
list.addOrder(order);
|
||||||
|
// "order\t11613\t1\tCeasar Salad\t4.5\tSALAD\n"
|
||||||
|
order = new Order(11613);
|
||||||
|
order.addItem(ITEM_VALUES[5]);
|
||||||
|
list.addOrder(order);
|
||||||
|
// "order\t74480\t2\tTofu\t35.95\tENTREE\tSprite\t4.15\tDRINK\n"
|
||||||
|
order = new Order(74480);
|
||||||
|
order.addItem(subs); // substituted for ITEM_VALUES[1]
|
||||||
|
order.addItem(ITEM_VALUES[8]);
|
||||||
|
list.addOrder(order);
|
||||||
|
// "order\t31423\t2\tUnmixed Vegetables\t6.5\tSIDE\tWater\t3.5\tDRINK\n"
|
||||||
|
order = new Order(31423);
|
||||||
|
order.addItem(ITEM_VALUES[13]);
|
||||||
|
order.addItem(ITEM_VALUES[7]);
|
||||||
|
list.addOrder(order);
|
||||||
|
|
||||||
|
// "order\t55259\t3\tTaco Salad\t5.65\tSALAD\tHot Seasoned
|
||||||
|
// Water\t10.1\tSOUP\tTofu\t35.95\tENTREE\n";
|
||||||
|
order = new Order(55259);
|
||||||
|
order.addItem(ITEM_VALUES[6]);
|
||||||
|
order.addItem(ITEM_VALUES[3]);
|
||||||
|
order.addItem(subs); // substituted for ITEM_VALUES[1]
|
||||||
|
list.addOrder(order);
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user