-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
first version #1235
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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(); | ||
inventory.synchronizeWithTheStorage(); | ||
|
||
Report report = new ReportToCsv(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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(); | ||
|
||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in the |
||
} | ||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface Inventory { | ||
|
||
void synchronizeWithTheStorage(); | ||
} |
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
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); | ||
} |
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface Report { | ||
void prepare(); | ||
} |
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
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); | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a static |
||
} |
There was a problem hiding this comment.
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 .