Created
February 25, 2018 13:12
-
-
Save akaimisa/502728671b0a5ccb1538177e992cdcb2 to your computer and use it in GitHub Desktop.
Shopping Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Customer{ | |
String [] shoppingList; | |
int [] quantity; | |
double totalPrice; | |
int [] aisle; | |
public Customer () { | |
shoppingList = new String [10]; | |
quantity = new int [10]; | |
totalPrice = 0.00; | |
aisle = new int [10]; | |
for (int i = 0; i < aisle.length; i++) { | |
aisle[i] = -1; | |
} | |
} | |
public void addItem (String item, int num) { | |
int i = 0; | |
while ((this.shoppingList[i] != null)) { | |
if (this.shoppingList[i].equals(item)) { | |
System.out.println("Item already in list. Quantity: " + quantity[i]); | |
break; | |
} | |
i++; | |
} | |
this.shoppingList[i] = item; | |
i = 0; | |
while (this.quantity[i] != 0) { | |
i++; | |
} | |
this.quantity[i] = num; | |
} | |
public void addTotalPrice( double price) { | |
totalPrice = totalPrice + price; | |
} | |
public void subtractTotalPrice( double price) { | |
totalPrice = totalPrice - price; | |
} | |
public double getTotalPrice () { | |
return totalPrice; | |
} | |
public String getItem (int listNum) { | |
return this.shoppingList[listNum]; | |
} | |
public void removeItem (int listNum) { | |
int quantityNum = listNum; | |
this.shoppingList[listNum] = null; | |
for (listNum = listNum + 1; listNum<this.shoppingList.length; listNum++) { | |
this.shoppingList[listNum-1]=this.shoppingList[listNum]; | |
} | |
for (quantityNum = quantityNum + 1; quantityNum<this.quantity.length; quantityNum++) { | |
this.quantity[quantityNum-1]=this.quantity[quantityNum]; | |
} | |
} | |
public void reduceQuantity(int quantityNum, int num, double price) { | |
if (num>quantity[quantityNum]) { | |
this.subtractTotalPrice(price*quantity[quantityNum]); | |
quantity[quantityNum] = 0; | |
this.removeItem(quantityNum); | |
this.removeAisle(quantityNum); | |
} else { | |
System.out.println("Quantity: " + quantity[quantityNum] + " | Subtract: " + num + " | Price: " + price); | |
quantity[quantityNum] = quantity[quantityNum]-num; | |
this.subtractTotalPrice(price*num); | |
} | |
} | |
public void increaseQuantity(int quantityNum, int num, double price) { | |
int difference = num - quantity[quantityNum]; | |
this.addTotalPrice(price*difference); | |
quantity[quantityNum] = num; | |
} | |
public int getQuantity (int listNum) { | |
return this.quantity[listNum]; | |
} | |
public void addAisle (int listNum, int aisleNum) { | |
int i = 0; | |
while (this.aisle[i] != -1) { | |
i++; | |
} | |
this.aisle[i] = aisleNum; | |
} | |
public void removeAisle (int listNum) { | |
this.aisle[listNum] = -1; | |
for (listNum = listNum + 1; listNum<this.aisle.length; listNum++) { | |
this.aisle[listNum-1]=this.aisle[listNum]; | |
} | |
} | |
public int getAisle (int listNum) { | |
return this.aisle[listNum]; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class CustomerTest{ | |
public static void main (String [] argv) { | |
Customer one = new Customer(); | |
int [][] stores = { //Made Up Zipcodes | |
{17020, 24}, | |
{17024, 22}, | |
{17048, 16}, | |
{17631, 19}}; | |
System.out.println("Enter Zipcode"); | |
Scanner store = new Scanner(System.in); //Scanner Initialized and Declared | |
int zip = store.nextInt(); | |
boolean hasZip = false; //Start of Zipcode Check | |
for (int i = 0; i<stores.length; i++) { | |
if(stores[i][0] == zip) { | |
hasZip = true; | |
System.out.println("Store Location Found for Zipcode: " + zip); | |
break; | |
} | |
} | |
if (hasZip == false) { | |
int i = 1; | |
while (hasZip==false) { | |
if(stores[i][0]>stores[i-1][0]) { | |
int prevZip = Math.abs(stores[i-1][0]-zip); | |
//System.out.println("prevZip:" + prevZip); | |
int afterZip = Math.abs(stores[i][0]-zip); | |
//System.out.println("afterZip:" + afterZip); | |
if (prevZip == afterZip) { | |
hasZip = true; | |
System.out.println("Closest store Location Found for Zipcode " + zip + ": " + stores[i-1][0] + " and " + stores[i][0]); | |
} else if (prevZip<afterZip) { | |
hasZip = true; | |
System.out.println("Closest store Location Found for Zipcode " + zip + ": " + stores[i-1][0]); | |
zip = stores[i-1][0]; | |
} else { | |
hasZip = true; | |
System.out.println("Closest store Location Found for Zipcode " + zip + ": " + stores[i][0]); | |
zip = stores[i][0]; | |
} | |
} | |
i=1+1; | |
} | |
} //End of Zipcode Check | |
String [] itemList = { //Made Up Items Names | |
"Water", | |
"Coffee", | |
"Bread"}; | |
double [] itemPrice = { //Made Up Items Prices | |
5.00, | |
10.00, | |
1.00}; | |
int [] aisle = new int [3]; | |
if (zip == 17020) { //Aisle of items per store | |
aisle[0]= 3; | |
aisle[1]= 2; | |
aisle[2]= 1; | |
} else if (zip == 17024) { | |
aisle[0]= 2; | |
aisle[1]= 1; | |
aisle[2]= 3; | |
} else if (zip == 17048) { | |
aisle[0]= 3; | |
aisle[1]= 1; | |
aisle[2]= 2; | |
} else { | |
aisle[0]= 1; | |
aisle[1]= 3; | |
aisle[2]= 2; | |
} | |
int action = -1; | |
String addItem = ""; | |
int itemListNum; //Index for Customer Object | |
int listNum; //Index for Checking Each Element of a List | |
Boolean complete = false; | |
double totalPrice = 0; | |
int quantity = 0; | |
while (complete == false) { | |
System.out.println("Would you like to (1)Add item, (2)Remove Item,(3)Print List, or (10)Complete list?"); | |
action = store.nextInt(); | |
if (action == 1) { //Add Item | |
System.out.println("Add Item: "); | |
addItem = store.next(); | |
itemListNum = findInList(itemList, addItem); | |
listNum = 0; | |
String changeQuantity = ""; | |
while (listNum<one.shoppingList.length) { | |
if (one.getItem(listNum)!=null) { | |
if (one.getItem(listNum).equals(addItem)){ | |
System.out.println("Item already in list. Quantity: " + one.quantity[listNum]); | |
System.out.println("Would you like to change the quantity? Y/N "); | |
changeQuantity = store.next(); | |
if(changeQuantity.equals("Y")) { | |
System.out.println("New Quantity: "); | |
quantity = store.nextInt(); | |
if (quantity>one.quantity[listNum]) { | |
one.increaseQuantity(listNum, quantity,itemPrice[itemListNum]); | |
System.out.println("ItemListNum: " + itemListNum); | |
one.addAisle(listNum, aisle[itemListNum]); | |
} else { | |
quantity = one.quantity[listNum]-quantity; | |
one.reduceQuantity(listNum,quantity,itemPrice[itemListNum]); | |
System.out.println("ItemListNum: " + itemListNum); | |
one.addAisle(listNum, aisle[itemListNum]); | |
} | |
} else { | |
continue; | |
} | |
} | |
} | |
listNum++; | |
} | |
if(!changeQuantity.equals("Y")) { | |
System.out.println("Quantity: "); | |
quantity = store.nextInt(); | |
listNum = 0; | |
while (listNum<itemList.length) { | |
//System.out.println(addItem + " " + itemList[listNum]); | |
if (addItem.equals(itemList[listNum])) { | |
one.addItem(addItem,quantity); | |
one.addTotalPrice(itemPrice[listNum]*quantity); | |
break; | |
} | |
listNum++; | |
} | |
} | |
System.out.println("Listnum: " + listNum); | |
System.out.println("Zip: "+ zip); | |
one.addAisle(listNum, aisle[listNum]); | |
} | |
if (action == 2) { //Remove Item | |
listNum = 0; | |
while (one.getItem(listNum) != null) { | |
System.out.println(listNum + " : " + one.getItem(listNum)); | |
listNum++; | |
} | |
System.out.println("Remove Which Item? (Choose List Number or '-1' to exit) "); | |
listNum = store.nextInt(); | |
if (listNum == -1) { | |
continue; | |
} | |
System.out.println("How many?: "); | |
quantity = store.nextInt(); | |
itemListNum = 0; | |
while (itemListNum<itemList.length) { | |
//System.out.println(addItem + " " + itemList[listNum]); | |
if (one.getItem(listNum).equals(itemList[itemListNum])) { | |
one.reduceQuantity(listNum,quantity,itemPrice[itemListNum]); | |
System.out.println("ItemListNum: " + itemListNum); | |
one.addAisle(listNum, aisle[itemListNum]); | |
break; | |
} | |
itemListNum++; | |
} | |
} | |
if (action == 3) { //Print List | |
listNum = 0; | |
while (one.getItem(listNum) != null) { | |
System.out.println(listNum + " : " + one.getItem(listNum) + " | Quantity: " + one.quantity[listNum] + " | Aisle: " + one.getAisle(listNum)); | |
listNum++; | |
} | |
System.out.println("Total Price: " + one.getTotalPrice()); | |
} | |
if (action == 10) { //Complete Transaction | |
complete = true; | |
} | |
} | |
System.out.println("Thank you for shopping at Shoprite!"); | |
} | |
public static int findInList (String [] list, String findMe) { | |
for (int i = 0; i<list.length; i++) { | |
//System.out.println("Item: " + list[i] + "Find Me: " + findMe); | |
if (list[i].equals(findMe)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment