05_ServiceClass
Code-Dateien
| Dateiname | Aktion |
|---|---|
| CODECode_Bank.zip | Download |
| CODECode_Pizza.zip | Download |
| CODECode_Urlaub.zip | Download |
Videos
| Dateiname | Aktion |
|---|---|
| VIDEOVideo_Pizza_E | Abspielen |
| VIDEOVideo_Urlaub_D | Abspielen |
Lernmaterialien
Service
Service Class
@Service
(org.springframework.stereotype.Service)
A service class is a class that contains the business logic of your application.
👉 It sits between:
- the controller (input / UI / API)
- and the data layer (repository / database)
Simple idea
- Entity (PizzaOrder) → data
- Service → logic (what to do with the data)
What the service does
- creates orders
- calculates price
- applies rules (business logic)
For your pizza example:
- in a small practice program, the
service classcan hold anArrayList<PizzaOrder> - in a real database application, the service class should not permanently hold the data itself; the data is usually stored in a repository/database
@Service
public class PizzaOrderService {
private ArrayList<PizzaOrder> orders;
public PizzaOrderService() {
orders = new ArrayList<>(1000);
}
@Override
public String toString() {
return orders.stream()
.map(order -> order.toString())
.collect(Collectors.joining("\n"));
}
}Faker Data
https://github.com/DiUS/java-faker
https://www.javadoc.io/doc/com.github.javafaker/javafaker/latest/com/github/javafaker/Faker.html
Faker is a Java class from a library that creates
fake test data.
It is used when you want sample values for testing, for example:
- names
- cities
- countries
- phone numbers
- prices
- random text
So instead of typing test data by hand, Faker generates
it for you.
Faker faker = new Faker();
System.out.println(faker.address().country());
System.out.println(faker.address().cityName());
System.out.println(faker.number().randomDouble(2, 10, 50));
Austria
Vienna
24.75pom.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
public void fillTestData(int anz) {
PizzaOrder p;
Faker faker;
String[] PIZZAS = {"Margherita", "Salami", "Tonno", "Hawaii", "Funghi", "Diavolo"};
String[] SIZES = { "Small", "Medium", "Large", "Family" };
faker = new Faker();
orders.clear();
for (int i = 0; i < anz; i++) {
p = new PizzaOrder();
p.setOrderId((long) (i + 1));
p.setOrderDate(LocalDate.now().minusDays(faker.number().numberBetween(0, 30)));
p.setPizza(PIZZAS[faker.number().numberBetween(0, PIZZAS.length)]);
p.setSize(SIZES[faker.number().numberBetween(0, SIZES.length)]);
p.setPrice(faker.number().randomDouble(2, 6, 20));
p.setQuantity(faker.number().numberBetween(1, 6));
p.setGarlic(faker.bool().bool());
orders.add(p);
} firstName = faker.address().firstName();
openingDate = LocalDate.now().minusDays((int) Math.random() * 3650);
accountType = accounttypes[faker.random().nextInt(0, accounttypes.length - 1)];
amount = faker.number().randomDouble(2, 0, 50000);
active = faker.bool().bool();
a = new Account(firstName, openingDate, accountType, amount, active);
accounts.add(a);Test
@Test
public void testFillTestData() {
PizzaOrderService orders = new PizzaOrderService();
System.out.println(orders);
}