Skip to content

Commit

Permalink
added UnknownOperationException class, fix OperationHandler and Stora…
Browse files Browse the repository at this point in the history
…geDao interfaces
  • Loading branch information
skotsurskiy committed Nov 2, 2024
1 parent b08583e commit ec5a7cc
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public static void main(String[] args) {
String report = reportService.getReport(storageDao.getAll());

FileWriterService fileWriterService = new FileWriterServiceImpl();
fileWriterService.write(report, "src/result");
fileWriterService.write(report, "src/result.csv");
}
}
4 changes: 2 additions & 2 deletions src/main/java/core/basesyntax/dao/StorageDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.Map;

public interface StorageDao {
Map<String, Integer> save(String fruit, int quantity);
void save(String fruit, int quantity);

Map<String, Integer> getAll();

int getQuantity(String key);
int getQuantityByFruitName(String key);
}
8 changes: 5 additions & 3 deletions src/main/java/core/basesyntax/dao/StorageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public StorageDaoImpl() {
}

@Override
public Map<String, Integer> save(String fruit, int quantity) {
public void save(String fruit, int quantity) {
if (fruit == null) {
throw new RuntimeException("fruit cannot be null");
}
storage.getFruitStorage().put(fruit, quantity);
return storage.getFruitStorage();
}

@Override
Expand All @@ -22,7 +24,7 @@ public Map<String, Integer> getAll() {
}

@Override
public int getQuantity(String key) {
public int getQuantityByFruitName(String key) {
return storage.getFruitStorage().get(key);
}
}
6 changes: 3 additions & 3 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getCode() {
return code;
}

public static boolean startWithOperation(String line) {
public static boolean startsWithOperation(String line) {
for (Operation operation : Operation.values()) {
if (line.startsWith(operation.getCode())) {
return true;
Expand All @@ -60,13 +60,13 @@ public static boolean startWithOperation(String line) {
return false;
}

public static Operation convertToOperaion(String data) {
public static Operation convertToOperation(String data) {
for (Operation operation : Operation.values()) {
if (data.equals(operation.getCode())) {
return operation;
}
}
throw new RuntimeException("Unknown operation code: " + data);
throw new UnknownOperationException("Unknown operation code" + data);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.model;

public class UnknownOperationException extends RuntimeException {
public UnknownOperationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class DataConverterServiceImpl implements DataConverterService {
@Override
public List<FruitTransaction> convert(List<String> data) {
return data.stream()
.filter(FruitTransaction.Operation::startWithOperation)
.filter(FruitTransaction.Operation::startsWithOperation)
.map(this::convertToFruitTransaction)
.collect(Collectors.toList());
}

private FruitTransaction convertToFruitTransaction(String data) {
String[] dataSplit = data.split(COMMA_SEPARATOR);
FruitTransaction.Operation operation
= FruitTransaction.Operation.convertToOperaion(dataSplit[OPERATION_TYPE_INDEX]);
= FruitTransaction.Operation.convertToOperation(dataSplit[OPERATION_TYPE_INDEX]);
String fruit = dataSplit[FRUIT_INDEX];
int quantity = Integer.parseInt(dataSplit[QUANTITY_INDEX]);
return new FruitTransaction(operation, fruit, quantity);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/core/basesyntax/service/ReportServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
public class ReportServiceImpl implements ReportService {
private static final String HEADER = "fruit,quantity";
private static final String COMMA_SEPARATOR = ",";
private StringBuilder builder;

public ReportServiceImpl() {
builder = new StringBuilder();
}

@Override
public String getReport(Map<String, Integer> data) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/service/ShopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ShopServiceImpl(OperationStrategy operationStrategy) {
@Override
public Map<String, Integer> process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
operationStrategy.get(transaction.getType()).getOperation(transaction);
operationStrategy.get(transaction.getType()).performOperation(transaction);
}
return storageDao.getAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public List<String> read(String path) {
try {
return Files.readAllLines(Path.of(path));
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + path);
throw new RuntimeException("Cannot read file: " + path, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void write(String data, String path) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
writer.write(data);
} catch (IOException e) {
throw new RuntimeException("Cannot write to file: " + path);
throw new RuntimeException("Cannot write to file: " + path, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

public class BalanceOperationHandler implements OperationHandler {
private StorageDao storageDao;
Expand All @@ -13,8 +12,7 @@ public BalanceOperationHandler() {
}

@Override
public Map<String, Integer> getOperation(FruitTransaction transaction) {
public void performOperation(FruitTransaction transaction) {
storageDao.save(transaction.getFruit(), transaction.getQuantity());
return storageDao.getAll();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package core.basesyntax.service.operation;

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

public interface OperationHandler {
Map<String, Integer> getOperation(FruitTransaction transaction);
void performOperation(FruitTransaction transaction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

public class PurchaseOperationHandler implements OperationHandler {
private StorageDao storageDao;
Expand All @@ -13,9 +12,15 @@ public PurchaseOperationHandler() {
}

@Override
public Map<String, Integer> getOperation(FruitTransaction transaction) {
storageDao.save(transaction.getFruit(),
storageDao.getQuantity(transaction.getFruit()) - transaction.getQuantity());
return storageDao.getAll();
public void performOperation(FruitTransaction transaction) {
int currentQuantity = storageDao.getQuantityByFruitName(transaction.getFruit());
int newQuantity = currentQuantity - transaction.getQuantity();

if (newQuantity < 0) {
throw new IllegalArgumentException("Insufficient quantity of "
+ transaction.getFruit() + " in storage.");
}

storageDao.save(transaction.getFruit(), newQuantity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

public class ReturnOperationHandler implements OperationHandler {
private StorageDao storageDao;
Expand All @@ -13,9 +12,9 @@ public ReturnOperationHandler() {
}

@Override
public Map<String, Integer> getOperation(FruitTransaction transaction) {
public void performOperation(FruitTransaction transaction) {
storageDao.save(transaction.getFruit(),
storageDao.getQuantity(transaction.getFruit()) + transaction.getQuantity());
return storageDao.getAll();
storageDao.getQuantityByFruitName(transaction.getFruit())
+ transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

public class SupplyOperationHandler implements OperationHandler {
private StorageDao storageDao;
Expand All @@ -13,9 +12,9 @@ public SupplyOperationHandler() {
}

@Override
public Map<String, Integer> getOperation(FruitTransaction transaction) {
public void performOperation(FruitTransaction transaction) {
storageDao.save(transaction.getFruit(),
storageDao.getQuantity(transaction.getFruit()) + transaction.getQuantity());
return storageDao.getAll();
storageDao.getQuantityByFruitName(transaction.getFruit())
+ transaction.getQuantity());
}
}
3 changes: 0 additions & 3 deletions src/result

This file was deleted.

0 comments on commit ec5a7cc

Please sign in to comment.