package net.bourgau.philippe.concurrency.kata;

import java.util.Scanner;

public class Client implements Broadcast {

    private final ChatRoom chatRoom;
    private final String name;
    private final Output out;

    public Client(String name, ChatRoom chatRoom, Output out) {
        this.chatRoom = chatRoom;
        this.name = name;
        this.out = out;
    }

    public void enter() {
        chatRoom.enter(this);
        chatRoom.broadcast(welcomeMessage(name));
    }

    public void write(String message) {
        chatRoom.broadcast(message(name, message));
    }


    @Override
    public void broadcast(String message) {
        out.write(message);
    }

    public void leave() {
        chatRoom.broadcast(exitMessage(name));
        chatRoom.leave(this);
    }

    public static String welcomeMessage(String name) {
        return "Welcome " + name + " !";
    }

    public static String message(String name, String message) {
        return name + " > " + message;
    }

    static String exitMessage(String name) {
        return name + " left";
    }

    public static void main(String[] args) {
        Client client = new Client(args[0], new ChatRoom(), new Output() {
            public void write(String line) {
                System.out.println(line);
            }
        });

        client.enter();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            client.broadcast(scanner.nextLine());
        }
    }
}

@@ -1308,24 +1308,42 @@
        %7D);%0A%0A
+        try %7B%0A    
         clie
@@ -1351,24 +1351,28 @@
 t.enter();%0A%0A
+    
         Scan
@@ -1417,16 +1417,20 @@
         
+  
+  
 while (s
@@ -1469,44 +1469,242 @@
     
-client.broadcast(scanner.nextLin
+    String message = scanner.nextLine();%0A                if (message.equals(%22bye%22)) %7B%0A                    break;%0A                %7D%0A                client.broadcast(message);%0A            %7D%0A        %7D finally %7B%0A            client.leav
 e()
-)
 ;%0A  

Clean exit from the command line client