package helloworld;

import static org.requirementsascode.UseCaseModelBuilder.newBuilder;

import java.util.function.Consumer;

import org.requirementsascode.UseCaseModel;
import org.requirementsascode.UseCaseModelBuilder;
import org.requirementsascode.UseCaseModelRunner;

public class HelloWorld04_EnterNameAndAgeExample extends AbstractHelloWorldExample{
	private static final Class<EnterText> ENTER_FIRST_NAME = EnterText.class;
	private static final Class<EnterText> ENTER_AGE = EnterText.class;
	
	private String firstName;
	private int age;
	
	public UseCaseModel buildWith(UseCaseModelBuilder modelBuilder) {
		UseCaseModel useCaseModel = 
			modelBuilder.useCase("Get greeted")
				.basicFlow()
					.step("S1").system(promptUserToEnterFirstName())
					.step("S2").user(ENTER_FIRST_NAME).system(saveFirstName())
					.step("S3").system(promptUserToEnterAge())
					.step("S4").user(ENTER_AGE).system(saveAge())
					.step("S5").system(greetUserWithFirstNameAndAge())
			.build();
		return useCaseModel;
	}
	
	private Consumer<UseCaseModelRunner> promptUserToEnterFirstName() {
		return r -> System.out.print("Please enter your first name: ");
	}
	
	private Consumer<UseCaseModelRunner> promptUserToEnterAge() {
		return r -> System.out.print("Please enter your age: ");
	}

	private Consumer<EnterText> saveFirstName() {
		return enterText -> firstName = enterText.text;
	}
	
	private Consumer<EnterText> saveAge() {
		return enterText -> age = Integer.parseInt(enterText.text);
	}
	
	private Consumer<UseCaseModelRunner> greetUserWithFirstNameAndAge() {
		return r -> System.out.println("Hello, " + firstName + " (" + age + ").");
	}
	
	public static void main(String[] args){		
		HelloWorld04_EnterNameAndAgeExample example = new HelloWorld04_EnterNameAndAgeExample();
		example.start();
	}

	private void start() {
		UseCaseModelRunner useCaseModelRunner = new UseCaseModelRunner();
		UseCaseModel useCaseModel = buildWith(newBuilder());
		useCaseModelRunner.run(useCaseModel);
		useCaseModelRunner.reactTo(enterText());
		useCaseModelRunner.reactTo(enterText());	
	}
}

@@ -88,45 +88,8 @@
 r;%0A%0A
-import java.util.function.Consumer;%0A%0A
 impo
@@ -676,32 +676,38 @@
 ep(%22S1%22).system(
+this::
 promptUserToEnte
@@ -708,34 +708,32 @@
 ToEnterFirstName
-()
 )%0A%09%09%09%09%09.step(%22S2
@@ -761,24 +761,30 @@
 AME).system(
+this::
 saveFirstNam
@@ -784,18 +784,16 @@
 irstName
-()
 )%0A%09%09%09%09%09.
@@ -810,16 +810,22 @@
 .system(
+this::
 promptUs
@@ -836,18 +836,16 @@
 EnterAge
-()
 )%0A%09%09%09%09%09.
@@ -878,25 +878,29 @@
 .system(
+this::
 saveAge
-()
 )%0A%09%09%09%09%09.
@@ -917,16 +917,22 @@
 .system(
+this::
 greetUse
@@ -951,18 +951,16 @@
 meAndAge
-()
 )%0A%09%09%09.bu
@@ -1007,216 +1007,194 @@
 ate 
-Consumer%3CUseCaseModelRunner%3E promptUserToEnterFirstName() %7B%0A%09%09return r -%3E System.out.print(%22Please enter your first name: %22);%0A%09%7D%0A%09%0A%09private Consumer%3CUseCaseModelRunner%3E promptUserToEnterAge() %7B%0A%09%09return r -%3E 
+void promptUserToEnterFirstName(UseCaseModelRunner runner) %7B%0A%09%09System.out.print(%22Please enter your first name: %22);%0A%09%7D%0A%09%0A%09private void promptUserToEnterAge(UseCaseModelRunner runner) %7B%0A%09%09
 Syst
@@ -1247,35 +1247,20 @@
 private 
-Consumer%3CEnterText%3E
+void
  saveFir
@@ -1266,28 +1266,25 @@
 rstName(
-) %7B%0A%09%09return
+EnterText
  enterTe
@@ -1285,20 +1285,22 @@
 nterText
- -%3E 
+) %7B%0A%09%09
 firstNam
@@ -1337,48 +1337,30 @@
 ate 
-Consumer%3CEnterText%3E saveAge() %7B%0A%09%09return
+void saveAge(EnterText
  ent
@@ -1365,20 +1365,22 @@
 nterText
- -%3E 
+) %7B%0A%09%09
 age = In
@@ -1429,36 +1429,12 @@
 ate 
-Consumer%3CUseCaseModelRunner%3E
+void
  gre
@@ -1463,26 +1463,39 @@
 Age(
-) %7B%0A%09%09return r -%3E 
+UseCaseModelRunner runner) %7B%0A%09%09
 Syst

Refactor to use method references