Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions src/main/java/dev/example/restaurantManager/model/Dessert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.example.restaurantManager.model;

import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Dessert extends MenuItem {
private boolean toShare;

public Dessert(String id, String name, String description, double price, boolean toShare) {
super(id, name, description, price);
this.toShare = toShare;
}

public boolean getToShare() {
return toShare;
}

public void setToShare(boolean toShare) {
this.toShare = toShare;
}

// Example method specific to Dessert
public void isToShare() {
System.out.println("Dessert to share? " + toShare);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.example.restaurantManager.model;

public interface IMenuItem {
String getName();
double getPrice();
String getDescription();
}

29 changes: 29 additions & 0 deletions src/main/java/dev/example/restaurantManager/model/MainCourse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.example.restaurantManager.model;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Entity
public class MainCourse extends MenuItem {
private String servingSize;

public MainCourse(String id, String name, String description, double price, String servingSize) {
super(id, name, description, price);
this.servingSize = servingSize;
}

public String getServingSize() {
return servingSize;
}

public void setServingSize(String servingSize) {
this.servingSize = servingSize;
}

public void printServingSize() {
System.out.println("Serving Size: " + servingSize);
}
}

18 changes: 17 additions & 1 deletion src/main/java/dev/example/restaurantManager/model/MenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class MenuItem {
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class MenuItem implements IMenuItem {

@Id
private String id;
Expand All @@ -27,4 +28,19 @@ public MenuItem(String id, String name, String description, double price) {
this.description = description;
this.price = price;
}

@Override
public String getName() {
return this.name;
}

@Override
public double getPrice() {
return this.price;
}

@Override
public String getDescription() {
return this.description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.example.restaurantManager.repository;

import dev.example.restaurantManager.model.Dessert;
import dev.example.restaurantManager.model.MainCourse;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DessertRepository extends JpaRepository<Dessert, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.example.restaurantManager.repository;

import dev.example.restaurantManager.model.MainCourse;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MainCourseRepository extends JpaRepository<MainCourse, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class DataLoader {
private ShippingOrderRepository shippingOrderRepository;
@Autowired
private TakeAwayOrderRepository takeAwayOrderRepository;
@Autowired
private MainCourseRepository mainCourseRepository;
@Autowired
private DessertRepository dessertRepository;
private final Faker faker = new Faker(new Locale("en-US"));

// master method orchestrating all other methods to
Expand All @@ -44,7 +48,8 @@ public void loadAllData() {
// then create relationships between them
createCustomers();
createTables();
createMenuItems();
createMainCourse();
createDessert();

// create and assign menu items
createMenusAndAssignMenuItems();
Expand Down Expand Up @@ -94,20 +99,34 @@ private void createTables() {
}
}

// we are going to create 25 menu items
// and save them in the H2 local database
private void createMenuItems() {
for (int i = 0; i < 25; i++) {
MenuItem menuItem = new MenuItem(
private void createMainCourse() {
for (int i = 0; i < 10; i++) {
MenuItem menuItem;
menuItem = new MainCourse(
UUID.randomUUID().toString(),
faker.food().dish(),
faker.food().ingredient() + " " + faker.food().ingredient(),
faker.number().randomDouble(2, 5, 30),
faker.options().option("Small", "Medium", "Large")
);
menuItemRepository.save(menuItem);
}
}
private void createDessert() {
for (int i = 0; i < 10; i++) {
MenuItem menuItem;
menuItem = new Dessert(
UUID.randomUUID().toString(),
faker.food().dish(),
faker.food().ingredient() + " " + faker.food().ingredient() ,
faker.number().randomDouble(2, 5, 30)
faker.food().ingredient() + " " + faker.food().ingredient(),
faker.number().randomDouble(2, 5, 30),
faker.random().nextBoolean()
);
menuItemRepository.save(menuItem);
}
}


// we are going to create 15 menus
// and assign 5 to 10 menu items to each menu
// to create a many-to-many relationship
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


# H2 LOCAL DB SERVER
spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDB
spring.datasource.url=jdbc:h2:/home/emma/MyProjects/DataBase/restaurantManager/restaurantManagerDB
#spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDBTest
spring.datasource.username=albert
spring.datasource.username=emma
spring.datasource.password=1234

# DDL OPTIONS: create-drop, create, update, none, validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.example.restaurantManager.model.Booking;
import dev.example.restaurantManager.model.Customer;
import dev.example.restaurantManager.model.MainCourse;
import dev.example.restaurantManager.model.TableRestaurant;
import dev.example.restaurantManager.repository.BookingRepository;
import dev.example.restaurantManager.repository.CustomerRepository;
Expand All @@ -13,6 +14,7 @@
import java.util.Date;
import java.util.Optional;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DataJpaTest
public class BookingTableManyToManyTest {
Expand Down Expand Up @@ -127,4 +129,5 @@ void createBooking() {


}

}
77 changes: 77 additions & 0 deletions src/test/java/dev/example/restaurantManager/MenuItemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dev.example.restaurantManager;

import dev.example.restaurantManager.model.Dessert;
import dev.example.restaurantManager.model.MainCourse;
import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.repository.DessertRepository;
import dev.example.restaurantManager.repository.MainCourseRepository;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@DataJpaTest
class MenuItemTest {

@Autowired
private DessertRepository dessertRepository;
@Autowired
private MainCourseRepository mainCourseRepository;

@Test
public void DessertRepositoryTest() {
Dessert dessert = new Dessert(
"D03",
"Apple Pie",
"Warm apple pie with cinnamon",
6.50,
true
);
dessertRepository.save(dessert);

Dessert found = dessertRepository.findById("D03").orElse(null);
assertNotNull(found);
assertEquals("Apple Pie", found.getName());
assertTrue(found.getToShare());
}

@Test
public void MainCourseRepositoryTest() {

MainCourse mainCourse = new MainCourse(
"MC03",
"Salmon",
"Grilled salmon with spices",
15.99,
"Large"
);
mainCourseRepository.save(mainCourse);

MainCourse found = mainCourseRepository.findById("MC03").orElse(null);
assertNotNull(found);
assertEquals("Salmon", found.getName());
assertEquals("Large", found.getServingSize());
}

@Test
void testPolymorphicBehavior() {
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new MainCourse("MC02", "Pasta", "Pasta with tomato sauce", 10.50, "Medium"));
menuItems.add(new Dessert("D02", "Ice Cream", "Vanilla flavor", 5.99, false));

for (MenuItem item : menuItems) {
assertNotNull(item.getName());
assertTrue(item.getPrice() > 0);
assertTrue(item.getDescription().length() > 0);
}
}

}