Added new table for services. Not tested yet

This commit is contained in:
Mary Tran 2018-10-25 16:14:38 -04:00
parent 319428eebb
commit d29b03972e
3 changed files with 177 additions and 30 deletions

View file

@ -43,8 +43,11 @@ public class DBHelper extends SQLiteOpenHelper {
// private static final String TABLE_SERVICEPROVIDERS = "serviceProviders"; // private static final String TABLE_SERVICEPROVIDERS = "serviceProviders";
//name of table containing services and rates
// private static final String TABLE_SERVICES = "services"; private static final String TABLE_SERVICES = "services";
//columns of TABLE_LOGIN
private static final String COLUMN_SERVICE = "service";
private static final String COLUMN_RATE = "rate";
/** /**
@ -64,18 +67,25 @@ public class DBHelper extends SQLiteOpenHelper {
public void onCreate(SQLiteDatabase db){ public void onCreate(SQLiteDatabase db){
String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "(" String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "("
+ COLUMN_USERNAME + " STRING UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK," + COLUMN_USERNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_PASSWORD + " STRING NOT NULL," + COLUMN_PASSWORD + " TEXT NOT NULL,"
+ COLUMN_FIRSTNAME + " STRING DEFAULT 'FirstName'," + COLUMN_FIRSTNAME + " TEXT DEFAULT 'FirstName',"
+ COLUMN_LASTNAME + " STRING DEFAULT 'LastName'," + COLUMN_LASTNAME + " TEXT DEFAULT 'LastName',"
+ COLUMN_USERTYPE + " STRING NOT NULL" + ")"; + COLUMN_USERTYPE + " TEXT NOT NULL" + ")";
db.execSQL(CREATE_LOGIN_TABLE); db.execSQL(CREATE_LOGIN_TABLE);
String CREATE_SERVICES_TABLE = "CREATE TABLE "+ TABLE_SERVICES + "("
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")";
db.execSQL(CREATE_SERVICES_TABLE);
} }
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SERVICES);
onCreate(db); onCreate(db);
} }
@ -85,6 +95,8 @@ public class DBHelper extends SQLiteOpenHelper {
} }
//methods for table of users
/** /**
* Adds a user to the database. Returns false if there is a user already * Adds a user to the database. Returns false if there is a user already
* existing in the database with the same username. Returns true if * existing in the database with the same username. Returns true if
@ -188,13 +200,133 @@ public class DBHelper extends SQLiteOpenHelper {
new String[]{username}) > 0; new String[]{username}) > 0;
} }
/**
* Returns a list of String arrays containing the username, first name,
* last name, and user type of every user in TABLE_LOGIN.
*
* @return list of arrays of [username, first name, last name, user type]
*/
public List<String[]> getAllUsers(){
return getAll("SELECT " + COLUMN_USERNAME + ", "
+ COLUMN_FIRSTNAME + ", "
+ COLUMN_LASTNAME + ", "
+ COLUMN_USERTYPE
+ " FROM "+TABLE_LOGIN);
}
//methods for table of services
/**
* Adds a service to the database. Returns false if service already
* exists in the database.
* Returns true if successful in adding service to database.
*
* @param service service to be added
* @return whether adding service was successful
*/
public boolean addService(Service service){
//Check for duplicate username by querying login table
Cursor cursor = writeDB.query(TABLE_SERVICES,
new String[] {COLUMN_SERVICE},
COLUMN_SERVICE + " = ?",
new String[]{service.getName()},
null, null, null,
"1");
//If cursor has 1+ elements in it, username already exists in table
if (cursor != null && cursor.getCount() > 0){
cursor.close();
return false;
}
cursor.close();
ContentValues values = new ContentValues();
values.put(COLUMN_SERVICE, service.getName());
values.put(COLUMN_RATE, service.getRate());
writeDB.insert(TABLE_SERVICES, null, values);
return true;
}
/**
* Looks in database for service with specified, and returns an
* object of Service if found.
* Returns null if no such service found.
*
* @param serviceName service to look up
* @return object representing service found
*/
public Service findService(String serviceName){
Service service;
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_SERVICES
+ " WHERE " + COLUMN_SERVICE + " = ?",
new String[]{serviceName});
if (cursor.moveToFirst()){
String servName = cursor.getString(0);
double rate = cursor.getDouble(1);
service = new Service(servName, rate);
} else {
service = null;
}
cursor.close();
return service;
}
/**
* Updates service rate using a Service object.
* Returns true if a service was found and entry updated.
* Returns false if no service was found.
*
*
* @param service service object containing updated values
*
* @return whether updating service information was successful
*/
public boolean updateService(Service service){
ContentValues values = new ContentValues();
values.put(COLUMN_RATE, service.getRate());
return writeDB.update(TABLE_SERVICES, values, COLUMN_SERVICE+" = ?",
new String[]{service.getName()}) > 0;
}
/**
* Updates service rate using input of service name and rate.
* Returns true if a service was found and entry updated.
* Returns false if no service was found.
*
*
* @param name name of service
* @param rate rate of service
*
* @return whether updating service information was successful
*/
public boolean updateService(String name, double rate){
ContentValues values = new ContentValues();
if (rate > 0)
values.put(COLUMN_RATE, rate);
return writeDB.update(TABLE_SERVICES, values, COLUMN_SERVICE+" = ?",
new String[]{name}) > 0;
}
/**
* Returns a list of String arrays containing the service categories,
* names and hourly rates.
*
* @return list of arrays of [service, rate]
*/
public List<String[]> getAllServices(){
return getAll("SELECT * FROM " + TABLE_SERVICES);
}
/** /**
* Prints all entries of table. One row is printed per line. Columns are * Prints all entries of table. One row is printed per line. Columns are
* separated by spaces. * separated by spaces.
* *
* @param tableName name of table to print * @param tableName name of table to print
*/ */
public void printTable(String tableName){ void printTable(String tableName){
Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null); Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null);
cursor.moveToFirst(); cursor.moveToFirst();
for (int i = 0; i<cursor.getCount(); i++){ for (int i = 0; i<cursor.getCount(); i++){
@ -208,32 +340,24 @@ public class DBHelper extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
/**
* Returns a list of String arrays containing the username, first name, private List<String[]> getAll(String rawQuery){
* last name, and user type of every user in TABLE_LOGIN. List<String[]> list = new LinkedList<>();
* String[] infoArray;
* @return list of arrays of [username, first name, last name, user type] Cursor cursor = readDB.rawQuery(rawQuery,null);
*/
public List<String[]> getAllUsers(){
List<String[]> listOfUsers = new LinkedList<>();
String[] user;
Cursor cursor = readDB.rawQuery("SELECT " + COLUMN_USERNAME + ", "
+ COLUMN_FIRSTNAME + ", "
+ COLUMN_LASTNAME + ", "
+ COLUMN_USERTYPE
+ " FROM "+TABLE_LOGIN, null);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) { for (int i = 0; i < cursor.getCount(); i++) {
user = new String[cursor.getColumnNames().length]; infoArray = new String[cursor.getColumnNames().length];
for (int j = 0; j < cursor.getColumnNames().length; j++) { for (int j = 0; j < cursor.getColumnNames().length; j++) {
user[j] = cursor.getString(j); infoArray[j] = cursor.getString(j);
} }
listOfUsers.add(user); list.add(infoArray);
cursor.moveToNext(); cursor.moveToNext();
} }
} }
cursor.close(); cursor.close();
return listOfUsers; return list;
} }
} }

View file

@ -1,4 +1,27 @@
package com.uottawa.olympus.olympusservices; package com.uottawa.olympus.olympusservices;
public class Service { public class Service {
private String name;
private double rate;
Service(String name, double rate) {
this.name = name;
this.rate = rate;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
public void setName(String name) {
this.name = name;
}
public void setRate(double rate) {
this.rate = rate;
}
} }

View file

@ -115,10 +115,10 @@ public class DBIntegrationTest {
List<String[]> allUsers = dbHelper.getAllUsers(); List<String[]> allUsers = dbHelper.getAllUsers();
for (String[] user : allUsers){ for (String[] user : allUsers){
// for (String s : user){ /* for (String s : user){
// System.out.print(s + " "); System.out.print(s + " ");
// } }
// System.out.println(); System.out.println();*/
UserType usertype = dbHelper.findUserByUsername(user[0]); UserType usertype = dbHelper.findUserByUsername(user[0]);
assertEquals(usertype.getFirstname(), user[1]); assertEquals(usertype.getFirstname(), user[1]);
assertEquals(usertype.getLastname(), user[2]); assertEquals(usertype.getLastname(), user[2]);