Skip to content

Commit

Permalink
hw-fruitShop v.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTaranenko committed Dec 6, 2024
1 parent f757eeb commit 7082b8b
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import dao.WareHouseDao;
import dao.impl.WareHouseDaoImpl;

public class Return extends OperationStrategy {
public class BalanceHandler extends OperationHandler {
private WareHouseDao wareHouseDao;

public BalanceHandler() {
this.wareHouseDao = new WareHouseDaoImpl();
}

@Override
public void makeOperation() {
WareHouseDao wareHouseDao = new WareHouseDaoImpl();
wareHouseDao.addFruitLot(this.getFruit(), this.getQuantity());
}
}
6 changes: 3 additions & 3 deletions src/main/java/core/basesyntax/FruitTransfer.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package core.basesyntax;

public class FruitTransfer {
private Operations operations;
private Operation operations;
private String fruit;
private int quantity;

public FruitTransfer(Operations operations, String fruit, int quantity) {
public FruitTransfer(Operation operations, String fruit, int quantity) {
this.operations = operations;
this.fruit = fruit;
this.quantity = quantity;
}

public Operations getOperations() {
public Operation getOperations() {
return operations;
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
import java.util.Map;
import service.DataConverter;
import service.DataReader;
import service.FileWriter;
import service.DataWriter;
import service.ReportGenerator;
import service.ShopService;
import service.impl.CsvDataReaderImpl;
import service.impl.CsvFileWriterImpl;
import service.impl.CsvDataWriterImpl;
import service.impl.DataConverterImpl;
import service.impl.ReportGeneratorImpl;
import service.impl.ShopServiceImpl;

public class Main {
public static void main(String[] args) {
//Declare Map object "handlers" for determine operation
Map<Operations, OperationStrategy> handlers = new HashMap<>();
handlers.put(Operations.BALANCE, new Balance());
handlers.put(Operations.PURCHASE, new Purchase());
handlers.put(Operations.RETURN, new Return());
handlers.put(Operations.SUPPLY, new Supply());
Map<Operation, OperationHandler> handlers = new HashMap<>();
handlers.put(Operation.BALANCE, new BalanceHandler());
handlers.put(Operation.PURCHASE, new PurchaseHandler());
handlers.put(Operation.RETURN, new ReturnHandler());
handlers.put(Operation.SUPPLY, new SupplyHandler());

//Read the data from source
DataReader dataReader = new CsvDataReaderImpl();
List<String> inputReport = dataReader.getDataFromFile("src/main/resources/balance.csv");
final List<String> inputReport
= dataReader.getDataFromFile("src/main/resources/balance.csv");

//Convert received data from reader to particular format
DataConverter dataConverter = new DataConverterImpl();
Expand All @@ -40,7 +41,7 @@ public static void main(String[] args) {
List<String> transitionalReport = reportGenerator.getReport();

//Write report to file CSV format
FileWriter fileWriter = new CsvFileWriterImpl();
DataWriter fileWriter = new CsvDataWriterImpl();
fileWriter.writeToFile(transitionalReport, "report");
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package core.basesyntax;

public enum Operations {
public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r"),
NOT_FOUND("Not found");
RETURN("r");

private String title;

Operations(String title) {
Operation(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public static Operations getOperations(String title) {
for (Operations operations : Operations.values()) {
public static Operation getOperations(String title) {
for (Operation operations : Operation.values()) {
if (operations.title.equals(title)) {
return operations;
}
}
return NOT_FOUND;
return null;
}
}

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

public abstract class OperationStrategy {
public abstract class OperationHandler {
private FruitTransfer fruitTransfer;

public void setFruitTransfer(FruitTransfer fruitTransfer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import dao.WareHouseDao;
import dao.impl.WareHouseDaoImpl;

public class Purchase extends OperationStrategy {
public class PurchaseHandler extends OperationHandler {
private WareHouseDao wareHouseDao;

public PurchaseHandler() {
this.wareHouseDao = new WareHouseDaoImpl();
}

@Override
public void makeOperation() {
WareHouseDao wareHouseDao = new WareHouseDaoImpl();
int theRest;
int storedFruits = wareHouseDao.getStoredQuantity(this.getFruit());
if (this.getQuantity() > storedFruits) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import dao.WareHouseDao;
import dao.impl.WareHouseDaoImpl;

public class Balance extends OperationStrategy {
public class ReturnHandler extends OperationHandler {
private WareHouseDao wareHouseDao;

public ReturnHandler() {
this.wareHouseDao = new WareHouseDaoImpl();
}

@Override
public void makeOperation() {
WareHouseDao wareHouseDao = new WareHouseDaoImpl();
wareHouseDao.addFruitLot(this.getFruit(), this.getQuantity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import dao.WareHouseDao;
import dao.impl.WareHouseDaoImpl;

public class Supply extends OperationStrategy {
public class SupplyHandler extends OperationHandler {
private WareHouseDao wareHouseDao;

public SupplyHandler() {
this.wareHouseDao = new WareHouseDaoImpl();
}

@Override
public void makeOperation() {
WareHouseDao wareHouseDao = new WareHouseDaoImpl();
wareHouseDao.addFruitLot(this.getFruit(), this.getQuantity());
}
}
17 changes: 10 additions & 7 deletions src/main/java/dao/impl/WareHouseDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@
import java.util.Arrays;

public class WareHouseDaoImpl implements WareHouseDao {
private static final int ZERO = 0;
private static final int FRUIT_NAME_INDEX = 0;
private static final int FRUIT_QUANTITY_INDEX = 1;

@Override
public int getStoredQuantity(String fruitName) {
if (isFruitPresence(fruitName)) {
String[] fruitLot = WareHouse.STORED_FRUITS
.stream()
.filter(array -> array[0].equals(fruitName))
.filter(array -> array[FRUIT_NAME_INDEX].equals(fruitName))
.findFirst().get();
return Integer.parseInt(fruitLot[1]);
}
return ZERO;
return 0;
}

@Override
public void addFruitLot(String fruitName, Integer quantity) {
if (isFruitPresence(fruitName)) {
for (String[] fruitLot : WareHouse.STORED_FRUITS) {
if (fruitLot[0].equals(fruitName)) {
fruitLot[1] = String.valueOf(Integer.parseInt(fruitLot[1]) + quantity);
if (fruitLot[FRUIT_NAME_INDEX].equals(fruitName)) {
fruitLot[FRUIT_QUANTITY_INDEX]
= String.valueOf(Integer.parseInt(fruitLot[FRUIT_QUANTITY_INDEX])
+ quantity);
break;
}
}
Expand All @@ -36,9 +39,9 @@ public void addFruitLot(String fruitName, Integer quantity) {

@Override
public void removeFruitLot(String fruitName) {
int lotIndex = ZERO;
int lotIndex = FRUIT_NAME_INDEX;
for (String[] fruitLot : WareHouse.STORED_FRUITS) {
if (fruitLot[0].equals(fruitName)) {
if (fruitLot[FRUIT_NAME_INDEX].equals(fruitName)) {
WareHouse.STORED_FRUITS.remove(lotIndex);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.util.List;

public interface FileWriter {
public interface DataWriter {
void writeToFile(List<String> report, String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import service.FileWriter;
import service.DataWriter;

public class CsvFileWriterImpl implements FileWriter {
public class CsvDataWriterImpl implements DataWriter {
private static final String TITLES = "fruit,quantity";
private static final String PATH = "src/main/resources";

Expand All @@ -15,7 +16,7 @@ public void writeToFile(List<String> report, String filename) {
File file = new File(PATH + File.separator + filename);
String lines = convertToStringFormat(report);
try (BufferedWriter bufferedWriter
= new BufferedWriter(new java.io.FileWriter(file, true))) {
= new BufferedWriter(new FileWriter(file))) {
bufferedWriter.write(lines);
} catch (IOException e) {
throw new RuntimeException("Can't write in CSV file", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/service/impl/DataConverterImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package service.impl;

import core.basesyntax.FruitTransfer;
import core.basesyntax.Operations;
import core.basesyntax.Operation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -16,7 +16,7 @@ public List<FruitTransfer> convertToTransfer(List<String> inputReport) {
.collect(Collectors.toList());

for (String[] line : collectedLinesInfo) {
Operations operations = Operations.getOperations(line[0]);
Operation operations = Operation.getOperations(line[0]);
String fruit = line[1];
int quantity = Integer.parseInt(line[2]);
transferList.add(new FruitTransfer(operations, fruit, quantity));
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/service/impl/ShopServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package service.impl;

import core.basesyntax.FruitTransfer;
import core.basesyntax.OperationStrategy;
import core.basesyntax.Operations;
import core.basesyntax.Operation;
import core.basesyntax.OperationHandler;
import java.util.List;
import java.util.Map;
import service.ShopService;

public class ShopServiceImpl implements ShopService {
private Map<Operations, OperationStrategy> handlers;
private Map<Operation, OperationHandler> handlers;

public ShopServiceImpl(Map<Operations, OperationStrategy> handlers) {
public ShopServiceImpl(Map<Operation, OperationHandler> handlers) {
this.handlers = handlers;
}

@Override
public void process(List<FruitTransfer> transferList) {
for (FruitTransfer lotOfFruit : transferList) {
OperationStrategy chosenStrategy = handlers.get(lotOfFruit.getOperations());
OperationHandler chosenStrategy = handlers.get(lotOfFruit.getOperations());
if (chosenStrategy != null) {
chosenStrategy.setFruitTransfer(lotOfFruit);
chosenStrategy.makeOperation();
Expand Down
9 changes: 0 additions & 9 deletions src/main/resources/balance.csv

This file was deleted.

0 comments on commit 7082b8b

Please sign in to comment.