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
21 changed files
with
265 additions
and
9 deletions.
There are no files selected for viewing
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,51 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.DataConverter; | ||
import core.basesyntax.service.DataConverterImpl; | ||
import core.basesyntax.service.FileReaderService; | ||
import core.basesyntax.service.FileReaderServiceImpl; | ||
import core.basesyntax.service.FileWriterService; | ||
import core.basesyntax.service.FileWriterServiceImpl; | ||
import core.basesyntax.service.ReportGenerator; | ||
import core.basesyntax.service.ReportGeneratorImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import core.basesyntax.strategy.BalanceOperationHandler; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.OperationStrategyImpl; | ||
import core.basesyntax.strategy.PurchaseOperationHandler; | ||
import core.basesyntax.strategy.ReturnOperationHandler; | ||
import core.basesyntax.strategy.SupplyOperationHandler; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] arg) { | ||
FileReaderService fileReader = new FileReaderServiceImpl(); | ||
List<String> inputReport = fileReader.readFile("reportToRead.csv"); | ||
|
||
// 2. Convert the incoming data into FruitTransactions list | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
final List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
|
||
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>(); | ||
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); | ||
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); | ||
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); | ||
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); | ||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers); | ||
|
||
// 4. Process the incoming transactions with applicable OperationHandler implementations | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
shopService.process(transactions); | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(); | ||
|
||
// 6. Write the received report into the destination file | ||
FileWriterService fileWriter = new FileWriterServiceImpl(); | ||
fileWriter.writeFile(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,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 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> data); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/core/basesyntax/service/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,20 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
@Override | ||
public List<FruitTransaction> convertToTransaction(List<String> data) { | ||
List<FruitTransaction> transactions = new ArrayList<>(); | ||
for (String line: data) { | ||
String[] elements = line.split(","); | ||
FruitTransaction fruitTransaction = | ||
new FruitTransaction(FruitTransaction.Operation.fromCode(elements[0]), | ||
elements[1], Integer.parseInt(elements[2])); | ||
transactions.add(fruitTransaction); | ||
} | ||
return transactions; | ||
} | ||
} |
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 FileReaderService { | ||
List<String> readFile(String fileName); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/core/basesyntax/service/FileReaderServiceImpl.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; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileReaderServiceImpl implements FileReaderService { | ||
@Override | ||
public List<String> readFile(String filepath) { | ||
List<String> lines = new ArrayList<>(); | ||
try (BufferedReader br = new BufferedReader(new FileReader(filepath))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return lines; | ||
} | ||
} |
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 FileWriterService { | ||
void writeFile(String report, String fileName); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/core/basesyntax/service/FileWriterServiceImpl.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,16 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterServiceImpl implements FileWriterService { | ||
@Override | ||
public void writeFile(String report, String filePath) { | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { | ||
writer.write(report); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't write a " + filePath, e); | ||
} | ||
} | ||
} |
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(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/core/basesyntax/service/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,13 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.storage.Storage; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
@Override | ||
public String getReport() { | ||
StringBuilder report = new StringBuilder("fruit,quantity\n"); | ||
Storage.fruits.forEach((fruit, quantity) -> report.append(fruit) | ||
.append(",").append(quantity).append("\n")); | ||
return report.toString(); | ||
} | ||
} |
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> transactions); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/core/basesyntax/service/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; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import java.util.List; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private OperationStrategy operationStrategy; | ||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> transactions) { | ||
for (FruitTransaction fruitTransaction: transactions) { | ||
operationStrategy.getHandler( | ||
fruitTransaction.getOperation()).apply(fruitTransaction.getFruit(), | ||
fruitTransaction.getQuantity()); | ||
} | ||
} | ||
} |
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.storage; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String, Integer> fruits = new HashMap<>(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/core/basesyntax/strategy/BalanceOperationHandler.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,10 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.storage.Storage; | ||
|
||
public class BalanceOperationHandler implements OperationHandler { | ||
@Override | ||
public void apply(String fruit, int quantity) { | ||
Storage.fruits.put(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 apply(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 getHandler(FruitTransaction.Operation operation); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/core/basesyntax/strategy/OperationStrategyImpl.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,18 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, | ||
OperationHandler> operationHandlers) { | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
@Override | ||
public OperationHandler getHandler(FruitTransaction.Operation operation) { | ||
return operationHandlers.get(operation); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/core/basesyntax/strategy/PurchaseOperationHandler.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,10 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.storage.Storage; | ||
|
||
public class PurchaseOperationHandler implements OperationHandler { | ||
@Override | ||
public void apply(String fruit, int quantity) { | ||
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/core/basesyntax/strategy/ReturnOperationHandler.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,10 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.storage.Storage; | ||
|
||
public class ReturnOperationHandler implements OperationHandler { | ||
@Override | ||
public void apply(String fruit, int quantity) { | ||
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/core/basesyntax/strategy/SupplyOperationHandler.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,10 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.storage.Storage; | ||
|
||
public class SupplyOperationHandler implements OperationHandler { | ||
@Override | ||
public void apply(String fruit, int quantity) { | ||
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity); | ||
} | ||
} |