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

resolve #1249

Closed
Closed

resolve #1249

Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions src/main/java/core/basesyntax/FruitShop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package core.basesyntax;

import core.basesyntax.handlers.BalanceOperationHandler;
import core.basesyntax.handlers.PurchaseOperationHandler;
import core.basesyntax.handlers.SupplyOperationHandler;
import core.basesyntax.handlers.ReturnOperationHandler;
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.services.*;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

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

/**
* Feel free to remove this class and create your own.
*/
public class FruitShop {
public static final String INPUT_FILE_PATH = "src/main/java/resources/input.csv";
public 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 = 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());

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);
}
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
// HINT: In the `public static void main(String[] args)` it is better to create instances of your classes,
// and call their methods, but do not write any business logic in the `main` method!
}
9 changes: 0 additions & 9 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.exception;

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

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

public class BalanceOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
Storage.STORAGE.put(transaction.getFruit(), transaction.getQuantity());
}
}
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.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class PurchaseOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int purchaseResult = currentAmount - quantity;
if (purchaseResult < 0) {
throw new RuntimeException("Not enough fruits in storage");
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
}
Storage.STORAGE.put(transaction.getFruit(), purchaseResult);
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/handlers/ReturnOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.handlers;

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

public class ReturnOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int returnResult = currentAmount + quantity;
Storage.STORAGE.put(transaction.getFruit(), returnResult);
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/handlers/SupplyOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.handlers;

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

public class SupplyOperationHandler implements OperationHandler {
@Override
public void calculateOperation(FruitTransaction transaction) {
int currentAmount = Storage.STORAGE.get(transaction.getFruit());
int quantity = transaction.getQuantity();
int supplyResult = currentAmount + quantity;
Storage.STORAGE.put(transaction.getFruit(), supplyResult);
}
}
63 changes: 63 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package core.basesyntax.model;

import java.util.Arrays;

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

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"));
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/resources/input.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/java/core/basesyntax/resources/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit,quantity
banana,152
apple,90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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,9 @@
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,33 @@
package core.basesyntax.services.impl;

import core.basesyntax.exception.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);
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
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,26 @@
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);
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
}
return linesFromFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 fileName, String report) {
try {
Files.writeString(Paths.get(filePath), report);
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
throw new RuntimeException("Can't find file by path" + filePath);
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.services.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.services.OperationProcessor;
import core.basesyntax.strategy.OperationHandler;
import java.util.List;
import java.util.Map;

public class OperationProcessorImpl implements OperationProcessor {
private final Map<FruitTransaction.Operation, OperationHandler> processedOperations;

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

@Override
public void manageTransactions(List<FruitTransaction> transactions) {
transactions.forEach(t -> processedOperations.get(t.getOperation()).calculateOperation(t));
DankevichMisha marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.services.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.services.ReportCreatorService;

public class ReportCreatorServiceImpl implements ReportCreatorService {
private static final String DEFAULT_MESSAGE = "fruit,quantity";
private static final String SPLIT_DELIMITER = ",";
private final StringBuilder reportBuilder = new StringBuilder();

@Override
public String createReport() {
reportBuilder.append(DEFAULT_MESSAGE).append(System.lineSeparator());
Storage.STORAGE.forEach((key, value) -> reportBuilder.append(key)
.append(SPLIT_DELIMITER)
.append(value)
.append(System.lineSeparator()));
return reportBuilder.toString();
}
}
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 calculateOperation(FruitTransaction transaction);
}
Loading