Commits

Steven! Ragnarök  committed 913b729

Begin implementation.

  • Participants
  • Parent commits 93f9d2e

Comments (0)

Files changed (7)

 syntax: glob
 
 target/*
+.redcar
+#!/bin/bash
+
+mvn package && java -cp target/messageme-1.0-SNAPSHOT.jar \
+	com.nuclearsandwich.messageme.App

File src/main/java/com/nuclearsandwich/App.java

-package com.nuclearsandwich;
+package com.nuclearsandwich.messageme;
+import java.util.ArrayList;
+import java.util.Scanner;
 
-/**
- * Hello world!
- *
- */
-public class App 
-{
-    public static void main( String[] args )
-    {
-        System.out.println( "Hello World!" );
-    }
+public class App {
+	public static void main(String[] args) {
+		/* Initialize users. */
+		for (int i = 0; i <= 10; i++) {
+			new User("u" + i);
+		}
+
+		printCommands();
+		loop();
+	}
+
+	public static void printCommands() {
+		System.out.println("Available commands are:");
+		printInboxUsage();
+	}
+
+	public static void loop() {
+		String promptString = "> ";
+		Scanner scanner = new Scanner(System.in);
+
+		String lastCommand = "";
+
+		while (lastCommand != "exit") {
+			System.out.print(promptString);
+			String nextCommand = scanner.nextLine();
+			runCommand(nextCommand);
+			System.out.println("Command " + nextCommand);
+		}
+	}
+
+	public static void runCommand(String commandString) {
+		String[] commandAndArgString = commandString.split(" ", 2);
+		String command = commandAndArgString[0];
+		String argList = commandAndArgString[1];
+
+		if (command.equals("send")) {
+			send(argList);
+		} else if (command.equals("inbox")) {
+			//inbox(argList);
+		} else if (command.equals("connect")) {
+			System.out.println("Pending");
+		} else if (command.equals("disconnect")) {
+			System.out.println("Pending");
+		}
+	}
+
+	public static void send(String argList) {
+		String[] args = argList.split(" ", 2);
+		User sender = User.by(args[0]);
+		User recipient = User.by(args[1]);
+		String body = args[2].split("'", "");
+		recipient.getInbox().add(new Message(sender, recipient, body));
+	}
+
+	public static void inbox(String argList) {
+		User.by(argList).getInbox.display();
+	}
+
+	public static void printInboxUsage() {
+		System.out.println("inbox USER_NAME");
+	}
+
+	private static ArrayList<User> users;
 }
+

File src/main/java/com/nuclearsandwich/Inbox.java

+package com.nuclearsandwich.messageme;
+import java.util.ArrayList;
+
+public class Inbox {
+	public Inbox(User owner) {
+		this.owner = owner;
+		this.messages = new ArrayList<Message>();
+	}
+
+	public void add(Message m) {
+		messages.add(m);
+	}
+
+	public void display() {
+		System.out.println("Inbox for " + owner.toString() + " contains " +
+				messages.size() + "messages:");
+
+		for (Message msg : messages) {
+			msg.markRead();
+			msg.display();
+		}
+	}
+
+	private User owner;
+	private ArrayList<Message> messages;
+}
+

File src/main/java/com/nuclearsandwich/Message.java

+package com.nuclearsandwich.messageme;
+import java.util.Date;
+
+public class Message {
+	public Message(User sender, User recipient, String body) {
+		this.sender = sender;
+		this.recipient = recipient;
+		this.sentTimestamp = new Date();
+		this.body = body;
+		this.read = false;
+	}
+
+	public void display() {
+		System.out.println("	From: " + sender.toString() + " | Message: " + body);
+	}
+
+	public void markRead() {
+		read = true;
+	}
+
+	private boolean read;
+	private User sender;
+	private User recipient;
+	private Date sentTimestamp;
+	private String body;
+}
+

File src/main/java/com/nuclearsandwich/User.java

+package com.nuclearsandwich.messageme;
+
+public class User {
+	public User(String name) {
+		this.name = name;
+		this.inbox = new Inbox(this);
+		userList.add(this);
+	}
+
+	public Inbox getInbox() {
+		return inbox;
+	}
+
+	public String toString() {
+		return name;
+	}
+
+	private String name;
+	private Inbox inbox;
+
+	public static User by(String name) {
+		for (User u : userList) {
+			if (u.getName().equals(name)) {
+				return u;
+			}
+		}
+		return null;
+	}
+
+	private static final ArrayList<User> userList = new ArrayList<User>();
+
+}
+

File src/test/java/com/nuclearsandwich/AppTest.java

+
 package com.nuclearsandwich;
 
 import junit.framework.Test;