generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Showing
25 changed files
with
407 additions
and
9 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,9 @@ | ||
type,fruit,quantity | ||
b,banana,20 | ||
b,apple,100 | ||
s,banana,100 | ||
p,banana,13 | ||
r,apple,10 | ||
p,apple,20 | ||
p,banana,5 | ||
s,banana,50 |
This file was deleted.
Oops, something went wrong.
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,56 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.DataConverter; | ||
import core.basesyntax.service.FileReader; | ||
import core.basesyntax.service.FileWriter; | ||
import core.basesyntax.service.ReportGenerator; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.impl.DataConverterImpl; | ||
import core.basesyntax.service.impl.FileReaderImpl; | ||
import core.basesyntax.service.impl.FileWriterImpl; | ||
import core.basesyntax.service.impl.ReportGeneratorImpl; | ||
import core.basesyntax.service.impl.ShopServiceImpl; | ||
import core.basesyntax.strategy.BalanceOperation; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.OperationStrategyImpl; | ||
import core.basesyntax.strategy.PurchaseOperation; | ||
import core.basesyntax.strategy.ReturnOperation; | ||
import core.basesyntax.strategy.SupplyOperation; | ||
import java.io.FileNotFoundException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] arg) throws FileNotFoundException { | ||
// 1. Read the data from the input CSV file | ||
FileReader fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read("reportToRead.csv"); | ||
|
||
// 2. Convert the incoming data into FruitTransactions list | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
|
||
// 3. Create and feel the map with all OperationHandler implementations | ||
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>(); | ||
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation()); | ||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers); | ||
|
||
// 4. Process the incoming transactions with applicable OperationHandler implementations | ||
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
shopService.process(transactions); | ||
|
||
// 5.Generate report based on the current Storage state | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(); | ||
|
||
// 6. Write the received report into the destination file | ||
FileWriter fileWriter = new FileWriterImpl("finalReport.csv"); | ||
fileWriter.write(resultingReport, "finalReport.csv"); | ||
} | ||
} |
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,12 @@ | ||
package core.basesyntax.db; | ||
|
||
import java.util.Map; | ||
|
||
public interface FruitDao { | ||
|
||
void add(String fruit, int quantity); | ||
|
||
void subtract(String fruit, int quantity); | ||
|
||
Map<String,Integer> getStorageQuantity(); | ||
} |
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,28 @@ | ||
package core.basesyntax.db; | ||
|
||
import core.basesyntax.db.storage.Storage; | ||
import java.util.Map; | ||
|
||
public class FruitDaoImpl implements FruitDao { | ||
@Override | ||
public void add(String fruit, int quantity) { | ||
if (!Storage.fruits.containsKey(fruit)) { | ||
Storage.fruits.put(fruit,quantity); | ||
return; | ||
} | ||
Storage.fruits.compute(fruit, (k, oldQuantity) -> oldQuantity + quantity); | ||
} | ||
|
||
@Override | ||
public void subtract(String fruit, int quantity) { | ||
if (!Storage.fruits.containsKey(fruit)) { | ||
throw new RuntimeException("There is not enough " + fruit + " in Storage"); | ||
} | ||
Storage.fruits.compute(fruit, (k, oldQuantity) -> oldQuantity - quantity); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> getStorageQuantity() { | ||
return Storage.fruits; | ||
} | ||
} |
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,8 @@ | ||
package core.basesyntax.db.storage; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String,Integer> fruits = new HashMap<>(); | ||
} |
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,27 @@ | ||
package core.basesyntax.model; | ||
|
||
public class Fruit { | ||
private String fruit; | ||
private int quantity; | ||
|
||
public Fruit(String fruit, int quantity) { | ||
this.fruit = fruit; | ||
this.quantity = quantity; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public void setFruit(String fruit) { | ||
this.fruit = fruit; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(int quantity) { | ||
this.quantity = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package core.basesyntax.model; | ||
|
||
public class FruitTransaction extends Fruit { | ||
private Operation operation; | ||
|
||
public FruitTransaction(String operation, String fruit, int quantity) { | ||
super(fruit,quantity); | ||
switch (operation) { | ||
case "b" -> this.operation = Operation.BALANCE; | ||
case "s" -> this.operation = Operation.SUPPLY; | ||
case "p" -> this.operation = Operation.PURCHASE; | ||
case "r" -> this.operation = Operation.RETURN; | ||
default -> throw new RuntimeException("Can't find operation"); | ||
} | ||
} | ||
|
||
public Operation getOperation() { | ||
return operation; | ||
} | ||
|
||
public void setOperation(Operation operation) { | ||
this.operation = operation; | ||
} | ||
|
||
// getters, setters, ... | ||
|
||
public enum Operation { | ||
BALANCE("b"), | ||
SUPPLY("s"), | ||
PURCHASE("p"), | ||
RETURN("r"); | ||
|
||
private final String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
} | ||
} |
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,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertToTransaction(List<String> listFromFile); | ||
} |
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,7 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String path); | ||
} |
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,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface FileWriter { | ||
void write(String report, String fileName); | ||
} |
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,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface ReportGenerator { | ||
String getReport(); | ||
} |
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,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface ShopService { | ||
void process(List<FruitTransaction> fruits); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/core/basesyntax/service/impl/DataConverterImpl.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.DataConverter; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
|
||
private static final int OPERATION_TYPE = 0; | ||
private static final int FRUIT_NAME = 1; | ||
private static final int QUANTITY_OF_FRUITS = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> convertToTransaction(List<String> listFromFile) { | ||
return listFromFile.stream() | ||
.filter(s -> s.startsWith(FruitTransaction.Operation.BALANCE.getCode()) | ||
|| s.startsWith(FruitTransaction.Operation.SUPPLY.getCode()) | ||
|| s.startsWith(FruitTransaction.Operation.PURCHASE.getCode()) | ||
|| s.startsWith(FruitTransaction.Operation.RETURN.getCode())) | ||
.map(s -> s.split(",")) | ||
.map(s -> new FruitTransaction(s[OPERATION_TYPE], | ||
s[FRUIT_NAME], | ||
Integer.parseInt(s[QUANTITY_OF_FRUITS]))) | ||
.toList(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/core/basesyntax/service/impl/FileReaderImpl.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements FileReader { | ||
private final Path filePath; | ||
|
||
public FileReaderImpl() { | ||
this.filePath = Path.of("reportToRead.csv"); | ||
} | ||
|
||
public List<String> read(String filename) { | ||
try { | ||
return Files.readAllLines(filePath); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read from file", e); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/core/basesyntax/service/impl/FileWriterImpl.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.FileWriter; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements FileWriter { | ||
|
||
private final File file; | ||
|
||
public FileWriterImpl(String fileName) { | ||
this.file = new File(fileName); | ||
} | ||
|
||
@Override | ||
public void write(String report, String fileName) { | ||
try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(file))) { | ||
writer.write(report); | ||
} catch (IOException e) { | ||
throw new RuntimeException("can't save report", e); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/core/basesyntax/service/impl/ReportGeneratorImpl.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.db.FruitDao; | ||
import core.basesyntax.db.FruitDaoImpl; | ||
import core.basesyntax.service.ReportGenerator; | ||
import java.util.stream.Collectors; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
@Override | ||
public String getReport() { | ||
FruitDao fruitDao = new FruitDaoImpl(); | ||
String listFromStorage = fruitDao.getStorageQuantity().keySet().stream() | ||
.map(k -> k + "," + fruitDao.getStorageQuantity().get(k) + "\n") | ||
.collect(Collectors.joining()); | ||
return "fruit,quantity\n" + listFromStorage; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/core/basesyntax/service/impl/ShopServiceImpl.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import java.util.List; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
|
||
private final OperationStrategy operationStrategy; | ||
|
||
public ShopServiceImpl(OperationStrategy fruitTransactionStrategy) { | ||
this.operationStrategy = fruitTransactionStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> fruits) { | ||
fruits.forEach((f -> operationStrategy | ||
.getOperationStrategy(f.getOperation()) | ||
.getOperation(f.getFruit(), f.getQuantity()))); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/core/basesyntax/strategy/BalanceOperation.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.db.FruitDao; | ||
import core.basesyntax.db.FruitDaoImpl; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
@Override | ||
public void getOperation(String fruit, int quantity) { | ||
FruitDao fruitDao = new FruitDaoImpl(); | ||
fruitDao.add(fruit, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.strategy; | ||
|
||
public interface OperationHandler { | ||
void getOperation(String fruit, int quantity); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/strategy/OperationStrategy.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler getOperationStrategy(FruitTransaction.Operation operation); | ||
} |
Oops, something went wrong.