[go: up one dir, main page]

Menu

[r34]: / keeping / USR.java  Maximize  Restore  History

Download this file

153 lines (141 with data), 3.7 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
package keeping;
/***
Stores static information about users: just their name and password.
Does not include the manager.
*/
import database.*;
abstract public class USR
// Please change this class from abstract to final.
{
private static final int USER_NAME_COLUMN = 0;
private static final int PASSWORD_COLUMN = 1;
private DB _db = null;
/***
Loads the list of users from file.
*/
USR()
{
_db = new DB(Tables.USR);
// not implemented
// remember to delte empty user names.
}
/***
Get some waitron's names.
@return All user names which have not yet been deleted.
*/
public String[] list() {
int iNumOfRows = _db.length();
String[] arrayUserNames = new String[iNumOfRows];
String sCurrentUserName = null;
for (int i = 0; i < iNumOfRows; i++) {
sCurrentUserName = _db.getString(USER_NAME_COLUMN, i);
arrayUserNames[i] = sCurrentUserName;
}
return arrayUserNames;
}
/***
Delte the specified user.
@param index Which user to delete. >= 0
*/
abstract void delete(int index) ; // Just set user name to "", then call db.delete().
/***
Adds a newly created user to the database.
@param userName This may contain spaces.
@param password String of digits.
@throws IllegalArgumentException if the password does not match the criteria specified in {@link change
}
Or if the userName parameter is empty.
*/
public void add(String userName, String password) {
if (isPasswordValid(password)) {
_db.add(new String[] {userName, password});
} else {
throw new IllegalArgumentException();
}
}
/***
* Checks if a given password is valid according to the change method logic.
* @param sPassword
* @return
*/
private boolean isPasswordValid(String sPassword) {
if (isStringLength3(sPassword) && isStringNumeric(sPassword)
&& !isPasswordInUse(sPassword)) {
return true;
} else {
return false;
}
}
/***
* Checks if a string's length equals 3.
* @param string
* @return
*/
private boolean isStringLength3(String string) {
if (string.length() == 3) {
return true;
} else {
return false;
}
}
/***
* Checks if a given password already exists in the DB.
* @param sPassword
* @return
*/
private boolean isPasswordInUse(String sPassword) {
String sCurrentPassword = null;
int iNumOfRows = _db.length();
for (int iCurrentRow = 0; iCurrentRow < iNumOfRows; iCurrentRow++) {
sCurrentPassword = _db.getString(PASSWORD_COLUMN, iCurrentRow);
if (sCurrentPassword.equals(sPassword)) {
return true;
}
}
return false;
}
/***
* Checks if a given string's characters are all numbers.
* @param string
* @return
*/
private boolean isStringNumeric(String string) {
try {
Integer.parseInt(string);
} catch (NumberFormatException numberFormatException) {
return false;
}
return true;
}
/***
Create a random password.
@return Some characters.
*/
static String generate()
{
return "123";
}
/***
Change password.
@param index the position of the user in the list() array.
@param newPassword Must comply with rules below:
@throws IllegalArgumentException If newPassword is not three characters long, or if it does not consist entirely of digits, or if it is in use by another user!
*/
public void change(int index, String newPassword) {
if (isPasswordValid(newPassword)) {
_db.setString(PASSWORD_COLUMN, index, newPassword);
} else {
throw new IllegalArgumentException();
}
}
/***
Have a quick look at a user's current password.
<p>
Naughty, naughty!
@param index User offset.
@return The user's password.
*/
public String peek(int index) {
return _db.getString(PASSWORD_COLUMN, index);
}
}