Last active
October 11, 2018 13:02
-
-
Save vinnnc/41d1adf7fc38fd4a2dfab5b0d2d6a82b to your computer and use it in GitHub Desktop.
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.time.LocalDateTime; | |
public class Cart | |
{ | |
private LocalDateTime addTime; | |
private Product product; | |
private int quantity; | |
public Cart(LocalDateTime addTime, Product product, int quantity) | |
{ | |
this.addTime = addTime; | |
this.product = product; | |
this.quantity = quantity; | |
} | |
public LocalDateTime getAddTime() | |
{ | |
return addTime; | |
} | |
public void setAddTime(LocalDateTime addTime) | |
{ | |
this.addTime = addTime; | |
} | |
public Product getProduct() | |
{ | |
return product; | |
} | |
public void setProduct(Product product) | |
{ | |
this.product = product; | |
} | |
public int getQuantity() | |
{ | |
return quantity; | |
} | |
public void setQuantity(int quantity) | |
{ | |
this.quantity = quantity; | |
} | |
public String displayCart() | |
{ | |
return product.getProductName() + " x " + quantity + " = " + (product.getPrice() * quantity); | |
} | |
} |
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.io.BufferedWriter; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.time.Duration; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Controller | |
{ | |
private ArrayList<Cart> cartList; | |
private ArrayList<LocalDate> holidayList; | |
private String loginID; | |
private ArrayList<Product> productList; | |
private ArrayList<Shipment> shipmentList; | |
private ArrayList<Transaction> transactionList; | |
private UserInterface ui; | |
private ArrayList<User> userList; | |
public Controller() | |
{ | |
cartList = new ArrayList<>(); | |
holidayList = new ArrayList<>(); | |
holidayList.add(LocalDate.of(2018, 1, 1)); | |
holidayList.add(LocalDate.of(2018, 1, 26)); | |
holidayList.add(LocalDate.of(2018, 3, 30)); | |
holidayList.add(LocalDate.of(2018, 4, 2)); | |
holidayList.add(LocalDate.of(2018, 4, 25)); | |
holidayList.add(LocalDate.of(2018, 12, 24)); | |
holidayList.add(LocalDate.of(2018, 12, 25)); | |
holidayList.add(LocalDate.of(2018, 12, 26)); | |
loginID = "N"; | |
productList = new ArrayList<>(); | |
shipmentList = new ArrayList<>(); | |
transactionList = new ArrayList<>(); | |
ui = new UserInterface(); | |
userList = new ArrayList<>(); | |
initialisation(); | |
} | |
private void viewTransaction(char user) | |
{ | |
if (user == 'O') | |
ui.displayTransaction(transactionList); | |
else | |
{ | |
ArrayList<Transaction> userTransactions = new ArrayList<>(); | |
for (Transaction t : transactionList) | |
{ | |
if (t.getUserID().equals(loginID)) | |
userTransactions.add(t); | |
} | |
ui.displayTransaction(userTransactions); | |
} | |
} | |
private boolean isHoliday() | |
{ | |
LocalDate current = LocalDate.now(); | |
return holidayList.contains(current); | |
} | |
private void checkout() | |
{ | |
while (true) | |
{ | |
if (loginID.charAt(0) != 'C') | |
{ | |
String userID = loginID; | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); | |
int transactionID = Integer.valueOf(sdf.format(LocalDate.now())); | |
ArrayList<String> items = new ArrayList<String>(); | |
for (Cart item : cartList) | |
{ | |
String temp = item.getProduct().getProductName() + " " + item.getQuantity(); | |
items.add(temp); | |
} | |
double totalPrice = calculateTotal(); | |
Transaction t = new Transaction(items, totalPrice, transactionID, userID); | |
transactionList.add(t); | |
cartList.clear(); | |
break; | |
} else | |
{ | |
ui.displayMessage("askForLogin"); | |
ui.checkoutMenu(); | |
while (true) | |
{ | |
int input = integerInput(); | |
if (input == 1) | |
{ | |
register(); | |
break; | |
} else if (input == 2) | |
{ | |
// login(); | |
break; | |
} else if (input == 999) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
} | |
} | |
private void viewCart() | |
{ | |
for (Cart c : cartList) | |
{ | |
if (!isLock(c.getAddTime())) | |
cartList.remove(c); | |
} | |
ui.displayCart(cartList); | |
} | |
private void addToCart(Product product) | |
{ | |
ui.displayMessage("quantity"); | |
while (true) | |
{ | |
int quantity = integerInput(); | |
if (quantity > 0 && quantity < 999) | |
{ | |
if (totalQuantity(product) >= quantity) | |
{ | |
lockProduct(product, quantity); | |
Cart c = new Cart(LocalDateTime.now(), product, quantity); | |
cartList.add(c); | |
break; | |
} else | |
ui.displayMessage("notEnough"); | |
ui.displayQuantity(totalQuantity(product)); | |
} | |
if (quantity == 999) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
private void selectCartItem() | |
{ | |
boolean stop = false; | |
viewCart(); | |
int input = integerInput(); | |
while (!stop) | |
{ | |
if (0 < input && input <= cartList.size()) | |
{ | |
stop = true; | |
ui.cartMenu(); | |
int select = integerInput(); | |
boolean finish = false; | |
while (!finish) | |
{ | |
if (select == 999) | |
{ | |
finish = true; | |
ui.displayMessage("exit"); | |
} | |
if (select == 1) // edit quantity | |
{ | |
finish = true; | |
editCart(input - 1); | |
} | |
if (select == 2) // remove product | |
{ | |
finish = true; | |
removeFromCart(input - 1); | |
} | |
if (select == 3) | |
{ | |
finish = true; | |
checkout(); | |
} | |
else | |
ui.displayMessage("invalid"); | |
} | |
} | |
if (input == 999) | |
{ | |
stop = true; | |
ui.displayMessage("exit"); | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
private void removeFromCart(int index) | |
{ | |
int unlockQuantity = cartList.get(index).getQuantity(); | |
Product product = cartList.get(index).getProduct(); | |
unlockProduct(product, unlockQuantity); | |
cartList.remove(index); | |
ui.displayMessage("success"); | |
} | |
private void editCart(int index) | |
{ | |
ui.displayMessage("quantity"); | |
int quantity = integerInput(); | |
while (true) | |
{ | |
if (quantity > 0 && quantity != 999) | |
{ | |
int total = totalQuantity(cartList.get(index).getProduct()); | |
if (quantity > total) | |
{ | |
ui.displayQuantity(total); | |
ui.displayMessage("notEnough"); | |
} else | |
{ | |
Cart c = cartList.get(index); | |
unlockProduct(c.getProduct(), c.getQuantity()); | |
lockProduct(c.getProduct(), quantity); | |
c.setQuantity(quantity); | |
ui.displayMessage("success"); | |
break; | |
} | |
} | |
if (quantity == 999) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
private void mainMenu() | |
{ | |
while (true) | |
{ | |
char user = loginID.charAt(0); | |
if (user == 'N') // have not login | |
{ | |
ui.mainMenu(0); | |
switch (integerInput()) | |
{ | |
case 1: | |
selectProduct(productList); | |
break; | |
case 2: | |
searchProduct(); | |
break; | |
case 3: | |
selectCartItem(); | |
break; | |
case 4: register(); break; | |
// case 5: login(); break; | |
case 999: exit(); break; | |
default: | |
ui.displayMessage("invalid"); | |
} | |
} else if (user == 'O') // owner login | |
{ | |
ui.mainMenu(1); | |
switch (integerInput()) | |
{ | |
case 1: | |
selectProduct(productList); | |
break; | |
case 2: | |
searchProduct(); | |
break; | |
case 3: | |
selectCartItem(); | |
break; | |
// case 4: changePassword(user); break; | |
// case 5: logout(); break; | |
case 6: | |
addProduct(); | |
break; | |
case 7: | |
selectProduct(productList); | |
break; | |
case 8: | |
selectProduct(productList); | |
break; | |
case 9: | |
viewTransaction(user); | |
break; | |
// case 10: removeCustomer(); break; | |
case 999: exit(); break; | |
default: | |
ui.displayMessage("invalid"); | |
} | |
} else // customer login | |
{ | |
ui.mainMenu(2); | |
switch (integerInput()) | |
{ | |
case 1: | |
selectProduct(productList); | |
break; | |
case 2: | |
searchProduct(); | |
break; | |
case 3: | |
selectCartItem(); | |
break; | |
// case 4: changePassword(user); break; | |
// case 5: logout(); break; | |
case 6: | |
viewTransaction(user); | |
break; | |
// case 7: unregister(); break; | |
case 999: exit(); break; | |
default: | |
ui.displayMessage("invalid"); | |
} | |
} | |
} | |
} | |
private boolean isLock(LocalDateTime time) | |
{ | |
double duration = Duration.between(time, LocalDateTime.now()).toMinutes(); | |
return duration < 600; | |
} | |
private int integerInput() | |
{ | |
String input = ""; | |
while (true) | |
{ | |
input = ui.getInput(); | |
if (input.equals("")) | |
ui.displayMessage("invalid"); | |
else if (input.matches("^[0-9]+$")) | |
break; | |
else | |
ui.displayMessage("invalid"); | |
} | |
return Integer.valueOf(input); | |
} | |
private int totalQuantity(Product product) | |
{ | |
int total = 0; | |
for (Shipment s : shipmentList) | |
{ | |
if (s.getProductID() == product.getProductID()) | |
total += s.getUnlockedQuantity(); | |
} | |
return total; | |
} | |
private void lockProduct(Product product, int quantity) | |
{ | |
int addToCart = quantity; | |
for (Shipment s : shipmentList) | |
{ | |
if (s.getProductID() == product.getProductID()) | |
{ | |
if (s.getUnlockedQuantity() >= addToCart) | |
{ | |
s.setUnlockedQuantity(s.getUnlockedQuantity() - addToCart); | |
break; | |
} | |
if (s.getUnlockedQuantity() < addToCart) | |
{ | |
addToCart -= s.getUnlockedQuantity(); | |
s.setUnlockedQuantity(0); | |
} | |
} | |
} | |
} | |
private void unlockProduct(Product product, int quantity) | |
{ | |
int unlockQuantity = quantity; | |
for (int i = shipmentList.size() - 1; i >= 0; i--) | |
{ | |
Shipment s = shipmentList.get(i); | |
if (s.getProductID() == product.getProductID() && s.getUnlockedQuantity() <= s.getTotalQuantity()) | |
{ | |
int difference = s.getTotalQuantity() - s.getUnlockedQuantity(); | |
if (difference > unlockQuantity) | |
{ | |
s.setUnlockedQuantity(s.getUnlockedQuantity() + unlockQuantity); | |
break; | |
} else | |
{ | |
s.setUnlockedQuantity(s.getTotalQuantity()); | |
unlockQuantity -= difference; | |
} | |
} | |
} | |
} | |
private void searchProduct() | |
{ | |
ui.displayMessage("search"); | |
while (true) | |
{ | |
String input = ui.getInput(); | |
if (input.equals("")) | |
{ | |
ui.displayMessage("invalid"); | |
} else if (input.equals("999")) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
{ | |
ArrayList<Product> searchList = new ArrayList<>(); | |
for (Product p : productList) | |
{ | |
if (p.getProductName().contains(input) || p.getCategory().contains(input)) | |
searchList.add(p); | |
} | |
ui.sortMenu(); | |
while (true) | |
{ | |
int select = integerInput(); | |
if (select == 1 || select == 2) | |
{ | |
selectProduct(sortProduct(searchList, select)); | |
break; | |
} else if (select == 999) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
break; | |
} | |
} | |
} | |
private ArrayList<Product> sortProduct(ArrayList<Product> list, int type) | |
{ | |
for (int i = 0; i < list.size(); i++) | |
{ | |
for (int j = 1; j < list.size() - 1; j++) | |
{ | |
// type 1: sort by price; type2: sort by name; | |
if ((type == 1 && list.get(j - 1).getPrice() > list.get(j).getPrice()) || | |
(type == 2 && list.get(j - 1).getProductName().compareTo(list.get(j).getProductName()) > 0)) | |
{ | |
Product temp = list.get(j - 1); | |
list.set(j - 1, list.get(j)); | |
list.set(j, temp); | |
} | |
} | |
} | |
return list; | |
} | |
private void selectProduct(ArrayList<Product> list) | |
{ | |
ui.displayProduct(list); | |
while (true) | |
{ | |
int index = integerInput() - 1; | |
if (index < list.size() && index >= 0) | |
{ | |
ui.productMenu(loginID.charAt(0)); | |
while (true) | |
{ | |
int option = integerInput(); | |
if (loginID.charAt(0) == 'O') | |
{ | |
if (option == 1) | |
{ | |
addToCart(list.get(index)); | |
break; | |
} else if (option == 2 || option == 999) | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} else | |
{ | |
if (option == 1) | |
{ | |
addShipment(list.get(index)); | |
break; | |
} else if (option == 2) | |
{ | |
editProduct(list.get(index)); | |
break; | |
} else if (option == 3) | |
{ | |
removeProduct(list.get(index)); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
break; | |
} else if (index == 998) // because index minus 1 | |
{ | |
ui.displayMessage("exit"); | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
} | |
private String createUserID() | |
{ | |
int index = 1001; | |
String userID = ""; | |
while (true) | |
{ | |
userID = "C" + index; | |
if (duplicateValidation(userID, "userID")) | |
index++; | |
else | |
return userID; | |
} | |
} | |
private boolean passwordValidation(String p) | |
{ | |
boolean pass1 = false; | |
boolean pass2 = false; | |
boolean pass3 = false; | |
boolean pass4 = false; | |
Pattern pa1 = Pattern.compile("[0-9]"); | |
Matcher m1 = pa1.matcher(p); | |
String regEx = "[`~!@#$%^&*()+=|{}':;,.<>/?]"; | |
Pattern pa2 = Pattern.compile(regEx); | |
Matcher m2 = pa2.matcher(p); | |
if (p.length() >= 8) | |
pass4 = true; | |
for (int i = 0; i < p.length(); i++) | |
{ | |
char c = p.charAt(i); | |
if (Character.isUpperCase(c)) | |
pass1 = true; | |
if (m1.find()) | |
pass2 = true; | |
if (m2.find()) | |
pass3 = true; | |
} | |
return (pass1 && pass2 && pass3 && pass4); | |
} | |
private void addProduct() | |
{ | |
ui.productInformation("name"); | |
String productName = ui.getInput(); | |
while (duplicateValidation(productName, "productName")) | |
{ | |
ui.displayMessage("warnDuplication"); | |
productName = ui.getInput(); | |
} | |
ui.productInformation("category"); | |
String category = ui.getInput(); | |
ui.productInformation("description"); | |
String description = ui.getInput(); | |
ui.productInformation("packaging"); | |
String packaging = ""; | |
while (true) | |
{ | |
int input = integerInput(); | |
if (input == 1) | |
{ | |
packaging = "KG"; | |
break; | |
} else if (input == 2) | |
{ | |
packaging = "package"; | |
break; | |
} else if (input == 3) | |
{ | |
packaging = "quantity"; | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
ui.displayMessage("price"); | |
String input = String.valueOf(integerInput()); | |
int size = input.length(); | |
double price = Double.valueOf(input.substring(0, size - 2) + "." + input.substring(size - 2)); | |
int productID = createProductID(); | |
Product p = new Product(category, description, packaging, price, productID, productName); | |
productList.add(p); | |
} | |
private void addShipment(Product p) | |
{ | |
String dateOfShipment = LocalDate.now().toString(); | |
ui.shipmentInformation("duration"); | |
int expireDuration = integerInput(); | |
ui.shipmentInformation("quantity"); | |
int totalQuantity = integerInput(); | |
int shipmentID = createShipmentID(); | |
int productID = p.getProductID(); | |
Shipment s = new Shipment(dateOfShipment, expireDuration, productID, shipmentID, totalQuantity); | |
shipmentList.add(s); | |
} | |
private void editProduct(Product product) | |
{ | |
boolean stop = false; | |
int index = productList.indexOf(product); | |
while (!stop) | |
{ | |
ui.selectProductAttribute(); | |
switch (integerInput()) | |
{ | |
case 1: | |
ui.productInformation("category"); | |
String category = ui.getInput(); | |
productList.get(index).setCategory(category); | |
ui.displayMessage("success"); | |
break; | |
case 2: | |
ui.productInformation("description"); | |
String description = ui.getInput(); | |
productList.get(index).setDescription(description); | |
ui.displayMessage("success"); | |
break; | |
case 3: | |
ui.productInformation("packaging"); | |
String packaging = ""; | |
while (true) | |
{ | |
int input = integerInput(); | |
if (input == 1) | |
{ | |
packaging = "KG"; | |
break; | |
} else if (input == 2) | |
{ | |
packaging = "package"; | |
break; | |
} else if (input == 3) | |
{ | |
packaging = "quantity"; | |
break; | |
} else | |
ui.displayMessage("invalid"); | |
} | |
productList.get(index).setPackaging(packaging); | |
ui.displayMessage("success"); | |
break; | |
case 4: | |
ui.productInformation("price"); | |
String input = String.valueOf(integerInput()); | |
int size = input.length(); | |
double price = Double.valueOf(input.substring(0, size - 2) + "." + input.substring(size - 2)); | |
productList.get(index).setPrice(price); | |
ui.displayMessage("success"); | |
break; | |
case 5: | |
ui.productInformation("name"); | |
String productName = ui.getInput(); | |
productList.get(index).setProductName(productName); | |
break; | |
case 999: | |
ui.displayMessage("exit"); | |
stop = true; | |
break; | |
default: | |
ui.displayMessage("invalid"); | |
} | |
} | |
} | |
private void removeProduct(Product product) | |
{ | |
for (Shipment s : shipmentList) | |
{ | |
if (s.getProductID() == product.getProductID()) | |
shipmentList.remove(s); | |
} | |
productList.remove(product); | |
ui.displayMessage("success"); | |
} | |
private int createProductID() | |
{ | |
int index = 1001; | |
while (!duplicateValidation(""+index, "productID")) | |
index++; | |
return index; | |
} | |
private int createShipmentID() | |
{ | |
int index = 1001; | |
while (!duplicateValidation(""+index, "shipmentID")) | |
index++; | |
return index; | |
} | |
private double calculateTotal() | |
{ | |
double totalPrice = 0; | |
double[] eachPrice = new double[cartList.size()]; | |
int index = 0; | |
for (Cart c : cartList) | |
{ | |
double expireDiscount = 1; | |
LocalDate expire = LocalDate.now(); | |
label1: | |
for (Shipment s : shipmentList) | |
{ | |
if (c.getProduct().getProductID() == s.getProductID() && s.getUnlockedQuantity() > 0) | |
{ | |
String[] date = s.getDateOfShipment().split("-"); | |
int add = s.getExpireDuration(); | |
expire = LocalDate.of(Integer.valueOf(date[0]), Integer.valueOf(date[1]), | |
Integer.valueOf(date[2])).plusDays(add); | |
break label1; | |
} | |
} | |
Duration duration = Duration.between(expire, LocalDate.now()); | |
if (duration.toDays() <= 2) | |
expireDiscount *= 0.8; | |
eachPrice[index] = c.getProduct().getPrice() * c.getQuantity() * expireDiscount; | |
index++; | |
} | |
for (double p : eachPrice) | |
totalPrice += p; | |
if (isHoliday()) | |
totalPrice *= 0.8; | |
if (totalPrice > 100) | |
{ | |
for (User u : userList) | |
{ | |
if (u.getUserID().equals(loginID) && u.isFirstDiscount()) | |
{ | |
u.setFirstDiscount(false); | |
totalPrice *= 0.9; | |
break; | |
} | |
} | |
} | |
return totalPrice; | |
} | |
private void register() | |
{ | |
ui.userInformation("askEmail"); | |
String tempEmail = ui.getInput(); | |
while (duplicateValidation(tempEmail, "email")) | |
{ | |
ui.userInformation("warnDuplication"); | |
tempEmail = ui.getInput(); | |
} | |
ui.userInformation("askPassword"); | |
String password = ui.getInput(); | |
while (!passwordValidation(password)) | |
{ | |
ui.userInformation("passwordWarn"); | |
ui.userInformation("askPassword"); | |
password = ui.getInput(); | |
} | |
ui.userInformation("confirmPassword"); | |
String password2 = ui.getInput(); | |
while (!password.equals(password2)) | |
{ | |
ui.userInformation("passwordDiffer"); | |
ui.userInformation("confirmPassword"); | |
password2 = ui.getInput(); | |
} | |
ui.userInformation("askAddress"); | |
String address = ui.getInput(); | |
ui.userInformation("askQA"); | |
ui.userInformation("setQ1"); | |
String Q1 = ui.getInput(); | |
ui.userInformation("setA1"); | |
String A1 = ui.getInput(); | |
ui.userInformation("setQ2"); | |
String Q2 = ui.getInput(); | |
ui.userInformation("setA2"); | |
String A2 = ui.getInput(); | |
String[] securityQA = {Q1, A1, Q2, A2}; | |
//////////////////////////////////////////////String userID = createUserID(); | |
String userID = "O1001"; | |
userList.add(new Customer(tempEmail, password, userID, address, true, securityQA)); | |
loginID = userID; | |
} | |
private boolean duplicateValidation(String item, String type) | |
{ | |
switch (type) | |
{ | |
case "email": | |
ArrayList<String> emails = new ArrayList<>(); | |
for (User u: userList) | |
emails.add(u.getEmail()); | |
return emails.contains(item); | |
case "userID": | |
ArrayList<String> userIDs = new ArrayList<>(); | |
for (User u: userList) | |
userIDs.add(u.getUserID()); | |
return userIDs.contains(item); | |
case "productID": | |
int productID = Integer.valueOf(item); | |
ArrayList<Integer> productIDs = new ArrayList<>(); | |
for (Product p: productList) | |
productIDs.add(p.getProductID()); | |
return productIDs.contains(productID); | |
case "shipmentID": | |
int shipmentID = Integer.valueOf(item); | |
ArrayList<Integer> shipmentIDs = new ArrayList<>(); | |
for (Shipment p: shipmentList) | |
shipmentIDs.add(p.getShipmentID()); | |
return shipmentIDs.contains(shipmentID); | |
case "productName": | |
ArrayList<String> productNames = new ArrayList<>(); | |
for (Product p: productList) | |
productNames.add(p.getProductName()); | |
return productNames.contains(item); | |
} | |
return true; | |
} | |
private void exit() | |
{ | |
ArrayList<String> arr1 = new ArrayList<>(); | |
ArrayList<String> arr2 = new ArrayList<>(); | |
ArrayList<String> arr3 = new ArrayList<>(); | |
ArrayList<String> arr4 = new ArrayList<>(); | |
for (User anUserList : userList) | |
arr1.add(anUserList.displayUser()); | |
for (Transaction aTransactionList : transactionList) | |
arr2.add(aTransactionList.displayTransaction1()); | |
for (Product aProductList : productList) | |
arr3.add(aProductList.displayProduct1()); | |
for (Shipment aShipmentList : shipmentList) | |
arr4.add(aShipmentList.displayShipment1()); | |
String[] arr11 = new String[arr1.size()]; | |
arr11 = arr1.toArray(arr11); | |
String[] arr22 = new String[arr2.size()]; | |
arr22 = arr2.toArray(arr22); | |
String[] arr33 = new String[arr3.size()]; | |
arr33 = arr3.toArray(arr33); | |
String[] arr44 = new String[arr4.size()]; | |
arr44 = arr4.toArray(arr44); | |
try | |
{ | |
write("User.txt", arr11); | |
write("Transaction.txt", arr22); | |
write("Product.txt", arr33); | |
write("Shipment.txt", arr44); | |
} catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
ui.displayMessage("exit"); | |
System.exit(0); | |
} | |
} | |
private void write(String filename, String[] array) throws IOException | |
{ | |
FileWriter fw = null; | |
fw = new FileWriter(filename, false); | |
BufferedWriter bw = new BufferedWriter(fw); | |
for (String arr : array) | |
{ | |
bw.write(arr + "\t\n"); | |
} | |
bw.close(); | |
fw.close(); | |
} | |
private void initialisation() | |
{ | |
String[] filename = {"User.txt", "Transaction.txt", "Product.txt", "Shipment.txt"}; | |
for (int i = 0; i <= 3; i++) | |
{ | |
try | |
{ | |
FileReader file = new FileReader(filename[i]); | |
Scanner parser = new Scanner(file); | |
String oneInfo = parser.nextLine(); | |
boolean firstUser = true; | |
while (parser.hasNextLine()) | |
{ | |
if (i == 0) | |
{ | |
if (firstUser) | |
{ | |
String temp[] = oneInfo.split(";"); | |
userList.add(new Owner(temp[0], temp[1], temp[2])); | |
firstUser = false; | |
} else | |
{ | |
boolean firstDiscount = true; | |
String temp[] = oneInfo.split(";"); | |
if (temp[4].equals("1")) | |
{ | |
firstDiscount = false; | |
} | |
String[] qA = {temp[5], temp[6], temp[7], temp[8]}; | |
userList.add(new Customer(temp[0], temp[1], temp[2], temp[3], firstDiscount, qA)); | |
} | |
} else if (i == 1) | |
{ | |
String temp[] = oneInfo.split(";"); | |
ArrayList<String> items = new ArrayList<>(); | |
int length = temp.length; | |
for (int a = 0; a < length - 2; a++) | |
{ | |
items.add(temp[i]); | |
} | |
transactionList.add(new Transaction(items, Double.valueOf(temp[length - 3]), | |
Integer.valueOf(temp[length - 2]), temp[length - 1])); | |
} else if (i == 2) | |
{ | |
String temp[] = oneInfo.split(";"); | |
productList.add(new Product(temp[0], temp[1], temp[2], Double.valueOf(temp[3]), | |
Integer.valueOf(temp[4]), temp[5])); | |
} else | |
{ | |
String temp[] = oneInfo.split(";"); | |
shipmentList.add(new Shipment(temp[0], Integer.valueOf(temp[1]), Integer.valueOf(temp[2]), | |
Integer.valueOf(temp[3]), Integer.valueOf(temp[4]))); | |
} | |
oneInfo = parser.nextLine(); | |
} | |
} catch (IOException exception) | |
{ | |
ui.displayMessage("ioError"); | |
} | |
finally | |
{ | |
mainMenu(); | |
} | |
} | |
} | |
} |
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 extends User | |
{ | |
private String address; | |
private boolean firstDiscount; | |
private String[] scurityQA; | |
public Customer(String email, String password, String userID, String address, boolean firstDiscount, String[] arr) | |
{ | |
super(email, password, userID); | |
this.address = address; | |
this.firstDiscount = firstDiscount; | |
this.scurityQA = arr; | |
} | |
public String getAddress() | |
{ | |
return address; | |
} | |
public void setAddress(String address) | |
{ | |
this.address = address; | |
} | |
public boolean isFirstDiscount() | |
{ | |
return firstDiscount; | |
} | |
public void setFirstDiscount(boolean firstDiscount) | |
{ | |
this.firstDiscount = firstDiscount; | |
} | |
public String[] getQA() | |
{ | |
return scurityQA; | |
} | |
public void setQA(String[] arr) | |
{ | |
this.scurityQA = arr; | |
} | |
public String displayUser() | |
{ | |
int a = 0; | |
if(firstDiscount == false) | |
{ | |
a = 1; | |
} | |
return getEmail() + ";" + getPassword() + ";" + getUserID() + ";" + getAddress() + ";" + a + ";" + scurityQA[0] | |
+ ";" + scurityQA[1] + ";" + scurityQA[2] + ";" + scurityQA[3] + ";" ; | |
} | |
} |
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 Owner extends User | |
{ | |
public Owner(String email, String password, String userID) | |
{ | |
super(email, password, userID); | |
} | |
} |
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
#BlueJ package file | |
dependency1.from=UserInterface | |
dependency1.to=Transaction | |
dependency1.type=UsesDependency | |
dependency10.from=Controller | |
dependency10.to=Customer | |
dependency10.type=UsesDependency | |
dependency11.from=Controller | |
dependency11.to=Owner | |
dependency11.type=UsesDependency | |
dependency12.from=Cart | |
dependency12.to=Product | |
dependency12.type=UsesDependency | |
dependency13.from=Test | |
dependency13.to=Controller | |
dependency13.type=UsesDependency | |
dependency2.from=UserInterface | |
dependency2.to=Cart | |
dependency2.type=UsesDependency | |
dependency3.from=UserInterface | |
dependency3.to=Product | |
dependency3.type=UsesDependency | |
dependency4.from=Controller | |
dependency4.to=Cart | |
dependency4.type=UsesDependency | |
dependency5.from=Controller | |
dependency5.to=Product | |
dependency5.type=UsesDependency | |
dependency6.from=Controller | |
dependency6.to=Shipment | |
dependency6.type=UsesDependency | |
dependency7.from=Controller | |
dependency7.to=Transaction | |
dependency7.type=UsesDependency | |
dependency8.from=Controller | |
dependency8.to=UserInterface | |
dependency8.type=UsesDependency | |
dependency9.from=Controller | |
dependency9.to=User | |
dependency9.type=UsesDependency | |
editor.fx.0.height=739 | |
editor.fx.0.width=816 | |
editor.fx.0.x=-32000 | |
editor.fx.0.y=-32000 | |
objectbench.height=93 | |
objectbench.width=760 | |
package.divider.horizontal=0.6 | |
package.divider.vertical=0.8 | |
package.editor.height=393 | |
package.editor.width=652 | |
package.editor.x=469 | |
package.editor.y=240 | |
package.frame.height=600 | |
package.frame.width=800 | |
package.numDependencies=13 | |
package.numTargets=10 | |
package.showExtends=true | |
package.showUses=true | |
project.charset=windows-1252 | |
readme.height=58 | |
readme.name=@README | |
readme.width=47 | |
readme.x=10 | |
readme.y=10 | |
target1.height=50 | |
target1.name=Owner | |
target1.showInterface=false | |
target1.type=ClassTarget | |
target1.width=80 | |
target1.x=30 | |
target1.y=120 | |
target10.height=50 | |
target10.name=Cart | |
target10.showInterface=false | |
target10.type=ClassTarget | |
target10.width=80 | |
target10.x=160 | |
target10.y=220 | |
target2.height=50 | |
target2.name=User | |
target2.showInterface=false | |
target2.type=ClassTarget | |
target2.width=80 | |
target2.x=100 | |
target2.y=30 | |
target3.height=50 | |
target3.name=Transaction | |
target3.showInterface=false | |
target3.type=ClassTarget | |
target3.width=100 | |
target3.x=20 | |
target3.y=260 | |
target4.height=50 | |
target4.name=Customer | |
target4.showInterface=false | |
target4.type=ClassTarget | |
target4.width=80 | |
target4.x=160 | |
target4.y=120 | |
target5.height=50 | |
target5.name=Test | |
target5.showInterface=false | |
target5.type=ClassTarget | |
target5.width=80 | |
target5.x=540 | |
target5.y=140 | |
target6.height=50 | |
target6.name=Product | |
target6.showInterface=false | |
target6.type=ClassTarget | |
target6.width=80 | |
target6.x=540 | |
target6.y=30 | |
target7.height=50 | |
target7.name=Shipment | |
target7.showInterface=false | |
target7.type=ClassTarget | |
target7.width=80 | |
target7.x=540 | |
target7.y=240 | |
target8.height=50 | |
target8.name=UserInterface | |
target8.showInterface=false | |
target8.type=ClassTarget | |
target8.width=110 | |
target8.x=310 | |
target8.y=310 | |
target9.height=50 | |
target9.name=Controller | |
target9.showInterface=false | |
target9.type=ClassTarget | |
target9.width=90 | |
target9.x=320 | |
target9.y=110 |
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 Product | |
{ | |
private String category; | |
private String description; | |
private String packaging; | |
private double price; | |
private int productID; | |
private String productName; | |
public Product(String category, String description, String packaging, double price, int productID, String productName) | |
{ | |
this.category = category; | |
this.description = description; | |
this.packaging = packaging; | |
this.price = price; | |
this.productID = productID; | |
this.productName = productName; | |
} | |
public String getCategory() | |
{ | |
return category; | |
} | |
public void setCategory(String category) | |
{ | |
this.category = category; | |
} | |
public String getDescription() | |
{ | |
return description; | |
} | |
public void setDescription(String description) | |
{ | |
this.description = description; | |
} | |
public String getPackaging() | |
{ | |
return packaging; | |
} | |
public void setPackaging(String packaging) | |
{ | |
this.packaging = packaging; | |
} | |
public double getPrice() | |
{ | |
return price; | |
} | |
public void setPrice(double price) | |
{ | |
this.price = price; | |
} | |
public int getProductID() | |
{ | |
return productID; | |
} | |
public void setProductID(int productID) | |
{ | |
this.productID = productID; | |
} | |
public String getProductName() | |
{ | |
return productName; | |
} | |
public void setProductName(String productName) | |
{ | |
this.productName = productName; | |
} | |
public String displayProduct() | |
{ | |
return "Product name:" + productName + "; price: " + price + "; packaging: " + packaging + "; \n description: " | |
+ description; | |
} | |
public String displayProduct1() | |
{ | |
return productName + "; " + price + ";" + packaging + "; "+ description + ";" ; | |
} | |
} |
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
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 Shipment | |
{ | |
; | |
private String dateOfShipment; | |
private int expireDuration; | |
private int productID; | |
private int shipmentID; | |
private int totalQuantity; | |
private int unlockedQuantity; | |
public Shipment(String dateOfShipment, int expireDuration, int productID, int shipmentID, int totalQuantity) | |
{ | |
this.dateOfShipment = dateOfShipment; | |
this.expireDuration = expireDuration; | |
this.productID = productID; | |
this.shipmentID = shipmentID; | |
this.totalQuantity = totalQuantity; | |
this.unlockedQuantity = totalQuantity; | |
} | |
public String getDateOfShipment() | |
{ | |
return dateOfShipment; | |
} | |
public void setDateOfShipment(String dateOfShipment) | |
{ | |
this.dateOfShipment = dateOfShipment; | |
} | |
public int getExpireDuration() | |
{ | |
return expireDuration; | |
} | |
public void setExpireDuration(int expireDuration) | |
{ | |
this.expireDuration = expireDuration; | |
} | |
public int getUnlockedQuantity() | |
{ | |
return unlockedQuantity; | |
} | |
public void setUnlockedQuantity(int unlockedQuantity) | |
{ | |
this.unlockedQuantity = unlockedQuantity; | |
} | |
public int getProductID() | |
{ | |
return productID; | |
} | |
public void setProductID(int productID) | |
{ | |
this.productID = productID; | |
} | |
public int getShipmentID() | |
{ | |
return shipmentID; | |
} | |
public void setShipmentID(int shipmentID) | |
{ | |
this.shipmentID = shipmentID; | |
} | |
public int getTotalQuantity() | |
{ | |
return totalQuantity; | |
} | |
public void setTotalQuantity(int totalQuantity) | |
{ | |
this.totalQuantity = totalQuantity; | |
} | |
public String displayShipment1() | |
{ | |
return dateOfShipment + ";" + expireDuration + ";" + productID + ";" + shipmentID + ";" + totalQuantity + ";" + unlockedQuantity + ";" ; | |
} | |
} |
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
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 Test | |
{ | |
public static void main(String[] args) | |
{ | |
new Controller(); | |
} | |
public void test(String[] args) | |
{ | |
System.out.println("SSS"); | |
} | |
} |
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.ArrayList; | |
public class Transaction | |
{ | |
private ArrayList<String> items; | |
private double totalPrice; | |
private int transactionID; | |
private String userID; | |
public Transaction(ArrayList<String> items, double totalPrice, int transactionID, String userID) | |
{ | |
this.items = items; | |
this.totalPrice = totalPrice; | |
this.transactionID = transactionID; | |
this.userID = userID; | |
} | |
public ArrayList<String> getItems() | |
{ | |
return items; | |
} | |
public void setItems(ArrayList<String> items) | |
{ | |
this.items = items; | |
} | |
public double getTotalPrice() | |
{ | |
return totalPrice; | |
} | |
public void setTotalPrice(double totalPrice) | |
{ | |
this.totalPrice = totalPrice; | |
} | |
public int getTransactionID() | |
{ | |
return transactionID; | |
} | |
public void setTransactionID(int transactionID) | |
{ | |
this.transactionID = transactionID; | |
} | |
public String getUserID() | |
{ | |
return userID; | |
} | |
public void setUserID(String userID) | |
{ | |
this.userID = userID; | |
} | |
public String displayTransaction() | |
{ | |
String transaction = ""; | |
transaction += "Transaction ID: " + transactionID + ";User ID: " + userID + ";items: \n"; | |
for (String item : items) | |
{ | |
transaction += item + ";\n"; | |
} | |
return transaction; | |
} | |
public String displayTransaction1() | |
{ | |
String transaction = ""; | |
transaction += transactionID + ";" + userID + ";"; | |
for (String item : items) | |
{ | |
transaction += item + ";"; | |
} | |
return transaction; | |
} | |
} |
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
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 User | |
{ | |
private String email; | |
private String password; | |
private String userID; | |
public User(String email, String password, String userID) | |
{ | |
this.email = email; | |
this.password = password; | |
this.userID = userID; | |
} | |
public String getEmail() | |
{ | |
return email; | |
} | |
public void setEmail(String email) | |
{ | |
this.email = email; | |
} | |
public String getPassword() | |
{ | |
return password; | |
} | |
public void setPassword(String password) | |
{ | |
this.password = password; | |
} | |
public String getUserID() | |
{ | |
return userID; | |
} | |
public void setUserID(String userID) | |
{ | |
this.userID = userID; | |
} | |
public boolean isFirstDiscount() | |
{ | |
return false; | |
} | |
public void setFirstDiscount(boolean boo) | |
{ | |
} | |
public String displayUser() | |
{ | |
return email + ";" + password + ";" + userID; | |
} | |
} |
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
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.ArrayList; | |
import java.util.Scanner; | |
public class UserInterface | |
{ | |
public UserInterface() | |
{ | |
} | |
public void displayTransaction(ArrayList<Transaction> list) | |
{ | |
if (list.size() > 0) | |
{ | |
for (Transaction transaction: list) | |
{ | |
String[] temp = transaction.displayTransaction().split(";"); | |
for (String s : temp) | |
System.out.println(s); | |
} | |
} | |
else | |
System.out.println("There is no transaction."); | |
} | |
public void displayCart(ArrayList<Cart> list) | |
{ | |
if (list.size() > 0) | |
{ | |
int i = 1; | |
for (Cart c : list) | |
{ | |
System.out.println(i + ". " + c.displayCart()); | |
i++; | |
} | |
} | |
else | |
System.out.println("There is no item in cart. Please enter 999 to return."); | |
} | |
public void displayProduct(ArrayList<Product> list) | |
{ | |
if (list.size() > 0) | |
{ | |
int i = 1; | |
for (Product p : list) | |
{ | |
System.out.println(i + ". " + p.displayProduct()); | |
i++; | |
} | |
} | |
else | |
System.out.println("There is no product. Please enter 999 to return."); | |
} | |
public void displayMessage(String type) | |
{ | |
switch(type) | |
{ | |
case "invalid": System.out.println("Invalid input, please enter again! (999 to exit)"); break; | |
case "exit": System.out.println("You have exit!"); break; | |
case "select": System.out.println("Please input number to select:"); break; | |
case "quantity": System.out.println("Please in put the quantity: (999 to exit)"); break; | |
case "success": System.out.println("Action Success."); break; | |
case "notEnough": System.out.println("Product quantity is not enough!"); break; | |
case "search": System.out.println("Please enter the name of product:"); break; | |
case "askForLogin": System.out.println("Please login before checkout."); break; | |
case "ioError": System.out.println("Unexpected I/O error occurred!"); | |
} | |
} | |
public void sortMenu() | |
{ | |
System.out.println("1. Sort by name"); | |
System.out.println("2. Sort by price"); | |
} | |
public void cartMenu() | |
{ | |
System.out.println("1. Edit the quantity"); | |
System.out.println("2. Remove this product"); | |
} | |
public void mainMenu(int type) | |
{ | |
System.out.println("1. View product"); | |
System.out.println("2. Search product"); | |
System.out.println("3. Show cart"); | |
if (type == 0) // have not login | |
{ | |
System.out.println("4. Register"); | |
System.out.println("5. Login"); | |
} | |
if (type == 1) // owner login | |
{ | |
System.out.println("4. Change password"); | |
System.out.println("5. Logout"); | |
System.out.println("6. Create product"); | |
System.out.println("7. Edit product"); | |
System.out.println("8. Remove product"); | |
System.out.println("9. View transaction"); | |
System.out.println("10. Remove registered customers"); | |
} | |
if (type == 2) // customer login | |
{ | |
System.out.println("4. Change password"); | |
System.out.println("5. Logout"); | |
System.out.println("6. View transaction"); | |
System.out.println("7. Unregister"); | |
} | |
System.out.println("999. Exit the MFV"); | |
} | |
public String getInput() | |
{ | |
Scanner sc = new Scanner(System.in); | |
return sc.nextLine(); | |
} | |
public boolean askForComfirmation() | |
{ | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Please enter \"Confirm\" or \"confirm\" to confirmation (others input will cancel to " + | |
"action): "); | |
return sc.nextLine().trim().toLowerCase().equals("confirm"); | |
} | |
public void displayQuantity(int quantity) | |
{ | |
System.out.println("Quantity: " + quantity); | |
} | |
public void productMenu(char user) | |
{ | |
if (user == 'O') | |
{ | |
System.out.println("1. Add new shipment."); | |
System.out.println("2. Edit this product."); | |
System.out.println("3. Remove this product."); | |
System.out.println("4. Return to main menu."); | |
} | |
else | |
{ | |
System.out.println("1. Add to cart"); | |
System.out.println("2. Return to main menu"); | |
} | |
} | |
public void checkoutMenu() | |
{ | |
System.out.println("1. Register"); | |
System.out.println("2. Login"); | |
} | |
public void productInformation(String type) | |
{ | |
switch (type) | |
{ | |
case "name": System.out.println("Please input product name:"); break; | |
case "warnDuplication" : System.out.println("This product already exists, please change!");break; | |
case "category": System.out.println("Please input product category:"); break; | |
case "description": System.out.println("Please input product description:"); break; | |
case "packaging": System.out.println("Please select type of packaging:"); | |
System.out.println("1. packaging per KG"); | |
System.out.println("2. packaging per package"); | |
System.out.println("3. packaging per quantity"); break; | |
case "price": System.out.println("Please input product price: (unit: cents)"); break; | |
} | |
} | |
public void shipmentInformation(String type) | |
{ | |
switch (type) | |
{ | |
case "dos": System.out.println("Please input date of shipment:"); break; | |
case "duration": System.out.println("Please input expire duration days:"); break; | |
case "quantity": System.out.println("Please input total quantity:"); break; | |
} | |
} | |
public void selectProductAttribute() | |
{ | |
System.out.println("1. Change category"); | |
System.out.println("2. Change description"); | |
System.out.println("3. Change packaging"); | |
System.out.println("4. Change price"); | |
System.out.println("5. Change product name"); | |
} | |
public void userInformation(String type) | |
{ | |
switch (type) | |
{ | |
case "askForLogin": System.out.println("Please login before checkout.");break; | |
case "askEmail" : System.out.println("Please enter your e-mail address");break; | |
case "warnDuplication" : System.out.println("Your email is already used, please change!");break; | |
case "askPassword" : System.out.println("Please set your password");break; | |
case "confirmPassword" : System.out.println("Please enter your password again!");break; | |
case "passwordWarn" : System.out.println("Your password dissatisfy requirement!"); | |
System.out.println("Password should contain at least one captial letter, at least one number and " + | |
"at least one symbol!");break; | |
case "passwordDiffer" : System.out.println("Your passwords are different!");break; | |
case "askAddress" : System.out.println("Please enter your address");break; | |
case "askQA" : System.out.println("Now,please set your Security Questions! (Security Question helps " + | |
"you get your password when you forget your password!)");break; | |
case "setQ1" : System.out.println("Please enter your first Security Question!");break; | |
case "setA1" : System.out.println("Please enter your answer to first Question!");break; | |
case "setQ2" : System.out.println("Please enter your second Security Question!");break; | |
case "setA2" : System.out.println("Please enter your answer to second Question!");break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment