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);
}
}