-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleDemo.java
113 lines (87 loc) · 2.98 KB
/
ConsoleDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package csc301.examples.iteration1;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Scanner;
import csc301.examples.iteration1.model.Post;
import csc301.examples.iteration1.model.User;
import csc301.examples.iteration1.service.DAO;
import csc301.examples.iteration1.service.DAOImplInMemory;
public class ConsoleDemo {
private Scanner inputScanner;
private DAO dao;
private User loggedInUser = null;
public ConsoleDemo(InputStream input, DAO dao) {
inputScanner = new Scanner(input);
this.dao = dao;
}
public void run(){
System.out.println("Welcome");
System.out.println("Please type a command [register, login, post, find-post, search-posts, exit].");
String s = inputScanner.nextLine().toLowerCase().trim();
while(! s.equals("exit")){
try{
if("register".equals(s)){
register();
} else if("login".equals(s)){
login();
} else if("post".equals(s)){
post();
} else if("find-post".equals(s)){
findPost();
} else if("search-posts".equals(s)){
searchPosts();
} else {
System.out.println("Unrecognized command - " + s);
}
} catch (Exception e){
System.out.println("ERROR: " + e.getMessage());
}
System.out.println("Please type a command [register, login, post, find-post, search-posts, exit].");
s = inputScanner.nextLine().toLowerCase().trim();
}
System.out.println("Goodbye");
}
private void register() {
System.out.println("Please enter a username:");
String username = inputScanner.nextLine().trim();
System.out.println("Please enter a password:");
String password = inputScanner.nextLine().trim();
dao.registerUser(username, password);
}
private void login() {
System.out.println("Please enter a username:");
String username = inputScanner.nextLine().trim();
System.out.println("Please enter a password:");
String password = inputScanner.nextLine().trim();
loggedInUser = dao.login(username, password);
}
private void post() {
if(loggedInUser == null){
throw new IllegalStateException("You must login in order to post something.");
}
System.out.println("Please enter a title");
String title = inputScanner.nextLine().trim();
System.out.println("Please enter an image URL");
String imageUrl = inputScanner.nextLine().trim();
System.out.println(dao.createPost(title, imageUrl, loggedInUser));
}
private void findPost() {
System.out.println("Please enter a post id");
String postId = inputScanner.nextLine().trim();
System.out.println(dao.getPostById(postId));
}
private void searchPosts() {
System.out.println("Please enter a username");
String username = inputScanner.nextLine().trim();
Iterator<Post> itr = dao.getPostsByUsername(username);
int count = 0;
while (itr.hasNext()) {
System.out.println(itr.next() + "\n");
count++;
}
System.out.println(count + " post(s) in total from " + username);
}
public static void main(String[] args) {
new ConsoleDemo(System.in, new DAOImplInMemory()).run();
}
}