-
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
SOLID #1256
base: master
Are you sure you want to change the base?
SOLID #1256
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
public void handle(FruitTransaction transaction) { | ||
Storage.add(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertToTransaction(List<String> rawData); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
public List<FruitTransaction> convertToTransaction(List<String> rawData) { | ||
List<FruitTransaction> fruitTransactions = new ArrayList<>(); | ||
for (int i = 1; i < rawData.size(); i++) { | ||
String[] parts = rawData.get(i).split(","); | ||
FruitTransaction.Operation operation = FruitTransaction.Operation.fromCode(parts[0]); | ||
String fruit = parts[1]; | ||
int quantity = Integer.parseInt(parts[2]); | ||
fruitTransactions.add(new FruitTransaction(operation, fruit, quantity)); | ||
} | ||
|
||
return fruitTransactions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements MyFileReader { | ||
public List<String> read(String filePath) { | ||
List<String> lines = new ArrayList<>(); | ||
|
||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to read file: " + filePath, e); | ||
} | ||
|
||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements MyFileWriter { | ||
@Override | ||
public void write(String report, String path) { | ||
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path))) { | ||
bufferedWriter.write(report); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write file: " + path, e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package core.basesyntax; | ||
|
||
public class FruitTransaction { | ||
private final Operation operation; | ||
private final String fruit; | ||
private final int quantity; | ||
|
||
public FruitTransaction(Operation operation, String fruit, int quantity) { | ||
this.operation = operation; | ||
this.fruit = fruit; | ||
this.quantity = quantity; | ||
} | ||
|
||
public Operation getOperation() { | ||
return operation; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public enum Operation { | ||
BALANCE("b"), | ||
SUPPLY("s"), | ||
PURCHASE("p"), | ||
RETURN("r"); | ||
|
||
private final String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public static Operation fromCode(String code) { | ||
for (Operation o : values()) { | ||
if (o.code.equals(code)) { | ||
return o; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Unknown operation code: " + code); | ||
} | ||
|
||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Feel free to remove this class and create your own. | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) { | ||
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); | ||
|
||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
MyFileReader fileReader = new FileReaderImpl(); | ||
//My IntelliJ version doesn't work with csv files, so I created a csv file locally on my PC | ||
String path = "C:/Users/hois/Desktop/toRead.csv"; | ||
List<String> inputReport = fileReader.read(path); | ||
shopService.process(dataConverter.convertToTransaction(inputReport)); | ||
|
||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(); | ||
|
||
MyFileWriter fileWriter = new FileWriterImpl(); | ||
fileWriter.write(resultingReport, "finalReport.csv"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
|
||
public interface MyFileReader { | ||
List<String> read(String filePath); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface MyFileWriter { | ||
void write(String report, String path); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface OperationHandler { | ||
void handle(FruitTransaction transaction); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler getOperationHandler(FruitTransaction.Operation operation); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, | ||
OperationHandler> operationHandlers) { | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
public OperationHandler getOperationHandler(FruitTransaction.Operation operation) { | ||
return operationHandlers.get(operation); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
public class PurchaseOperation implements OperationHandler { | ||
public void handle(FruitTransaction transaction) { | ||
Storage.subtract(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface ReportGenerator { | ||
String getReport(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
public static final String REPORT_TITLE = "fruit,quantity\n"; | ||
|
||
@Override | ||
public String getReport() { | ||
StringBuilder reportBuilder = new StringBuilder(REPORT_TITLE); | ||
Storage.getAll().forEach((fruit, quantity) -> reportBuilder.append(fruit) | ||
.append(",") | ||
.append(quantity) | ||
.append("\n")); | ||
|
||
return reportBuilder.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
public class ReturnOperation implements OperationHandler { | ||
public void handle(FruitTransaction transaction) { | ||
Storage.add(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
|
||
public interface ShopService { | ||
void process(List<FruitTransaction> fruitTransactions); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private final OperationStrategy operationStrategy; | ||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> fruitTransactions) { | ||
for (FruitTransaction transaction : fruitTransactions) { | ||
OperationHandler handler = operationStrategy | ||
.getOperationHandler(transaction.getOperation()); | ||
handler.handle(transaction); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
private static final Map<String, Integer> fruits = new HashMap<>(); | ||
|
||
public static void add(String fruit, int quantity) { | ||
fruits.put(fruit, fruits.getOrDefault(fruit, 0) + quantity); | ||
} | ||
|
||
public static void subtract(String fruit, int quantity) { | ||
fruits.put(fruit, fruits.getOrDefault(fruit, 0) - quantity); | ||
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 the resulting quantity after subtraction is not negative. If it is, throw a |
||
} | ||
|
||
public static Map<String, Integer> getAll() { | ||
return new HashMap<>(fruits); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
public class SupplyOperation implements OperationHandler { | ||
public void handle(FruitTransaction transaction) { | ||
Storage.add(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
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.
Ensure that the resulting balance in the storage is not negative after a purchase. If it is, throw a
RuntimeException
as per the checklist requirement: 'Handling Purchase operation. Check result balance before saving it in the Storage - it should be positive. ThrowRuntimeException
in case the balance is negative.'