-
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
covered solution with tests #817
base: main
Are you sure you want to change the base?
Changes from 3 commits
cdcc324
c34f14d
90b9866
8fa3ad9
fe6051d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea/* | ||
*.iml | ||
target/* | ||
**/.DS_Store | ||
|
This file was deleted.
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); | ||
} | ||
} |
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(); | ||
} |
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); | ||
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. (get(name) != null) 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. still exists. |
||
} | ||
|
||
@Override | ||
public Map<Fruit, Integer> getALl() { | ||
return new HashMap<>(Storage.fruits); | ||
} | ||
} |
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<>(); | ||
} |
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 + '\'' + '}'; | ||
} | ||
} |
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; | ||
} | ||
} |
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)) | ||
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. please choose a proper name for parameters in the lambdas; other developers will need to spend additional time figuring out what 'o' means 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. still exists. |
||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("There is no such operation by letter " | ||
+ "'" + letter + "'")); | ||
} | ||
} |
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); | ||
} |
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); | ||
|
||
} |
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!!!"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface ReportCreator { | ||
String createReport(); | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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()); | ||
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. transaction.getFruitName() can be extracted to a separate variable to avoid code duplication 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. still exists. |
||
} | ||
} | ||
} |
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); | ||||
|
||||
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. empty line 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
|
||||
} | ||||
|
||||
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()) { | ||||
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.
|
||||
throw new RuntimeException("Amount of " + transaction.getFruitName() | ||||
+ " isn't enough!!!"); | ||||
} | ||||
} | ||||
} |
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()); | ||
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.
|
||
} else { | ||
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName()); | ||
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); | ||
} | ||
} | ||
} |
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()); | ||
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.
|
||
} else { | ||
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName()); | ||
Integer fruitAmount = fruitAndAmount.getValue(); | ||
Integer fruitAmountFromTransaction = transaction.getAmount(); | ||
fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction); | ||
} | ||
} | ||
} |
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.
Minor: a little problem with the name
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.
still exists.