added old projects
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* App.java
|
||||
* Project 2
|
||||
* This has the main method and handles the menu
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// initialize the list and the scanner
|
||||
ShoppingCart shoppingList = new ShoppingCart();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// show the options initially
|
||||
shoppingList.PrintOptions();
|
||||
|
||||
// Main loop
|
||||
while (true) {
|
||||
|
||||
// Get input from user
|
||||
System.out.print("\nWhat would you like to do? (Type 8 to view options again): ");
|
||||
String input;
|
||||
input = sc.nextLine();
|
||||
|
||||
switch (input) { // switch method to run menu. using strings to prevent issues with user entering
|
||||
// possible wrong type
|
||||
case "1": // Print Items
|
||||
shoppingList.print();
|
||||
break;
|
||||
case "2": // Add single item
|
||||
shoppingList.addItem();
|
||||
break;
|
||||
case "3": // Add multiple items
|
||||
shoppingList.addMultiple();
|
||||
break;
|
||||
case "4": // remove item
|
||||
shoppingList.removeItem();
|
||||
break;
|
||||
case "5": // sort by name
|
||||
shoppingList.sortName();
|
||||
break;
|
||||
case "6": // sort by cost
|
||||
shoppingList.sortCost();
|
||||
break;
|
||||
case "7": // search the list for item
|
||||
shoppingList.search();
|
||||
break;
|
||||
case "8": // print the options again
|
||||
shoppingList.PrintOptions();
|
||||
break;
|
||||
case "9": // Checkout
|
||||
shoppingList.checkout();
|
||||
System.exit(0);
|
||||
default: // used if user inputs a non valid choice
|
||||
System.out.println("Invalid Choice");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* Item.java
|
||||
* Project 2
|
||||
* This provides a class for the items to add to the shopping cart
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
public class Item { // used to hold the item name and the item cost
|
||||
public String _name;
|
||||
public Double _cost;
|
||||
|
||||
public Item(String name, double cost) { // constuctor
|
||||
_name = name;
|
||||
_cost = cost;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* LinearSearch.java
|
||||
* Project 2
|
||||
* This handles searching the list
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LinearSearch {
|
||||
// runs through all elements until it find the one it's looking for
|
||||
static public int search(List<Item> list, String input) { // O(n)
|
||||
int index = -1;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i)._name.toLowerCase().equals(input.toLowerCase())) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* MergeSort.java
|
||||
* Project 2
|
||||
* This handles sorting the list
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MergeSort {
|
||||
public static void sort(List<Item> list, int sortBy) {
|
||||
if (list.size() < 2) { // check to make sure that list can be sorted
|
||||
return;
|
||||
}
|
||||
int mid = list.size() / 2; // Getting the midpoint
|
||||
List<Item> left = new ArrayList<Item>(list.subList(0, mid)); // left sub list
|
||||
List<Item> right = new ArrayList<Item>(list.subList(mid, list.size())); // right sub list
|
||||
|
||||
// recursive sorting for left and right trees until the list size is 1
|
||||
sort(left, sortBy);
|
||||
sort(right, sortBy);
|
||||
|
||||
merge(left, right, list, sortBy);
|
||||
}
|
||||
|
||||
// merges the l and r lists
|
||||
private static void merge(
|
||||
List<Item> left, List<Item> right, List<Item> list, int sortBy) {
|
||||
int lIndex = 0;
|
||||
int rIndex = 0;
|
||||
int listIndex = 0;
|
||||
|
||||
// sorting
|
||||
while (lIndex < left.size() && rIndex < right.size()) {
|
||||
|
||||
// if sortBy is 1 -> sort by name
|
||||
if (sortBy == 1) {
|
||||
if (left.get(lIndex)._name.compareTo(right.get(rIndex)._name) < 0) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
} else {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
// if sortBy is 2 -> sort by cost
|
||||
if (sortBy == 2) {
|
||||
if (left.get(lIndex)._cost.compareTo(right.get(rIndex)._cost) < 0) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
} else {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (lIndex < left.size()) {
|
||||
list.set(listIndex++, left.get(lIndex++)); // sets the new element
|
||||
}
|
||||
while (rIndex < right.size()) {
|
||||
list.set(listIndex++, right.get(rIndex++)); // sets the new element
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Austin Bennett
|
||||
* ShoppingList.java
|
||||
* Project 2
|
||||
* This has all the methods for about everything in the project
|
||||
*/
|
||||
|
||||
package Project2;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ShoppingCart {
|
||||
// initialize the variables and scanner
|
||||
List<Item> list = new ArrayList<Item>();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// prints all the items in the list exactly how they were ordered
|
||||
public void print() { // O(n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
System.out.println(list.get(i)._name + ": $" + list.get(i)._cost);
|
||||
|
||||
}
|
||||
|
||||
// adds items to the list
|
||||
public void addItem() { // O(1) ignoring a user just spmming the wrong input type
|
||||
|
||||
System.out.print("\nEnter Item: ");
|
||||
String name = sc.nextLine();
|
||||
|
||||
// getting how much the item costs
|
||||
Double cost = 0.0;
|
||||
while (true) { // forces user to input a double for price
|
||||
System.out.print("Enter Cost ($): ");
|
||||
String costInput = sc.nextLine();
|
||||
try {
|
||||
cost = Double.parseDouble(costInput); // parses input for double
|
||||
break;
|
||||
} catch (NumberFormatException nfe) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// creates new item to add and adds it
|
||||
Item newItem = new Item(name, cost);
|
||||
list.add(newItem);
|
||||
}
|
||||
|
||||
// adds multiple items for convience
|
||||
public void addMultiple() { // O(n) ignoring a user just spamming the wrong input type
|
||||
int num;
|
||||
while (true) { // forces user to input a int for items to add
|
||||
System.out.print("Enter number of Items: ");
|
||||
String numInput = sc.nextLine();
|
||||
try {
|
||||
num = Integer.parseInt(numInput); // parses input for int
|
||||
break;
|
||||
} catch (NumberFormatException nfe) {
|
||||
|
||||
}
|
||||
}
|
||||
// runs through loop to add items
|
||||
for (int i = 0; i < num; i++) {
|
||||
addItem();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem() { // O(n) due to the Linear Search
|
||||
// gets item to remove
|
||||
System.out.print("Enter item to remove: ");
|
||||
String item = sc.nextLine();
|
||||
|
||||
int index = LinearSearch.search(list, item); // gets index of item to search for
|
||||
|
||||
if (index != -1) // if item is in list
|
||||
list.remove(index);
|
||||
else
|
||||
System.out.println("Not in shopping list");
|
||||
|
||||
}
|
||||
|
||||
// MergeSort based on name
|
||||
public void sortName() { // O(n log n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
MergeSort.sort(list, 1);
|
||||
print();
|
||||
}
|
||||
}
|
||||
|
||||
// MergeSort base on price
|
||||
public void sortCost() { // O(n log n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
MergeSort.sort(list, 2);
|
||||
print();
|
||||
}
|
||||
}
|
||||
|
||||
// returns result of the search
|
||||
public void search() { // O(n)
|
||||
if (list.size() == 0)
|
||||
System.out.println("Add items to cart.");
|
||||
else {
|
||||
System.out.print("Enter item to search for: ");
|
||||
String input = sc.nextLine();
|
||||
int index = LinearSearch.search(list, input);
|
||||
if (index != -1)
|
||||
System.out.println("Item is in cart at index " + index);
|
||||
else
|
||||
System.out.println("Item is not in cart.");
|
||||
}
|
||||
}
|
||||
|
||||
// prints all the options
|
||||
public void PrintOptions() { // O(1)
|
||||
System.out.println();
|
||||
System.out.println("1. View Shopping Cart");
|
||||
System.out.println("2. Add Singular Item");
|
||||
System.out.println("3. Add Multiple Items");
|
||||
System.out.println("4. Remove Item");
|
||||
System.out.println("5. Sort by name");
|
||||
System.out.println("6. Sort by cost");
|
||||
System.out.println("7. Check if item is in cart");
|
||||
System.out.println("8. Show options");
|
||||
System.out.println("9. Checkout");
|
||||
}
|
||||
|
||||
// totals the cart and prints the result
|
||||
public void checkout() { // O(n)
|
||||
Double total = 0.0;
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
total += list.get(i)._cost;
|
||||
|
||||
System.out.println("\nYour total is: $" + total);
|
||||
print();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user