This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserService.java
117 lines (107 loc) · 3.26 KB
/
UserService.java
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
package teoskanta.user;
import java.sql.SQLException;
import java.util.*;
import teoskanta.user.dao.DBUserDao;
/**
* Class for handling actions between the user interface and DBUserDao.
*
*/
public class UserService {
private DBUserDao dbUserDao;
private User loggedIn;
/**
* Constructor for class.
*
* @param userDao DBUserDao-type variable to construct the class with Dao
* input
*/
public UserService(DBUserDao userDao) {
this.dbUserDao = userDao;
loggedIn = new User();
}
/**
* Method to create a new user.
*
* @param username String-type input username
* @param password String-type input password
* @return returns true if user creation was successful false if not
*/
public boolean newUser(String username, String password) {
System.out.println(username + " " + password);
try {
dbUserDao.create(new User(username, password));
if (dbUserDao.findUser(username, password)) {
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println("User creation had problems: " + e);
}
return false;
}
/**
* Method to login by setting the username and password values for the
* username and checking db for user.
*
* @param username String-type variable username
* @param password String-type variable password
* @return returns true if user is found from db and false if not
*/
public boolean login(String username, String password) {
//System.out.println(username + " " + password);
//System.out.println("Now we're in userService-class");
int id;
try {
if (dbUserDao.findUser(username, password)) {
id = dbUserDao.getUserIdFromDB(username, password);
//System.out.println("this is the id: " + id);
loggedIn.setId(id);
loggedIn.setUsername(username);
loggedIn.setPassword(password);
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println("Login doesn't work or username not found: " + e);
e.printStackTrace();
}
return false;
}
/**
* Method to logout by setting User object to null.
*
* @return returns true if successful and false if not
*/
public boolean logout() {
loggedIn = null;
if (loggedIn == null) {
return true;
}
return false;
}
public User getLoggedInUser() {
return loggedIn;
}
/**
* Checks if database and user table exists.
*/
public void checkDatabase() {
try {
dbUserDao.checkDBFile();
} catch (Exception e) {
System.out.println("Database check or creation for users failed: " + e);
}
}
/**
* Method for getting the user id from User-object.
* @return returns int-type variable id
*/
public int getUserId() {
int id = 0;
//System.out.println("This is the loggedin id: " + loggedIn.getId());
id = loggedIn.getId();
return id;
}
}