finish code of project 3
This commit is contained in:
@@ -31,6 +31,11 @@ public class Order {
|
||||
items = new MenuItem[INITIAL_SIZE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a given order.
|
||||
*
|
||||
* @param other
|
||||
*/
|
||||
public Order(Order other) {
|
||||
orderNum = other.orderNum;
|
||||
count = other.count;
|
||||
@@ -177,6 +182,14 @@ public class Order {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitute an item in the order that matches subItem. Creates a new item
|
||||
* with the same cost and type, but with replacementItem as the name.
|
||||
*
|
||||
* @param subItem The item to be replaced
|
||||
* @param replacementItem The name of the new item
|
||||
* @return If an item was replaced or not
|
||||
*/
|
||||
public boolean substituteItem(MenuItem subItem, String replacementItem) {
|
||||
boolean substituted = false;
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
@@ -1,7 +1,127 @@
|
||||
package uwstout.courses.cs144.projects.project3.orders;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderProcessor {
|
||||
|
||||
private OrderList oList;
|
||||
|
||||
public OrderProcessor(OrderList nOList) {
|
||||
oList = null;
|
||||
if (nOList != null) {
|
||||
oList = new OrderList(nOList);
|
||||
}
|
||||
}
|
||||
|
||||
public OrderList getList() {
|
||||
return oList;
|
||||
}
|
||||
|
||||
public void processOrders(Scanner sc) {
|
||||
if (oList == null) {
|
||||
return;
|
||||
}
|
||||
oList.clearList();
|
||||
|
||||
while (sc.hasNext()) {
|
||||
String line = sc.nextLine();
|
||||
Scanner lineSc = new Scanner(line);
|
||||
lineSc.useDelimiter("\t");
|
||||
|
||||
String cmd = lineSc.next();
|
||||
switch (cmd) {
|
||||
case "order":
|
||||
orderCommand(lineSc);
|
||||
break;
|
||||
|
||||
case "items":
|
||||
itemCommand(lineSc);
|
||||
break;
|
||||
|
||||
case "subs":
|
||||
subCommand(lineSc);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Do nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getOrderSummary() {
|
||||
if (oList == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < oList.getOrderCount(); i++) {
|
||||
Order order = oList.getOrder(i);
|
||||
builder.append(order.getOrderNumber() + " " + order.getTotalCost()
|
||||
+ order.getTax() + "\n");
|
||||
}
|
||||
|
||||
builder.append("\nGrand Total: " + oList.getGrandTotal());
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String getMenuItemSummary(MenuItemType[] types) {
|
||||
if (oList == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
MenuItemType type = types[i];
|
||||
int count = 0;
|
||||
|
||||
for (int j = 0; j < oList.getOrderCount(); j++) {
|
||||
Order order = oList.getOrder(j);
|
||||
|
||||
count += order.countItems(type);
|
||||
}
|
||||
|
||||
builder.append(type.toString() + " " + count + "\n");
|
||||
}
|
||||
|
||||
return builder.toString().trim();
|
||||
}
|
||||
|
||||
private void orderCommand(Scanner sc) {
|
||||
int orderNumber = sc.nextInt();
|
||||
int itemCount = sc.nextInt();
|
||||
|
||||
Order order = new Order(orderNumber);
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
order.addItem(readItem(sc));
|
||||
}
|
||||
|
||||
oList.addOrder(order);
|
||||
}
|
||||
|
||||
private void itemCommand(Scanner sc) {
|
||||
int orderNumber = sc.nextInt();
|
||||
Order order = oList.findOrder(orderNumber);
|
||||
|
||||
order.addItem(readItem(sc));
|
||||
}
|
||||
|
||||
private void subCommand(Scanner sc) {
|
||||
MenuItem subItem = readItem(sc);
|
||||
String replacementItem = sc.next();
|
||||
|
||||
oList.substituteItems(subItem, replacementItem);
|
||||
}
|
||||
|
||||
private MenuItem readItem(Scanner sc) {
|
||||
String itemName = sc.next();
|
||||
double itemCost = sc.nextDouble();
|
||||
MenuItemType itemType = MenuItemType.valueOf(sc.next());
|
||||
|
||||
return new MenuItem(itemName, itemCost, itemType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ public class OrderTest {
|
||||
// get the string
|
||||
String order2Str = order2.toString();
|
||||
// for debugging
|
||||
System.out.println(order2Str);
|
||||
// System.out.println(order2Str);
|
||||
|
||||
// Check if order number is in string
|
||||
// indexOf returns the position in the String where
|
||||
|
||||
Reference in New Issue
Block a user