-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
"""); | ||
} | ||
} |
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 | ||
"""); | ||
} | ||
} |
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"); | ||
} | ||
} |
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(""); | ||
} | ||
} |