Skip to content

Commit

Permalink
added new custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
skotsurskiy committed Nov 5, 2024
1 parent ec5a7cc commit 6f594c5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/main/java/core/basesyntax/dao/StorageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import core.basesyntax.storage.Storage;
import java.util.Map;
import java.util.NoSuchElementException;

public class StorageDaoImpl implements StorageDao {
private Storage storage;

public StorageDaoImpl() {
storage = new Storage();
}
private final Storage storage = new Storage();

@Override
public void save(String fruit, int quantity) {
Expand All @@ -25,6 +22,9 @@ public Map<String, Integer> getAll() {

@Override
public int getQuantityByFruitName(String key) {
if (!storage.getFruitStorage().containsKey(key)) {
throw new NoSuchElementException("No such key in storage: " + key);
}
return storage.getFruitStorage().get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DataConverterServiceImpl implements DataConverterService {
private static final int OPERATION_TYPE_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int QUANTITY_INDEX = 2;
private static final int VALID_ARRAY_LENGTH = 3;

@Override
public List<FruitTransaction> convert(List<String> data) {
Expand All @@ -19,11 +20,26 @@ public List<FruitTransaction> convert(List<String> data) {
}

private FruitTransaction convertToFruitTransaction(String data) {
String[] dataSplit = data.split(COMMA_SEPARATOR);
String[] dataSplit = splitIfValid(data);
FruitTransaction.Operation operation
= 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);
}

private String[] splitIfValid(String data) {
String[] dataSplit = data.split(COMMA_SEPARATOR);
if (dataSplit.length != VALID_ARRAY_LENGTH) {
throw new InvalidArraySplitLength("Data split length is not 3, but: "
+ dataSplit.length);
}
try {
Integer.parseInt(dataSplit[QUANTITY_INDEX]);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Expected a numeric value, but got: "
+ dataSplit[QUANTITY_INDEX]);
}
return dataSplit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

public class InvalidArraySplitLength extends RuntimeException {
public InvalidArraySplitLength(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.operation.OperationHandler;
import java.util.Map;
import java.util.NoSuchElementException;

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

public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler>
operationHandlerMap) {
Expand All @@ -14,6 +15,9 @@ public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler>

@Override
public OperationHandler get(FruitTransaction.Operation operation) {
if (!operationHandlerMap.containsKey(operation)) {
throw new NoSuchElementException("No such operation in handler map: " + operation);
}
return operationHandlerMap.get(operation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service.file;

public class FileReadException extends RuntimeException {
public FileReadException(String message) {
super(message);
}
}
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, e);
throw new FileReadException("Cannot read file: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service.file;

public class FileWriteException extends RuntimeException {
public FileWriteException(String message) {
super(message);
}
}
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, e);
throw new FileWriteException("Cannot write to file: " + path);
}
}
}

0 comments on commit 6f594c5

Please sign in to comment.