idk ive done too much

This commit is contained in:
2022-12-11 21:40:09 -06:00
parent 9a1bc9cfb5
commit 4ee4e7551a
59 changed files with 3553 additions and 13 deletions

15
LAB-9/.classpath Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

17
LAB-9/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LAB-9</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,10 @@
/**
*
*/
/**
* @author brett
*
*/
module uwstout.courses.cs144 {
requires junit;
}

View File

@@ -0,0 +1,61 @@
package uwstout.courses.cs144.projects.project3.orders;
/**
* Represents a menu item in an Order.
*
* @author Brett Bender
* @version 2022.12.08
*/
public class MenuItem {
private static final double DEFAULT_COST = 9.99;
private MenuItemType type;
private String name;
private double cost;
/**
* Create a MenuItem with the given name, cost, and type. If name is null or
* empty, will be replaced with the type + " of the Day". If type is null,
* will be replaced with {@link MenuItemType#SIDE}. If cost is less than
* $3.50, will be set to {@link #DEFAULT_COST}.
*
* @param nName Name of the item
* @param nCost Cost of the item
* @param nType Type of the item
*/
public MenuItem(String nName, double nCost, MenuItemType nType) {
type = (nType != null) ? nType : MenuItemType.SIDE;
name = (nName != null && !nName.isEmpty()) ? nName
: (type.toString() + " of the Day");
cost = (nCost >= 3.50) ? nCost : DEFAULT_COST;
}
/**
* Get the type of the item
*
* @return The item type
*/
public MenuItemType getType() {
return type;
}
/**
* Get the name of the item
*
* @return The item name
*/
public String getName() {
return name;
}
/**
* Get the cost of the item
*
* @return The item cost
*/
public double getCost() {
return cost;
}
}

View File

@@ -0,0 +1,39 @@
package uwstout.courses.cs144.projects.project3.orders;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* Tests the MenuItem class
*
* @author Brett Bender
* @version 2022.12.09
*/
public class MenuItemTest {
private static final double DELTA = 0.001;
/**
* Test the MenuItem(String, double, MenuItemType), as a result, also tests
* the getters.
*/
@Test
public void testMenuItem() {
MenuItem mi1 = new MenuItem(null, 0, null);
assertEquals(9.99, mi1.getCost(), DELTA);
assertEquals(MenuItemType.SIDE, mi1.getType());
assertEquals("Side Dish of the Day", mi1.getName());
MenuItem mi2 = new MenuItem("Steak", 12.99, MenuItemType.ENTREE);
assertEquals(12.99, mi2.getCost(), DELTA);
assertEquals(MenuItemType.ENTREE, mi2.getType());
assertEquals("Steak", mi2.getName());
MenuItem mi3 = new MenuItem("", 3.49, MenuItemType.DESSERT);
assertEquals(9.99, mi3.getCost(), DELTA);
assertEquals(MenuItemType.DESSERT, mi3.getType());
assertEquals("Dessert of the Day", mi3.getName());
}
}

View File

@@ -0,0 +1,67 @@
package uwstout.courses.cs144.projects.project3.orders;
/**
* Represents the various types of Menu Items.
*
* @author Brett Bender
* @version 2022.12.08
*/
public enum MenuItemType {
/**
* The main course
*/
ENTREE,
/**
* A bowl of soup
*/
SOUP,
/**
* A side salad
*/
SALAD,
/**
* A drink
*/
DRINK,
/**
* A side dish
*/
SIDE,
/**
* A dessert item
*/
DESSERT;
@Override
public String toString() {
String out = "";
switch (this) {
case ENTREE:
out = "Main Course";
break;
case SOUP:
out = "Bowl of Soup";
break;
case SALAD:
out = "Side Salad";
break;
case DRINK:
out = "Drink";
break;
case SIDE:
out = "Side Dish";
break;
case DESSERT:
default:
out = "Dessert";
break;
}
return out;
}
}

View File

@@ -0,0 +1,28 @@
package uwstout.courses.cs144.projects.project3.orders;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* Tests the MenuItemType enum
*
* @author Brett Bender
* @version 2022.12.08
*/
public class MenuItemTypeTest {
/**
* Tests the toString method
*/
@Test
public void testToString() {
assertEquals("Main Course", MenuItemType.ENTREE.toString());
assertEquals("Bowl of Soup", MenuItemType.SOUP.toString());
assertEquals("Side Salad", MenuItemType.SALAD.toString());
assertEquals("Drink", MenuItemType.DRINK.toString());
assertEquals("Side Dish", MenuItemType.SIDE.toString());
assertEquals("Dessert", MenuItemType.DESSERT.toString());
}
}

View File

@@ -0,0 +1,216 @@
package uwstout.courses.cs144.projects.project3.orders;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* Represents an Order with a variety of items.
*
* @author Brett Bender
* @version 2022.12.08
*/
public class Order {
private static final int INITIAL_SIZE = 5;
private static final int LABEL_WIDTH = 20;
private static final int VALUE_WIDTH = 10;
private static final NumberFormat DF = DecimalFormat.getCurrencyInstance();
private int orderNum;
private MenuItem[] items;
private int count;
/**
* Create an order with a given order number
*
* @param nOrderNum The number of the order
*/
public Order(int nOrderNum) {
orderNum = nOrderNum;
items = new MenuItem[INITIAL_SIZE];
}
public Order(Order other) {
orderNum = other.orderNum;
count = other.count;
items = other.items.clone();
}
/**
* Get the order number
*
* @return The order number
*/
public int getOrderNumber() {
return orderNum;
}
/**
* Get the number of items in the order
*
* @return Number of items
*/
public int getItemCount() {
return count;
}
/**
* Get an item at a given position. Returns null if outside of bounds.
*
* @param pos The position to pull from
* @return The item
*/
public MenuItem getItem(int pos) {
if (pos >= 0 && pos < count) {
return items[pos];
}
return null;
}
/**
* Iterates over the items in the order, and counts the number of the given
* type.
*
* @param type The type of item we are searching for.
* @return The number of items of the given type
*/
public int countItems(MenuItemType type) {
int c = 0;
for (int i = 0; i < count; i++) {
if (items[i].getType() == type) {
c++;
}
}
return c;
}
/**
* Attempt to add an item to the order. Only allows 5 items per order. Will
* only allow one of each item type (allows two of SIDE).
*
* @param item The item to add to the order
* @return If the item was added
*/
public boolean addItem(MenuItem item) {
if (count < items.length) {
MenuItemType type = item.getType();
int iCount = countItems(type);
if ((type == MenuItemType.SIDE && iCount < 2)
|| (type != MenuItemType.SIDE && iCount < 1)) {
items[count++] = item;
return true;
}
}
return false;
}
/**
* Loops over the items in the order and sums the cost of each item.
*
* @return The total cost of the order.
*/
public double getTotalCost() {
double c = 0;
for (int i = 0; i < count; i++) {
c += items[i].getCost();
}
return c;
}
/**
* Calculates tax at a 5.56% rate.
*
* @return The amount of tax
*/
public double getTax() {
return getTotalCost() * 0.056;
}
/**
* Get the tip amount for a given percentage
*
* @param percentage The percent tip you'd like to give
* @return The tip amount
*/
public double getTipAmount(double percentage) {
return getTotalCost() * percentage;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
double tax = getTax();
double total = getTotalCost() + tax;
// Order # 100
builder.append("Order # " + orderNum);
builder.append("\n");
//
for (int i = 0; i < count; i++) {
MenuItem item = items[i];
builder.append(formatLabel(LABEL_WIDTH, VALUE_WIDTH, item.getCost(),
item.getName()));
builder.append("\n");
}
// Tax
builder.append(
formatLabel(LABEL_WIDTH, VALUE_WIDTH, tax, "Tax") + "\n");
// Total
builder.append(
formatLabel(LABEL_WIDTH, VALUE_WIDTH, total, "Total") + "\n");
builder.append("\n");
// Tips
builder.append(formatLabel(LABEL_WIDTH, VALUE_WIDTH, getTipAmount(0.18),
"18% Tip") + "\n");
builder.append(formatLabel(LABEL_WIDTH, VALUE_WIDTH, getTipAmount(0.20),
"20% Tip") + "\n");
builder.append(formatLabel(LABEL_WIDTH, VALUE_WIDTH, getTipAmount(0.25),
"25% Tip") + "\n");
return builder.toString();
}
public boolean substituteItem(MenuItem subItem, String replacementItem) {
boolean substituted = false;
for (int i = 0; i < count; i++) {
MenuItem item = items[i];
if (item.getCost() == subItem.getCost()
&& item.getName().equals(subItem.getName())) {
items[i] = new MenuItem(replacementItem, item.getCost(),
item.getType());
substituted = true;
}
}
return substituted;
}
/**
* Formats a value such that it lines everything up. Formats the value with
* a currency formatter.
*
* @param labelWidth The width of the label
* @param valueWidth The width of the value
* @param value The value we're printing
* @param label The label of the value
* @return A formatted string
*/
private String formatLabel(int labelWidth, int valueWidth, double value,
String label) {
String retString = label + " ".repeat(labelWidth - label.length());
String valString = DF.format(value);
retString += " ".repeat(Math.max(valueWidth - valString.length(), 0))
+ valString;
return retString;
}
}

View File

@@ -0,0 +1,87 @@
package uwstout.courses.cs144.projects.project3.orders;
public class OrderList {
private static final int INITIAL_SIZE = 3;
private Order[] orders;
private int count;
public OrderList() {
orders = new Order[INITIAL_SIZE];
count = 0;
}
public OrderList(OrderList other) {
count = other.count;
orders = new Order[other.orders.length];
for (int i = 0; i < count; i++) {
orders[i] = new Order(other.orders[i]);
}
}
public int getOrderCount() {
return count;
}
public Order getOrder(int pos) {
if (pos >= 0 && pos < count) {
return orders[pos];
}
return null;
}
public void addOrder(Order other) {
if (count == orders.length) {
growArray();
}
orders[count++] = other;
}
public Order findOrder(int orderNumber) {
Order found = null;
for (int i = 0; i < count; i++) {
Order potOrder = orders[i];
if (potOrder.getOrderNumber() == orderNumber) {
found = potOrder;
break;
}
}
return found;
}
public double getGrandTotal() {
double sum = 0;
for (int i = 0; i < count; i++) {
Order o = orders[i];
sum += o.getTotalCost();
sum += o.getTax();
}
return sum;
}
public int substituteItems(MenuItem subItem, String replacementName) {
int replaced = 0;
for (int i = 0; i < 0; i++) {
if (orders[i].substituteItem(subItem, replacementName)) {
replaced++;
}
}
return replaced;
}
public void clearList() {
count = 0;
}
private void growArray() {
Order[] temp = new Order[orders.length + 3];
for (int i = 0; i < count; i++) {
temp[i] = orders[i];
}
orders = temp;
}
}

View File

@@ -0,0 +1,7 @@
package uwstout.courses.cs144.projects.project3.orders;
public class OrderProcessor {
private OrderList oList;
}

View File

@@ -0,0 +1,302 @@
package uwstout.courses.cs144.projects.project3.orders;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the Order class
*
* @author Brett Bender
* @version 2022.12.09
*/
public class OrderTest {
private static final double DELTA = 0.01;
/**
* Some standard menu items to be used in tests. TODO add more if needed
*/
private static final MenuItem[] MENU_ITEMS = {
new MenuItem("Steak", 25.45, 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("Taco Salad", 5.65, MenuItemType.SALAD),
new MenuItem("Water", 3.50, MenuItemType.DRINK),
new MenuItem("Cold Water", 5.50, MenuItemType.DRINK),
new MenuItem("Fries", 6.95, MenuItemType.SIDE),
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("Cake", 6.10, MenuItemType.DESSERT), };
private Order order1;
private Order order2;
/**
* Sets up some orders for testing
*
* @throws Exception Not used
*/
@Before
public void setUp() throws Exception {
order1 = new Order(531);
order1.addItem(MENU_ITEMS[0]);
order1.addItem(MENU_ITEMS[8]);
order1.addItem(MENU_ITEMS[14]);
order2 = new Order(97645);
order2.addItem(MENU_ITEMS[3]);
order2.addItem(MENU_ITEMS[6]);
order2.addItem(MENU_ITEMS[11]);
order2.addItem(MENU_ITEMS[12]);
}
/**
* Test the Order(int) constructor
*/
@Test
public void testOrder() {
Order o1 = new Order(1000);
assertEquals(1000, o1.getOrderNumber());
assertEquals(531, order1.getOrderNumber());
assertEquals(97645, order2.getOrderNumber());
}
/**
* Test the {@link Order#getItemCount()} method
*/
@Test
public void testGetItemCount() {
assertEquals(3, order1.getItemCount());
assertEquals(4, order2.getItemCount());
}
/**
* Test the {@link Order#countItems(MenuItemType)} method
*/
@Test
public void testCountItems() {
assertEquals(1, order1.countItems(MenuItemType.ENTREE));
assertEquals(1, order1.countItems(MenuItemType.SIDE));
assertEquals(1, order1.countItems(MenuItemType.DESSERT));
assertEquals(1, order2.countItems(MenuItemType.SOUP));
assertEquals(1, order2.countItems(MenuItemType.DRINK));
assertEquals(2, order2.countItems(MenuItemType.SIDE));
}
/**
* Test the addItem method (and thus some of the getItem method)
*/
@Test
public void testAddItem() {
Order o1 = new Order(100);
assertEquals(0, o1.getItemCount());
assertTrue(o1.addItem(MENU_ITEMS[0]));
assertFalse(o1.addItem(MENU_ITEMS[0]));
assertTrue(checkMenuItem(MENU_ITEMS[0], o1.getItem(0)));
assertEquals(1, o1.getItemCount());
assertTrue(o1.addItem(MENU_ITEMS[2]));
assertFalse(o1.addItem(MENU_ITEMS[2]));
assertTrue(checkMenuItem(MENU_ITEMS[2], o1.getItem(1)));
assertEquals(2, o1.getItemCount());
assertTrue(o1.addItem(MENU_ITEMS[8]));
assertTrue(o1.addItem(MENU_ITEMS[9]));
assertFalse(o1.addItem(MENU_ITEMS[8]));
assertTrue(checkMenuItem(MENU_ITEMS[8], o1.getItem(2)));
assertTrue(checkMenuItem(MENU_ITEMS[9], o1.getItem(3)));
assertEquals(4, o1.getItemCount());
assertTrue(o1.addItem(MENU_ITEMS[13]));
assertFalse(o1.addItem(MENU_ITEMS[13]));
assertTrue(checkMenuItem(MENU_ITEMS[13], o1.getItem(4)));
assertEquals(5, o1.getItemCount());
}
/**
* Tests the {@link Order#getItem(int)} method
*/
@Test
public void testGetItem() {
assertNull(order1.getItem(-1));
assertTrue(checkMenuItem(MENU_ITEMS[0], order1.getItem(0)));
assertTrue(checkMenuItem(MENU_ITEMS[8], order1.getItem(1)));
assertTrue(checkMenuItem(MENU_ITEMS[14], order1.getItem(2)));
assertNull(order1.getItem(3));
assertNull(order2.getItem(-1));
assertTrue(checkMenuItem(MENU_ITEMS[3], order2.getItem(0)));
assertTrue(checkMenuItem(MENU_ITEMS[6], order2.getItem(1)));
assertTrue(checkMenuItem(MENU_ITEMS[11], order2.getItem(2)));
assertTrue(checkMenuItem(MENU_ITEMS[12], order2.getItem(3)));
assertNull(order2.getItem(4));
}
/**
* Tests the {@link Order#getTotalCost()} method
*/
@Test
public void testGetTotalCost() {
Order blankOrder = new Order(0);
assertEquals(0, blankOrder.getTotalCost(), DELTA);
assertEquals(38.50, order1.getTotalCost(), DELTA);
assertEquals(25.60, order2.getTotalCost(), DELTA);
}
/**
* Tests the {@link Order#getTax()} method
*/
@Test
public void testGetTax() {
assertEquals(2.16, order1.getTax(), DELTA);
assertEquals(1.43, order2.getTax(), DELTA);
}
/**
* Tests the {@link Order#getTipAmount(double)} method
*/
@Test
public void testGetTipAmount() {
assertEquals(6.93, order1.getTipAmount(0.18), DELTA);
assertEquals(7.70, order1.getTipAmount(0.20), DELTA);
assertEquals(9.62, order1.getTipAmount(0.25), DELTA);
assertEquals(4.61, order2.getTipAmount(0.18), DELTA);
assertEquals(5.12, order2.getTipAmount(0.20), DELTA);
assertEquals(6.40, order2.getTipAmount(0.25), DELTA);
}
/**
* Tests the {@link Order#toString()} method
*/
@Test
public void testToString() {
// get the string
String order1Str = order1.toString();
// for debugging
// System.out.println(order1Str);
// Check if order number is 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, order1Str.indexOf("531"));
// May have to adjust if formatting is different
// Check for names and prices are in string
assertNotEquals("Item name missing", -1, order1Str.indexOf("Steak"));
assertNotEquals("Item name missing", -1, order1Str.indexOf("Fries"));
assertNotEquals("Item name missing", -1, order1Str.indexOf("Cake"));
assertNotEquals("Item cost missing", -1, order1Str.indexOf("25.45"));
assertNotEquals("Item cost missing", -1, order1Str.indexOf("6.95"));
assertNotEquals("Item cost missing", -1, order1Str.indexOf("6.10"));
// Check for tax
assertNotEquals("Tax missing", -1, order1Str.indexOf("2.16"));
// Check for total
assertNotEquals("Total (with tax) missing", -1,
order1Str.indexOf("40.66"));
// Check for tips
assertNotEquals("18% Tip missing", -1, order1Str.indexOf("6.93"));
assertNotEquals("20% Tip missing", -1, order1Str.indexOf("7.70"));
assertNotEquals("25% Tip missing", -1, order1Str.indexOf("9.62"));
// get the string
String order2Str = order2.toString();
// for debugging
System.out.println(order2Str);
// Check if order number is 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,
order2Str.indexOf("97645"));
// May have to adjust if formatting is different
// Check for names and prices are in string
assertNotEquals("Item name missing", -1,
order2Str.indexOf("Hot Seasoned Water"));
assertNotEquals("Item name missing", -1, order2Str.indexOf("Water"));
assertNotEquals("Item name missing", -1,
order2Str.indexOf("Mixed Vegetables"));
assertNotEquals("Item name missing", -1,
order2Str.indexOf("Unmixed Vegetables"));
assertNotEquals("Item cost missing", -1, order2Str.indexOf("10.10"));
assertNotEquals("Item cost missing", -1, order2Str.indexOf("3.50"));
assertNotEquals("Item cost missing", -1, order2Str.indexOf("5.50"));
assertNotEquals("Item cost missing", -1, order2Str.indexOf("6.50"));
// Check for tax
assertNotEquals("Tax missing", -1, order2Str.indexOf("1.43"));
// Check for total
assertNotEquals("Total (with tax) missing", -1,
order2Str.indexOf("27.03"));
// Check for tips
assertNotEquals("18% Tip missing", -1, order2Str.indexOf("4.61"));
assertNotEquals("20% Tip missing", -1, order2Str.indexOf("5.12"));
assertNotEquals("25% Tip missing", -1, order2Str.indexOf("6.40"));
}
/**
* 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;
}
}