-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
212 lines (172 loc) · 5.05 KB
/
Copy pathServer.java
File metadata and controls
212 lines (172 loc) · 5.05 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
import java.util.Vector;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Server {
private static Server instance;
public Vector<User> users;
public Database database;
public Scanner stdin;
public static BuyerNotifier notifier;
private Server()
{
users = new Vector<User>();
database = new Database();
stdin = new Scanner(System.in);
notifier = new BuyerNotifier();
}
private Server(Server src)
{
this.users = src.users;
this.instance = src.instance;
}
public Database getDatabase() {
return database;
}
public User validate(String username, String password)
{
for(int i = 0; i < users.size(); i++)
{
if(username.equals(users.get(i).getUsername()))
if(password.equals(users.get(i).getPassword()))
return users.get(i);
}
System.out.println("Invalid Username/Password");
return null;
}
public void add(User user)
{
users.add(user);
}
public static Server getInstance(){
if(instance == null){
instance = new Server();
}
return instance;
}
public static void main(String [] args) throws IOException
{
//BETTER IMPLEMENTATION OF SERVER WITH LOGIN
Server server = new Server();
int choice = 0;
User user;
//SETUP
File file = new File("Users.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st = br.readLine()) != null)
{
String[] parts = st.split("-");
if(parts[0].equals("o"))
{
user = new Operator();
user.setUsername(parts[1]);
user.setPassword(parts[2]);
server.users.add(user);
}
if(parts[0].equals("r"))
{
user = new RegisteredBuyer();
user.setUsername(parts[1]);
user.setPassword(parts[2]);
server.users.add(user);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//PROGRAM START
System.out.println("Welcome to the sunshine tropical paradise of Isle Delfino"
+ "\n all inputs and options will be dealt with by entering a number to select an option unless otherwise stated...");
//INITIAL OPTIONS
choice = server.loopForInput(2, "Please select an option:"
+ "\nView as Buyer (1)"
+ "\nLogin (2)");
//registered Buyer
if(choice == 1) {
user = new Buyer();
}
else if(choice == 2)
{
System.out.println("Please enter your username:");
String username = server.stdin.next();
System.out.println("Please enter your password:");
String password = server.stdin.next();
user = server.validate(username, password);
while(user == null)
{
System.out.println("Please enter your username:");
username = server.stdin.next();
System.out.println("Please enter your password:");
password = server.stdin.next();
user = server.validate(username, password);
}
}
else //SHOULD NEVER GET HERE
user = new Buyer();
user.showOptions();
//////////////////////////////////////////////////////////////////////////////////
//OLD VERSION OF SERVER
/*
String username;
String password;
Server server = new Server();
int choice = 0;
User user;;
//SETUP
//PROGRAM START
System.out.println("Welcome to the sunshine tropical paradise of Isle Delfino"
+ "\n all inputs and options will be dealt with by entering a number to select an option unless otherwise stated...");
//INITIAL OPTIONS
choice = server.loopForInput(6, "Please select a User type:"
+ "\nRegistered Buyer (1)"
+ "\nOperator (2)"
+ "\nSystemAdmin (3)"
+ "\nManager (4)"
+ "\nAuthor (5)"
+ "\nBuyer (6)");
//registered Buyer
if(choice == 1) {
user = new Buyer();
}
//Operator
else if(choice == 2) {
user = new Operator();
}
//System Admin
else if(choice == 3) {
user = new Buyer();
}
//Manager
else if(choice == 4) {
user = new Buyer();
}
//Author
else if(choice == 5) {
user = new Buyer();
}
//Buyer
else if(choice == 6) {
user = new Buyer();
}
//should never get here
else
user = new Buyer();
user.showOptions();
*/
}
public int loopForInput(int max, String str)
{
int i;
do {
System.out.println(str);
i = stdin.nextInt();
}while(i > max);
return i;
}
}