-
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
done #1296
base: master
Are you sure you want to change the base?
done #1296
Changes from 1 commit
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,8 @@ | ||
package core.basesyntax; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
@Override | ||
public void apply(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> data); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
import java.util.List; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
class DataConverterImpl implements DataConverter { | ||||||
@Override | ||||||
public List<FruitTransaction> convertToTransaction(List<String> data) { | ||||||
// Пропускаем первую строку с заголовками | ||||||
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. remove comment |
||||||
return data.stream() | ||||||
.skip(1) | ||||||
.map(line -> { | ||||||
String[] parts = line.split(","); | ||||||
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. create constant for comma |
||||||
String opCode = parts[0]; | ||||||
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. create constants for all magic numbers |
||||||
FruitTransaction.Operation op = switch (opCode) { | ||||||
case "b" -> FruitTransaction.Operation.BALANCE; | ||||||
case "s" -> FruitTransaction.Operation.SUPPLY; | ||||||
case "p" -> FruitTransaction.Operation.PURCHASE; | ||||||
case "r" -> FruitTransaction.Operation.RETURN; | ||||||
default -> throw new IllegalArgumentException("Invalid operation code: " | ||||||
+ opCode); | ||||||
}; | ||||||
String fruit = parts[1]; | ||||||
int quantity = Integer.parseInt(parts[2]); | ||||||
return new FruitTransaction(op, fruit, quantity); | ||||||
}) | ||||||
.collect(Collectors.toList()); | ||||||
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.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
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. remove redundant empty lines |
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String filepath) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements FileReader { | ||
public List<String> read(String filePath) throws IOException { | ||
return Files.readAllLines(Paths.get(filePath)); | ||
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. let's use try catch here |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.IOException; | ||
|
||
public interface FileWriter { | ||
void write(String content, String filePath) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
class FileWriterImpl implements FileWriter { | ||
public void write(String content, String filePath) throws IOException { | ||
Files.write(Paths.get(filePath), content.getBytes()); | ||
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. same here |
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package core.basesyntax; | ||
|
||
public class FruitTransaction { | ||
private Operation operation; | ||
private String fruit; | ||
private 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 void setOperation(Operation operation) { | ||
this.operation = operation; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public void setFruit(String fruit) { | ||
this.fruit = fruit; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(int quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
public enum Operation { | ||
BALANCE("b"), | ||
SUPPLY("s"), | ||
PURCHASE("p"), | ||
RETURN("r"); | ||
|
||
private String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
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. you can also create method to check if particular code exists |
||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] arg) throws IOException { | ||
// 1. Read the data from the input CSV file | ||
FileReader fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read("reportToRead.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. input and output files should be in |
||
|
||
// 2. Convert the incoming data into FruitTransactions list | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
final List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
|
||
// 3. Create and feel the map with all OperationHandler implementations | ||
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); | ||
|
||
// 4. Process the incoming transactions with applicable OperationHandler implementations | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
shopService.process(transactions); | ||
|
||
// 5.Generate report based on the current Storage state | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(); | ||
|
||
// 6. Write the received report into the destination file | ||
FileWriter fileWriter = new FileWriterImpl(); | ||
fileWriter.write(resultingReport, "finalReport.csv"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface OperationHandler { | ||
void apply(FruitTransaction transaction); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
|
||
public interface OperationStrategy { | ||
void process(List<FruitTransaction> transactions); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> | ||
operationHandlers) { | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> transactions) { | ||
for (FruitTransaction transaction : transactions) { | ||
OperationHandler handler = operationHandlers.get(transaction.getOperation()); | ||
if (handler != null) { | ||
handler.apply(transaction); | ||
} else { | ||
throw new IllegalArgumentException("No handler for operation: " | ||
+ transaction.getOperation()); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
public class PurchaseOperation implements OperationHandler { | ||
@Override | ||
public void apply(FruitTransaction transaction) { | ||
Storage.remove(transaction.getFruit(), transaction.getQuantity()); | ||
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 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 |
||
} | ||
} |
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,13 @@ | ||
package core.basesyntax; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
@Override | ||
public String getReport() { | ||
StringBuilder report = new StringBuilder(); | ||
Storage.getFruits().forEach((fruit, quantity) -> report.append(fruit) | ||
.append(",") | ||
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. create constant for comma |
||
.append(quantity) | ||
.append("\n")); | ||
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. Use |
||
return report.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
public class ReturnOperation implements OperationHandler { | ||
@Override | ||
public void apply(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 ShopService { | ||
public void process(List<FruitTransaction> transactions); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
import java.util.List; | ||||||
|
||||||
class ShopServiceImpl implements ShopService { | ||||||
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.
Suggested change
|
||||||
private final OperationStrategy operationStrategy; | ||||||
|
||||||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||||||
this.operationStrategy = operationStrategy; | ||||||
} | ||||||
|
||||||
public void process(List<FruitTransaction> transactions) { | ||||||
operationStrategy.process(transactions); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public 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 remove(String fruit, int quantity) { | ||
fruits.remove(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. The 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 |
||
} | ||
|
||
public static Map<String, Integer> getFruits() { | ||
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. redundant method |
||
return fruits; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax; | ||
|
||
public class SupplyOperation implements OperationHandler { | ||
@Override | ||
public void apply(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.
Let's revise common mistakes file and check project structure