-
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
add solution1 #799
Open
evgenkolner
wants to merge
4
commits into
mate-academy:main
Choose a base branch
from
evgenkolner:hw1
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
add solution1 #799
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,49 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.DataConverterService; | ||
import core.basesyntax.service.FileWriteService; | ||
import core.basesyntax.service.ProccessService; | ||
import core.basesyntax.service.ReaderService; | ||
import core.basesyntax.service.ReportCreatorService; | ||
import core.basesyntax.service.impl.DataConverterServiceImpl; | ||
import core.basesyntax.service.impl.FileReaderServiceImpl; | ||
import core.basesyntax.service.impl.FileWriteServiceImpl; | ||
import core.basesyntax.service.impl.ProccesServiceImpl; | ||
import core.basesyntax.service.impl.ReportCreatorServiceImpl; | ||
import core.basesyntax.strategy.OperationService; | ||
import core.basesyntax.strategy.impl.BalanceOperation; | ||
import core.basesyntax.strategy.impl.PurchaceOperation; | ||
import core.basesyntax.strategy.impl.ReturnOperation; | ||
import core.basesyntax.strategy.impl.SupplyOperation; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String FROM_FILE_NAME = "src/main/resources/text.txt"; | ||
private static final String TO_FILE_NAME = "src/main/resources/report.txt"; | ||
|
||
public static void main(String[] args) { | ||
ReaderService readerService = new FileReaderServiceImpl(); | ||
DataConverterService dataConverterService = new DataConverterServiceImpl(); | ||
ProccessService proccessService = new ProccesServiceImpl(getOperationMap()); | ||
ReportCreatorService reportCreatorService = new ReportCreatorServiceImpl(); | ||
FileWriteService fileWriteService = new FileWriteServiceImpl(); | ||
List<String> strings = readerService.readFromFile(FROM_FILE_NAME); | ||
List<FruitTransaction> fruitTransactions = dataConverterService.convertText(strings); | ||
proccessService.proccessing(fruitTransactions); | ||
String reportString = reportCreatorService.createReport(); | ||
fileWriteService.writeToFile(reportString, TO_FILE_NAME); | ||
} | ||
|
||
private static Map<Operation, OperationService> getOperationMap() { | ||
Map<Operation, OperationService> operationMap = new HashMap<>(); | ||
operationMap.put(Operation.BALANCE, new BalanceOperation()); | ||
operationMap.put(Operation.RETURN,new ReturnOperation()); | ||
operationMap.put(Operation.PURCHASE,new PurchaceOperation()); | ||
operationMap.put(Operation.SUPPLY, new SupplyOperation()); | ||
return operationMap; | ||
} | ||
} |
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,37 @@ | ||
package core.basesyntax.model; | ||
|
||
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; | ||
} | ||
} |
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,27 @@ | ||
package core.basesyntax.model; | ||
|
||
import java.util.Arrays; | ||
|
||
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)) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("Can`t find operation by code")); | ||
} | ||
} |
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 { | ||
public List<FruitTransaction> convertText(List<String> strings); | ||
} |
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 FileWriteService { | ||
void writeToFile(String report, String toFileName); | ||
} |
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 ProccessService { | ||
void proccessing(List<FruitTransaction> fruitTransactionsList); | ||
} |
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 ReaderService { | ||
List<String> readFromFile(String pathToFile); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/core/basesyntax/service/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.service; | ||
|
||
public interface ReportCreatorService { | ||
String createReport(); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/core/basesyntax/service/impl/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.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.DataConverterService; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataConverterServiceImpl implements DataConverterService { | ||
private static final String SEPARATOR = ","; | ||
private static final int INDEX_BY_TRANSACTION = 0; | ||
private static final int INDEX_BY_FRUIT = 1; | ||
private static final int INDEX_BY_AMOUNT = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> convertText(List<String> strings) { | ||
List<FruitTransaction> dataFruitTransaction = new ArrayList<>(); | ||
for (int i = 0; i < strings.size(); i++) { | ||
String[] data = strings.get(i).split(SEPARATOR); | ||
dataFruitTransaction | ||
.add(new FruitTransaction( | ||
Operation.getOperationByCode(data[INDEX_BY_TRANSACTION]), | ||
data[INDEX_BY_FRUIT], | ||
Integer.parseInt(data[INDEX_BY_AMOUNT]) | ||
)); | ||
} | ||
return dataFruitTransaction; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/core/basesyntax/service/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.service.impl; | ||
|
||
import core.basesyntax.service.ReaderService; | ||
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 ReaderService { | ||
private static final int FIRST_LINE = 1; | ||
|
||
@Override | ||
public List<String> readFromFile(String pathToFile) { | ||
List<String> linesFromFile = new ArrayList<>(); | ||
Path path = Paths.get(pathToFile); | ||
try { | ||
Files.lines(path).skip(FIRST_LINE).forEach(linesFromFile::add); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can`t read data from file" + pathToFile); | ||
} | ||
return linesFromFile; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/core/basesyntax/service/impl/FileWriteServiceImpl.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.service.impl; | ||
|
||
import core.basesyntax.service.FileWriteService; | ||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriteServiceImpl implements FileWriteService { | ||
@Override | ||
public void writeToFile(String report, String toFileName) { | ||
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toFileName))) { | ||
bufferedWriter.write(report); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't write to file " + toFileName); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.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.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.ProccessService; | ||
import core.basesyntax.strategy.OperationService; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ProccesServiceImpl implements ProccessService { | ||
private final Map<Operation, OperationService> processedStoreHanler; | ||
|
||
public ProccesServiceImpl(Map<Operation, OperationService> processedStoreHanler) { | ||
this.processedStoreHanler = processedStoreHanler; | ||
} | ||
|
||
@Override | ||
public void proccessing(List<FruitTransaction> fruitTransactionsList) { | ||
for (FruitTransaction fruitTransaction : fruitTransactionsList) { | ||
processedStoreHanler.get(fruitTransaction.getOperation()).doOperation(fruitTransaction); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/core/basesyntax/service/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,21 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.ReportCreatorService; | ||
import core.basesyntax.storage.Storage; | ||
import java.util.Map; | ||
|
||
public class ReportCreatorServiceImpl implements ReportCreatorService { | ||
private static final String FIRST_STRING = "fruit,quantity" + System.lineSeparator(); | ||
private static final String COMMA = ","; | ||
|
||
@Override | ||
public String createReport() { | ||
StringBuilder builder = new StringBuilder(FIRST_STRING); | ||
builder.append(System.lineSeparator()); | ||
for (Map.Entry<String,Integer> fruits : Storage.getStorage().entrySet()) { | ||
builder.append(fruits.getKey()).append(COMMA).append(fruits.getValue()) | ||
.append(System.lineSeparator()); | ||
} | ||
return builder.toString().trim(); | ||
} | ||
} |
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.storage; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
private static Map<String, Integer> storage = new HashMap<>(); | ||
|
||
public static Map<String, Integer> getStorage() { | ||
return storage; | ||
} | ||
} |
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 OperationService { | ||
void doOperation(FruitTransaction fruitTransaction); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/core/basesyntax/strategy/impl/BalanceOperation.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.strategy.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.storage.Storage; | ||
import core.basesyntax.strategy.OperationService; | ||
|
||
public class BalanceOperation implements OperationService { | ||
@Override | ||
public void doOperation(FruitTransaction fruitTransaction) { | ||
Storage.getStorage().put(fruitTransaction.getFruit(),fruitTransaction.getQuantity()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.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.strategy.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.storage.Storage; | ||
import core.basesyntax.strategy.OperationService; | ||
|
||
public class PurchaceOperation implements OperationService { | ||
@Override | ||
public void doOperation(FruitTransaction fruitTransaction) { | ||
int balance = Storage.getStorage().get(fruitTransaction.getFruit()); | ||
int purchase = balance - fruitTransaction.getQuantity(); | ||
if (purchase < 0) { | ||
throw new RuntimeException("Not enough quantity in shop"); | ||
} | ||
Storage.getStorage().put(fruitTransaction.getFruit(),purchase); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/core/basesyntax/strategy/impl/ReturnOperation.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,14 @@ | ||
package core.basesyntax.strategy.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.storage.Storage; | ||
import core.basesyntax.strategy.OperationService; | ||
|
||
public class ReturnOperation implements OperationService { | ||
@Override | ||
public void doOperation(FruitTransaction fruitTransaction) { | ||
int balance = Storage.getStorage().get(fruitTransaction.getFruit()); | ||
int retOperation = balance + fruitTransaction.getQuantity(); | ||
Storage.getStorage().put((fruitTransaction.getFruit()),retOperation); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/core/basesyntax/strategy/impl/SupplyOperation.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,14 @@ | ||
package core.basesyntax.strategy.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.storage.Storage; | ||
import core.basesyntax.strategy.OperationService; | ||
|
||
public class SupplyOperation implements OperationService { | ||
@Override | ||
public void doOperation(FruitTransaction fruitTransaction) { | ||
int balance = Storage.getStorage().get(fruitTransaction.getFruit()); | ||
int supply = balance + fruitTransaction.getQuantity(); | ||
Storage.getStorage().put(fruitTransaction.getFruit(), supply); | ||
} | ||
} |
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,2 @@ | ||
fruit,quantity | ||
banana,40 |
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,4 @@ | ||
package core.basesyntax; | ||
|
||
class MainTest { | ||
} |
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.
Redundant class