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 5aef4be
Showing
22 changed files
with
359 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,53 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.handlers.BalanceOperationHandler; | ||
import core.basesyntax.handlers.PurchaseOperationHandler; | ||
import core.basesyntax.handlers.ReturnOperationHandler; | ||
import core.basesyntax.handlers.SupplyOperationHandler; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.services.DataProcessorService; | ||
import core.basesyntax.services.FileReaderService; | ||
import core.basesyntax.services.FileWriterService; | ||
import core.basesyntax.services.OperationProcessor; | ||
import core.basesyntax.services.ReportCreatorService; | ||
import core.basesyntax.services.impl.DataProcessorServiceImpl; | ||
import core.basesyntax.services.impl.FileReaderServiceImpl; | ||
import core.basesyntax.services.impl.FileWriterServiceImpl; | ||
import core.basesyntax.services.impl.OperationProcessorImpl; | ||
import core.basesyntax.services.impl.ReportCreatorServiceImpl; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class FruitShop { | ||
private static final String INPUT_FILE_PATH = "src/main/java/resources/input.csv"; | ||
private static final String OUTPUT_FILE_PATH = "src/main/java/resources/output.csv"; | ||
|
||
public static void main(String[] args) { | ||
FileReaderService fileReaderService = new FileReaderServiceImpl(); | ||
List<String> lines = fileReaderService.readFromFile(INPUT_FILE_PATH); | ||
DataProcessorService dataProcessorService = new DataProcessorServiceImpl(); | ||
|
||
Map<FruitTransaction.Operation, OperationHandler> operationMap = getOperationOperationMap(); | ||
|
||
OperationProcessor operationService = new OperationProcessorImpl(operationMap); | ||
List<FruitTransaction> fruitTransactionList = dataProcessorService.processInputData(lines); | ||
operationService.manageTransactions(fruitTransactionList); | ||
|
||
ReportCreatorService reportCreatorService = new ReportCreatorServiceImpl(); | ||
String report = reportCreatorService.createReport(); | ||
|
||
FileWriterService fileWriterService = new FileWriterServiceImpl(); | ||
fileWriterService.writeToFile(OUTPUT_FILE_PATH, report); | ||
} | ||
|
||
private static Map<FruitTransaction.Operation, OperationHandler> getOperationOperationMap() { | ||
Map<FruitTransaction.Operation, OperationHandler> operationMap = new HashMap<>(); | ||
operationMap.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler()); | ||
operationMap.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler()); | ||
operationMap.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler()); | ||
operationMap.put(FruitTransaction.Operation.RETURN, new ReturnOperationHandler()); | ||
return operationMap; | ||
} | ||
} |
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,8 @@ | ||
package core.basesyntax.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String, Integer> STORAGE = new HashMap<>(); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/exception/InvalidDataException.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.exception; | ||
|
||
public class InvalidDataException extends RuntimeException { | ||
public InvalidDataException(String message) { | ||
super(message); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/core/basesyntax/handlers/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,12 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class BalanceOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
Storage.STORAGE.put(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/core/basesyntax/handlers/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,18 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class PurchaseOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
int currentAmount = Storage.STORAGE.get(transaction.getFruit()); | ||
int quantity = transaction.getQuantity(); | ||
int purchaseResult = currentAmount - quantity; | ||
if (purchaseResult < 0) { | ||
throw new RuntimeException("Not enough fruits in storage"); | ||
} | ||
Storage.STORAGE.put(transaction.getFruit(), purchaseResult); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/core/basesyntax/handlers/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,15 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class ReturnOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
int currentAmount = Storage.STORAGE.get(transaction.getFruit()); | ||
int quantity = transaction.getQuantity(); | ||
int returnResult = currentAmount + quantity; | ||
Storage.STORAGE.put(transaction.getFruit(), returnResult); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/core/basesyntax/handlers/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,15 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class SupplyOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
int currentAmount = Storage.STORAGE.get(transaction.getFruit()); | ||
int quantity = transaction.getQuantity(); | ||
int supplyResult = currentAmount + quantity; | ||
Storage.STORAGE.put(transaction.getFruit(), supplyResult); | ||
} | ||
} |
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,63 @@ | ||
package core.basesyntax.model; | ||
|
||
import java.util.Arrays; | ||
|
||
public class FruitTransaction { | ||
private Operation operation; | ||
private String fruit; | ||
private int quantity; | ||
|
||
public FruitTransaction(Operation operation, String fruit, int quantity) { | ||
this.operation = operation; | ||
this.fruit = fruit; | ||
this.quantity = quantity; | ||
} | ||
|
||
public Operation getOperation() { | ||
return operation; | ||
} | ||
|
||
public void setOperation(Operation operation) { | ||
this.operation = operation; | ||
} | ||
|
||
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 Operation getOperationByCode(String code) { | ||
return Arrays.stream(Operation.values()) | ||
.filter(o -> o.getCode().equals(code)) | ||
.findAny() | ||
.orElseThrow(() -> new RuntimeException("No such operation")); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/core/basesyntax/services/DataProcessorService.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.services; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataProcessorService { | ||
List<FruitTransaction> processInputData(List<String> dataFromFile); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/services/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.services; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReaderService { | ||
List<String> readFromFile(String filename); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/core/basesyntax/services/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.services; | ||
|
||
public interface FileWriterService { | ||
void writeToFile(String fileName, String report); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/core/basesyntax/services/OperationProcessor.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.services; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface OperationProcessor { | ||
void manageTransactions(List<FruitTransaction> transactions); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/core/basesyntax/services/ReportCreatorService.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.services; | ||
|
||
public interface ReportCreatorService { | ||
String createReport(); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/core/basesyntax/services/impl/DataProcessorServiceImpl.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,33 @@ | ||
package core.basesyntax.services.impl; | ||
|
||
import core.basesyntax.exception.InvalidDataException; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.services.DataProcessorService; | ||
import java.util.List; | ||
|
||
public class DataProcessorServiceImpl implements DataProcessorService { | ||
private static final String SPLIT_DELIMITER = ","; | ||
private static final int OPERATION_TYPE_INDEX = 0; | ||
private static final int FRUIT_TYPE_INDEX = 1; | ||
private static final int FRUIT_QUANTITY_INDEX = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> processInputData(List<String> dataFromFile) { | ||
return dataFromFile.stream() | ||
.map(this::getFruitTransaction) | ||
.toList(); | ||
} | ||
|
||
private FruitTransaction getFruitTransaction(String data) { | ||
String[] processedData = data.split(SPLIT_DELIMITER); | ||
FruitTransaction.Operation operation = FruitTransaction | ||
.Operation.getOperationByCode(processedData[OPERATION_TYPE_INDEX]); | ||
String fruitType = processedData[FRUIT_TYPE_INDEX]; | ||
int fruitQuantity = Integer.parseInt(processedData[FRUIT_QUANTITY_INDEX]); | ||
if (fruitQuantity < 0) { | ||
throw new InvalidDataException("Invalid Quantity, fruit quantity is: " | ||
+ fruitQuantity); | ||
} | ||
return new FruitTransaction(operation, fruitType, fruitQuantity); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/core/basesyntax/services/impl/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,25 @@ | ||
package core.basesyntax.services.impl; | ||
|
||
import core.basesyntax.services.FileReaderService; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileReaderServiceImpl implements FileReaderService { | ||
private static final int SKIP_LINE = 1; | ||
|
||
@Override | ||
public List<String> readFromFile(String filename) { | ||
List<String> linesFromFile = new ArrayList<>(); | ||
Path path = Paths.get(filename); | ||
try { | ||
Files.lines(path).skip(SKIP_LINE).forEach(linesFromFile::add); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read data from file : " + filename); | ||
} | ||
return linesFromFile; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/core/basesyntax/services/impl/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,17 @@ | ||
package core.basesyntax.services.impl; | ||
|
||
import core.basesyntax.services.FileWriterService; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class FileWriterServiceImpl implements FileWriterService { | ||
@Override | ||
public void writeToFile(String filePath, String report) { | ||
try { | ||
Files.writeString(Paths.get(filePath), report); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't find file by path" + filePath); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/core/basesyntax/services/impl/OperationProcessorImpl.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,21 @@ | ||
package core.basesyntax.services.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.services.OperationProcessor; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OperationProcessorImpl implements OperationProcessor { | ||
private final Map<FruitTransaction.Operation, OperationHandler> processedOperations; | ||
|
||
public OperationProcessorImpl( | ||
Map<FruitTransaction.Operation, OperationHandler> processedOperations) { | ||
this.processedOperations = processedOperations; | ||
} | ||
|
||
@Override | ||
public void manageTransactions(List<FruitTransaction> transactions) { | ||
transactions.forEach(t -> processedOperations.get(t.getOperation()).calculateOperation(t)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/core/basesyntax/services/impl/ReportCreatorServiceImpl.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.services.impl; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.services.ReportCreatorService; | ||
|
||
public class ReportCreatorServiceImpl implements ReportCreatorService { | ||
private static final String DEFAULT_MESSAGE = "fruit,quantity"; | ||
private static final String SPLIT_DELIMITER = ","; | ||
private final StringBuilder reportBuilder = new StringBuilder(); | ||
|
||
@Override | ||
public String createReport() { | ||
reportBuilder.append(DEFAULT_MESSAGE).append(System.lineSeparator()); | ||
Storage.STORAGE.forEach((key, value) -> reportBuilder.append(key) | ||
.append(SPLIT_DELIMITER) | ||
.append(value) | ||
.append(System.lineSeparator())); | ||
return reportBuilder.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,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
void calculateOperation(FruitTransaction transaction); | ||
} |
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,3 @@ | ||
fruit,quantity | ||
banana,152 | ||
apple,90 |