Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
*.iml
target/*
**/.DS_Store

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
Binary file added src/main/java/core/.DS_Store
Binary file not shown.
Binary file added src/main/java/core/basesyntax/.DS_Store
Binary file not shown.
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/main/java/core/basesyntax/Main.java
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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/dao/StorageDao.java
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();

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still exists.

}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/dao/StorageDaoImpl.java
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(get(name) != null)
these parentheses are redundant

Copy link

Choose a reason for hiding this comment

The 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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
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<>();
}
41 changes: 41 additions & 0 deletions src/main/java/core/basesyntax/model/Fruit.java
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 + '\'' + '}';
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/model/Operation.java
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))

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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 + "'"));
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/DataProcessing.java
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);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/DataReader.java
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 src/main/java/core/basesyntax/service/OperationHandler.java
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!!!");
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/ReportCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface ReportCreator {
String createReport();
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/Writer.java
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());

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line

Copy link

Choose a reason for hiding this comment

The 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()) {
Copy link

Choose a reason for hiding this comment

The 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

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());
Copy link

Choose a reason for hiding this comment

The 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

} 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());
Copy link

Choose a reason for hiding this comment

The 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

} else {
Map.Entry<Fruit, Integer> fruitAndAmount = storageDao.get(transaction.getFruitName());
Integer fruitAmount = fruitAndAmount.getValue();
Integer fruitAmountFromTransaction = transaction.getAmount();
fruitAndAmount.setValue(fruitAmount + fruitAmountFromTransaction);
}
}
}
Loading
Loading