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

add solution1 #799

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

This file was deleted.

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.DataConverterService;
import core.basesyntax.service.FileWriteService;
import core.basesyntax.service.ProccessService;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.ReportCreatorService;
import core.basesyntax.service.impl.DataConverterServiceImpl;
import core.basesyntax.service.impl.FileReaderServiceImpl;
import core.basesyntax.service.impl.FileWriteServiceImpl;
import core.basesyntax.service.impl.ProccesServiceImpl;
import core.basesyntax.service.impl.ReportCreatorServiceImpl;
import core.basesyntax.strategy.OperationService;
import core.basesyntax.strategy.impl.BalanceOperation;
import core.basesyntax.strategy.impl.PurchaceOperation;
import core.basesyntax.strategy.impl.ReturnOperation;
import core.basesyntax.strategy.impl.SupplyOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String FROM_FILE_NAME = "src/main/resources/text.txt";
private static final String TO_FILE_NAME = "src/main/resources/report.txt";

public static void main(String[] args) {
ReaderService readerService = new FileReaderServiceImpl();
DataConverterService dataConverterService = new DataConverterServiceImpl();
ProccessService proccessService = new ProccesServiceImpl(getOperationMap());
ReportCreatorService reportCreatorService = new ReportCreatorServiceImpl();
FileWriteService fileWriteService = new FileWriteServiceImpl();
List<String> strings = readerService.readFromFile(FROM_FILE_NAME);
List<FruitTransaction> fruitTransactions = dataConverterService.convertText(strings);
proccessService.proccessing(fruitTransactions);
String reportString = reportCreatorService.createReport();
fileWriteService.writeToFile(reportString, TO_FILE_NAME);
}

private static Map<Operation, OperationService> getOperationMap() {
Map<Operation, OperationService> operationMap = new HashMap<>();
operationMap.put(Operation.BALANCE, new BalanceOperation());
operationMap.put(Operation.RETURN,new ReturnOperation());
operationMap.put(Operation.PURCHASE,new PurchaceOperation());
operationMap.put(Operation.SUPPLY, new SupplyOperation());
return operationMap;
}
}
37 changes: 37 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package core.basesyntax.model;

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;
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/model/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.model;

import java.util.Arrays;

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))
.findFirst()
.orElseThrow(() -> new RuntimeException("Can`t find operation by code"));
}
}
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 DataConverterService {
public List<FruitTransaction> convertText(List<String> strings);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/FileWriteService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface FileWriteService {
void writeToFile(String report, String toFileName);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ProccessService.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 ProccessService {
void proccessing(List<FruitTransaction> fruitTransactionsList);
}
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 pathToFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

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

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

public class DataConverterServiceImpl implements DataConverterService {
private static final String SEPARATOR = ",";
private static final int INDEX_BY_TRANSACTION = 0;
private static final int INDEX_BY_FRUIT = 1;
private static final int INDEX_BY_AMOUNT = 2;

@Override
public List<FruitTransaction> convertText(List<String> strings) {
List<FruitTransaction> dataFruitTransaction = new ArrayList<>();
for (int i = 0; i < strings.size(); i++) {
String[] data = strings.get(i).split(SEPARATOR);
dataFruitTransaction
.add(new FruitTransaction(
Operation.getOperationByCode(data[INDEX_BY_TRANSACTION]),
data[INDEX_BY_FRUIT],
Integer.parseInt(data[INDEX_BY_AMOUNT])
));
}
return dataFruitTransaction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class FileReaderServiceImpl implements ReaderService {
private static final int FIRST_LINE = 1;

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

import core.basesyntax.service.FileWriteService;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteServiceImpl implements FileWriteService {
@Override
public void writeToFile(String report, String toFileName) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toFileName))) {
bufferedWriter.write(report);
} catch (IOException e) {
throw new RuntimeException("Can't write to file " + toFileName);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ProccesServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.ProccessService;
import core.basesyntax.strategy.OperationService;
import java.util.List;
import java.util.Map;

public class ProccesServiceImpl implements ProccessService {
private final Map<Operation, OperationService> processedStoreHanler;

public ProccesServiceImpl(Map<Operation, OperationService> processedStoreHanler) {
this.processedStoreHanler = processedStoreHanler;
}

@Override
public void proccessing(List<FruitTransaction> fruitTransactionsList) {
for (FruitTransaction fruitTransaction : fruitTransactionsList) {
processedStoreHanler.get(fruitTransaction.getOperation()).doOperation(fruitTransaction);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.ReportCreatorService;
import core.basesyntax.storage.Storage;
import java.util.Map;

public class ReportCreatorServiceImpl implements ReportCreatorService {
private static final String FIRST_STRING = "fruit,quantity" + System.lineSeparator();
private static final String COMMA = ",";

@Override
public String createReport() {
StringBuilder builder = new StringBuilder(FIRST_STRING);
builder.append(System.lineSeparator());
for (Map.Entry<String,Integer> fruits : Storage.getStorage().entrySet()) {
builder.append(fruits.getKey()).append(COMMA).append(fruits.getValue())
.append(System.lineSeparator());
}
return builder.toString().trim();
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.storage;

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

public class Storage {
private static Map<String, Integer> storage = new HashMap<>();

public static Map<String, Integer> getStorage() {
return storage;
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationService.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 OperationService {
void doOperation(FruitTransaction fruitTransaction);
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/strategy/impl/BalanceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.storage.Storage;
import core.basesyntax.strategy.OperationService;

public class BalanceOperation implements OperationService {
@Override
public void doOperation(FruitTransaction fruitTransaction) {
Storage.getStorage().put(fruitTransaction.getFruit(),fruitTransaction.getQuantity());
}
}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/strategy/impl/PurchaceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.storage.Storage;
import core.basesyntax.strategy.OperationService;

public class PurchaceOperation implements OperationService {
@Override
public void doOperation(FruitTransaction fruitTransaction) {
int balance = Storage.getStorage().get(fruitTransaction.getFruit());
int purchase = balance - fruitTransaction.getQuantity();
if (purchase < 0) {
throw new RuntimeException("Not enough quantity in shop");
}
Storage.getStorage().put(fruitTransaction.getFruit(),purchase);
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/strategy/impl/ReturnOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.storage.Storage;
import core.basesyntax.strategy.OperationService;

public class ReturnOperation implements OperationService {
@Override
public void doOperation(FruitTransaction fruitTransaction) {
int balance = Storage.getStorage().get(fruitTransaction.getFruit());
int retOperation = balance + fruitTransaction.getQuantity();
Storage.getStorage().put((fruitTransaction.getFruit()),retOperation);
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/strategy/impl/SupplyOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.storage.Storage;
import core.basesyntax.strategy.OperationService;

public class SupplyOperation implements OperationService {
@Override
public void doOperation(FruitTransaction fruitTransaction) {
int balance = Storage.getStorage().get(fruitTransaction.getFruit());
int supply = balance + fruitTransaction.getQuantity();
Storage.getStorage().put(fruitTransaction.getFruit(), supply);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fruit,quantity
banana,40
9 changes: 9 additions & 0 deletions src/main/resources/text.txt
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
7 changes: 0 additions & 7 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.

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

class MainTest {

Choose a reason for hiding this comment

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

Redundant class

}
Loading
Loading