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

first version #1235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.service.Inventory;
import core.basesyntax.service.InventoryFromCsv;
import core.basesyntax.service.Report;
import core.basesyntax.service.ReportToCsv;
import core.basesyntax.storage.Storage;

public class Mail {
public static void main(String[] args) {
Storage storage = new Storage();

Inventory inventory = new InventoryFromCsv();

Choose a reason for hiding this comment

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

Avoid hardcoding file names outside of the Main class. Consider passing the file name as a parameter to the InventoryFromCsv class or using a configuration file to manage such constants .

inventory.synchronizeWithTheStorage();

Report report = new ReportToCsv();

Choose a reason for hiding this comment

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

Ensure that services are independent and called in the correct order. The checklist suggests that each service should return specific data types and pass them to the next service, maintaining independence and adhering to SOLID principles .

report.prepare();
}
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/db/StockDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax.db;

import java.util.Set;

public interface StockDao {
void set(String product, Integer amount);

Integer get(String product);

void decrease(String product, Integer amount);

void increase(String product, Integer amount);

boolean contain(String product);

Set<String> getProductsList();

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

import core.basesyntax.storage.Storage;
import java.util.Set;

public class StockDaoStorageImpl implements StockDao {

@Override
public void set(String product, Integer amount) {
Storage.stock.put(product, amount);
}

@Override
public Integer get(String product) {
return Storage.stock.get(product);
}

@Override
public void decrease(String product, Integer amount) {
if (contain(product)) {
Storage.stock.put(product, get(product) - amount);

Choose a reason for hiding this comment

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

Consider adding a check to ensure that the resulting stock amount is not negative after decreasing. This will prevent logical errors where stock becomes negative, which is usually not desired in inventory systems.

} else if (get(product) - amount < 0) {
throw new RuntimeException(
"the quantity being subtracted is greater than the stock value");
} else {
throw new RuntimeException("There was no such product before");
}
Comment on lines +20 to +27

Choose a reason for hiding this comment

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

The logic in the decrease method is flawed. The check for get(product) - amount < 0 should be inside the if (contain(product)) block. Currently, it is incorrectly placed in the else if block, which will never be executed because the else block will be executed if the product does not exist.

}

@Override
public void increase(String product, Integer amount) {
if (!contain(product)) {
throw new RuntimeException("There was no such product before");
} else if (get(product) + amount < 0) {
throw new RuntimeException("amount out limit");
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

The condition get(product) + amount < 0 in the increase method is unnecessary and logically incorrect. Since amount is being added, it should not be negative. Consider removing this condition.

} else {
Storage.stock.put(product, get(product) + amount);
}
}

@Override
public boolean contain(String product) {
return Storage.stock.containsKey(product);
}

@Override
public Set<String> getProductsList() {
return Storage.stock.keySet();
}
}
6 changes: 6 additions & 0 deletions src/main/java/core/basesyntax/service/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core.basesyntax.service;

public interface Inventory {

void synchronizeWithTheStorage();
}
51 changes: 51 additions & 0 deletions src/main/java/core/basesyntax/service/InventoryFromCsv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package core.basesyntax.service;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;
import core.basesyntax.service.operations.Balance;
import core.basesyntax.service.operations.Operation;
import core.basesyntax.service.operations.Purchase;
import core.basesyntax.service.operations.Return;
import core.basesyntax.service.operations.Supply;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class InventoryFromCsv implements Inventory {
private static final String FILE_NAME = "inventory.csv";

Choose a reason for hiding this comment

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

Avoid hardcoding the file name directly in the class. Consider passing the file name as a parameter to the constructor or using a configuration file to manage such constants, as suggested in the checklist.

private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void synchronizeWithTheStorage() {
Map<String, Operation> operationMap = new HashMap<>();
operationMap.put("b", new Balance());
operationMap.put("s", new Supply());
operationMap.put("r", new Return());
operationMap.put("p", new Purchase());

OperationStrategy operationStrategy = new OperationStrategyImpl(operationMap);

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line = reader.readLine();
while (line != null) {
String[] splitDataFromCurrentLine = line.split(",");
if (splitDataFromCurrentLine.length != 3) {
throw new RuntimeException("wrong data format");
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

The exception message 'wrong data format' could be more descriptive. Consider including the line number or content to help identify the problematic data.

}
try {
Integer.parseInt(splitDataFromCurrentLine[2]);
} catch (NumberFormatException e) {
throw new RuntimeException("amount is not a number", e);

Choose a reason for hiding this comment

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

The exception message 'amount is not a number' could be more descriptive by including the actual value that caused the error.

}
operationStrategy.getOperation(splitDataFromCurrentLine[0])
.update(splitDataFromCurrentLine[1],
Integer.parseInt(splitDataFromCurrentLine[2]));
line = reader.readLine();
}
} catch (IOException e) {
throw new RuntimeException("Cant read from file", e);

Choose a reason for hiding this comment

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

The exception message 'Cant read from file' should be more descriptive. Consider including the file name in the message to provide more context.

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

import core.basesyntax.service.operations.Operation;

public interface OperationStrategy {
Operation getOperation(String operationSymbol);
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/service/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.service;

import core.basesyntax.service.operations.Operation;
import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {
private Map<String, Operation> operationMap;

public OperationStrategyImpl(Map<String, Operation> operationMap) {
this.operationMap = operationMap;
}

@Override
public Operation getOperation(String operationSymbol) {
if (operationMap.containsKey(operationSymbol)) {
return operationMap.get(operationSymbol);
} else {
throw new RuntimeException("unknown operation symbol");

Choose a reason for hiding this comment

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

The exception message 'unknown operation symbol' could be more descriptive by including the actual symbol that caused the error. This would help in debugging and understanding the issue.

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

public interface Report {
void prepare();
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/service/ReportToCsv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.service;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class ReportToCsv implements Report {
private static final String FILE_NAME = "report.csv";

Choose a reason for hiding this comment

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

Avoid hardcoding the file name directly in the class. Consider passing the file name as a parameter to the constructor or using a configuration file to manage such constants, as suggested in the checklist.

private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void prepare() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (String product : stockDao.getProductsList()) {
writer.write(product + "," + stockDao.get(product) + System.lineSeparator());
}
} catch (IOException e) {
throw new RuntimeException("Cant print to file", e);

Choose a reason for hiding this comment

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

The exception message 'Cant print to file' should be more descriptive. Consider including the file name in the message to provide more context.

}
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/service/operations/Balance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.service.operations;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;

public class Balance implements Operation {
private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void update(String product, Integer amount) {
stockDao.set(product, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service.operations;

public interface Operation {
void update(String product, Integer amount);
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/service/operations/Purchase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.service.operations;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;

public class Purchase implements Operation {
private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void update(String product, Integer amount) {
stockDao.decrease(product, amount);
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/service/operations/Return.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.service.operations;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;

public class Return implements Operation {
private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void update(String product, Integer amount) {
stockDao.increase(product, amount);
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/service/operations/Supply.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.service.operations;

import core.basesyntax.db.StockDao;
import core.basesyntax.db.StockDaoStorageImpl;

public class Supply implements Operation {
private final StockDao stockDao = new StockDaoStorageImpl();

@Override
public void update(String product, Integer amount) {
stockDao.increase(product, amount);
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.storage;

import java.util.HashMap;

public class Storage {
public static final HashMap<String, Integer> stock = new HashMap<>();

Choose a reason for hiding this comment

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

Using a static HashMap for storing stock data can lead to potential issues with data consistency and thread safety. Consider using a more robust solution, such as dependency injection, to manage the storage instance, especially if the application is expected to run in a multi-threaded environment.

}
Loading