Skip to content

Commit

Permalink
java pizza project #2 file repository & test
Browse files Browse the repository at this point in the history
  • Loading branch information
biblelamp committed May 9, 2024
1 parent c013181 commit 3f21eec
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 10 deletions.
8 changes: 8 additions & 0 deletions BerlinAIT/Cohort42.1project/JavaPizzaMvn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package pizza;

import pizza.controller.AppController;
import pizza.repository.CustomerRepository;
import pizza.repository.ExtComponentRepository;
import pizza.repository.OrderRepository;
import pizza.repository.PizzaRepository;
import pizza.domain.Pizza;
import pizza.repository.*;
import pizza.service.CustomerService;
import pizza.service.ExtComponentService;
import pizza.service.OrderService;
Expand All @@ -19,8 +17,11 @@
*/
public class JavaPizzaApp {
public static void main(String[] args) {
// define db name && file name
final String SQLITE_DB_NAME = "jdbc:sqlite:C:/temp/java_pizza.db";
final String PIZZA_FILE = "C:\\temp\\java_pizza_pizza.txt";
// create all repositories
PizzaRepository pizzaRepository = new PizzaRepository("jdbc:sqlite:C:/temp/java_pizza.db");
CrudRepository<Integer, Pizza> pizzaRepository = new PizzaFileRepository(PIZZA_FILE);//new PizzaRepository(SQLITE_DB_NAME);
ExtComponentRepository extComponentRepository = new ExtComponentRepository();
CustomerRepository customerRepository = new CustomerRepository();
OrderRepository orderRepository = new OrderRepository();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package pizza.repository;

import pizza.domain.Pizza;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PizzaFileRepository implements CrudRepository<Integer, Pizza> {
private String fileName;
private final String SPLIT_CHAR = ";";

public PizzaFileRepository(String fileName) {
this.fileName = fileName;
}

@Override
public Collection<Pizza> findAll() {
Collection<Pizza> pizzas = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(Paths.get(fileName));
lines.forEach(str -> {
String[] fields = str.split(SPLIT_CHAR);
pizzas.add(new Pizza(Integer.valueOf(fields[0]), fields[1], fields[2], Integer.valueOf(fields[3])));
});
} catch (NoSuchFileException e) {
// if file not found - return empty list
} catch (IOException e) {
throw new RuntimeException(e);
}
return pizzas;
}

@Override
public Pizza findById(Integer id) {
Collection<Pizza> pizzas = findAll();
for (Pizza pizza : pizzas) {
if (pizza.getId() == id) {
return pizza;
}
}
return null;
}

@Override
public void save(Pizza pizza) {
// read all records from file
Collection<Pizza> pizzas = findAll();
if (pizza.getId() == null) {
// add record and generate id
int id = 0;
for (Pizza p : pizzas) { // TODO optimization
if (id < p.getId()) {
id = p.getId();
}
}
pizza.setId(id + 1);
pizzas.add(pizza);
} else {
// update record
for (Pizza p : pizzas) {
if (p.getId() == pizza.getId()) {
p.update(pizza.getName(), pizza.getComposition(), pizza.getPrice());
break;
}
}
}
// rewrite all records to file
rewiteFile(pizzas);
}

@Override
public void remove(Integer id) {
Collection<Pizza> pizzas = findAll();
Pizza deletedPizza = null;
for (Pizza pizza : pizzas) {
if (pizza.getId() == id) {
deletedPizza = pizza;
break;
}
}
// if pizza found by id
if (deletedPizza != null) {
pizzas.remove(deletedPizza);
// rewrite all records to file
rewiteFile(pizzas);
}
}

private void rewiteFile(Collection<Pizza> pizzas) {
try (FileWriter writer = new FileWriter(fileName)) { // TODO Buffered
for (Pizza p : pizzas) {
String str = p.getId() + SPLIT_CHAR + p.getName() + SPLIT_CHAR + p.getComposition() + SPLIT_CHAR + p.getPrice();
writer.write(str + "\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public PizzaRepository(String dbName) {
this.dbName = dbName;
}

public PizzaRepository() {
//pizzaMap = new HashMap<>();
}
// public PizzaRepository() {
// pizzaMap = new HashMap<>();
// }

@Override
public void save(Pizza pizza) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pizza.service;

import pizza.domain.Pizza;
import pizza.repository.CrudRepository;
import pizza.repository.PizzaRepository;

/**
Expand All @@ -11,9 +12,9 @@
* @version 18-Apr-24
*/
public class PizzaService {
private PizzaRepository repository;
private CrudRepository<Integer, Pizza> repository;

public PizzaService(PizzaRepository repository) {
public PizzaService(CrudRepository<Integer, Pizza> repository) {
this.repository = repository;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pizza;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pizza.domain.Pizza;
import pizza.repository.CrudRepository;
import pizza.repository.PizzaFileRepository;
import pizza.service.PizzaService;

import java.util.Collection;

public class PizzaServiceTest {
final String PIZZA_FILE = "C:\\temp\\java_pizza_pizza_test.txt";
CrudRepository<Integer, Pizza> pizzaRepository;
private PizzaService pizzaService;

@BeforeEach
public void init() {
pizzaRepository = new PizzaFileRepository(PIZZA_FILE);
pizzaService = new PizzaService(pizzaRepository);
}

@Test
public void testAdd() {
Collection<Pizza> pizzas = pizzaRepository.findAll();
Assertions.assertEquals(0, pizzas.size());

pizzaService.add("Pizza", "Components...", 100);
pizzas = pizzaRepository.findAll();
Assertions.assertEquals(1, pizzas.size());

Pizza pizza = pizzas.iterator().next();
Assertions.assertEquals("Pizza", pizza.getName());
}
}

0 comments on commit 3f21eec

Please sign in to comment.