Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pgdp.test;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import java.io.*;
- import java.net.ConnectException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class PinguinTest {
- @BeforeEach
- public static void main(String[] args) {
- Socket sock = null;
- boolean server = false;
- try {
- // connection established
- ServerSocket serverSock = new ServerSocket(25565);
- System.out.println("Server started, waiting for connections...");
- sock = serverSock.accept();
- serverSock.close();
- server = true;
- } catch (UnknownHostException e) {
- System.err.println("Unknown host, try again!");
- } catch (NumberFormatException e) {
- System.err.println("Port invalid, try again!");
- } catch (ConnectException e) {
- System.err.println("Connection refused, try again!");
- } catch (IOException e) {
- System.err.println("I/O error, try again!");
- }
- try (BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- PrintWriter out = new PrintWriter(sock.getOutputStream(), true)) {
- if (server) {
- System.out.println("Connected! You can now send something.");
- String input = in.readLine();
- if (input.equals("Was ergibt die folgende Summe?")) {
- BufferedReader in2 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- input = in2.readLine();
- if (input.matches("<int>( + <int>)*")) {
- out.println(input);
- out.flush();
- long sum = Arrays.stream(args[0].split("\\\\d+")).mapToLong(Long::parseLong).sum();
- System.out.println(sum);
- } else {
- System.err.println("Das ist keine Summe!");
- }
- } else if (input.equals("Zähle die Fische in")) {
- BufferedReader in2 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- input = in2.readLine();
- if (input.length() == 0) {
- System.out.println(0);
- }
- List<String> words = new ArrayList<String>();
- Arrays.stream(input.split("\\\\w+")).forEach(word -> {
- int i = Integer.parseInt(word);
- if (i == 0 && word.contains("Fisch") || word.contains("fisch")) {
- words.add(word);
- }
- });
- int quantity = (int) words.stream().count();
- out.println(input);
- out.flush();
- System.out.println(quantity);
- }
- // jump to step 1
- PinguinTest.main(new String[1]);
- if (!args[0].equals("Was ergibt die folgende Summe?") || !args[0].equals("Zähle die Fische in")) {
- out.println(args[0]);
- out.flush();
- System.err.println("Tut mir leid, das habe ich nicht verstanden.");
- }
- BufferedReader in3 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- args[0] = in3.readLine();
- if (args[0].contains("Bye") || args[0].contains("Goodbye") || args[0].contains("Servus")
- || args[0].contains("Bis dann")) {
- out.println(args[0]);
- out.flush();
- System.out.println(args[0]);
- } else {
- System.out.println("See ya in a bit.");
- }
- }
- } catch (IOException ex) {
- System.err.println("Connection error, exit.");
- } finally {
- try {
- sock.close();
- } catch (IOException e) {
- System.err.println("Connection failed.");
- }
- }
- }
- @Test
- public void Test01() {
- // tomorrow
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement