generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implemented the FruitShop using Solid principles #959
Open
dima-semeniuk
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
dima-semeniuk:develop
base: master
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 3 commits
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.dao.StorageDao; | ||
import core.basesyntax.dao.StorageDaoImpl; | ||
import core.basesyntax.model.Operation; | ||
import core.basesyntax.service.DataProcessing; | ||
import core.basesyntax.service.DataReader; | ||
import core.basesyntax.service.OperationHandler; | ||
import core.basesyntax.service.ReportCreator; | ||
import core.basesyntax.service.Writer; | ||
import core.basesyntax.service.handler.impl.BalanceOperationHandler; | ||
import core.basesyntax.service.handler.impl.PurchaseOperationHandler; | ||
import core.basesyntax.service.handler.impl.ReturnOperationHandler; | ||
import core.basesyntax.service.handler.impl.SupplyOperationHandler; | ||
import core.basesyntax.service.impl.CsvWriter; | ||
import core.basesyntax.service.impl.DataProcessingImpl; | ||
import core.basesyntax.service.impl.DataReaderFromCsv; | ||
import core.basesyntax.service.impl.FruitShopServiceImpl; | ||
import core.basesyntax.service.impl.ReportCreatorImpl; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.impl.OperationStrategyImpl; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String FILE_FROM = "src/main/resources/data.csv"; | ||
private static final String FILE_TO = "src/main/resources/report.csv"; | ||
|
||
public static void main(String[] args) { | ||
StorageDao storageDao = new StorageDaoImpl(); | ||
Map<Operation, OperationHandler> operationHandlerMap = new HashMap<>(); | ||
operationHandlerMap.put(Operation.BALANCE, | ||
new BalanceOperationHandler(storageDao)); | ||
operationHandlerMap.put(Operation.SUPPLY, | ||
new SupplyOperationHandler(storageDao)); | ||
operationHandlerMap.put(Operation.PURCHASE, | ||
new PurchaseOperationHandler(storageDao)); | ||
operationHandlerMap.put(Operation.RETURN, | ||
new ReturnOperationHandler(storageDao)); | ||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlerMap); | ||
DataReader dataReader = new DataReaderFromCsv(); | ||
DataProcessing dataProcessing = new DataProcessingImpl(operationStrategy); | ||
ReportCreator reportCreator = new ReportCreatorImpl(storageDao); | ||
Writer writer = new CsvWriter(); | ||
FruitShopServiceImpl fruitShopService = new FruitShopServiceImpl(dataReader, | ||
dataProcessing, reportCreator, writer); | ||
fruitShopService.createDailyReport(FILE_FROM, FILE_TO); | ||
} | ||
} |
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.dao; | ||
|
||
import core.basesyntax.model.Fruit; | ||
import java.util.Map; | ||
|
||
public interface StorageDao { | ||
void add(Fruit fruit, Integer amount); | ||
|
||
Map.Entry<Fruit, Integer> get(String name); | ||
|
||
boolean isInStorage(String name); | ||
|
||
Map<Fruit, Integer> getALl(); | ||
} |
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.dao; | ||
|
||
import core.basesyntax.db.Storage; | ||
import core.basesyntax.model.Fruit; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class StorageDaoImpl implements StorageDao { | ||
@Override | ||
public void add(Fruit fruit, Integer amount) { | ||
Storage.fruits.put(fruit, amount); | ||
} | ||
|
||
@Override | ||
public Map.Entry<Fruit, Integer> get(String name) { | ||
for (Map.Entry<Fruit, Integer> entry : Storage.fruits.entrySet()) { | ||
if (entry.getKey().getName().equals(name)) { | ||
return entry; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isInStorage(String name) { | ||
return (get(name) != null); | ||
} | ||
|
||
@Override | ||
public Map<Fruit, Integer> getALl() { | ||
return new HashMap<>(Storage.fruits); | ||
} | ||
} |
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 core.basesyntax.model.Fruit; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<Fruit, Integer> fruits = 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,41 @@ | ||
package core.basesyntax.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class Fruit { | ||
private String name; | ||
|
||
public Fruit(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Fruit fruit = (Fruit) o; | ||
return Objects.equals(name, fruit.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Fruit{" + "name='" + name + '\'' + '}'; | ||
} | ||
} |
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,31 @@ | ||
package core.basesyntax.model; | ||
|
||
public class FruitTransaction { | ||
private Operation operation; | ||
private String fruitName; | ||
private int amount; | ||
|
||
public Operation getOperation() { | ||
return operation; | ||
} | ||
|
||
public void setOperation(Operation operation) { | ||
this.operation = operation; | ||
} | ||
|
||
public String getFruitName() { | ||
return fruitName; | ||
} | ||
|
||
public void setFruitName(String fruit) { | ||
this.fruitName = fruit; | ||
} | ||
|
||
public int getAmount() { | ||
return amount; | ||
} | ||
|
||
public void setAmount(int amount) { | ||
this.amount = amount; | ||
} | ||
} |
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.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 getCodeOfOperation() { | ||
return code; | ||
} | ||
|
||
public static Operation getOperationOf(String letter) { | ||
return Arrays.stream(Operation.values()) | ||
.filter(o -> o.getCodeOfOperation().equals(letter)) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("There is no such operation by letter " | ||
+ "'" + letter + "'")); | ||
} | ||
} |
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 DataProcessing { | ||
void processTransaction(List<String> data); | ||
} |
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 java.util.List; | ||
|
||
public interface DataReader { | ||
List<String> readData(String file); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/core/basesyntax/service/OperationHandler.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; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
int MIN_AMOUNT = 0; | ||
|
||
void updateStorage(FruitTransaction transaction); | ||
|
||
default void validAmount(FruitTransaction transaction) { | ||
if (transaction.getAmount() < MIN_AMOUNT) { | ||
throw new RuntimeException("OPERATION " + transaction.getOperation() | ||
+ "! Amount is less then zero!!!"); | ||
} | ||
} | ||
} |
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 ReportCreator { | ||
String createReport(); | ||
} |
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 { | ||
void writeReportToFile(String report, String toFile); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/core/basesyntax/service/handler/impl/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,25 @@ | ||
package core.basesyntax.service.handler.impl; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.model.Fruit; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.OperationHandler; | ||
|
||
public class BalanceOperationHandler implements OperationHandler { | ||
private final StorageDao storageDao; | ||
|
||
public BalanceOperationHandler(StorageDao storageDao) { | ||
this.storageDao = storageDao; | ||
} | ||
|
||
@Override | ||
public void updateStorage(FruitTransaction transaction) { | ||
validAmount(transaction); | ||
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); | ||
} else { | ||
storageDao.get(transaction.getFruitName()).setValue(transaction.getAmount()); | ||
} | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/core/basesyntax/service/handler/impl/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,36 @@ | ||
package core.basesyntax.service.handler.impl; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.model.Fruit; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.OperationHandler; | ||
import java.util.Map; | ||
|
||
public class PurchaseOperationHandler implements OperationHandler { | ||
private final StorageDao storageDao; | ||
|
||
public PurchaseOperationHandler(StorageDao storageDao) { | ||
this.storageDao = storageDao; | ||
} | ||
|
||
@Override | ||
public void updateStorage(FruitTransaction transaction) { | ||
validAmountOfFruit(transaction); | ||
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName()); | ||
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount - fruitAmountFromTransaction); | ||
|
||
} | ||
|
||
private void validAmountOfFruit(FruitTransaction transaction) { | ||
validAmount(transaction); | ||
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
throw new RuntimeException("There is no such fruit!!!"); | ||
} | ||
if (storageDao.get(transaction.getFruitName()).getValue() < transaction.getAmount()) { | ||
throw new RuntimeException("Amount of " + transaction.getFruitName() | ||
+ " isn't enough!!!"); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/core/basesyntax/service/handler/impl/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,28 @@ | ||
package core.basesyntax.service.handler.impl; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.model.Fruit; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.OperationHandler; | ||
import java.util.Map; | ||
|
||
public class ReturnOperationHandler implements OperationHandler { | ||
private final StorageDao storageDao; | ||
|
||
public ReturnOperationHandler(StorageDao storageDao) { | ||
this.storageDao = storageDao; | ||
} | ||
|
||
@Override | ||
public void updateStorage(FruitTransaction transaction) { | ||
validAmount(transaction); | ||
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); | ||
} else { | ||
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName()); | ||
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/core/basesyntax/service/handler/impl/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,28 @@ | ||
package core.basesyntax.service.handler.impl; | ||
|
||
import core.basesyntax.dao.StorageDao; | ||
import core.basesyntax.model.Fruit; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.OperationHandler; | ||
import java.util.Map; | ||
|
||
public class SupplyOperationHandler implements OperationHandler { | ||
private final StorageDao storageDao; | ||
|
||
public SupplyOperationHandler(StorageDao storageDao) { | ||
this.storageDao = storageDao; | ||
} | ||
|
||
@Override | ||
public void updateStorage(FruitTransaction transaction) { | ||
validAmount(transaction); | ||
if (!storageDao.isInStorage(transaction.getFruitName())) { | ||
storageDao.add(new Fruit(transaction.getFruitName()), transaction.getAmount()); | ||
} else { | ||
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName()); | ||
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); | ||
} | ||
} | ||
} |
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.
Probably we can make it
protected
orprivate
here?