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

implemented the requirement of the task #816

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions file.CSV
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type,fruit,quantity
b,apple,100
b,banana,20
s,apple,20
p,apple,50
r,banana,40
p,banana,25
1 change: 1 addition & 0 deletions file1.CSV
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fruit, quantity
3 changes: 3 additions & 0 deletions newFIle.CSV
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit, quantity
apple,100
banana,25
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<goal>check</goal>
</goals>
<configuration>
<excludes>
<exclude>**/Main*</exclude>
</excludes>
<rules>
<rule>
<element>BUNDLE</element>
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.dao.FruitTransactionDaoImpl;
import core.basesyntax.model.Operation;
import core.basesyntax.service.FruitService;
import core.basesyntax.service.amount.ActivityHandler;
import core.basesyntax.service.amount.BalanceActivityHandler;
import core.basesyntax.service.amount.PurchaseActivityHandler;
import core.basesyntax.service.amount.ReturnActivityHandler;
import core.basesyntax.service.amount.SupplyActivityHandler;
import core.basesyntax.service.impl.FruitServiceImpl;
import core.basesyntax.service.impl.ParserServiceImpl;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.strategy.TypeActivityStrategy;
import core.basesyntax.strategy.TypeActivityStrategyImpl;
import java.util.HashMap;
import java.util.Map;

/**
* Feel free to remove this class and create your own.
*/
public class Main {
private static final String FILE = "file.CSV";
private static final String TO_FILE = "newFile.CSV";

public static void main(String[] args) {
Map<Operation, ActivityHandler>
amountOfFruitsHandlersMap = new HashMap<>();

amountOfFruitsHandlersMap
.put(Operation.RETURN,
new ReturnActivityHandler(new FruitTransactionDaoImpl()));

amountOfFruitsHandlersMap
.put(Operation.BALANCE,
new BalanceActivityHandler(new FruitTransactionDaoImpl()));

amountOfFruitsHandlersMap
.put(Operation.PURCHASE,
new PurchaseActivityHandler(new FruitTransactionDaoImpl()));

amountOfFruitsHandlersMap
.put(Operation.SUPPLY,
new SupplyActivityHandler(new FruitTransactionDaoImpl()));

TypeActivityStrategy typeActivityStrategy =
new TypeActivityStrategyImpl(amountOfFruitsHandlersMap);

FruitService fruitService =
new FruitServiceImpl(new ReaderServiceImpl(new ParserServiceImpl()),
new WriterServiceImpl(), typeActivityStrategy);
fruitService.writeReport(FILE);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitTransactionDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax.dao;

import core.basesyntax.model.FruitTransaction;

public interface FruitTransactionDao {
FruitTransaction getFromStorage(FruitTransaction fruitTransaction);

void addToStorage(FruitTransaction fruitTransaction);
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitTransactionDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.dao;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;

public class FruitTransactionDaoImpl implements FruitTransactionDao {
@Override
public FruitTransaction getFromStorage(FruitTransaction fruitTransaction) {
for (FruitTransaction fruit : Storage.fruitTransactions) {
if (fruit.getFruit().equals(fruitTransaction.getFruit())) {
return fruit;
}
}
return null;
}

@Override
public void addToStorage(FruitTransaction fruitTransaction) {
if (fruitTransaction == null) {
throw new RuntimeException("Fruit cannot be null");
}
Storage.fruitTransactions.add(fruitTransaction);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax.db;

import core.basesyntax.model.FruitTransaction;
import java.util.ArrayList;
import java.util.List;

public class Storage {
public static final List<FruitTransaction> fruitTransactions = new ArrayList<>();
}
79 changes: 79 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package core.basesyntax.model;

import java.util.Objects;

public class FruitTransaction {
private String fruit;
private int quantity;
private Operation operation;

private FruitTransaction(Operation operation, String fruit, int quantity) {
this.operation = operation;
this.fruit = fruit;
this.quantity = quantity;
}

public static FruitTransaction of(Operation operation, String fruit, int quantity) {
if (quantity < 0) {
throw new RuntimeException("Quantity cannot be less than 0");
}
return new FruitTransaction(operation, fruit, quantity);
}

public String getFruit() {
return fruit;
}

public void subtract(int quantity) {
if (quantity > this.quantity) {
throw new RuntimeException("Cannot sell fruits more than in stock");
}
this.quantity -= quantity;
}

public void add(int quantity) {
this.quantity += quantity;
}

public void setFruit(String fruit) {
this.fruit = fruit;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
if (quantity > 0) {
this.quantity = quantity;
} else {
throw new RuntimeException("Quantity cannot be less than 0");
}
}

public Operation getOperation() {
return operation;
}

public void setOperation(Operation operation) {
this.operation = operation;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FruitTransaction that = (FruitTransaction) o;
return quantity == that.quantity
&& Objects.equals(fruit, that.fruit) && operation == that.operation;
}

@Override
public int hashCode() {
return Objects.hash(fruit, quantity, operation);
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/model/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.model;

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;
}

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

public interface FruitService {
void writeReport(String file);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ParserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import java.util.List;

public interface ParserService {
List<FruitTransaction> parseStringsIntoObjects(List<String> strings);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ReaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import java.util.List;

public interface ReaderService {
List<FruitTransaction> readFromFile(String fromFile);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/WriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface WriterService {
void writeToFile(String toFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service.amount;

import core.basesyntax.model.FruitTransaction;

public interface ActivityHandler {
void setAmountOfFruit(FruitTransaction fruitTransaction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.amount;

import core.basesyntax.dao.FruitTransactionDao;
import core.basesyntax.model.FruitTransaction;

public class BalanceActivityHandler implements ActivityHandler {
private final FruitTransactionDao fruitTransactionDao;

public BalanceActivityHandler(FruitTransactionDao fruitTransactionDao) {
this.fruitTransactionDao = fruitTransactionDao;
}

@Override
public void setAmountOfFruit(FruitTransaction fruitTransaction) {
fruitTransactionDao.addToStorage(fruitTransaction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.service.amount;

import core.basesyntax.dao.FruitTransactionDao;
import core.basesyntax.model.FruitTransaction;

public class PurchaseActivityHandler implements ActivityHandler {
private final FruitTransactionDao fruitTransactionDao;

public PurchaseActivityHandler(FruitTransactionDao fruitTransactionDao) {
this.fruitTransactionDao = fruitTransactionDao;
}

@Override
public void setAmountOfFruit(FruitTransaction fruitTransaction) {
fruitTransactionDao.getFromStorage(fruitTransaction)
.subtract(fruitTransaction.getQuantity());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.amount;

import core.basesyntax.dao.FruitTransactionDao;
import core.basesyntax.model.FruitTransaction;

public class ReturnActivityHandler implements ActivityHandler {
private final FruitTransactionDao fruitTransactionDao;

public ReturnActivityHandler(FruitTransactionDao fruitTransactionDao) {
this.fruitTransactionDao = fruitTransactionDao;
}

@Override
public void setAmountOfFruit(FruitTransaction fruitTransaction) {
fruitTransactionDao.getFromStorage(fruitTransaction).add(fruitTransaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.amount;

import core.basesyntax.dao.FruitTransactionDao;
import core.basesyntax.model.FruitTransaction;

public class SupplyActivityHandler implements ActivityHandler {
private final FruitTransactionDao fruitTransactionDao;

public SupplyActivityHandler(FruitTransactionDao fruitTransactionDao) {
this.fruitTransactionDao = fruitTransactionDao;
}

@Override
public void setAmountOfFruit(FruitTransaction fruitTransaction) {
fruitTransactionDao.getFromStorage(fruitTransaction).add(fruitTransaction.getQuantity());
}
}
36 changes: 36 additions & 0 deletions src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FruitService;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.WriterService;
import core.basesyntax.strategy.TypeActivityStrategy;
import java.util.List;

public class FruitServiceImpl implements FruitService {
private static final String COMA = ",";
private static final String TO_FILE = "newFile.CSV";

private ReaderService readerService;

private WriterService writerService;

private TypeActivityStrategy typeActivityStrategy;

public FruitServiceImpl(ReaderService readerService, WriterService writerService,
TypeActivityStrategy typeActivityStrategy) {
this.readerService = readerService;
this.writerService = writerService;
this.typeActivityStrategy = typeActivityStrategy;
}

@Override
public void writeReport(String file) {
List<FruitTransaction> fruitTransactions = readerService.readFromFile(file);
fruitTransactions.stream()
.forEach(fruit -> typeActivityStrategy
.get(fruit.getOperation()).setAmountOfFruit(fruit));
writerService.writeToFile(TO_FILE);
}

}
Loading
Loading