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

hw done #1083

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

hw done #1083

wants to merge 4 commits into from

Conversation

sergiikonov
Copy link

No description provided.

Copy link

@liliia-ponomarenko liliia-ponomarenko left a comment

Choose a reason for hiding this comment

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

Good job! But firstly let’s fix some issues with the main solution ;)

Comment on lines 41 to 48
private FruitTransaction.Operation mapOperation(String code) {
return switch (code) {
case "b" -> FruitTransaction.Operation.BALANCE;
case "s" -> FruitTransaction.Operation.SUPPLY;
case "p" -> FruitTransaction.Operation.PURCHASE;
case "r" -> FruitTransaction.Operation.RETURN;
default -> throw new IllegalArgumentException("Unknown operation code: " + code);
};

Choose a reason for hiding this comment

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

don't use switch case here, it will be difficult for you to maintain it in the future. Instead, add the following method to the Operation class:

public static Operation getOperation(String code) {
        for (Operation value : Operation.values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        throw new IllegalArgumentException(code + " operation doesn't exist.");
    }

public List<FruitTransaction> convertToTransaction(List<String> data) {
List<FruitTransaction> convertedTransaction = new ArrayList<>();
for (int i = 1; i < data.size(); i++) { // Skip header
String[] parts = data.get(i).split(",");

Choose a reason for hiding this comment

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

make separator and other magic numbers constant field

@Override
public String getReport() {
if (Storage.fruits.isEmpty()) {
return "No records to report";

Choose a reason for hiding this comment

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

better throw exception

}
StringBuilder report = new StringBuilder();
for (Map.Entry<String, Integer> entry : Storage.fruits.entrySet()) {
report.append(entry.getKey()).append(",").append(entry.getValue()).append("\n");

Choose a reason for hiding this comment

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

Make separator constant field and use System.lineseparator() instead \n

public OperationHandler getHandler(FruitTransaction.Operation operation) {
OperationHandler handler = operationHandlerMap.get(operation);
if (handler == null) {
throw new NullPointerException("No handler found for operation: " + operation);

Choose a reason for hiding this comment

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

better throw IllegalArgumentException or general RuntimeException

}

public enum Operation {
BALANCE, SUPPLY, PURCHASE, RETURN

Choose a reason for hiding this comment

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

Suggested change
BALANCE, SUPPLY, PURCHASE, RETURN
BALANCE("b")...

use values

Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

Good job overall!

  • use src/test/resources for testing

Comment on lines 36 to 41
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>();
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation());
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);
Copy link

Choose a reason for hiding this comment

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

Use Map.of for brevity in such cases

}

@Test
void converter_testConverterToTransactions_ok() {
Copy link

Choose a reason for hiding this comment

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

Suggested change
void converter_testConverterToTransactions_ok() {
void converter_validTransactions_ok() {

second part is about given conditions

FruitTransaction actual = actualTransaction.get(i);

Assertions.assertEquals(expected.getOperation(), actual.getOperation(),
"Операції не збігаються");
Copy link

Choose a reason for hiding this comment

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

don't use anything but english in code

Comment on lines 62 to 71
} catch (IOException e) {
Assertions.fail("Failed to create test directory");
} finally {
try {
Files.deleteIfExists(directory);
} catch (IOException e) {
Assertions.fail("Failed to delete test directory");
}
}
}
Copy link

Choose a reason for hiding this comment

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

it's better to keep our tests clean if possible, move cleanup handling to afterEach if needed, or loop up how to create temp files (like Files.createTempFile)
If exception is thrown, test will fail anyway

@sergiikonov sergiikonov requested a review from okuzan December 11, 2024 14:27
new FruitTransaction(FruitTransaction.Operation.SUPPLY, "apple", 50),
new FruitTransaction(FruitTransaction.Operation.PURCHASE, "banana", 10)
);
Assertions.assertEquals(expectedTransaction.size(), actualTransaction.size(),
Copy link

Choose a reason for hiding this comment

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

Use static import for assertions

}

@Test
void converter_testInvalidDataToConvert_notOk() {
Copy link

Choose a reason for hiding this comment

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

Let's use the same naming convetion for all test method0s. Please, check everywhere

Suggested change
void converter_testInvalidDataToConvert_notOk() {
void convertToTransaction_invalidDataToConvert_notOk() {

}

@Test
void converter_validTransactions_ok() {
Copy link

Choose a reason for hiding this comment

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

Suggested change
void converter_validTransactions_ok() {
void convertToTransaction_validTransactions_ok() {

@Test
void readFromFile_validFile_ok() throws IOException {
String testFilePath = "src/main/resources/testFile.csv";
String fileContent = "type,fruit,quantity\nb,banana,20\ns,apple,50";
Copy link

Choose a reason for hiding this comment

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

Let's use text block to make this line more readable

Comment on lines 61 to 72
} catch (IOException e) {
Assertions.fail("Failed to create temporary directory", e);
} finally {
if (tempDirectory != null) {
try {
Files.deleteIfExists(tempDirectory);
} catch (IOException e) {
Assertions.fail("Failed to delete temporary directory", e);
}
}
}
}
Copy link

Choose a reason for hiding this comment

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

Move it to the method with @AfterEach annotation, like you did in FileWriterTest

Comment on lines 40 to 42
Assertions.assertThrows(IllegalArgumentException.class,
() -> operationStrategy.getHandler(null),
"IllegalArgumentException.");
Copy link

Choose a reason for hiding this comment

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

Let's check error message content

Comment on lines 30 to 36
@Test
void operationStrategy_returnsCorrectHandler_ok() {
OperationHandler handler = operationStrategy.getHandler(FruitTransaction.Operation.SUPPLY);

Assertions.assertTrue(handler instanceof SupplyOperation,
"Handler for SUPPLY should be of type SupplyOperation.");
}
Copy link

Choose a reason for hiding this comment

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

Let's do the same for other handlers


@Test
void reportGenerator_EmptyStorage_notOk() {
Assertions.assertThrows(RuntimeException.class, () -> reportGenerator.getReport());
Copy link

Choose a reason for hiding this comment

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

Same here

new FruitTransaction(
FruitTransaction.Operation.PURCHASE, "banana", 20)
);
Assertions.assertThrows(IllegalArgumentException.class,
Copy link

Choose a reason for hiding this comment

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

Same here

@sergiikonov sergiikonov requested a review from JJJazl December 12, 2024 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants