-
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 test for service #836
Open
DevIgork
wants to merge
7
commits into
mate-academy:main
Choose a base branch
from
DevIgork:study
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 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
307c4d5
added test for service
DevIgork 4d3f124
fixed maven check
DevIgork c2ba34f
added some optimisation
DevIgork d93112a
generic improvement
DevIgork 9a5866b
added handler test and dao test
DevIgork 83cb494
added minor changers
DevIgork 3f8c04e
added maven fix
DevIgork 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,4 +119,4 @@ | |
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
</project> | ||
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,55 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.dao.FruitDao; | ||
import core.basesyntax.dao.FruitDaoImp; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.Parser; | ||
import core.basesyntax.service.Producer; | ||
import core.basesyntax.service.Reader; | ||
import core.basesyntax.service.Writer; | ||
import core.basesyntax.service.impl.FruitTransactionParser; | ||
import core.basesyntax.service.impl.ProduceMapReportService; | ||
import core.basesyntax.service.impl.ReadFromCsvService; | ||
import core.basesyntax.service.impl.WriteCsvService; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.OperationStrategyImp; | ||
import core.basesyntax.strategy.handler.BalanceHandler; | ||
import core.basesyntax.strategy.handler.OperationHandler; | ||
import core.basesyntax.strategy.handler.PurchaseHandler; | ||
import core.basesyntax.strategy.handler.ReturnHandler; | ||
import core.basesyntax.strategy.handler.SupplyHandler; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
|
||
private static final String FILE_FROM = "src/main/resources/csvFile.csv"; | ||
private static final String FILE_TO = "src/main/resources/Report.csv"; | ||
|
||
public static void main(String[] args) { | ||
Map<Operation, OperationHandler> operationHandlerMap = new HashMap<>(); | ||
Reader reader = new ReadFromCsvService(); | ||
FruitDao fruitDao = new FruitDaoImp(); | ||
initializeOperationHandlers(operationHandlerMap, fruitDao); | ||
OperationStrategy operationStrategy = new OperationStrategyImp(operationHandlerMap); | ||
List<String> fileData = reader.read(FILE_FROM); | ||
List<FruitTransaction> fruitTransactions; | ||
Parser parser = new FruitTransactionParser(); | ||
fruitTransactions = parser.parse(fileData); | ||
fruitDao.add(fruitTransactions); | ||
operationStrategy.getHandle(fruitTransactions); | ||
Writer writer = new WriteCsvService(); | ||
Producer producer = new ProduceMapReportService(); | ||
writer.write(producer.produceReport(fruitDao.getStorage()), FILE_TO); | ||
} | ||
|
||
private static void initializeOperationHandlers(Map<Operation, | ||
OperationHandler> handlerMap, FruitDao fruitDao) { | ||
handlerMap.put(Operation.BALANCE, new BalanceHandler(fruitDao)); | ||
handlerMap.put(Operation.PURCHASE, new PurchaseHandler(fruitDao)); | ||
handlerMap.put(Operation.RETURN, new ReturnHandler(fruitDao)); | ||
handlerMap.put(Operation.SUPPLY, new SupplyHandler(fruitDao)); | ||
} | ||
} |
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.dao; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface FruitDao { | ||
Map<String, Integer> getStorage(); | ||
|
||
Integer get(String key); | ||
|
||
void add(String type, Integer quantity); | ||
|
||
void add(List<FruitTransaction> list); | ||
} |
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 static core.basesyntax.db.Storage.STORAGE; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class FruitDaoImp implements FruitDao { | ||
public Map<String, Integer> getStorage() { | ||
return STORAGE; | ||
} | ||
|
||
public void add(String type, Integer quantity) { | ||
STORAGE.put(type, quantity); | ||
} | ||
|
||
@Override | ||
public void add(List<FruitTransaction> list) { | ||
for (FruitTransaction item : list) { | ||
STORAGE.put(item.getFruit(), 0); | ||
} | ||
} | ||
|
||
public Integer get(String key) { | ||
return STORAGE.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,9 @@ | ||
package core.basesyntax.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String, Integer> STORAGE = 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,55 @@ | ||
package core.basesyntax.model; | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
FruitTransaction that = (FruitTransaction) o; | ||
|
||
if (quantity != that.quantity) { | ||
return false; | ||
} | ||
if (operation != that.operation) { | ||
return false; | ||
} | ||
return Objects.equals(fruit, that.fruit); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = operation.hashCode(); | ||
result = 31 * result + (fruit != null ? fruit.hashCode() : 0); | ||
result = 31 * result + quantity; | ||
return result; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public int getQuantity() { | ||
return 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,18 @@ | ||
package core.basesyntax.model; | ||
|
||
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; | ||
} | ||
} |
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 Parser { | ||
List<FruitTransaction> parse(List<String> fileData); | ||
} |
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 Producer { | ||
String produceReport(Map<String, Integer> map); | ||
} |
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 Reader { | ||
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 Writer { | ||
boolean write(String content, String path); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/core/basesyntax/service/impl/FruitTransactionParser.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,36 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.Parser; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FruitTransactionParser implements Parser { | ||
private static final String SPLITER = ","; | ||
private static final int INDEX_OF_OPERATION = 0; | ||
private static final int INDEX_OF_FRUIT_NAME = 1; | ||
private static final int INDEX_OF_QUANTITY = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> parse(List<String> fileData) { | ||
if (fileData == null) { | ||
throw new IllegalArgumentException("Can't parse this list because list is null"); | ||
} | ||
List<FruitTransaction> fruits = new ArrayList<>(); | ||
for (String line : fileData) { | ||
String[] splitedLine = line.split(SPLITER); | ||
if (splitedLine.length > 3 || splitedLine.length <= 1) { | ||
throw new IllegalArgumentException("Can't parse this list"); | ||
} | ||
for (Operation operation : Operation.values()) { | ||
if (operation.getCode().equalsIgnoreCase(splitedLine[INDEX_OF_OPERATION])) { | ||
fruits.add(new FruitTransaction(operation, | ||
splitedLine[INDEX_OF_FRUIT_NAME], | ||
Integer.parseInt(splitedLine[INDEX_OF_QUANTITY]))); | ||
} | ||
} | ||
} | ||
return fruits; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/core/basesyntax/service/impl/ProduceMapReportService.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.service.impl; | ||
|
||
import core.basesyntax.service.Producer; | ||
import java.util.Map; | ||
|
||
public class ProduceMapReportService implements Producer { | ||
private static final String NEW_LINE = System.lineSeparator(); | ||
private static final String COMA = ","; | ||
|
||
@Override | ||
public String produceReport(Map<String, Integer> map) { | ||
if (map == null) { | ||
throw new IllegalArgumentException("can't produce this map because map is null"); | ||
} | ||
StringBuilder report = new StringBuilder(); | ||
int size = map.size(); | ||
int index = 0; | ||
for (Map.Entry<String, Integer> entry : map.entrySet()) { | ||
if (entry.getValue() < 0) { | ||
throw new IllegalArgumentException("can't produce this map integer value below 0"); | ||
} | ||
report.append(entry.getKey()) | ||
.append(COMA) | ||
.append(entry.getValue()); | ||
if (index < size - 1) { | ||
report.append(NEW_LINE); | ||
} | ||
index++; | ||
} | ||
return report.toString(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/core/basesyntax/service/impl/ReadFromCsvService.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,28 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.Reader; | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ReadFromCsvService implements Reader { | ||
private static final int INDEX_OF_TITLE = 0; | ||
private static final String CANT_READ_EXCEPTION_MESSAGE = "can't read file"; | ||
|
||
@Override | ||
public List<String> read(String path) { | ||
List<String> data = new ArrayList<>(); | ||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) { | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null) { | ||
data.add(line.trim()); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(CANT_READ_EXCEPTION_MESSAGE + path,e); | ||
} | ||
data.remove(INDEX_OF_TITLE); | ||
return data; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/core/basesyntax/service/impl/WriteCsvService.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.impl; | ||
|
||
import core.basesyntax.service.Writer; | ||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class WriteCsvService implements Writer { | ||
private static final String TITLE_TEXT = "fruit,quantity"; | ||
private static final String RUNTIME_EXCEPTION_MESSAGE = "Can't write file "; | ||
|
||
@Override | ||
public boolean write(String content, String path) { | ||
if (!path.endsWith(".csv")) { | ||
throw new IllegalArgumentException("Csv writer write only csv file"); | ||
} | ||
try { | ||
BufferedWriter bufferedWriter = new BufferedWriter( | ||
new FileWriter(path)); | ||
bufferedWriter.write(TITLE_TEXT); | ||
bufferedWriter.newLine(); | ||
bufferedWriter.write(content); | ||
bufferedWriter.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(RUNTIME_EXCEPTION_MESSAGE + path, e); | ||
} | ||
return true; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface OperationStrategy { | ||
boolean getHandle(List<FruitTransaction> fruitTransactions); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/core/basesyntax/strategy/OperationStrategyImp.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,28 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.strategy.handler.OperationHandler; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImp implements OperationStrategy { | ||
private static final String ILLEGAL_ARGUMENT_MESSAGE = "Cant get handle the list size is 0"; | ||
private Map<Operation, OperationHandler> operationOperationHandlerMap; | ||
|
||
public OperationStrategyImp(Map<Operation, | ||
OperationHandler> operationOperationHandlerMap) { | ||
this.operationOperationHandlerMap = operationOperationHandlerMap; | ||
} | ||
|
||
@Override | ||
public boolean getHandle(List<FruitTransaction> fruitTransaction) { | ||
if (fruitTransaction.size() == 0) { | ||
throw new IllegalArgumentException(ILLEGAL_ARGUMENT_MESSAGE); | ||
} | ||
for (FruitTransaction fruit : fruitTransaction) { | ||
operationOperationHandlerMap.get(fruit.getOperation()).handle(fruit); | ||
} | ||
return true; | ||
} | ||
} |
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.
Please don't commit this change, it's redundant