From 3f21eec24e0f91eeb36a86d52ace6180b9b27734 Mon Sep 17 00:00:00 2001 From: Sergey Iryupin Date: Thu, 9 May 2024 11:50:08 +0200 Subject: [PATCH] java pizza project #2 file repository & test --- .../Cohort42.1project/JavaPizzaMvn/pom.xml | 8 ++ .../src/main/java/pizza/JavaPizzaApp.java | 11 +- .../pizza/repository/PizzaFileRepository.java | 105 ++++++++++++++++++ .../pizza/repository/PizzaRepository.java | 6 +- .../main/java/pizza/service/PizzaService.java | 5 +- .../src/test/java/pizza/PizzaServiceTest.java | 36 ++++++ 6 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaFileRepository.java create mode 100644 BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/test/java/pizza/PizzaServiceTest.java diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/pom.xml b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/pom.xml index 3adc522e..bec607e7 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/pom.xml +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/pom.xml @@ -26,6 +26,14 @@ slf4j-simple 1.7.36 + + + + org.junit.jupiter + junit-jupiter-api + 5.10.2 + test + \ No newline at end of file diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java index 66e0f197..310d2b58 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java @@ -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; @@ -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 pizzaRepository = new PizzaFileRepository(PIZZA_FILE);//new PizzaRepository(SQLITE_DB_NAME); ExtComponentRepository extComponentRepository = new ExtComponentRepository(); CustomerRepository customerRepository = new CustomerRepository(); OrderRepository orderRepository = new OrderRepository(); diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaFileRepository.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaFileRepository.java new file mode 100644 index 00000000..442d2699 --- /dev/null +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaFileRepository.java @@ -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 { + private String fileName; + private final String SPLIT_CHAR = ";"; + + public PizzaFileRepository(String fileName) { + this.fileName = fileName; + } + + @Override + public Collection findAll() { + Collection pizzas = new ArrayList<>(); + try { + List 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 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 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 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 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); + } + } +} diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaRepository.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaRepository.java index a546dd1f..f3d6722c 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaRepository.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/PizzaRepository.java @@ -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) { diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/PizzaService.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/PizzaService.java index dc76f01c..27192874 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/PizzaService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/PizzaService.java @@ -1,6 +1,7 @@ package pizza.service; import pizza.domain.Pizza; +import pizza.repository.CrudRepository; import pizza.repository.PizzaRepository; /** @@ -11,9 +12,9 @@ * @version 18-Apr-24 */ public class PizzaService { - private PizzaRepository repository; + private CrudRepository repository; - public PizzaService(PizzaRepository repository) { + public PizzaService(CrudRepository repository) { this.repository = repository; } diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/test/java/pizza/PizzaServiceTest.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/test/java/pizza/PizzaServiceTest.java new file mode 100644 index 00000000..1312cd92 --- /dev/null +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/test/java/pizza/PizzaServiceTest.java @@ -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 pizzaRepository; + private PizzaService pizzaService; + + @BeforeEach + public void init() { + pizzaRepository = new PizzaFileRepository(PIZZA_FILE); + pizzaService = new PizzaService(pizzaRepository); + } + + @Test + public void testAdd() { + Collection 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()); + } +}