Skip to content

Commit

Permalink
fix bugs of donating discount codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aboots committed Jun 22, 2020
1 parent 3886556 commit 092c5a0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Resources/Users/Customers/kure.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"discountCodesIds":[1,2,3],"previousOrders":[1,7,9,11,13,15,17,20,22,24,26,28,30,32],"credit":106710000,"username":"kure","firstName":"ghahreman","lastName":"kure","email":"[email protected]","phoneNumber":"09980000000","password":"Kure1"}
{"discountCodesIds":[1,2],"previousOrders":[1,7,9,11,13,15,17,20,22,24,26,28,30,32],"credit":106710000,"username":"kure","firstName":"ghahreman","lastName":"kure","email":"[email protected]","phoneNumber":"09980000000","password":"Kure1"}
26 changes: 17 additions & 9 deletions src/main/java/ApProject_OnlineShop/model/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ApProject_OnlineShop.model.productThings.*;
import ApProject_OnlineShop.model.requests.Request;

import java.io.DataInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;
Expand Down Expand Up @@ -59,8 +60,8 @@ public HashMap<Long, Company> getAllCompanies() {
return allCompanies;
}

public void addCompany(Company company){
allCompanies.put(company.getId(),company);
public void addCompany(Company company) {
allCompanies.put(company.getId(), company);
}

public ArrayList<Category> getAllCategories() {
Expand Down Expand Up @@ -95,8 +96,8 @@ public ArrayList<Rate> getAllRates() {
return allRates;
}

public void addSellerRelatedInfoAboutGood(SellerRelatedInfoAboutGood sellerRelatedInfoAboutGood){
this.allSellerRelatedInfoAboutGood.put(sellerRelatedInfoAboutGood.getSellerRelatedInfoAboutGoodId(),sellerRelatedInfoAboutGood);
public void addSellerRelatedInfoAboutGood(SellerRelatedInfoAboutGood sellerRelatedInfoAboutGood) {
this.allSellerRelatedInfoAboutGood.put(sellerRelatedInfoAboutGood.getSellerRelatedInfoAboutGoodId(), sellerRelatedInfoAboutGood);
}

public HashMap<String, Category> getHashMapOfCategories() {
Expand Down Expand Up @@ -142,9 +143,13 @@ public void addPerson(Person user) {
allPersons.add(user);
}

public void addGoodToAllGoods(Good good) { allGoods.put(good.getGoodId(), good); }
public void addGoodToAllGoods(Good good) {
allGoods.put(good.getGoodId(), good);
}

public void removeGoodFromAllGoods(Good good) { allGoods.remove(good.getGoodId()); }
public void removeGoodFromAllGoods(Good good) {
allGoods.remove(good.getGoodId());
}

public void removeProduct(Good good) throws IOException, FileCantBeSavedException {
good.getSubCategory().deleteGood(good);
Expand Down Expand Up @@ -320,6 +325,10 @@ public void generatePeriodRandomDiscountCodes(LocalDate endDate) throws IOExcept
DiscountCode discountCode = new DiscountCode(code, LocalDate.now(), endDate, 100000L, 20);
discountCode.addAllCustomers(randomCustomers(5, 1, discountCode));
allDiscountCodes.put(discountCode.getId(), discountCode);
Database.getInstance().saveItem(discountCode);
for (Customer customer : discountCode.getIncludedCustomers().keySet()) {
Database.getInstance().saveItem(customer);
}
this.lastRandomPeriodDiscountCodeCreatedDate = LocalDate.now();
}

Expand All @@ -329,7 +338,8 @@ private HashMap<Customer, Integer> randomCustomers(int customerNumbers, int repe
int randomNumber = ((int) (Math.random() * 1000000)) % allPersons.size();
Person person = allPersons.get(randomNumber);
if (person instanceof Customer) {
randomCustomers.put((Customer) person, repeatingTimes);
if (!randomCustomers.containsKey(person))
randomCustomers.put((Customer) person, repeatingTimes);
}
}
return randomCustomers;
Expand Down Expand Up @@ -425,8 +435,6 @@ public void donatePeriodRandomDiscountCodes() throws IOException, FileCantBeSave
}
}



public void expireItemsThatTheirTimeIsFinished() throws IOException, FileCantBeSavedException, FileCantBeDeletedException {
for (Off off : this.getOffs()) {
if (off.isOffExpired()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ApProject_OnlineShop.model.persons;

import ApProject_OnlineShop.database.Database;
import ApProject_OnlineShop.exception.FileCantBeSavedException;
import ApProject_OnlineShop.model.Shop;
import ApProject_OnlineShop.model.orders.OrderForCustomer;
import ApProject_OnlineShop.model.productThings.DiscountCode;
import ApProject_OnlineShop.model.productThings.GoodInCart;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;

Expand Down Expand Up @@ -82,7 +85,7 @@ public boolean hasBuyProduct(long productId) {
return false;
}

public void donateDiscountCodeTOBestCustomers() {
public void donateDiscountCodeTOBestCustomers() throws IOException, FileCantBeSavedException {
long allPricesOfOrdersWithOutLastOne = 0L;
for (OrderForCustomer order : this.getPreviousOrders()) {
if (!order.equals(this.getPreviousOrders().get(this.getPreviousOrders().size() - 1))) {
Expand All @@ -95,6 +98,8 @@ public void donateDiscountCodeTOBestCustomers() {
, LocalDate.now(),LocalDate.now().plusMonths(1), 10000L,30);
discountCode.addCustomerToCode(this,1);
Shop.getInstance().addDiscountCode(discountCode);
Database.getInstance().saveItem(discountCode);
Database.getInstance().saveItem(this);
}
}

Expand Down

0 comments on commit 092c5a0

Please sign in to comment.