Add: Main, ClientApp, and ServerApp classes for application entry point with primitive input validation

This commit is contained in:
Lars Simon Winzer
2026-03-04 11:23:39 +01:00
parent 77d2b671ff
commit 2183c591bf
3 changed files with 64 additions and 0 deletions
@@ -0,0 +1,42 @@
package ch.unibas.dmi.dbis.cs108.casono;
import ch.unibas.dmi.dbis.cs108.casono.client.ClientApp;
import ch.unibas.dmi.dbis.cs108.casono.server.ServerApp;
public final class Main {
public static void main(String[] args) {
if (!isValid(args)) {
printUsage();
System.exit(1);
}
switch (args[0]) {
case "server" -> ServerApp.start(args[1]);
case "client" -> ClientApp.start(args[1]);
default -> {
printUsage();
System.exit(1);
}
}
}
private static boolean isValid(String[] args) {
if (args.length != 2) {
return false;
}
return switch (args[0]) {
case "server", "client" -> true;
default -> false;
};
}
private static void printUsage() {
System.err.println("""
Usage:
java -jar xyz.jar server <listenPort>
java -jar xyz.jar client <serverIp>:<serverPort>
""");
}
}
@@ -0,0 +1,14 @@
package ch.unibas.dmi.dbis.cs108.casono.client;
public class ClientApp {
public static void start(String arg) {
String[] parts = arg.split(":", 2);
if (parts.length != 2) {
throw new IllegalArgumentException("Address must be in format <ip>:<port>.");
}
String host = parts[0];
int port = Integer.parseInt(parts[1]);
System.out.println("You've selected the client. It will connect port " + port + " at host " + host);
}
}
@@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.casono.server;
public class ServerApp {
public static void start(String arg) {
int port = Integer.parseInt(arg);
System.out.println("You've selected the server. It will accept connections at port" + port);
}
}