-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
148 lines (122 loc) · 3.91 KB
/
Main.java
File metadata and controls
148 lines (122 loc) · 3.91 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE
package executable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.*;
import elements.Message;
import elements.Server;
import elements.User;
/**
* Defines a class that implements a simulation of a basic messaging program.
* @author leylayayladere
* @version 1.0
*/
public class Main {
/**
* Reads the input from input file, takes the type of event number from input and then does the appropriate operations.
* <p> While doing the appropriate operations which are given in input file, writes required informations to output file.
* @param args Takes input file to give an appropriate output file, also in order to test the code.
* @throws FileNotFoundException Prevents from an error in case of the file not found.
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File(args[0]));
PrintStream output = new PrintStream(new File(args[1]));
int numOfUsers = input.nextInt();
int numOfQueries = input.nextInt();
long capacity = input.nextLong();
ArrayList<User> users = new ArrayList<User>();
int id = 0;
while(numOfUsers != 0) {
users.add(new User(id));
id++;
numOfUsers--;
}
Server server = new Server(capacity);
int time = 0;
while(numOfQueries != 0) {
int typeOfEvent = input.nextInt();
if(typeOfEvent == 0) { //sends a message
int senderID = input.nextInt();
int receiverID = input.nextInt();
String msgBody = input.nextLine();
msgBody = msgBody.substring(1);
users.get(senderID).sendMessage(users.get(receiverID), msgBody, time, server);
server.checkServerLoad(output);
time++;
}
if(typeOfEvent == 1) { //receives messages
int receiverID = input.nextInt();
users.get(receiverID).getInbox().receiveMessages(server, time);
server.checkServerLoad(output);
time++;
}
if(typeOfEvent == 2) { //reads certain amount of messages
int receiverID = input.nextInt();
int numOfMsgs = input.nextInt();
int num = users.get(receiverID).getInbox().readMessages(numOfMsgs, time);
while(num != 0) {
time++;
num--;
}
}
if(typeOfEvent == 21) { //reads all messages from a sender
int receiverID = input.nextInt();
int senderID = input.nextInt();
int num = users.get(receiverID).getInbox().readMessages(users.get(senderID), time);
while(num != 0) {
time++;
num--;
}
}
if(typeOfEvent == 22) { //reads a specific message
int receiverID = input.nextInt();
int msgID = input.nextInt();
users.get(receiverID).getInbox().readMessage(msgID, time);
time++;
}
if(typeOfEvent == 3) { //adds a friend
int id1 = input.nextInt();
int id2 = input.nextInt();
if(users.get(id1).isFriendWith(users.get(id2))) {
// do nothing
}else {
users.get(id1).addFriend(users.get(id2));
}
time++;
}
if(typeOfEvent == 4) { //removes a friend
int id1 = input.nextInt();
int id2 = input.nextInt();
if(users.get(id1).isFriendWith(users.get(id2))) {
users.get(id1).removeFriend(users.get(id2));
}else {
// do nothing
}
time++;
}
if(typeOfEvent == 5) { //flushes server
server.flush();
time++;
}
if(typeOfEvent == 6) { //prints the current size of the serve
output.println("Current load of the server is " + server.getCurrentSize() + " characters.");
time++;
}
if(typeOfEvent == 61) { //prints the last message a user has read
int userID = input.nextInt();
Iterator<Message> itr = users.get(userID).getInbox().getRead().iterator();
if(!users.get(userID).getInbox().getRead().isEmpty()) {
Message last = itr.next();
while(itr.hasNext()) {
last = itr.next();
}
output.println(last.toString());
}
time++;
}
numOfQueries--;
}
}
}
//DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE