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

used service, strategy packages #960

Open
wants to merge 1 commit into
base: master
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
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FruitShopService;
import core.basesyntax.service.ParseOperationService;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.WriterService;
import core.basesyntax.service.impl.FruitShopServiceImpl;
import core.basesyntax.service.impl.ParseOperationServiceImpl;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.strategy.BalanceOperationHandlerImpl;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImpl;
import core.basesyntax.strategy.PurchaseOperationHandlerImpl;
import core.basesyntax.strategy.ReturnOperationHandlerImpl;
import core.basesyntax.strategy.SupplyOperationHandlerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public static void main(String[] args) {
Map<FruitTransaction.Operation, OperationHandler> operationHandlerMap = new HashMap<>();
operationHandlerMap.put(FruitTransaction.Operation.BALANCE,
new BalanceOperationHandlerImpl());
operationHandlerMap.put(FruitTransaction.Operation.PURCHASE,
new PurchaseOperationHandlerImpl());
operationHandlerMap.put(FruitTransaction.Operation.SUPPLY,
new SupplyOperationHandlerImpl());
operationHandlerMap.put(FruitTransaction.Operation.RETURN,
new ReturnOperationHandlerImpl());

ReaderService readerService = new ReaderServiceImpl();
List<String> inputData = readerService.readFromFile(INPUT_FILE_PATH);

ParseOperationService transactionService = new ParseOperationServiceImpl();
List<FruitTransaction> fruitTransactions =
transactionService.parseContentForOperations(inputData);

OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlerMap);
FruitShopService transactionHandler = new FruitShopServiceImpl(operationStrategy);
transactionHandler.processOfOperations(fruitTransactions);

ReportService reportService = new ReportServiceImpl();
String report = reportService.report();
System.out.println(report);

WriterService writerService = new WriterServiceImpl();
writerService.writeToFile(OUTPUT_FILE_PATH, report);
}
}

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

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

public class FruitStorage {
public static final Map<String, Integer> fruitQuantities = new HashMap<>();
}


Comment on lines +9 to +10

Choose a reason for hiding this comment

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

lines

Suggested change

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

import java.util.Arrays;

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

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(values())
.filter(operation -> operation.getCode().equals(code))
.findAny()
.orElseThrow(() -> new RuntimeException("Invalid operation code " + code));
}
}
}

Choose a reason for hiding this comment

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

line

Suggested change

8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/FruitShopService.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 FruitShopService {
void processOfOperations(List<FruitTransaction> parsedData);
}
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 ParseOperationService {
List<FruitTransaction> parseContentForOperations(List<String> inputData);
}
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> readFromFile(String filePath);
}
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 report();
}
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 writeToFile(String filePath, String data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.service.impl;

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

public class FruitShopServiceImpl implements FruitShopService {
private final OperationStrategy operationStrategy;

public FruitShopServiceImpl(OperationStrategy operationStrategy) {
this.operationStrategy = operationStrategy;
}

@Override
public void processOfOperations(List<FruitTransaction> parsedData) {
for (FruitTransaction line : parsedData) {
operationStrategy.get(line.getOperation()).process(line);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax.service.impl;

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

public class ParseOperationServiceImpl implements ParseOperationService {
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 POINT_TO_START_READING = 1;

@Override
public List<FruitTransaction> parseContentForOperations(List<String> inputData) {
List<FruitTransaction> parsedData = new ArrayList<>();
for (int i = 1; i < inputData.size(); i++) {
parsedData.add(parseInputData(inputData.get(i)));
}
return parsedData;
}

private FruitTransaction parseInputData(String line) {
FruitTransaction fruitTransaction = new FruitTransaction(
);
String[] separetedLine = line.split(SEPARATOR);
fruitTransaction.setOperation(FruitTransaction.Operation
.getOperationByCode(separetedLine[OPERATION_INDEX]));
fruitTransaction.setFruit(separetedLine[FRUIT_INDEX]);
fruitTransaction.setQuantity(Integer.parseInt(separetedLine[QUANTITY_INDEX]));
return fruitTransaction;
}
}
21 changes: 21 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,21 @@
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> readFromFile(String filePath) {
Path path = Path.of(filePath);
try {
return Files.readAllLines(path);
} catch (IOException e) {
throw new RuntimeException("Can't read data from your amazing file "
+ path + " !", e);
}
}
}
20 changes: 20 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,20 @@
package core.basesyntax.service.impl;

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

public class ReportServiceImpl implements ReportService {
@Override
public String report() {
StringBuilder report = new StringBuilder("fruit,quantity").append(System.lineSeparator());

Choose a reason for hiding this comment

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

Suggested change
StringBuilder report = new StringBuilder("fruit,quantity").append(System.lineSeparator());
StringBuilder report = new StringBuilder(HEADER).append(System.lineSeparator());

for (Map.Entry<String, Integer> entry : FruitStorage.fruitQuantities.entrySet()) {
report.append(entry.getKey())
.append(",")

Choose a reason for hiding this comment

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

Suggested change
.append(",")
.append(SEPARATOR)

.append(entry.getValue())
.append(System.lineSeparator());
}
return report.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 writeToFile(String filePath, String data) {
try {
Files.writeString(Path.of(filePath), data);
} catch (IOException e) {
throw new RuntimeException("Can't write to file " + filePath, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.strategy;

import core.basesyntax.db.FruitStorage;
import core.basesyntax.model.FruitTransaction;

public class BalanceOperationHandlerImpl implements OperationHandler {

@Override
public void process(FruitTransaction transaction) {
FruitStorage.fruitQuantities.put(transaction.getFruit(), transaction.getQuantity());
}
}

Choose a reason for hiding this comment

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

Suggested change

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 process(FruitTransaction transaction);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.strategy;

import core.basesyntax.model.FruitTransaction;

public interface OperationStrategy {
OperationHandler get(FruitTransaction.Operation operation);
}

Choose a reason for hiding this comment

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

Suggested change

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

Choose a reason for hiding this comment

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

Move the entire implementation from the strategy package to the impl package

Suggested change
package core.basesyntax.strategy;
package core.basesyntax.strategy.impl;


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

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

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

@Override
public OperationHandler get(FruitTransaction.Operation operation) {
return operationHandlerMap.get(operation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.strategy;

import core.basesyntax.db.FruitStorage;
import core.basesyntax.model.FruitTransaction;

public class PurchaseOperationHandlerImpl implements OperationHandler {

@Override
public void process(FruitTransaction transaction) {
int currentQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit());
if (currentQuantity < transaction.getQuantity()) {
throw new RuntimeException("Not enough " + transaction.getFruit()
+ " in stock for purchase.");
}
FruitStorage.fruitQuantities
.put(transaction.getFruit(), currentQuantity - transaction.getQuantity());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.strategy;

import core.basesyntax.db.FruitStorage;
import core.basesyntax.model.FruitTransaction;

public class ReturnOperationHandlerImpl implements OperationHandler {

@Override
public void process(FruitTransaction transaction) {
int oldQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit());
FruitStorage.fruitQuantities
.put(transaction.getFruit(), oldQuantity + transaction.getQuantity());
}
}

Choose a reason for hiding this comment

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

Suggested change

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy;

import core.basesyntax.db.FruitStorage;
import core.basesyntax.model.FruitTransaction;

public class SupplyOperationHandlerImpl implements OperationHandler {

@Override
public void process(FruitTransaction transaction) {
int oldQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit());
FruitStorage.fruitQuantities
.put(transaction.getFruit(), oldQuantity + transaction.getQuantity());
}
}
Loading
Loading