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

added tests for Fruit Shop #790

Open
wants to merge 6 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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.8</minimum>
<minimum>0.8 </minimum>

Choose a reason for hiding this comment

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

why u changed this ?

</limit>
</limits>
</rule>
Expand All @@ -97,7 +97,7 @@
</executions>
<configuration>
<excludes>
<exclude>**/Main*</exclude>
<exclude>**/FruitShop*</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/core/basesyntax/FruitShop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package core.basesyntax;

import core.basesyntax.handlers.BalanceOperationHandler;
import core.basesyntax.handlers.PurchaseOperationHandler;
import core.basesyntax.handlers.ReturnOperationHandler;
import core.basesyntax.handlers.SupplyOperationHandler;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.services.DataProcessorService;
import core.basesyntax.services.FileReaderService;
import core.basesyntax.services.FileWriterService;
import core.basesyntax.services.OperationProcessor;
import core.basesyntax.services.ReportCreatorService;
import core.basesyntax.services.impl.DataProcessorServiceImpl;
import core.basesyntax.services.impl.FileReaderServiceImpl;
import core.basesyntax.services.impl.FileWriterServiceImpl;
import core.basesyntax.services.impl.OperationProcessorImpl;
import core.basesyntax.services.impl.ReportCreatorServiceImpl;
import core.basesyntax.strategy.OperationHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FruitShop {
private static final String INPUT_FILE_PATH = "src/main/java/resources/input.csv";
private static final String OUTPUT_FILE_PATH = "src/main/java/resources/output.csv";

public static void main(String[] args) {
FileReaderService fileReaderService = new FileReaderServiceImpl();
List<String> lines = fileReaderService.readFromFile(INPUT_FILE_PATH);
DataProcessorService dataProcessorService = new DataProcessorServiceImpl();

Map<FruitTransaction.Operation, OperationHandler> operationMap = getOperationOperationMap();

OperationProcessor operationService = new OperationProcessorImpl(operationMap);
List<FruitTransaction> fruitTransactionList = dataProcessorService.processInputData(lines);
operationService.manageTransactions(fruitTransactionList);

ReportCreatorService reportCreatorService = new ReportCreatorServiceImpl();
String report = reportCreatorService.createReport();

FileWriterService fileWriterService = new FileWriterServiceImpl();
fileWriterService.writeToFile(OUTPUT_FILE_PATH, report);
}

private static Map<FruitTransaction.Operation, OperationHandler> getOperationOperationMap() {
Map<FruitTransaction.Operation, OperationHandler> operationMap = new HashMap<>();
operationMap.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler());
operationMap.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler());
operationMap.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler());
operationMap.put(FruitTransaction.Operation.RETURN, new ReturnOperationHandler());
return operationMap;
}
}
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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> STORAGE = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.excteption;

public class InvalidDataException extends RuntimeException {
public InvalidDataException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax.handlers;

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

public class BalanceOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
if (transaction.getQuantity() < 0) {
throw new InvalidDataException("Negative quantity");
} else if (transaction.getFruit() == null || transaction.getOperation() == null) {
throw new NullPointerException("Invalid transaction or fruit type");
}
Storage.STORAGE.put(transaction.getFruit(), transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax.handlers;

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

public class PurchaseOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {

if (transaction.getQuantity() < 0) {
throw new InvalidDataException("Negative quantity");
} else if (transaction.getFruit() == null) {
throw new NullPointerException("Invalid fruit type");
}
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int purchaseResult = currentAmount - quantity;
if (purchaseResult < 0) {
throw new InvalidDataException("Not enough fruits in storage");
}
Storage.STORAGE.put(transaction.getFruit(), purchaseResult);
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/handlers/ReturnOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.handlers;

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

public class ReturnOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
if (transaction.getQuantity() < 0) {
throw new InvalidDataException("Negative quantity");
} else if (transaction.getFruit() == null) {
throw new NullPointerException("Invalid fruit type");

Choose a reason for hiding this comment

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

Suggested change
throw new NullPointerException("Invalid fruit type");
throw new InvalidDataException("Invalid fruit type");

Choose a reason for hiding this comment

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

same everywhere

}
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int returnResult = currentAmount + quantity;
Storage.STORAGE.put(transaction.getFruit(), returnResult);
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/handlers/SupplyOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.handlers;

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

public class SupplyOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
if (transaction.getQuantity() < 0) {
throw new InvalidDataException("Negative quantity");
} else if (transaction.getFruit() == null || transaction.getOperation() == null) {
throw new NullPointerException("Invalid transaction or fruit type");
}
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int supplyResult = currentAmount + quantity;
Storage.STORAGE.put(transaction.getFruit(), supplyResult);
}
}
83 changes: 83 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package core.basesyntax.model;

import java.util.Arrays;
import java.util.Objects;

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;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FruitTransaction that = (FruitTransaction) o;
return quantity == that.quantity
&& operation == that.operation
&& Objects.equals(fruit, that.fruit);
}

@Override
public int hashCode() {
return Objects.hash(operation, fruit, 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 getOperationByCode(String code) {
return Arrays.stream(Operation.values())
.filter(o -> o.getCode().equals(code))
.findAny()
.orElseThrow(() -> new RuntimeException("No such operation"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.services;

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

public interface DataProcessorService {
List<FruitTransaction> processInputData(List<String> dataFromFile);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/services/FileReaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.services;

import java.util.List;

public interface FileReaderService {
List<String> readFromFile(String fileName);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/services/FileWriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.services;

public interface FileWriterService {
void writeToFile(String fileName, String report);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.services;

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

public interface OperationProcessor {
void manageTransactions(List<FruitTransaction> transactions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.services;

public interface ReportCreatorService {
String createReport();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax.services.impl;

import core.basesyntax.excteption.InvalidDataException;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.services.DataProcessorService;
import java.util.List;

public class DataProcessorServiceImpl implements DataProcessorService {
private static final String SPLIT_DELIMITER = ",";
private static final int OPERATION_TYPE_INDEX = 0;
private static final int FRUIT_TYPE_INDEX = 1;
private static final int FRUIT_QUANTITY_INDEX = 2;

@Override
public List<FruitTransaction> processInputData(List<String> dataFromFile) {
return dataFromFile.stream()
.map(this::getFruitTransaction)
.toList();
}

private FruitTransaction getFruitTransaction(String data) {
String[] processedData = data.split(SPLIT_DELIMITER);
FruitTransaction.Operation operation = FruitTransaction.Operation
.getOperationByCode(processedData[OPERATION_TYPE_INDEX]);
String fruitType = processedData[FRUIT_TYPE_INDEX];
int fruitQuantity = Integer.parseInt(processedData[FRUIT_QUANTITY_INDEX]);
if (fruitQuantity < 0) {
throw new InvalidDataException("Invalid Quantity,fruit quantity is: " + fruitQuantity);
}
return new FruitTransaction(operation, fruitType, fruitQuantity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax.services.impl;

import core.basesyntax.services.FileReaderService;
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 FileReaderService {
private static final int SKIP_LINE = 1;

@Override
public List<String> readFromFile(String fileName) {
List<String> linesFromFile = new ArrayList<>();
Path path = Paths.get(fileName);
try {
Files.lines(path).skip(SKIP_LINE).forEach(linesFromFile::add);
} catch (IOException e) {
throw new RuntimeException("Can't read data from file : " + fileName);
}
return linesFromFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.services.impl;

import core.basesyntax.services.FileWriterService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileWriterServiceImpl implements FileWriterService {
@Override
public void writeToFile(String filePath, String report) {
try {
Files.writeString(Paths.get(filePath), report);
} catch (IOException e) {
throw new RuntimeException("Can't find file by path" + filePath);
}
}
}
Loading
Loading