-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
298 lines (266 loc) · 8.46 KB
/
Copy pathUser.java
File metadata and controls
298 lines (266 loc) · 8.46 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
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileNotFoundException;
/**
* Things to add
*
* make program continuous
* Posting feature
* profile page
* following feature
*/
/**
* Write a description of class User here.
*
* @Segi (your name)
* @1.0
*
* First draft of User class, no online funcitonality
* creates groups of users on individual computers
*/
public class User
{
// instance variables
private String username;
private String email;
private String password;
private int internalID;
private boolean loggedIn;
//List for all users
private static ArrayList<User> listOfAllUsers=new ArrayList<User>();
private static ArrayList<Post> allPosts=new ArrayList<Post>();
//File and FileWriter
private static File listOfUsers=new File("ListOfUsers.txt.");
private static FileWriter writer;
//Additional Profile Identifiers
private Profile p;
/**
* Constructor for objects of class User
*/
public User(String u, String p,String e)
{
username=u;
password=p;
email=e;
loggedIn=false;
//Adds created User to a list of all users
//May run into problem with parameter passing
//Uses 2x the amount of data by creating new user
}
//Constructor for adding to listOfAllUsers
//I dont really think this is the most efficient way\
/**
private User(String u, String p, String e, int iD)
{
username=u;
password=p;
internalID=iD;
}
**/
//getter for username variable
//used for viewing feed
public String getUsername()
{
return username;
}
public static void createFileWriter(){
try {
writer = new FileWriter("ListOfUsers.txt");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
//Method to login to an account
public static void login(String username, String password)
{
//Checks for existance through every user created
String us, p;
Scanner s1=new Scanner(System.in);
Scanner s2=new Scanner(System.in);
User copy=new User("","","");
for(User u: listOfAllUsers)
{
if(u.username.equals(username) && u.password.equals(password))
{
System.out.println("\nLogin Successful");
u.loggedIn=true;
copy=u;
copy.loggedIn=true;
break;
}
}
if(copy.loggedIn=false)
{
System.out.println("\nInvalid Credentials");
System.out.println("\nUsername:");
us=s2.nextLine();
System.out.println("\nPassword:");
p=s2.nextLine();
login(username,password);
}
}
//method for loggin out of an account
public void logout()
{
loggedIn=false;
System.out.println("Logout Successful");
}
//Resets the List for testing
//Will be deleted later
public void resetNetwork()
{
for(User u: listOfAllUsers)
{
listOfAllUsers.remove(u);
}
}
//Method to reset password
public static void resetPassword(User accountToReset,String password)
{
accountToReset.password=password;
}
//Method for profile setup
public void setUpProfile()
{
String n,b,s,w;
String action;
Scanner scanner1=new Scanner(System.in);
Scanner scanner2=new Scanner(System.in);
Profile p=new Profile();
System.out.println("Add a name to your account?(y/n)");
action=scanner1.nextLine();
if(action.equals("y")||action.equals("Y")){
System.out.println("Enter a name: ");
n=scanner2.nextLine();
p.setName(n);
}
else{
System.out.println("No worries:). you can edit or set it at anytime");
}
System.out.println("Edit bio?(y/n)");
action=scanner1.nextLine();
if(action.equals("y")||action.equals("Y")){
System.out.println("Enter your bio(you can change it later)");
b=scanner2.nextLine();
p.setBio(b);
}
else{
System.out.println("No worries:). you can edit or set it at anytime");
}
System.out.println(p.toString());
}
//User interactive method
public static void run()
{
Scanner s1=new Scanner(System.in);
Scanner s2=new Scanner(System.in);
String action,email,username,password,confirmPassword;
createFileWriter();
System.out.println("\nWould you like to login(A), create an account(B), or quit program(C)?");
action=s1.nextLine();
if(action.equals("A")||action.equals("a")){
System.out.println("\nUsername: ");
username=s2.nextLine();
System.out.println("\nPassword: ");
password=s1.nextLine();
login(username, password);
}
else if(action.equals("B")|| action.equals("b")){
createUser();
}
else if(action.equals("C")|| action.equals("c")){
System.out.println("\nGoodbye:)");
}
else{
System.out.println("\nInvalid input");
run();
}
}
//Creating user method
public static void createUser()
{
String password,confirmPassword,email,username;
Scanner s1=new Scanner(System.in);
Scanner s2=new Scanner(System.in);
boolean userAlreadyExists;
User u2;
System.out.println("\nEnter email: ");
email=s2.nextLine();
System.out.println("\nWhat would you like to set as a username: ");
username=s1.nextLine();
System.out.println("\nSet password: ");
password=s2.nextLine();
System.out.println("\nConfirm password: ");
confirmPassword=s1.nextLine();
while(!password.equals(confirmPassword))
{
System.out.println("\nPasswords do not match");
System.out.println("\nSet password: ");
password=s2.nextLine();
System.out.println("\nConfirm password");
confirmPassword=s1.nextLine();
}
System.out.println("\nPassword Confirmed");
System.out.println("\nAccount Created :)");
u2=new User(username, password, email);
listOfAllUsers.add(u2);
try {
writer.write(username+password+"\n");
writer.close();
System.out.println("User successfully created");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
System.out.println("\n\nLogin\n\nUsername: ");
username=s2.nextLine();
System.out.println("\nPassword: ");
password=s1.nextLine();
login(username,password);
u2.userActions();
}
//Method for user actions after logged in
public void userActions()
{
Scanner s1=new Scanner(System.in);
Scanner s2=new Scanner(System.in);
String action,post;
while(true)
{
System.out.println("\n\nWhat would you like to do?\nView feed(A)\nView/Edit Profile(B)\nPost(C)\nLogout(D)");
action=s1.nextLine();
if(action.equals("A")||action.equals("a"))
{
System.out.println("Most recent posts");
if(allPosts.size()>0)
{
for(Post p: allPosts)
{
System.out.println("\n"+p.getAccount().getUsername()+": "+p.getText());
}
}
else{
System.out.println("\nNo posts have been made");
}
}
if(action.equals("B")||action.equals("b"))
{
}
if(action.equals("C")||action.equals("c"))
{
System.out.println("\n\nWhat would you like to post?");
post=s2.nextLine();
allPosts.add(new Post(this,post));
}
if(action.equals("D")||action.equals("d"))
{
logout();
run();
}
}
}
}