Skip to content

Commit

Permalink
finish phase one
Browse files Browse the repository at this point in the history
  • Loading branch information
ashkan-khd committed Jun 4, 2020
1 parent 949e2bf commit 7ef17e7
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 124 deletions.
49 changes: 0 additions & 49 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@

public class Main {
public static void main(String[] args) {
/*try {
ProductTable.tempAddProducts();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}*/
/*try {
addDiscountCodes();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}*/

/* for (Product showingProduct : ProductTable.getAllShowingProducts()) {
System.out.println(showingProduct.getName() + ": " + showingProduct.getApprovalDate());
}*/

Processor.initProcessHashMaps();
Menu menu = Menu.makeMenu("Main Menu");
Expand All @@ -39,35 +21,4 @@ public static void main(String[] args) {
}

}

private static void addDiscountCodes() throws SQLException, ClassNotFoundException {
Discount discount = new Discount();
discount.setID("d1234567");
discount.setCode("HelloWorld");
HashMap<String, Integer> customers = new HashMap<>();
customers.put("AliKhd", 4);
customers.put("Naghii", 2);
discount.setCustomersWithRepetition(customers);
DiscountTable.addDiscount(discount);
discount.setID("a2345678");
discount.setCode("HelloWorld");
customers = new HashMap<>();
customers.put("Karimi", 3);
customers.put("Mostafa", 0);
discount.setCustomersWithRepetition(customers);
DiscountTable.addDiscount(discount);
discount.setID("a2345679");
discount.setCode("FuckAlexis");
customers = new HashMap<>();
customers.put("Alexis", 3);
customers.put("Molly", 0);
customers.put("Arya Fae", 20);
customers.put("Sasha Grey", 15);
customers.put("Karla Kush", 20);
customers.put("Riley Reid", 2);
customers.put("Danni Daniels", 12);
customers.put("Aleta Ocean", 8);
discount.setCustomersWithRepetition(customers);
DiscountTable.addDiscount(discount);
}
}
2 changes: 1 addition & 1 deletion src/main/java/controller/IOControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Notification login(Account account){
CartTable.addTempToUsername(account.getUsername());
}
CartTable.removeTemp();
if (DiscountTable.didFiveDaysPast())
if (AccountTable.didPeriodPass("Ya Zahra"))
AdminControl.getController().getGiftDiscount();
return Notification.LOGIN_SUCCESSFUL;
} else {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/controller/account/AccountControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public ArrayList<String> getAllCustomerNames() {
public Off getOffByID(String offID) {
try {
Off off = OffTable.getSpecificOff(offID);
//System.out.println(off.getProductIDs());
return off;
} catch (SQLException e) {
e.printStackTrace();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/controller/account/AdminControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public ArrayList<String> getAllDiscountCodes() {
ArrayList<String> discountCodes = new ArrayList<>();
try {
DiscountTable.updateDiscountCodesTime();
//DiscountTable.updateDiscountCodesRep();
for (Discount discountCode : DiscountTable.getAllDiscountCodes()) {
discountCodes.add(discountCode.getCode());
}
Expand Down Expand Up @@ -563,7 +562,7 @@ public void getGiftDiscount() {
customerNum = ((int) (Math.random() * 100000000)) % AccountTable.getAllAccounts().size();
DiscountTable.addGiftDiscount(discount, AccountTable.getAllAccounts().get(customerNum).getUsername());
}
DiscountTable.updateGiftDiscountDate();
AccountTable.updatePeriod("Ya Zahra");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/controller/account/CustomerControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public Notification decreaseCount(String productID, String command) {

public Notification increaseAmount(String productID, String command) {
try {
//System.out.println("Command = " + command);
double input = Double.parseDouble(command);
if(input > 0)
{
Expand All @@ -167,7 +166,6 @@ public Notification increaseAmount(String productID, String command) {

public Notification decreaseAmount(String productID, String command) {
try {
//System.out.println("Command = " + command);
double input = Double.parseDouble(command);
if(input > 0)
{
Expand Down Expand Up @@ -227,7 +225,6 @@ public ArrayList<String> getDiscountCodes() {
ArrayList<String> discountCodes = new ArrayList<>();
try {
DiscountTable.updateDiscountCodesTime();
//DiscountTable.updateDiscountCodesRep();
for (Discount discountCode : DiscountTable.getCustomerDiscountCodes(Control.getUsername())) {
if(discountCode.getMaxRepetition() > discountCode.getCustomersWithRepetition().get(Control.getUsername()))
discountCodes.add(discountCode.getCode());
Expand Down Expand Up @@ -313,10 +310,7 @@ public Notification purchase()
initPrice += product.getPrice() * product.getAmount();
initPrice += product.getPrice() * product.getCount();
}
//TODO
//System.out.println("init price : " + initPrice + "$ off price : " + offPrice + "$");
finalPrice = calculateFinalPrice(hasDiscount, discount, offPrice);
//System.out.println("fin pri : " + finalPrice);
return affordability(initPrice, offPrice, finalPrice);

} catch (SQLException e) {
Expand All @@ -336,10 +330,7 @@ private Notification affordability(double initPrice, double offPrice, double fin
giveCreditToVendors(customer.getUsername());
int giftState = createLog(customer);
if(hasDiscount) {
//System.out.println("Step 1");
DiscountTable.addRepetitionToDiscount(discount, customer.getUsername());
/* if(discount.getMaxRepetition() <= discount.getCustomersWithRepetition().get(customer.getUsername()))
DiscountTable.removeDiscountCodeForUsername(discount.getID(),customer.getUsername());*/
}
reduceProductFromStock(customer.getUsername());
CartTable.deleteCustomerCart(customer.getUsername());
Expand Down Expand Up @@ -400,12 +391,9 @@ private double calculateFinalPrice(boolean hasDiscount, Discount discount, doubl
if(!hasDiscount)
return offPrice;
double discountVal = (discount.getDiscountPercent()/100) * offPrice;
//System.out.println("Discount Val : " + discountVal);
if(discountVal <= discount.getMaxDiscount()) {
//System.out.println("off - dis : " + (offPrice - discountVal));
return offPrice - discountVal;
}
//System.out.println("off - dis : " + (offPrice - discount.getMaxDiscount()));
return offPrice - discount.getMaxDiscount();
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/controller/product/ProductControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ private String generateCommentID() {

public ArrayList<String> getShowingCommentTitles(){
ArrayList<String> allShowingCommentTitles = new ArrayList<>();
//System.out.println("What The Fuck");
try {
for (Comment comment : ProductTable.getAllLoggedInUserComment(Control.getUsername(), getCurrentProduct())) {
allShowingCommentTitles.add(comment.getTitle());
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/controller/product/Sorting.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public int compare(Product o1, Product o2) {
public static class TimeSortDescending implements Comparator<Product> {
@Override
public int compare(Product o1, Product o2) {
/* System.out.println(o1.getName() + "," + o2.getName());
System.out.println(o1.getApprovalDate() + ", " + o2.getApprovalDate());*/
return -o1.getApprovalDate().compareTo(o2.getApprovalDate());
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/model/db/AccountTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import model.existence.Account;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class AccountTable extends Database {
public static boolean isUsernameFree(String username) throws SQLException, ClassNotFoundException {
Expand Down Expand Up @@ -159,4 +161,41 @@ public static ArrayList<Account> getAllCustomers() throws SQLException, ClassNot
allCustomers.add(Account.makeAccount(resultSet));
return allCustomers;
}

public static boolean didPeriodPass(String ID) throws SQLException, ClassNotFoundException {
String command = "SELECT * FROM TimeLapse WHERE ID = ?";
PreparedStatement preparedStatement = getConnection().prepareStatement(command);
preparedStatement.setString(1, ID);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
if (resultSet.getDate("FinishDate").compareTo(new Date(System.currentTimeMillis())) < 1)
return true;
return false;
}
return false;
}

public static void updatePeriod(String ID) throws SQLException, ClassNotFoundException {
String command = "SELECT * FROM TimeLapse WHERE ID = ?";
PreparedStatement preparedStatement = getConnection().prepareStatement(command);
preparedStatement.setString(1, ID);
ResultSet resultSet = preparedStatement.executeQuery();
Date startDate = resultSet.getDate("StartDate"); Date finishDate = resultSet.getDate("FinishDate");
String newCommand = "UPDATE TimeLapse SET StartDate = ?, FinishDate = ? WHERE ID = ?";
preparedStatement = getConnection().prepareStatement(newCommand);
preparedStatement.setString(3, ID);
preparedStatement.setDate(1, new Date(System.currentTimeMillis()));
preparedStatement.setDate(2, new Date(System.currentTimeMillis() + (finishDate.getTime() - startDate.getTime())));
preparedStatement.execute();
}

public static HashMap<Date, Date> getTimeLapse(String ID) throws SQLException, ClassNotFoundException {
String command = "SELECT * FROM TimeLapse WHERE ID = ?";
PreparedStatement preparedStatement = getConnection().prepareStatement(command);
preparedStatement.setString(1, ID);
ResultSet resultSet = preparedStatement.executeQuery();
HashMap<Date, Date> map = new HashMap<>();
map.put(resultSet.getDate("StartDate"), resultSet.getDate("FinishDate"));
return map;
}
}
34 changes: 24 additions & 10 deletions src/main/java/model/db/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,36 @@ private static void initDB() throws SQLException, ClassNotFoundException {
initLogTable(initConnection.createStatement());
initScoreTable(initConnection.createStatement());
initCommentTable(initConnection.createStatement());
initTimeLapseTable(initConnection.createStatement());
removeTempAccountsFromCarts(initConnection);

isDBInit = true;
initConnection.close();
}

private static void initTimeLapseTable(Statement statement) throws SQLException, ClassNotFoundException {
String command = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'TimeLapse'";
ResultSet resultSet = statement.executeQuery(command);
if (!resultSet.next()){
statement.execute("CREATE TABLE TimeLapse(" +
"ID varchar (16)," +
"StartDate DATE," +
"FinishDate DATE," +
"primary key(ID)" +
");");
String sql = "INSERT INTO TimeLapse(ID, StartDate, FinishDate) VALUES(?,?,?)";
PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
preparedStatement.setString(1, "Ya Zahra");
Date date = new Date(new java.util.Date(120, Calendar.MAY,21,0,0,0).getTime());
preparedStatement.setDate(2, date);
date = new Date(new java.util.Date(120, Calendar.MAY, 26, 0, 0, 0).getTime());
preparedStatement.setDate(3, date);
preparedStatement.execute();
}
statement.close();
resultSet.close();
}

private static void initCommentTable(Statement statement) throws SQLException {
String command = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'Comments'";
ResultSet resultSet = statement.executeQuery(command);
Expand Down Expand Up @@ -90,15 +114,11 @@ private static void initEditingOffTable(Statement statement) throws SQLException
}

private static void removeTempAccountsFromCarts(Connection initConnection) throws SQLException {
//System.out.println("Hello");
String command = "DELETE FROM Carts WHERE CustomerUsername = ? ";
PreparedStatement preparedStatement = initConnection.prepareStatement(command);
preparedStatement.setString(1, "temp");
//System.out.println("Before");
preparedStatement.execute();
//System.out.println("After");
preparedStatement.close();
//System.out.println("World");
}

private static void initLogTable(Statement statement) throws SQLException {
Expand Down Expand Up @@ -157,12 +177,6 @@ private static void initDiscountTable(Statement statement) throws SQLException,
"MaxRepetition int," +
"CustomerUsername varchar(16)" +
");");
String sql = "INSERT INTO Discounts(ID, StartDate) VALUES(?,?)";
PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
preparedStatement.setString(1, "" + 11111111);
Date date = new Date(new java.util.Date(120, Calendar.MAY,21,0,0,0).getTime());
preparedStatement.setDate(2, date);
preparedStatement.execute();
}
statement.close();
resultSet.close();
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/model/db/DiscountTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,6 @@ public static void addRepetitionToDiscount(Discount discount, String username) t
preparedStatement.execute();
}

public static boolean didFiveDaysPast() throws SQLException, ClassNotFoundException {
String command = "SELECT * FROM Discounts WHERE ID = ?";
PreparedStatement preparedStatement = getConnection().prepareStatement(command);
preparedStatement.setString(1, "" + 11111111);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
if ((resultSet.getDate("StartDate").getTime() + (long) 4.32e+8) < System.currentTimeMillis())
return true;
return false;
}
return false;
}

public static void addGiftDiscount(Discount discount, String username) throws SQLException, ClassNotFoundException {
String command = "INSERT INTO Discounts(Code, StartDate, FinishDate, DiscountPercent, MaxDiscount," +
"Repetition, MaxRepetition, CustomerUsername, ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
Expand All @@ -183,13 +170,4 @@ public static void addGiftDiscount(Discount discount, String username) throws SQ
preparedStatement.setString(9, discount.getID());
preparedStatement.execute();
}

public static void updateGiftDiscountDate() throws SQLException, ClassNotFoundException {
String command = "UPDATE Discounts SET StartDate = ? WHERE ID = ?";
PreparedStatement preparedStatement = getConnection().prepareStatement(command);
Date date = new Date(new java.util.Date().getTime());
preparedStatement.setDate(1, date);
preparedStatement.setString(2, "" + 11111111);
preparedStatement.execute();
}
}
7 changes: 0 additions & 7 deletions src/main/java/model/db/ProductTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ public static ArrayList<Product> getAllProducts() throws SQLException, ClassNotF
}

public static Product getProductByID(String ID) throws SQLException, ClassNotFoundException {
//System.out.println(ID);
String queryTask = "SELECT * FROM Products WHERE ID = ?;";
PreparedStatement preparedStatement = getConnection().prepareStatement(queryTask);
preparedStatement.setString(1, ID);
//System.out.println("Hello World");
Product product = new Product(preparedStatement.executeQuery());
//System.out.println(product.getName() + " : " + product.getID());
return product;
}

Expand Down Expand Up @@ -269,13 +266,9 @@ public static ArrayList<Comment> getAllLoggedInUserComment(String username, Stri
preparedStatement.setInt(4, 3);
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList<Comment> comments = new ArrayList<>();
//System.out.println("Shit");
while (resultSet.next()){
//System.out.println("Ha :|");
comments.add(new Comment(resultSet));
//System.out.println(ProductTable.getProductByID(comments.get(comments.size() - 1).getProductID()).getName());
}
//System.out.println(comments);
return comments;
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/model/existence/Off.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ public class Off {

private ArrayList<String> productIDs = new ArrayList<>();

/*public Off(ResultSet resultSet) throws SQLException {
String offID = resultSet.getString("OffID");
this.offID = resultSet.getString("OffID");
this.offName = resultSet.getString("OffName");
this.vendorUsername = resultSet.getString("VendorUsername");
this.status = resultSet.getInt("Status");
this.startDate = resultSet.getDate("StartDate");
this.finishDate = resultSet.getDate("FinishDate");
this.offPercent = resultSet.getDouble("OffPercent");
productIDs.add(resultSet.getString("ProductID"));
while (resultSet.getString("OffID").equals(offID) && resultSet.next()) {
productIDs.add(resultSet.getString("ProductID"));
}
}*/

public static Off makeOffByID(ResultSet resultSet) throws SQLException {
Off off = new Off();
String offID = resultSet.getString("OffID");
Expand All @@ -42,9 +27,7 @@ public static Off makeOffByID(ResultSet resultSet) throws SQLException {
off.setStatus(resultSet.getInt("Status"));
off.setOffPercent(resultSet.getDouble("OffPercent"));
ArrayList<String> productIDs = new ArrayList<>();
/*productIDs.add(resultSet.getString("ProductID"));*/
while (resultSet.next()) {
//System.out.println(resultSet.getString("ProductID"));
productIDs.add(resultSet.getString("ProductID"));
}
off.setProductIDs(productIDs);
Expand Down

0 comments on commit 7ef17e7

Please sign in to comment.