Newer
Older
import java.text.ParseException;
import java.text.SimpleDateFormat;
import exceptions.CategoryInvalidException;
import exceptions.DateInvalidException;
import exceptions.LoginInvalidException;
import exceptions.MailInvalidException;
import exceptions.PasswordInvalidException;
import exceptions.PriceInvalidException;
import exceptions.TitleInvalidException;
import exceptions.UsernameEmailEmptyException;
static int idCounter;
public static ArrayList<User> users = new ArrayList<User>();
private ArrayList<Item> myItem = new ArrayList<Item>();
private int id;
private String username, email, password;
username = "";
email = "";
password = "";
this.id = idCounter++;
users.add(this);
public User(int id) {
username = "";
email = "";
password = "";
this.id = id;
users.add(this);
}
public User(String usernameInput, String mailInput, String passwordInput) {
username = usernameInput;
email = mailInput;
password = passwordInput;
this.id = idCounter++;
users.add(this);
}
User u = new User();
return u;
public static boolean login(String username, String email, String password)
throws UsernameEmailEmptyException, PasswordInvalidException, MailInvalidException, LoginInvalidException {
if (username.equals("") && email.equals("")) {
throw new UsernameEmailEmptyException();
} else {
if (password.equals("")) {
throw new PasswordInvalidException();
} else if (!username.equals("")) {
User toCheck = null;
for (User u : users) {
if (u.username.equals(username)) {
toCheck = u;
}
}
if (toCheck == null) {
throw new LoginInvalidException();
if (password.equals(toCheck.password)) {
return true;
} else {
throw new LoginInvalidException();
}
} else if (!email.matches("\\S+@\\S+\\.\\S+")) {
throw new MailInvalidException();
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
User toCheck = null;
for (User u : users) {
if (u.email.equals(email)) {
toCheck = u;
}
}
if (toCheck == null) {
throw new LoginInvalidException();
} else {
if (password.equals(toCheck.password)) {
return true;
} else {
throw new LoginInvalidException();
}
}
public ArrayList<Auction> retrieveSubscribedAuction() {
// TODO Auto-generated method stub
return null;
}
public ArrayList<Item> searchItem(String title, String category)
throws CategoryInvalidException, TitleInvalidException {

Righi Andrea (Student Com17)
committed
if (!title.matches("^[a-zA-Z\\s]+$") || title.length() > 30 || title.length() < 1) {
throw new TitleInvalidException();
} else {
Category c = Category.getCategory(category);
if (c == null) {
throw new CategoryInvalidException();
} else {
ArrayList<Item> toReturn = new ArrayList<Item>();
for (Item i : Item.items) {

Righi Andrea (Student Com17)
committed
if (i.getTitle().equals(title) && i.getCategory().equals(c)) {
toReturn.add(i);
}
}
return toReturn;
}
}
public ArrayList<Auction> searchAuction(String startTime, String endTime, double price, String title,
String category)
throws CategoryInvalidException, TitleInvalidException, DateInvalidException, PriceInvalidException {
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
String format = "dd/MM/yyyy hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date start, end;
if (!title.matches("^[a-zA-Z]+$") || title.length() > 30 || title.length() < 1) {
throw new TitleInvalidException();
} else {
Category c = Category.getCategory(category);
if (c == null) {
throw new CategoryInvalidException();
} else if (!Seller.isValidFormat(format, startTime) || !Seller.isValidFormat(format, endTime)) {
throw new DateInvalidException();
} else if (price <= 0) {
throw new PriceInvalidException();
} else {
try {
start = sdf.parse(startTime);
end = sdf.parse(endTime);
} catch (ParseException p) {
throw new DateInvalidException();
}
ArrayList<Auction> toReturn = new ArrayList<Auction>();
for (Auction a : Auction.auctions) {
if (a.getTitle().equals(title) || a.getCategory().equals(c) || a.getStart().equals(start)
|| a.getEnd().equals(end) || a.getPrice() - price <= 0.01) {
toReturn.add(a);
}
}
return toReturn;
}
}
public boolean addItem(int itemId) {
if (myItem.contains(Item.getItem(itemId))) {
return myItem.add(Item.getItem(itemId));
}
return false;
}
public void setId(int i) {
this.id = i;
}
public int getId() {
public void setUsername(String usernameInput) {
this.username = usernameInput;
}
public void setEmail(String emailInput) {
this.email = emailInput;
}
public void setPassword(String passwordInput) {
this.password = passwordInput;
}
public static User getUserById(int user) {
for (User u : users) {
if (u.getId() == user) {
return u;
}
}
return null;
}