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

test coverage fruit-shop task #808

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.WriterService;
import core.basesyntax.service.impl.FruitShopService;
import core.basesyntax.service.impl.OperationStrategyImpl;
import core.basesyntax.service.impl.ParserServiceImpl;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.service.impl.operationhadler.BalanceHandler;
import core.basesyntax.service.impl.operationhadler.PurchaseHandler;
import core.basesyntax.service.impl.operationhadler.ReturnHandler;
import core.basesyntax.service.impl.operationhadler.SupplyHandler;
import core.basesyntax.strategy.OperationHandler;
import java.util.List;
import java.util.Map;

public class Main {
private static final String INPUT_FILE_PATH = "src/main/resources/FruitFiles.csv";
private static final String REPORT_FILE_PATH = "src/main/resources/reportFiles/report.csv";

public static void main(String[] args) {
Map<FruitTransaction.Operation, OperationHandler> operationStrategyMap =
Map.of(FruitTransaction.Operation.BALANCE, new BalanceHandler(),
FruitTransaction.Operation.PURCHASE, new PurchaseHandler(),
FruitTransaction.Operation.SUPPLY, new SupplyHandler(),
FruitTransaction.Operation.RETURN, new ReturnHandler());
ReaderService fileReader = new ReaderServiceImpl();
List<String> dataFromFile = fileReader.readFromCsvFile(INPUT_FILE_PATH);
ParserServiceImpl fileParser = new ParserServiceImpl();
List<FruitTransaction> operation = fileParser.parseData(dataFromFile);
FruitShopService fruitShop =
new FruitShopService(new OperationStrategyImpl(
operationStrategyMap));
fruitShop.processOfOperations(operation);
ReportServiceImpl report = new ReportServiceImpl();
String dataForReport = report.reportPreparation();
WriterService writer = new WriterServiceImpl();
writer.writeData(dataForReport, REPORT_FILE_PATH);
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.db;

import java.util.HashMap;
import java.util.Map;

public class Storage {
public static final Map<String, Integer> fruitsStorage = new HashMap<>();
}
60 changes: 60 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package core.basesyntax.model;

public class FruitTransaction {
private Operation operation;
private String fruit;
private int 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;
}

@Override
public String toString() {
return "FruitsTransaction{" + "operation=" + operation + ", fruit='" + fruit + '\'' + ", "
+ "quantity=" + quantity + '}';
}

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 fromString(String code) {
for (Operation operation : Operation.values()) {
if (operation.code.equals(code)) {
return operation;
}
}
throw new IllegalArgumentException("Unknown operation - " + code + "!!!");
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ParserService.java
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 ParserService {
List<FruitTransaction> parseData(List<String> dataFromFile);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReaderService.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 ReaderService {
List<String> readFromCsvFile(String path);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface ReportService {
String reportPreparation();
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/WriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface WriterService {
void writeData(String data, String outputPath);
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/service/impl/FruitShopService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationStrategy;
import java.util.List;

public class FruitShopService {
private final OperationStrategy strategy;

public FruitShopService(OperationStrategy strategy) {
this.strategy = strategy;
}

public void processOfOperations(List<FruitTransaction> operations) {
for (FruitTransaction operation : operations) {
strategy.getOperationHandler(operation.getOperation())
.handleTransaction(operation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {
private final Map<FruitTransaction.Operation, OperationHandler> fruitsMap;

public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> fruitsMap) {
this.fruitsMap = fruitsMap;
}

@Override
public OperationHandler getOperationHandler(FruitTransaction.Operation operationType) {
return fruitsMap.get(operationType);
}
}
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.ParserService;
import java.util.ArrayList;
import java.util.List;

public class ParserServiceImpl implements ParserService {
private static final String SEPARATOR = ",";
private static final int OPERATION_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int QUANTITY_INDEX = 2;
private static final int TITLE_INDEX = 0;

@Override
public List<FruitTransaction> parseData(List<String> dataFromFile) {
dataFromFile.remove(TITLE_INDEX);
List<FruitTransaction> operationList = new ArrayList<>();
for (String record : dataFromFile) {
String[] array = record.split(SEPARATOR);
FruitTransaction operation = new FruitTransaction();
operation.setOperation(
FruitTransaction.Operation.fromString(array[OPERATION_INDEX]));
operation.setFruit(array[FRUIT_INDEX]);
operation.setQuantity(Integer.parseInt(array[QUANTITY_INDEX]));
operationList.add(operation);
}
return operationList;
}
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.util.List;

public class ReaderServiceImpl implements ReaderService {
@Override
public List<String> readFromCsvFile(String filePath) {
try {
return Files.readAllLines(Path.of(filePath));
} catch (IOException e) {
throw new RuntimeException("Can't read file " + filePath, e);
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.service.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.service.ReportService;
import java.util.Map;

public class ReportServiceImpl implements ReportService {
private static final String TITLE = "fruit,quantity\n";
private static final String DELIMITER = ",";

@Override
public String reportPreparation() {
Map<String, Integer> reportMap = Storage.fruitsStorage;
StringBuilder reportString = new StringBuilder(TITLE);
for (Map.Entry<String, Integer> entry : reportMap.entrySet()) {
reportString.append(entry.getKey()).append(DELIMITER).append(entry.getValue())
.append(System.lineSeparator());
}
return reportString.toString();
}
}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.WriterService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class WriterServiceImpl implements WriterService {
@Override
public void writeData(String data, String outputPath) {
try {
Files.writeString(Path.of(outputPath), data);
} catch (IOException e) {
throw new RuntimeException("Can't write to file " + outputPath, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.service.impl.operationhadler;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class BalanceHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction) {
Storage.fruitsStorage.put(transaction.getFruit(), transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.impl.operationhadler;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class PurchaseHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction) {
int result = Storage.fruitsStorage.get(transaction.getFruit())
- transaction.getQuantity();
if (result < 0) {
throw new RuntimeException("Not enough fruits!");
}
Storage.fruitsStorage.put(transaction.getFruit(), result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.service.impl.operationhadler;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class ReturnHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction) {
int currentValue = Storage.fruitsStorage.get(transaction.getFruit());
Storage.fruitsStorage.put(transaction.getFruit(), currentValue
+ transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.service.impl.operationhadler;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class SupplyHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction) {
int currentQuantity = Storage.fruitsStorage.get(transaction.getFruit());
Storage.fruitsStorage.put(transaction.getFruit(), currentQuantity
+ transaction.getQuantity());
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationHandler.java
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 OperationHandler {
void handleTransaction(FruitTransaction transaction);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
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 OperationStrategy {
OperationHandler getOperationHandler(FruitTransaction.Operation operation);
}
Empty file.
9 changes: 9 additions & 0 deletions src/main/resources/FruitFiles.csv
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
3 changes: 3 additions & 0 deletions src/main/resources/reportFiles/report.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit,quantity
banana,152
apple,90
7 changes: 0 additions & 7 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.

Loading
Loading