-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextTwitter.java
More file actions
305 lines (268 loc) · 11 KB
/
TextTwitter.java
File metadata and controls
305 lines (268 loc) · 11 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* SELF NOTES: Started Jan 2nd 2025. Finished March 11th 2025 (hurray)
Currently learning C++ might do it in that language for practice.
Shout out to the Liang textbook and my TA for always checking my code and
helping me out in ideas or new methods to try.
*/
import java.io.*;
import java.util.*;
public class TextTwitter {
// Map to store users, using the username as the key and User object as the value
private static Map<String, User> users = new HashMap<>();
// The currently logged-in user
private static User currentUser = null;
// File names for saving and loading user and tweet data
private static final String USER_DATA_FILE = "users_data.txt";
private static final String TWEETS_DATA_FILE = "tweets_data.txt";
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// Load users and tweets data from the files
loadUsersData();
loadTweetsData();
// Main loop for the program
while (true) {
// Display the menu
showMenu();
// Get the user's choice from input
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
// Process the user's choice
switch (choice) {
case 1:
registerUser(); // Register a new user
break;
case 2:
loginUser(); // Log in an existing user
break;
case 3:
postTweet(); // Post a new tweet
break;
case 4:
displayAllTweets(); // View all tweets
break;
case 5:
followUser(); // Follow another user
break;
case 6:
likeTweet(); // Like a tweet
break;
case 7:
logout(); // Log out the current user
break;
case 8:
exit(); // Exit the program
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
// Displays the main menu based on the current user status (logged in or not)
private static void showMenu() {
System.out.println("\nWelcome to TextTwitter!");
if (currentUser == null) {
// If not logged in, show options for registering or logging in
System.out.println("1. Register a new user");
System.out.println("2. Login");
} else {
// If logged in, show options for tweeting, following, and liking
System.out.println("Logged in as: " + currentUser.username);
System.out.println("3. Post a tweet");
System.out.println("4. View all tweets");
System.out.println("5. Follow a user");
System.out.println("6. Like a tweet");
System.out.println("7. Logout");
}
// Always show exit option
System.out.println("8. Exit");
System.out.print("Choose an option: ");
}
// Registers a new user by getting their username and password
private static void registerUser() {
System.out.print("Enter username to register: ");
String username = scanner.nextLine();
// Check if the username already exists
if (users.containsKey(username)) {
System.out.println("Username already exists! Please choose a different one.");
return;
}
System.out.print("Enter a password: ");
String password = scanner.nextLine();
// Create a new User and store it in the map
User newUser = new User(username, password);
users.put(username, newUser);
saveUsersData(); // Save updated user data to file
System.out.println("User registered successfully!");
}
// Logs in an existing user by verifying their username and password
private static void loginUser() {
System.out.print("Enter username to login: ");
String username = scanner.nextLine();
// Check if the user exists in the database
if (!users.containsKey(username)) {
System.out.println("User not found! Please register first.");
return;
}
System.out.print("Enter password: ");
String password = scanner.nextLine();
// Verify the password
User user = users.get(username);
if (user.password.equals(password)) {
currentUser = user;
System.out.println("Logged in successfully!");
} else {
System.out.println("Incorrect password! Try again.");
}
}
// Allows the logged-in user to post a tweet
private static void postTweet() {
if (currentUser == null) {
System.out.println("You need to log in first!");
return;
}
// Get the tweet message from the user
System.out.print("What's happening? ");
String message = scanner.nextLine();
currentUser.postTweet(message); // Post the tweet using the User object
saveTweetsData(); // Save the tweet data to file
System.out.println("Tweet posted!");
}
// Displays all tweets from users the current user is following or their own tweets
private static void displayAllTweets() {
if (currentUser == null) {
System.out.println("You need to log in first!");
return;
}
// Loop through all users and display tweets from followed users or the current user
for (User user : users.values()) {
if (currentUser.isFollowing(user.username) || user.username.equals(currentUser.username)) {
user.displayTweets(); // Show tweets from the user
}
}
}
// Allows the current user to follow another user
private static void followUser() {
if (currentUser == null) {
System.out.println("You need to log in first!");
return;
}
System.out.print("Enter the username to follow: ");
String username = scanner.nextLine();
// Check if the user to follow exists
if (!users.containsKey(username)) {
System.out.println("User not found!");
return;
}
if (username.equals(currentUser.username)) {
System.out.println("You can't follow yourself!");
return;
}
// Follow the user and add the current user as a follower of the target user
currentUser.followUser(username);
users.get(username).addFollower(currentUser.username);
System.out.println("Now following @" + username);
}
// Allows the current user to like a tweet
private static void likeTweet() {
if (currentUser == null) {
System.out.println("You need to log in first!");
return;
}
System.out.print("Enter the username of the tweet you want to like: ");
String username = scanner.nextLine();
// Check if the user exists
if (!users.containsKey(username)) {
System.out.println("User not found!");
return;
}
User user = users.get(username);
user.displayTweets(); // Display all tweets from the user
// Get the tweet index to like
System.out.print("Enter the index of the tweet to like: ");
int index = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
// Check if the tweet index is valid
if (index < 0 || index >= user.tweets.size()) {
System.out.println("Invalid tweet index!");
return;
}
Tweet tweet = user.tweets.get(index);
currentUser.likeTweet(tweet); // Like the tweet
System.out.println("You liked the tweet!");
}
// Logs out the current user
private static void logout() {
if (currentUser != null) {
System.out.println("Logged out successfully!");
currentUser = null;
} else {
System.out.println("You're not logged in!");
}
}
// Exits the program and saves all data
private static void exit() {
System.out.println("Goodbye!");
saveUsersData(); // Save all users' data to file
saveTweetsData(); // Save all tweets' data to file
}
// Loads user data from the file and populates the users map
private static void loadUsersData() {
try (BufferedReader reader = new BufferedReader(new FileReader(USER_DATA_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
// Split the line to get username and password
String[] userInfo = line.split(",");
String username = userInfo[0];
String password = userInfo[1];
// Create and store the user
User user = new User(username, password);
users.put(username, user);
}
} catch (IOException e) {
System.out.println("No previous user data found.");
}
}
// Saves all users' data to the file
private static void saveUsersData() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(USER_DATA_FILE))) {
// Loop through all users and save their data to the file
for (User user : users.values()) {
writer.write(user.username + "," + user.password);
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error saving user data.");
}
}
// Loads tweet data from the file and populates users' tweets
private static void loadTweetsData() {
try (BufferedReader reader = new BufferedReader(new FileReader(TWEETS_DATA_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
// Split the line to get username and tweet message
String[] tweetData = line.split(",", 2);
String username = tweetData[0];
String message = tweetData[1];
// If the user exists, post the tweet
if (users.containsKey(username)) {
users.get(username).postTweet(message);
}
}
} catch (IOException e) {
System.out.println("No previous tweet data found.");
}
}
// Saves all tweets' data to the file
private static void saveTweetsData() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(TWEETS_DATA_FILE))) {
// Loop through all users and their tweets and save them to the file
for (User user : users.values()) {
for (Tweet tweet : user.tweets) {
writer.write(tweet.username + "," + tweet.message);
writer.newLine();
}
}
} catch (IOException e) {
System.out.println("Error saving tweet data.");
}
}
}