forked from raghaddmahgoub/OOP-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
218 lines (199 loc) · 7.54 KB
/
UserInterface.java
File metadata and controls
218 lines (199 loc) · 7.54 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;
public class UserInterface {
private static String username;
private static String password;
static Scanner in = new Scanner(System.in);
private static User user;
public static void ProgramStart(){
System.out.println("\t Welcome To our Facebook");
System.out.println("1.Login");
System.out.println("2.Signup");
System.out.print("Enter a choice :");
int choice=0;
Boolean validate=new Boolean(false);
while(!validate) {
try {
choice=in.nextInt();
validate=true;
} catch (InputMismatchException e) {
System.out.println("invaild choice try again");
System.out.print("Enter a choice :");
in.nextLine();
}
}
switch (choice) {
case 1:
user = LogIn();
break;
case 2:
user = SignUp();
break;
default:
System.out.println("Invalid Choice please try again :( ");
}
}
public static User LogIn() {
User user;
System.out.println("Login page");
System.out.println("-if you want to return to main menu enter 0 -");
System.out.println("Enter Username :");
username = in.next();
if (username.equals("0")) {
ProgramStart();
}
while (username.isEmpty()){
System.out.println("Username is Empty please Try again :( ");
System.out.println("Enter Username :");
username = in.next();
if (username.equals("0")) {
ProgramStart();
}
}
System.out.println("Enter Password :");
password = in.next();
if (password.equals("0")) {
ProgramStart();
}
while (password.isEmpty()){
System.out.println("Password is Empty please Try again :( ");
System.out.println("Enter Password :");
password = in.next();
if (password.equals("0")) {
ProgramStart();
}
}
if(!LoginStatus(username,password)){
System.out.println("Username or Password is not correct please Try again :( ");
LogIn();
}
new Feed(Feed.GetUserData(username));
return Feed.GetUserData(username);
}
public static User SignUp() {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
System.out.println("/t Welcome To our Facebook");
System.out.println("Enter Username (at least 8 chars):");
username = in.next();
while (username.length() < 8){
System.out.println("Username is Empty please Try again :( ");
System.out.println("Enter Username :");
username = in.next();
}
while (!CheckUserIsUnique(username)){
System.out.println("Username is already exist please Try another one :) ");
System.out.println("Enter Username :");
username = in.next();
}
System.out.println("Enter Password :");
password = in.next();
while (password.length()<8){
System.out.println("Password is Empty please Try again :( ");
System.out.println("Enter Password :");
password = in.next();
}
user=new User(username, password);
System.out.println("SignUp Successful");
System.out.println("\n\n\tNow you need to enter some information");
System.out.println("Enter your name:");
String name = new String();
try {
name = reader.readLine(); // Read a line of text from the console
} catch (IOException e) {
System.out.println("Error reading input: " + e.getMessage());
}
while (name.isEmpty()){
System.out.println("name is Empty please Try again :( ");
System.out.println("Enter your name :");
try {
name = reader.readLine(); // Read a line of text from the console
} catch (IOException e) {
System.out.println("Error reading input: " + e.getMessage());
}
}
System.out.println("Enter gender :");
String gender=in.next();
while ((!(gender.toLowerCase()).equals("female")) && (!(gender.toLowerCase()).equals("male"))){
System.out.println("gender is not valid please Try again :( ");
System.out.println("Enter gender :");
gender = in.next();
}
Long phonenumber=new Long(0);
while (true) {
System.out.println("Please enter your phone number (digits only): ");
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("Phone number should contain digits only. Please try again.");
} else if (!(phonenumber<99999999)) {
System.out.println("Phone Number is not valid please Try again :( ");
} else {
phonenumber = Long.parseLong(input);
break; // Exit the loop if the input contains only digits
}
}
System.out.println("Enter E_mail :");
String email = in.next();
while (email.isEmpty() || email.contains(" ") || !email.contains("@") || email.length()<10){
System.out.println("E_mail is Empty please Try again :( ");
System.out.println("Enter E_mail :");
email = in.next();
}
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
Date dateOfBirth = null;
Boolean isValidDate = new Boolean(false);
System.out.print("Please enter your date of birth (YYYY-MM-DD): ");
String userInput = in.next();
while (!isValidDate){
try {
dateOfBirth = dateFormat.parse(userInput);
isValidDate = true;
} catch (ParseException e) {
System.out.println("Invalid date format. Please enter the date in YYYY-MM-DD format.");
userInput = in.next();
}
}
//===========================
user.setName(name);
user.setPhoneNumber(phonenumber);
user.setEmail(email);
user.setBirthdate(dateOfBirth);
if(gender.equals("female")){
user.setGender(User.GenderOptions.FEMALE);
}
else if (gender.equals("male")){
user.setGender(User.GenderOptions.MALE);
}
else{
user.setGender(User.GenderOptions.NONE);
}
System.out.println(user.getGender());
Main.vec.add(user);
System.out.println("Wish you enjoy our facebook :)");
//=============================
new Feed(user);
return user;
}
public static boolean CheckUserIsUnique (String Username){
for (User user:Main.vec){
if(user.getUserName().equals(Username)){
return false;
}
}
return true;
}
public static boolean LoginStatus(String Username,String Password){
for (User user:Main.vec){
if(user.getUserName().equals(Username) && user.getPassword().equals(Password)){
return true;
}
}
return false;
}
}