-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3348402
commit 7aa30d9
Showing
28 changed files
with
595 additions
and
78 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type,fruit,quantity | ||
b,apple,-10 | ||
b,banana,-10 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,39 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.service.CreateReportService; | ||
import core.basesyntax.service.DataConvertService; | ||
import core.basesyntax.service.DataProcessService; | ||
import core.basesyntax.service.ReadFromCsvFileService; | ||
import core.basesyntax.service.WriteToCsvFileService; | ||
import core.basesyntax.service.impl.CreateReportServiceImpl; | ||
import core.basesyntax.service.impl.DataConvertServiceImpl; | ||
import core.basesyntax.service.impl.DataProcessServiceImpl; | ||
import core.basesyntax.service.impl.ReadFromCsvFileServiceImpl; | ||
import core.basesyntax.service.impl.WriteToCsvFileServiceImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.*; | ||
import core.basesyntax.service.impl.*; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String FIRST_TEST_CSV = "fruits1.csv"; | ||
private static final String SECOND_TEST_CSV = "fruits2.csv"; | ||
private static final String THIRD_TEST_CSV = "fruits3.csv"; | ||
private static final String REPORT_FILENAME = "report.csv"; | ||
private static final Map<Operation, OperationHandler> operationPicker = | ||
Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(), | ||
Operation.PURCHASE, new PurchaseOperationHandlerImpl(), | ||
Operation.RETURN, new ReturnOperationHandlerImpl(), | ||
Operation.SUPPLY, new SupplyOperationHandlerImpl()); | ||
|
||
public static void main(String[] args) { | ||
ReadFromCsvFileService reader = new ReadFromCsvFileServiceImpl(); | ||
DataConvertService convertor = new DataConvertServiceImpl(); | ||
DataProcessService processor = new DataProcessServiceImpl(); | ||
DataProcessService processor = new DataProcessServiceImpl(operationPicker); | ||
CreateReportService reportCreator = new CreateReportServiceImpl(); | ||
WriteToCsvFileService writer = new WriteToCsvFileServiceImpl(); | ||
WriteToCsvFileService writer = new WriteToCsvFileServiceImpl(REPORT_FILENAME); | ||
|
||
List<FruitTransaction> convertedFruitsFromFile = | ||
convertor.convert(reader.readFile(THIRD_TEST_CSV)); | ||
|
||
processor.processFruits(convertedFruitsFromFile); | ||
|
||
processor.processFruits(convertor.convert(reader.readFile(THIRD_TEST_CSV))); | ||
writer.write(reportCreator.createReport()); | ||
} | ||
} |
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
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
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
42 changes: 29 additions & 13 deletions
42
src/main/java/core/basesyntax/service/impl/DataProcessServiceImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.db.dao.StorageDao; | ||
import core.basesyntax.db.dao.StorageDaoImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.DataProcessService; | ||
import core.basesyntax.strategy.FruitOperationStrategy; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.FruitOperationStrategyImpl; | ||
import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl; | ||
import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DataProcessServiceImpl implements DataProcessService { | ||
private final Map<Operation, OperationHandler> operationPicker; | ||
private final StorageDao storageDao; | ||
|
||
public DataProcessServiceImpl(Map<Operation, OperationHandler> operationPicker) { | ||
this.operationPicker = operationPicker; | ||
storageDao = new StorageDaoImpl(); | ||
} | ||
|
||
@Override | ||
public void processFruits(List<FruitTransaction> fruits) { | ||
Map<Operation, OperationHandler> operationPicker = Map.of(Operation.BALANCE, | ||
new BalanceOperationHandlerImpl(), | ||
Operation.PURCHASE, | ||
new PurchaseOperationHandlerImpl(), | ||
Operation.RETURN, | ||
new ReturnOperationHandlerImpl(), | ||
Operation.SUPPLY, | ||
new SupplyOperationHandlerImpl()); | ||
List<String> fruitNames = fruits.stream() | ||
.map(FruitTransaction::getFruitName) | ||
.distinct() | ||
.toList(); | ||
|
||
FruitOperationStrategy fruitOperationStrategy = | ||
new FruitOperationStrategyImpl(operationPicker); | ||
|
||
fruitOperationStrategy.countFruits(fruits); | ||
for (String fruitName : fruitNames) { | ||
int fruitCount = 0; | ||
for (FruitTransaction fruitTransaction : fruits) { | ||
if (fruitTransaction.getFruitName().equals(fruitName)) { | ||
fruitCount += fruitOperationStrategy.countFruits(fruitTransaction); | ||
} | ||
} | ||
|
||
if (fruitCount < 0) { | ||
throw new RuntimeException("Cannot be less then 0: " + fruitCount); | ||
} | ||
|
||
storageDao.add(fruitName, fruitCount); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/core/basesyntax/service/impl/ReadFromCsvFileServiceImpl.java
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
11 changes: 8 additions & 3 deletions
11
src/main/java/core/basesyntax/service/impl/WriteToCsvFileServiceImpl.java
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
3 changes: 1 addition & 2 deletions
3
src/main/java/core/basesyntax/strategy/FruitOperationStrategy.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface FruitOperationStrategy { | ||
void countFruits(List<FruitTransaction> fruitTransactions); | ||
int countFruits(FruitTransaction fruitTransactions); | ||
} |
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
31 changes: 5 additions & 26 deletions
31
src/main/java/core/basesyntax/strategy/impl/FruitOperationStrategyImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,22 @@ | ||
package core.basesyntax.strategy.impl; | ||
|
||
import core.basesyntax.db.dao.StorageDao; | ||
import core.basesyntax.db.dao.StorageDaoImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.strategy.FruitOperationStrategy; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import java.util.List; | ||
|
||
import java.util.Map; | ||
|
||
public class FruitOperationStrategyImpl implements FruitOperationStrategy { | ||
private Map<Operation, OperationHandler> handlerOperationMap; | ||
private final StorageDao storageDao = new StorageDaoImpl(); | ||
private final Map<Operation, OperationHandler> handlerOperationMap; | ||
|
||
public FruitOperationStrategyImpl(Map<Operation, OperationHandler> handlerOperationMap) { | ||
this.handlerOperationMap = handlerOperationMap; | ||
} | ||
|
||
@Override | ||
public void countFruits(List<FruitTransaction> fruitTransactions) { | ||
List<String> fruitNames = fruitTransactions.stream() | ||
.map(FruitTransaction::getFruitName) | ||
.distinct() | ||
.toList(); | ||
|
||
for (String fruitName : fruitNames) { | ||
int fruitCount = 0; | ||
for (FruitTransaction fruitTransaction : fruitTransactions) { | ||
if (fruitTransaction.getFruitName().equals(fruitName)) { | ||
fruitCount += handlerOperationMap.get(fruitTransaction.getOperation()) | ||
.count(fruitTransaction); | ||
} | ||
} | ||
|
||
if (fruitCount < 0) { | ||
throw new RuntimeException("Cannot be less then 0: " + fruitCount); | ||
} | ||
|
||
storageDao.add(fruitName, fruitCount); | ||
} | ||
public int countFruits(FruitTransaction fruitTransaction) { | ||
return handlerOperationMap.get(fruitTransaction.getOperation()) | ||
.count(fruitTransaction); | ||
} | ||
} |
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
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
Oops, something went wrong.