Skip to content

Commit

Permalink
cohort 42.1 project #1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
biblelamp committed Apr 18, 2024
1 parent beb2664 commit 178c696
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 98 deletions.
71 changes: 5 additions & 66 deletions BerlinAIT/Cohort42.1project/JavaPizza/src/pizza/JavaPizza.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,76 +15,16 @@
*/
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();
CustomerService customerService = new CustomerService();
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();
}
}
Original file line number Diff line number Diff line change
@@ -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');
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

/**
* Pizza customer class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

/**
* Extra (additional) component to standard pizza
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

/**
* Pizza order class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,10 +10,13 @@
* @version 15-Apr-24
*/
public class OrderPizza {
private int id;
private Pizza pizza;
private List<ExtСomponent> components;
private static int idCounter = 0;

public OrderPizza(Pizza pizza) {
this.id = ++idCounter;
this.pizza = pizza;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

/**
* Order states
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pizza.base;
package pizza.data;

/**
* Standard pizza class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pizza.service;

import pizza.base.Customer;
import pizza.data.Customer;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -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<Customer> customers;
private Map<Integer, Customer> 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);
}

Expand All @@ -47,14 +43,13 @@ 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;
}
return false;
}

public void print() {
customers.forEach(System.out::println);
customerMap.values().forEach(System.out::println);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ExtСomponent> components;
private Map<Integer, ExtСomponent> componentMap;

public ExtComponentService() {
components = new ArrayList<>();
componentMap = new HashMap<>();
}

public void init() {
List<ExtСomponent> components = new ArrayList<>();
components.addAll(List.of(
new ExtСomponent("ham", 25),
new ExtСomponent("mozzarella", 25),
Expand All @@ -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);
}

Expand All @@ -53,14 +51,13 @@ 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;
}
return false;
}

public void print() {
components.forEach(System.out::println);
componentMap.values().forEach(System.out::println);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pizza.service;

import pizza.base.Order;
import pizza.data.Order;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -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<Order> orders;
private Map<Integer, Order> orderMap;

public OrderService() {
orders = new ArrayList<>();
orderMap = new HashMap<>();
}

Expand Down
Loading

0 comments on commit 178c696

Please sign in to comment.