-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added tests for Fruit Shop #790
Open
dmytrocherepov
wants to merge
6
commits into
mate-academy:main
Choose a base branch
from
dmytrocherepov:fruit-shop-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4080a89
added tests for Fruit Shop
dmytrocherepov 69c1c5a
added fixes
dmytrocherepov caea4f8
added tests for task
dmytrocherepov 22d1097
added fixes
dmytrocherepov 9872b22
added fixes after review
dmytrocherepov a91d0e7
added fixes
dmytrocherepov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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/excteption/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.excteption; | ||
|
||
public class InvalidDataException extends RuntimeException { | ||
public InvalidDataException(String message) { | ||
super(message); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.excteption.InvalidDataException; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class BalanceOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
if (transaction.getQuantity() < 0) { | ||
throw new InvalidDataException("Negative quantity"); | ||
} else if (transaction.getFruit() == null || transaction.getOperation() == null) { | ||
throw new NullPointerException("Invalid transaction or fruit type"); | ||
} | ||
Storage.STORAGE.put(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.excteption.InvalidDataException; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class PurchaseOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
|
||
if (transaction.getQuantity() < 0) { | ||
throw new InvalidDataException("Negative quantity"); | ||
} else if (transaction.getFruit() == null) { | ||
throw new NullPointerException("Invalid fruit type"); | ||
} | ||
int currentAmount = Storage.STORAGE.get(transaction.getFruit()); | ||
int quantity = transaction.getQuantity(); | ||
int purchaseResult = currentAmount - quantity; | ||
if (purchaseResult < 0) { | ||
throw new InvalidDataException("Not enough fruits in storage"); | ||
} | ||
Storage.STORAGE.put(transaction.getFruit(), purchaseResult); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||||||
package core.basesyntax.handlers; | ||||||
|
||||||
import core.basesyntax.db.Storage; | ||||||
import core.basesyntax.excteption.InvalidDataException; | ||||||
import core.basesyntax.model.FruitTransaction; | ||||||
import core.basesyntax.strategy.OperationHandler; | ||||||
|
||||||
public class ReturnOperationHandler implements OperationHandler { | ||||||
@Override | ||||||
public void calculateOperation(FruitTransaction transaction) { | ||||||
if (transaction.getQuantity() < 0) { | ||||||
throw new InvalidDataException("Negative quantity"); | ||||||
} else if (transaction.getFruit() == null) { | ||||||
throw new NullPointerException("Invalid fruit type"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same everywhere |
||||||
} | ||||||
int currentAmount = Storage.STORAGE.get(transaction.getFruit()); | ||||||
int quantity = transaction.getQuantity(); | ||||||
int returnResult = currentAmount + quantity; | ||||||
Storage.STORAGE.put(transaction.getFruit(), returnResult); | ||||||
} | ||||||
} |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
package core.basesyntax.handlers; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.excteption.InvalidDataException; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.strategy.OperationHandler; | ||
|
||
public class SupplyOperationHandler implements OperationHandler { | ||
@Override | ||
public void calculateOperation(FruitTransaction transaction) { | ||
if (transaction.getQuantity() < 0) { | ||
throw new InvalidDataException("Negative quantity"); | ||
} else if (transaction.getFruit() == null || transaction.getOperation() == null) { | ||
throw new NullPointerException("Invalid transaction or fruit type"); | ||
} | ||
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,83 @@ | ||
package core.basesyntax.model; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
FruitTransaction that = (FruitTransaction) o; | ||
return quantity == that.quantity | ||
&& operation == that.operation | ||
&& Objects.equals(fruit, that.fruit); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(operation, fruit, 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(); | ||
} |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
package core.basesyntax.services.impl; | ||
|
||
import core.basesyntax.excteption.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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why u changed this ?