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
1 parent
2d980d8
commit b08583e
Showing
25 changed files
with
453 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,54 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.dao.StorageDaoImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.DataConverterService; | ||
import core.basesyntax.service.DataConverterServiceImpl; | ||
import core.basesyntax.service.OperationStrategy; | ||
import core.basesyntax.service.OperationStrategyImpl; | ||
import core.basesyntax.service.ReportService; | ||
import core.basesyntax.service.ReportServiceImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import core.basesyntax.service.file.FileReaderService; | ||
import core.basesyntax.service.file.FileReaderServiceImpl; | ||
import core.basesyntax.service.file.FileWriterService; | ||
import core.basesyntax.service.file.FileWriterServiceImpl; | ||
import core.basesyntax.service.operation.BalanceOperationHandler; | ||
import core.basesyntax.service.operation.OperationHandler; | ||
import core.basesyntax.service.operation.PurchaseOperationHandler; | ||
import core.basesyntax.service.operation.ReturnOperationHandler; | ||
import core.basesyntax.service.operation.SupplyOperationHandler; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
String dataBasePath = "src/DataBase.csv"; | ||
|
||
FileReaderService readerService = new FileReaderServiceImpl(); | ||
List<String> read = readerService.read(dataBasePath); | ||
|
||
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); | ||
|
||
DataConverterService dataConverterService = new DataConverterServiceImpl(); | ||
List<FruitTransaction> convert = dataConverterService.convert(read); | ||
|
||
ShopService service = new ShopServiceImpl(operationStrategy); | ||
service.process(convert); | ||
|
||
ReportService reportService = new ReportServiceImpl(); | ||
StorageDao storageDao = new StorageDaoImpl(); | ||
String report = reportService.getReport(storageDao.getAll()); | ||
|
||
FileWriterService fileWriterService = new FileWriterServiceImpl(); | ||
fileWriterService.write(report, "src/result"); | ||
} | ||
} |
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,11 @@ | ||
package core.basesyntax.dao; | ||
|
||
import java.util.Map; | ||
|
||
public interface StorageDao { | ||
Map<String, Integer> save(String fruit, int quantity); | ||
|
||
Map<String, Integer> getAll(); | ||
|
||
int getQuantity(String key); | ||
} |
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.dao; | ||
|
||
import core.basesyntax.storage.Storage; | ||
import java.util.Map; | ||
|
||
public class StorageDaoImpl implements StorageDao { | ||
private Storage storage; | ||
|
||
public StorageDaoImpl() { | ||
storage = new Storage(); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> save(String fruit, int quantity) { | ||
storage.getFruitStorage().put(fruit, quantity); | ||
return storage.getFruitStorage(); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> getAll() { | ||
return storage.getFruitStorage(); | ||
} | ||
|
||
@Override | ||
public int getQuantity(String key) { | ||
return storage.getFruitStorage().get(key); | ||
} | ||
} |
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,72 @@ | ||
package core.basesyntax.model; | ||
|
||
public class FruitTransaction { | ||
private Operation type; | ||
private String fruit; | ||
private int quantity; | ||
|
||
public FruitTransaction(Operation type, String fruit, int quantity) { | ||
this.type = type; | ||
this.fruit = fruit; | ||
this.quantity = quantity; | ||
} | ||
|
||
public Operation getType() { | ||
return type; | ||
} | ||
|
||
public void setType(Operation type) { | ||
this.type = type; | ||
} | ||
|
||
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; | ||
} | ||
|
||
public enum Operation { | ||
BALANCE("b"), | ||
SUPPLY("s"), | ||
PURCHASE("p"), | ||
RETURN("r"); | ||
|
||
private String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public static boolean startWithOperation(String line) { | ||
for (Operation operation : Operation.values()) { | ||
if (line.startsWith(operation.getCode())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static Operation convertToOperaion(String data) { | ||
for (Operation operation : Operation.values()) { | ||
if (data.equals(operation.getCode())) { | ||
return operation; | ||
} | ||
} | ||
throw new RuntimeException("Unknown operation code: " + data); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/core/basesyntax/service/DataConverterService.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,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataConverterService { | ||
List<FruitTransaction> convert(List<String> data); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/core/basesyntax/service/DataConverterServiceImpl.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,29 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class DataConverterServiceImpl implements DataConverterService { | ||
private static final String COMMA_SEPARATOR = ","; | ||
private static final int OPERATION_TYPE_INDEX = 0; | ||
private static final int FRUIT_INDEX = 1; | ||
private static final int QUANTITY_INDEX = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> convert(List<String> data) { | ||
return data.stream() | ||
.filter(FruitTransaction.Operation::startWithOperation) | ||
.map(this::convertToFruitTransaction) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private FruitTransaction convertToFruitTransaction(String data) { | ||
String[] dataSplit = data.split(COMMA_SEPARATOR); | ||
FruitTransaction.Operation operation | ||
= FruitTransaction.Operation.convertToOperaion(dataSplit[OPERATION_TYPE_INDEX]); | ||
String fruit = dataSplit[FRUIT_INDEX]; | ||
int quantity = Integer.parseInt(dataSplit[QUANTITY_INDEX]); | ||
return new FruitTransaction(operation, 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,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.operation.OperationHandler; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler get(FruitTransaction.Operation operation); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/core/basesyntax/service/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,19 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.operation.OperationHandler; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private Map<FruitTransaction.Operation, OperationHandler> operationHandlerMap; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> | ||
operationHandlerMap) { | ||
this.operationHandlerMap = operationHandlerMap; | ||
} | ||
|
||
@Override | ||
public OperationHandler get(FruitTransaction.Operation operation) { | ||
return operationHandlerMap.get(operation); | ||
} | ||
} |
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.Map; | ||
|
||
public interface ReportService { | ||
String getReport(Map<String, Integer> data); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/core/basesyntax/service/ReportServiceImpl.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 java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class ReportServiceImpl implements ReportService { | ||
private static final String HEADER = "fruit,quantity"; | ||
private static final String COMMA_SEPARATOR = ","; | ||
private StringBuilder builder; | ||
|
||
public ReportServiceImpl() { | ||
builder = new StringBuilder(); | ||
} | ||
|
||
@Override | ||
public String getReport(Map<String, Integer> data) { | ||
String body = data.entrySet().stream() | ||
.map(s -> s.getKey() + COMMA_SEPARATOR + s.getValue()) | ||
.collect(Collectors.joining(System.lineSeparator())); | ||
return HEADER + System.lineSeparator() + body; | ||
} | ||
} |
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 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ShopService { | ||
Map<String, Integer> process(List<FruitTransaction> transactions); | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.dao.StorageDaoImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private OperationStrategy operationStrategy; | ||
private StorageDao storageDao; | ||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
storageDao = new StorageDaoImpl(); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> process(List<FruitTransaction> transactions) { | ||
for (FruitTransaction transaction : transactions) { | ||
operationStrategy.get(transaction.getType()).getOperation(transaction); | ||
} | ||
return storageDao.getAll(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/service/file/FileReaderService.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.service.file; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReaderService { | ||
List<String> read(String path); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/core/basesyntax/service/file/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,18 @@ | ||
package core.basesyntax.service.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public class FileReaderServiceImpl implements FileReaderService { | ||
|
||
@Override | ||
public List<String> read(String path) { | ||
try { | ||
return Files.readAllLines(Path.of(path)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Cannot read file: " + path); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/core/basesyntax/service/file/FileWriterService.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,5 @@ | ||
package core.basesyntax.service.file; | ||
|
||
public interface FileWriterService { | ||
void write(String data, String path); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/core/basesyntax/service/file/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.file; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterServiceImpl implements FileWriterService { | ||
@Override | ||
public void write(String data, String path) { | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) { | ||
writer.write(data); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Cannot write to file: " + path); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/core/basesyntax/service/operation/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,20 @@ | ||
package core.basesyntax.service.operation; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.dao.StorageDaoImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class BalanceOperationHandler implements OperationHandler { | ||
private StorageDao storageDao; | ||
|
||
public BalanceOperationHandler() { | ||
storageDao = new StorageDaoImpl(); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> getOperation(FruitTransaction transaction) { | ||
storageDao.save(transaction.getFruit(), transaction.getQuantity()); | ||
return storageDao.getAll(); | ||
} | ||
} |
Oops, something went wrong.