praktikum1/aufgabe3
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
package org.example.demo.praktikum1.aufgabe3;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.Context;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
|
||||||
|
"org.wildfly.naming.client.WildFlyInitialContextFactory");
|
||||||
|
props.setProperty(Context.PROVIDER_URL, "http-remoting://localhost:8080");
|
||||||
|
|
||||||
|
InitialContext ctx = new InitialContext(props);
|
||||||
|
String jndiName = "ejb:/ejb-server-1.0-SNAPSHOT/GuessingGame!org.example.demo.praktikum1.aufgabe3.GuessingGameRemote";
|
||||||
|
var game = (GuessingGameRemote) ctx.lookup(jndiName);
|
||||||
|
var scanner = new Scanner(System.in);
|
||||||
|
var another = true;
|
||||||
|
while (another) {
|
||||||
|
playRound(game, scanner);
|
||||||
|
|
||||||
|
// todo: input exception handling
|
||||||
|
System.out.print("Do you want to play another? ");
|
||||||
|
another = scanner.nextBoolean();
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void playRound(GuessingGameRemote game, Scanner scanner) {
|
||||||
|
game.init();
|
||||||
|
|
||||||
|
GuessResult result;
|
||||||
|
do {
|
||||||
|
// todo: input exception handling
|
||||||
|
System.out.print("Guess a number from 1-100: ");
|
||||||
|
var guess = scanner.nextInt();
|
||||||
|
result = game.testGuess(guess);
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
case Success:
|
||||||
|
System.out.println("Correct! The number was "+ guess);
|
||||||
|
break;
|
||||||
|
case TooBig:
|
||||||
|
System.out.println("Your guess was too big!");
|
||||||
|
break;
|
||||||
|
case TooSmall:
|
||||||
|
System.out.println("Your guess was too small!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (result != GuessResult.Success);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.example.demo.praktikum1.aufgabe3;
|
||||||
|
|
||||||
|
public enum GuessResult {
|
||||||
|
TooSmall,
|
||||||
|
TooBig,
|
||||||
|
Success,
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.example.demo.praktikum1.aufgabe3;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import jakarta.ejb.Stateful;
|
||||||
|
|
||||||
|
@Stateful
|
||||||
|
public class GuessingGame implements GuessingGameRemote {
|
||||||
|
private int num;
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
var rng = new Random();
|
||||||
|
num = 1 + rng.nextInt(0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GuessResult testGuess(int guess) {
|
||||||
|
if (guess < num) {
|
||||||
|
return GuessResult.TooSmall;
|
||||||
|
}
|
||||||
|
if (guess > num) {
|
||||||
|
return GuessResult.TooBig;
|
||||||
|
}
|
||||||
|
return GuessResult.Success;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.example.demo.praktikum1.aufgabe3;
|
||||||
|
import jakarta.ejb.Remote;
|
||||||
|
|
||||||
|
@Remote
|
||||||
|
public interface GuessingGameRemote {
|
||||||
|
// start ein spiel, generiere ein zufallszahl.
|
||||||
|
void init();
|
||||||
|
|
||||||
|
// test whether the guess is correct.
|
||||||
|
GuessResult testGuess(int guess);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user