diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/JavaPizza.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/JavaPizza.java index b9bfcbd4..88f02583 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/JavaPizza.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/JavaPizza.java @@ -1,12 +1,11 @@ package pizza; +import pizza.controller.AppController; import pizza.service.CustomerService; import pizza.service.ExtComponentService; import pizza.service.OrderService; import pizza.service.PizzaService; -import java.util.Scanner; - /** * AIT-TR, cohort 42.1, Java Basic, Project #1 * JavaPizza based on https://www.foodora.cz/en/restaurant/uyou/saporito-pizza-and-pasta @@ -16,12 +15,6 @@ */ public class JavaPizza { public static void main(String[] args) { - // define variables - char cmd; - String[] input; - String name, composition; - int id, price; - Scanner scanner = new Scanner(System.in); // create all services PizzaService pizzaService = new PizzaService(); ExtComponentService extComponentService = new ExtComponentService(); @@ -29,63 +22,9 @@ public static void main(String[] args) { OrderService orderService = new OrderService(); // init data pizzaService.init(); - // main menu - do { - System.out.print("Choose service: [p]izza, [e]xtcomponent, [c]ustomer, [o]rder, e[x]it: "); - cmd = scanner.nextLine().charAt(0); - switch (cmd) { - case 'p': - do { - System.out.print("Pizza service: [a]dd, [u]pdate, [d]elete, [p]rint, [b]ack: "); - cmd = scanner.nextLine().charAt(0); - switch (cmd) { - case 'a': - System.out.print("Pizza service: add: name & composition & price: "); - input = scanner.nextLine().split("&"); - name = input[0].trim(); - composition = input[1].trim(); - price = Integer.valueOf(input[2].trim()); - pizzaService.add(name, composition, price); - break; - case 'u': - System.out.print("Pizza service: update: id & name & composition & price: "); - input = scanner.nextLine().split("&"); - id = Integer.valueOf(input[0].trim()); - name = input[1].trim(); - composition = input[2].trim(); - price = Integer.valueOf(input[3].trim()); - pizzaService.update(id, name, composition, price); - break; - case 'd': - System.out.print("Pizza service: delete: id: "); - id = Integer.valueOf(scanner.nextLine()); - pizzaService.delete(id); - break; - case 'p': - pizzaService.print(); - break; - case 'b': - break; - default: - System.out.println("Unrecognized command: " + cmd); - } - } while (cmd != 'b'); - break; - case 'e': - // TODO Call ExtComponentService - break; - case 'c': - // TODO Call CustomerService - break; - case 'o': - // TODO Call OrderService - break; - case 'x': - break; - default: - System.out.println("Unrecognized command: " + cmd); - } - } while (cmd != 'x'); - System.out.println("Exit."); + // create controller + AppController controller = new AppController(pizzaService, extComponentService, customerService, orderService); + // run application + controller.run(); } } diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/controller/AppController.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/controller/AppController.java new file mode 100644 index 00000000..813209a4 --- /dev/null +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/controller/AppController.java @@ -0,0 +1,96 @@ +package pizza.controller; + +import pizza.service.CustomerService; +import pizza.service.ExtComponentService; +import pizza.service.OrderService; +import pizza.service.PizzaService; + +import java.util.Scanner; + +public class AppController { + private PizzaService pizzaService; + private ExtComponentService extСomponentService; + private CustomerService customerService; + private OrderService orderService; + private Scanner scanner; + + public AppController(PizzaService pizzaService, + ExtComponentService extСomponentService, + CustomerService customerService, + OrderService orderService) { + this.pizzaService = pizzaService; + this.extСomponentService = extСomponentService; + this.customerService = customerService; + this.orderService = orderService; + this.scanner = new Scanner(System.in); + } + + public void run() { + char cmd; + do { + System.out.print("Choose service: [p]izza, [e]xtcomponent, [c]ustomer, [o]rder, e[x]it: "); + cmd = scanner.nextLine().charAt(0); + switch (cmd) { + case 'p': + pizzaServiceMenu(); + break; + case 'e': + // TODO Call ExtComponentService + break; + case 'c': + // TODO Call CustomerService + break; + case 'o': + // TODO Call OrderService + break; + case 'x': + break; + default: + System.out.println("Unrecognized command: " + cmd); + } + } while (cmd != 'x'); + System.out.println("Exit."); + } + + private void pizzaServiceMenu() { + char cmd; + String[] input; + String name, composition; + int id, price; + do { + System.out.print("Pizza service: [a]dd, [u]pdate, [d]elete, [p]rint, [b]ack: "); + cmd = scanner.nextLine().charAt(0); + switch (cmd) { + case 'a': + System.out.print("Pizza service: add: name & composition & price: "); + input = scanner.nextLine().split("&"); + name = input[0].trim(); + composition = input[1].trim(); + price = Integer.valueOf(input[2].trim()); + pizzaService.add(name, composition, price); + break; + case 'u': + System.out.print("Pizza service: update: id & name & composition & price: "); + input = scanner.nextLine().split("&"); + id = Integer.valueOf(input[0].trim()); + name = input[1].trim(); + composition = input[2].trim(); + price = Integer.valueOf(input[3].trim()); + pizzaService.update(id, name, composition, price); + break; + case 'd': + System.out.print("Pizza service: delete: id: "); + id = Integer.valueOf(scanner.nextLine()); + pizzaService.delete(id); + break; + case 'p': + pizzaService.print(); + break; + case 'b': + break; + default: + System.out.println("Unrecognized command: " + cmd); + } + } while (cmd != 'b'); + } +} diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Customer.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Customer.java similarity index 97% rename from BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Customer.java rename to BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Customer.java index 54c78f06..f1d4ddeb 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Customer.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Customer.java @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; /** * Pizza customer class diff --git "a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Ext\320\241omponent.java" "b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Ext\320\241omponent.java" similarity index 97% rename from "BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Ext\320\241omponent.java" rename to "BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Ext\320\241omponent.java" index 38c4209f..c17981d2 100644 --- "a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Ext\320\241omponent.java" +++ "b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Ext\320\241omponent.java" @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; /** * Extra (additional) component to standard pizza diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Order.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Order.java similarity index 83% rename from BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Order.java rename to BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Order.java index 09579bb4..725c1d96 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Order.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Order.java @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; /** * Pizza order class diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderPizza.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderPizza.java similarity index 82% rename from BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderPizza.java rename to BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderPizza.java index 6642c8f5..174b0aea 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderPizza.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderPizza.java @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; import java.util.ArrayList; import java.util.List; @@ -10,10 +10,13 @@ * @version 15-Apr-24 */ public class OrderPizza { + private int id; private Pizza pizza; private List components; + private static int idCounter = 0; public OrderPizza(Pizza pizza) { + this.id = ++idCounter; this.pizza = pizza; } diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderState.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderState.java similarity index 90% rename from BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderState.java rename to BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderState.java index 596f89fb..2c3514ea 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/OrderState.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/OrderState.java @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; /** * Order states diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Pizza.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Pizza.java similarity index 97% rename from BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Pizza.java rename to BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Pizza.java index cb64142b..4b28952f 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/base/Pizza.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/data/Pizza.java @@ -1,4 +1,4 @@ -package pizza.base; +package pizza.data; /** * Standard pizza class diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/CustomerService.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/CustomerService.java index 3178891d..e5960f8a 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/CustomerService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/CustomerService.java @@ -1,6 +1,6 @@ package pizza.service; -import pizza.base.Customer; +import pizza.data.Customer; import java.util.ArrayList; import java.util.HashMap; @@ -12,26 +12,22 @@ * Encapsulates the list of customets & CRUD operations with them * * @author Sergey Iryupin - * @version 15-Apr-24 + * @version 17-Apr-24 */ public class CustomerService { - private List customers; private Map customerMap; public CustomerService() { - customers = new ArrayList<>(); customerMap = new HashMap<>(); } public void init() { Customer customer = new Customer("Anonymous", null, null); - customers.add(customer); customerMap.put(customer.getId(), customer); } public void add(String name, String address, String phone) { Customer customer = new Customer(name, address, phone); - customers.add(customer); customerMap.put(customer.getId(), customer); } @@ -47,7 +43,6 @@ public boolean update(int id, String name, String address, String phone) { public boolean delete(int id) { Customer delCustomer = customerMap.get(id); if (delCustomer != null) { - customers.remove(delCustomer); customerMap.remove(id); return true; } @@ -55,6 +50,6 @@ public boolean delete(int id) { } public void print() { - customers.forEach(System.out::println); + customerMap.values().forEach(System.out::println); } } diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/ExtComponentService.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/ExtComponentService.java index 94fc9b5f..cf365c32 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/ExtComponentService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/ExtComponentService.java @@ -1,6 +1,6 @@ package pizza.service; -import pizza.base.ExtСomponent; +import pizza.data.ExtСomponent; import java.util.ArrayList; import java.util.HashMap; @@ -13,18 +13,17 @@ * Encapsulates the list of extra components & CRUD operations with them * * @author Sergey Iryupin - * @version 15-Apr-24 + * @version 17-Apr-24 */ public class ExtComponentService { - private List components; private Map componentMap; public ExtComponentService() { - components = new ArrayList<>(); componentMap = new HashMap<>(); } public void init() { + List components = new ArrayList<>(); components.addAll(List.of( new ExtСomponent("ham", 25), new ExtСomponent("mozzarella", 25), @@ -37,7 +36,6 @@ public void init() { public void add(String name, int price) { ExtСomponent component = new ExtСomponent(name, price); - components.add(component); componentMap.put(component.getId(), component); } @@ -53,7 +51,6 @@ public boolean update(int id, String name, int price) { public boolean delete(int id) { ExtСomponent delComponent = componentMap.get(id); if (delComponent != null) { - components.remove(delComponent); componentMap.remove(id); return true; } @@ -61,6 +58,6 @@ public boolean delete(int id) { } public void print() { - components.forEach(System.out::println); + componentMap.values().forEach(System.out::println); } } diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/OrderService.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/OrderService.java index 68dd18b7..bc2edab9 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/OrderService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/OrderService.java @@ -1,6 +1,6 @@ package pizza.service; -import pizza.base.Order; +import pizza.data.Order; import java.util.ArrayList; import java.util.HashMap; @@ -12,14 +12,12 @@ * Encapsulates the list of orders & CRUD operations with them * * @author Sergey Iryupin - * @version 15-Apr-24 + * @version 17-Apr-24 */ public class OrderService { - private List orders; private Map orderMap; public OrderService() { - orders = new ArrayList<>(); orderMap = new HashMap<>(); } diff --git a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/PizzaService.java b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/PizzaService.java index 48efe15c..c9078cc2 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/PizzaService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/service/PizzaService.java @@ -1,6 +1,6 @@ package pizza.service; -import pizza.base.Pizza; +import pizza.data.Pizza; import java.util.ArrayList; import java.util.HashMap; @@ -13,18 +13,17 @@ * Encapsulates the list of pizzas & CRUD operations with them * * @author Sergey Iryupin - * @version 15-Apr-24 + * @version 17-Apr-24 */ public class PizzaService { - private List pizzas; private Map pizzaMap; public PizzaService() { - pizzas = new ArrayList<>(); pizzaMap = new HashMap<>(); } public void init() { + List pizzas = new ArrayList<>(); pizzas.addAll(List.of( new Pizza("Margherita", "tomato base, mozzarella, basil", 174), new Pizza("Capriciosa", "tomato base, mozzarella, ham, champignons, olives", 190), @@ -37,7 +36,6 @@ public void init() { public void add(String name, String composition, int price) { Pizza pizza = new Pizza(name, composition, price); - pizzas.add(pizza); pizzaMap.put(pizza.getId(), pizza); } @@ -53,7 +51,6 @@ public boolean update(int id, String name, String composition, int price) { public boolean delete(int id) { Pizza delPizza = pizzaMap.get(id); if (delPizza != null) { - pizzas.remove(delPizza); pizzaMap.remove(id); return true; } @@ -61,6 +58,6 @@ public boolean delete(int id) { } public void print() { - pizzas.forEach(System.out::println); + pizzaMap.values().forEach(System.out::println); } }