Skip to content

Commit

Permalink
Stage 2 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
wisskirchenj committed Aug 14, 2023
1 parent 5121458 commit f659b9a
Show file tree
Hide file tree
Showing 17 changed files with 395 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kubernetes-settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ Sources for all project tasks (4 stages) with tests and configurations.
10.08.23 Stage 1 completed. Just the creation of an H2-file database with a company table. Modelled with JPA entity and
JPARepository and have Spring JPA to the DDL-creation. Take database-filename from program arguments and programatically
set the `spring.datasource.url` property before running the `SpringApplication`.

14.08.23 Stage 2 completed. Have command line menus provided as Spring beans (`@Controller`) and have a stdin-scanner
provided as bean. The CommandLineRunner uses a service to connect (query / insert) with the database vie the JPA-repository.
9 changes: 9 additions & 0 deletions src/main/java/de/cofinpro/cars/CarSharingApp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.cofinpro.cars;

import de.cofinpro.cars.controller.MenuRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;

import java.util.Properties;
Expand All @@ -19,4 +22,10 @@ public static void main(String[] args) {
app.setDefaultProperties(properties);
app.run(args);
}

@Bean
CommandLineRunner commandLineRunner(MenuRunner menuRunner) {
return args -> menuRunner.run();
}

}
54 changes: 54 additions & 0 deletions src/main/java/de/cofinpro/cars/controller/ManagerMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.cofinpro.cars.controller;

import de.cofinpro.cars.io.ConsolePrinter;
import de.cofinpro.cars.service.CompanyService;
import org.springframework.stereotype.Controller;

import java.util.Scanner;

@Controller
public class ManagerMenu {

private final CompanyService companyService;
private final ConsolePrinter printer;
private final Scanner scanner;

public ManagerMenu(CompanyService companyService, ConsolePrinter printer, Scanner scanner) {
this.companyService = companyService;
this.printer = printer;
this.scanner = scanner;
}

public void run() {
printMenu();
var choice = Integer.parseInt(scanner.nextLine());
while (choice != 0) {
switch (choice) {
case 1 -> listCompanies();
case 2 -> createCompany();
default -> throw new IllegalStateException("invalid choice");
}
printMenu();
choice = Integer.parseInt(scanner.nextLine());
}
}

private void createCompany() {
printer.printInfo("Enter the company name:");
var name = scanner.nextLine();
companyService.createCompany(name);
printer.printInfo("The company was created!");
}

private void listCompanies() {
printer.printCompanyList(companyService.listCompanies());
}

private void printMenu() {
printer.printInfo("""
1. Company list
2. Create a company
0. Back
""");
}
}
38 changes: 38 additions & 0 deletions src/main/java/de/cofinpro/cars/controller/MenuRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.cofinpro.cars.controller;

import de.cofinpro.cars.io.ConsolePrinter;
import org.springframework.stereotype.Controller;

import java.util.Scanner;

@Controller
public class MenuRunner {
private final ConsolePrinter printer;
private final ManagerMenu managerMenu;
private final Scanner scanner;

public MenuRunner(ConsolePrinter printer,
ManagerMenu managerMenu,
Scanner stdinScanner) {
this.printer = printer;
this.managerMenu = managerMenu;
this.scanner = stdinScanner;
}

public void run() {
printMenu();
var choice = Integer.parseInt(scanner.nextLine());
while (choice != 0) {
managerMenu.run();
printMenu();
choice = Integer.parseInt(scanner.nextLine());
}
}

private void printMenu() {
printer.printInfo("""
1. Log in as a manager
0. Exit
""");
}
}
24 changes: 24 additions & 0 deletions src/main/java/de/cofinpro/cars/io/CommandLineConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.cofinpro.cars.io;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/**
* Configuration class, offering (singleton) beans as the stdin-scanner or date-formatter.
*/
@Configuration
public class CommandLineConfiguration {

@Bean
public Scanner getStdinScanner() {
return new Scanner(System.in);
}

@Bean
public DateTimeFormatter getDateFormatter() {
return DateTimeFormatter.ofPattern("yyyy.MM.dd");
}
}
28 changes: 28 additions & 0 deletions src/main/java/de/cofinpro/cars/io/ConsolePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.cofinpro.cars.io;

import de.cofinpro.cars.persistence.Company;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Slf4J wrapping printer class, accepting input to print to the console (stdout)
*/
@Slf4j
@Component
public class ConsolePrinter {

public void printInfo(String message) {
log.info(message);
}

public void printCompanyList(List<Company> companies) {
if (companies.isEmpty()) {
printInfo("The company list is empty!");
} else {
companies.forEach(company -> printInfo("%d. %s".formatted(company.getId(), company.getName())));
}
printInfo("");
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/cofinpro/cars/persistence/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public class Company {
@Column(name = "ID", columnDefinition = "INT")
private int id;

@Column(name = "NAME", columnDefinition = "VARCHAR_IGNORECASE(255)")
@Column(name = "NAME", columnDefinition = "VARCHAR_IGNORECASE(255) UNIQUE NOT NULL")
private String name;
}

0 comments on commit f659b9a

Please sign in to comment.