Added description field to database and fixed some tests with old ServiceProvider constructor

This commit is contained in:
Mary Tran 2018-11-16 15:33:54 -05:00
parent 66d33d4cc4
commit d7cf9ba311
6 changed files with 50 additions and 41 deletions

View file

@ -23,7 +23,7 @@ import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
//version of db used for update method
private static final int DB_VERSION = 3;
private static final int DB_VERSION = 4;
//name of db in app data
private static final String DB_NAME = "UsersDB.db";
@ -45,6 +45,8 @@ public class DBHelper extends SQLiteOpenHelper {
private static final String COLUMN_PHONE = "phone";
private static final String COLUMN_COMPANY = "company";
private static final String COLUMN_LICENSED = "licensed";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_SALT = "salt";
//name of table containing services and rates
private static final String TABLE_SERVICES = "services";
@ -108,7 +110,9 @@ public class DBHelper extends SQLiteOpenHelper {
+ COLUMN_ADDRESS + " TEXT, "
+ COLUMN_PHONE + " TEXT, "
+ COLUMN_COMPANY + " TEXT, "
+ COLUMN_LICENSED + " TEXT "
+ COLUMN_LICENSED + " TEXT, "
+ COLUMN_DESCRIPTION + " TEXT, "
+ COLUMN_SALT + " TEXT "
+ ")";
db.execSQL(CREATE_LOGIN_TABLE);
@ -197,6 +201,10 @@ public class DBHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_PHONE + " TEXT");
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_COMPANY + " TEXT");
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_LICENSED + " TEXT");
case 3:
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_DESCRIPTION + " TEXT");
db.execSQL("ALTER TABLE " + TABLE_LOGIN + " ADD COLUMN " + COLUMN_SALT + " TEXT");
}
}
@ -259,6 +267,11 @@ public class DBHelper extends SQLiteOpenHelper {
String licensed = String.valueOf(serviceProvider.isLicensed());
values.put(COLUMN_LICENSED, licensed);
String description = serviceProvider.getDescription();
if (description != null){
values.put(COLUMN_DESCRIPTION, description);
}
}
writeDB.insert(TABLE_LOGIN, null, values);
@ -324,14 +337,14 @@ public class DBHelper extends SQLiteOpenHelper {
*/
public boolean updateUserInfo(String username, String password, String firstname, String lastname){
return updateUserInfo(username, password, firstname, lastname,
null, null, null, null);
null, null, null, null, null);
}
public boolean updateUserInfo(String username, String password, String firstname, String lastname,
String address, String phonenumber, String companyname, Boolean licensed){
String address, String phonenumber, String companyname, Boolean licensed,
String description){
ContentValues values = new ContentValues();
if (password != null && !password.equals("")) values.put(COLUMN_PASSWORD, password);
if (firstname != null && !firstname.equals("")) values.put(COLUMN_FIRSTNAME, firstname);
@ -339,7 +352,8 @@ public class DBHelper extends SQLiteOpenHelper {
if (address != null && !address.equals(""))values.put(COLUMN_ADDRESS, address);
if (phonenumber != null && !phonenumber.equals(""))values.put(COLUMN_PHONE, phonenumber);
if (companyname != null && !companyname.equals(""))values.put(COLUMN_COMPANY, companyname);
if (licensed != null)values.put(COLUMN_LICENSED, String.valueOf(licensed));
if (licensed != null) values.put(COLUMN_LICENSED, String.valueOf(licensed));
if (description != null && !description.equals(""))values.put(COLUMN_DESCRIPTION, description);
return writeDB.update(TABLE_LOGIN, values, COLUMN_USERNAME+" = ?",
@ -358,7 +372,7 @@ public class DBHelper extends SQLiteOpenHelper {
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN
+ " WHERE " + COLUMN_USERNAME + " = ?",
new String[]{username});
new String[]{username});
if (!cursor.moveToFirst()) return false;
@ -589,7 +603,7 @@ public class DBHelper extends SQLiteOpenHelper {
return writeDB.delete(TABLE_SERVICEPROVIDERS,
COLUMN_SERVICEPROVIDERNAME + " = ? AND "
+ COLUMN_SERVICEPROVIDERSERVICE + " = ?",
new String[]{serviceProviderUsername, serviceName}) > 0;
new String[]{serviceProviderUsername, serviceName}) > 0;
}
public List<String[]> getAllServicesProvidedByUser(ServiceProvider serviceProvider){
@ -622,9 +636,9 @@ public class DBHelper extends SQLiteOpenHelper {
serviceName = serviceName.toLowerCase().trim();
return getAll("SELECT " + COLUMN_SERVICEPROVIDERNAME
+ " FROM " + TABLE_SERVICEPROVIDERS
+ " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '"
+ serviceName + "'");
+ " FROM " + TABLE_SERVICEPROVIDERS
+ " WHERE " + COLUMN_SERVICEPROVIDERSERVICE + " = '"
+ serviceName + "'");
}
public boolean updateAvailability(ServiceProvider serviceProvider){
@ -635,7 +649,7 @@ public class DBHelper extends SQLiteOpenHelper {
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_AVAILABILITY
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
new String[]{serviceProvider.getUsername()});
new String[]{serviceProvider.getUsername()});
ContentValues contentValues = new ContentValues();
addAvailabilityToContentValues(contentValues, COLUMN_MONSTART, COLUMN_MONEND, availabilities[0]);
@ -682,14 +696,14 @@ public class DBHelper extends SQLiteOpenHelper {
public int[][] getAvailabilities(ServiceProvider serviceProvider){
if (serviceProvider==null) return new int[7][4];
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_AVAILABILITY
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
new String[]{serviceProvider.getUsername()});
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
new String[]{serviceProvider.getUsername()});
if (cursor.moveToFirst()){
for (int i = 0; i < 7; i++) {
int start = cursor.getInt(i*2+1);
int end = cursor.getInt(i*2+2);
serviceProvider.setAvailabilities(i, start/60, start%60,
end/60, end%60);
end/60, end%60);
}
} else {
return new int[7][4];
@ -703,7 +717,7 @@ public class DBHelper extends SQLiteOpenHelper {
Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_AVAILABILITY
+ " WHERE " + COLUMN_AVAILABILITYNAME + " = ?",
new String[]{serviceProviderName});
new String[]{serviceProviderName});
if (cursor.moveToFirst()){
for (int i = 0; i < 7; i++) {
int start = cursor.getInt(i*2+1);

View file

@ -79,7 +79,7 @@ public class EditProfile extends AppCompatActivity {
&& address.getText().toString().replaceAll("\\s+","").length()>0) {
if(dbHelper.updateUserInfo(username, password.getText().toString(), firstname.getText().toString(), lastname.getText().toString(),
address.getText().toString(), phonenumber.getText().toString(), companyname.getText().toString(), licensed.isChecked())){
address.getText().toString(), phonenumber.getText().toString(), companyname.getText().toString(), licensed.isChecked(), description.getText().toString())){
//add comment method here
Toast.makeText(this, "Profile has been updated", Toast.LENGTH_LONG).show();
}

View file

@ -142,7 +142,6 @@ public class ServiceProvider extends UserType {
this.licensed = licensed;
}
<<<<<<< HEAD
public String getDescription() {
return description;
}
@ -150,10 +149,5 @@ public class ServiceProvider extends UserType {
public void setDescription(String phonenumber) {
this.description = description;
}
=======
public String getDescription() {return description; }
public void setDescription(String description) { this.description = description; }
>>>>>>> 587e99040b46bc068178844c11165b0730c21628
}

View file

@ -52,7 +52,7 @@ public class DBIntegrationTest {
//add a ServiceProvider to database
originalUser = new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman",
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
addedTwo = dbHelper.addUser(originalUser);
//test retrieving ServiceProvider, and confirm that user info is the same as that in object passed
@ -79,7 +79,7 @@ public class DBIntegrationTest {
public void testAddAndGetServiceProvider(){
//ServiceProviders have extra fields that can be added to the DB
ServiceProvider serviceProvider = new ServiceProvider("7MuF1c59XP", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, "Not a penguin");
dbHelper.addUser(serviceProvider);
//retrieve ServiceProvider and test the newly added fields
@ -93,6 +93,7 @@ public class DBIntegrationTest {
assertEquals(serviceProvider.getPhonenumber(), dbServiceProvider.getPhonenumber());
assertEquals(serviceProvider.getCompanyname(), dbServiceProvider.getCompanyname());
assertEquals(serviceProvider.isLicensed(), dbServiceProvider.isLicensed());
assertEquals(serviceProvider.getDescription(), dbServiceProvider.getDescription());
dbHelper.deleteUser("7MuF1c59XP");
}
@ -101,7 +102,7 @@ public class DBIntegrationTest {
public void testDeleteServiceProvider(){
//make sure all the rows related to ServiceProvider in all tables are deleted
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
dbHelper.addUser(serviceProvider);
Service service1 = new Service("Hitman", 12358);
@ -142,7 +143,7 @@ public class DBIntegrationTest {
added = dbHelper.addUser(new HomeOwner("jbO4aBF4dC", "seg2105", "Miguel", "Garzon"));
assertTrue(!added);
added = dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman",
"testaddress", "8888888888", "companydotcom", true));
"testaddress", "8888888888", "companydotcom", true, null));
assertTrue(!added);
dbHelper.deleteUser("jbO4aBF4dC");
@ -278,7 +279,7 @@ public class DBIntegrationTest {
@Test
public void testAddAndDeleteServiceProvidedByUser(){
dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true));
"testaddress", "8888888888", "companydotcom", true, null));
dbHelper.addService(new Service("Hitman", 12358));
boolean added = dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "hitman");
assertTrue(added);
@ -291,7 +292,7 @@ public class DBIntegrationTest {
@Test
public void testGetAllServicesProvidedByUserAndDeleteService(){
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
dbHelper.addUser(serviceProvider);
Service service1 = new Service("Hitman", 12358);
@ -324,9 +325,9 @@ public class DBIntegrationTest {
public void testGetAllProvidersByService(){
dbHelper.addService(new Service("Exterminating flatworms", 392.457));
dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true));
"testaddress", "8888888888", "companydotcom", true, null));
dbHelper.addUser(new ServiceProvider("7MuF1c59XP", null, null, null,
"testaddress", "8888888888", "companydotcom", true));
"testaddress", "8888888888", "companydotcom", true, null));
dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms");
dbHelper.addServiceProvidedByUser("7MuF1c59XP", "exterminating flatworms");
@ -346,7 +347,7 @@ public class DBIntegrationTest {
@Test
public void testDeleteServiceProvidedByUser(){
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
dbHelper.addUser(serviceProvider);
Service service1 = new Service("Hitman", 12358);
@ -376,7 +377,7 @@ public class DBIntegrationTest {
@Test
public void testUpdateAndGetAvailability(){
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
serviceProvider.setAvailabilities(0, 4, 18, 19, 30);
serviceProvider.setAvailabilities(1, 5, 20, 21, 11);
serviceProvider.setAvailabilities(3, 7, 12, 15, 14);
@ -384,7 +385,7 @@ public class DBIntegrationTest {
//TODO:Perhaps implement a deep clone function for UserType?
ServiceProvider serviceProvider2 = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
serviceProvider2.setAvailabilities(0, 4, 18, 19, 30);
serviceProvider2.setAvailabilities(1, 5, 20, 21, 11);
serviceProvider2.setAvailabilities(3, 7, 12, 15, 14);
@ -413,7 +414,7 @@ public class DBIntegrationTest {
@Test
public void testInvalidAvailability(){
ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null,
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, null);
serviceProvider.setAvailabilities(2, 8, 14, 8, 14);
serviceProvider.setAvailabilities(3, 15, 12, 8, 14);

View file

@ -29,15 +29,15 @@ public class ServiceTest {
@Test
public void addServiceProviderTest(){
service.addServiceProvider( new ServiceProvider( "John123", "1234567890", "John", "Doe",
"testaddress", "8888888888", "companydotcom", true) );
"testaddress", "8888888888", "companydotcom", true, null) );
service.addServiceProvider( new ServiceProvider( "Jane123", "1234567890", "Jane", "Doe",
"testaddress", "8888888888", "companydotcom", true) );
"testaddress", "8888888888", "companydotcom", true, null) );
service.addServiceProvider( new ServiceProvider( "John123", "1234567890", "John", "Doe",
"testaddress", "8888888888", "companydotcom", true) );
"testaddress", "8888888888", "companydotcom", true, null) );
service.addServiceProvider( new ServiceProvider( "Jane123", "1234567890", "Jane", "Doe",
"testaddress", "8888888888", "companydotcom", true) );
"testaddress", "8888888888", "companydotcom", true, null) );
service.addServiceProvider( new ServiceProvider( "Jane123", "1234567890", "John", "Doe",
"testaddress", "8888888888", "companydotcom", true) );
"testaddress", "8888888888", "companydotcom", true, null) );
int numOfSP = service.getServiceProviders().size();
assertEquals( 2,numOfSP );
}

View file

@ -20,7 +20,7 @@ public class UserTypeTest {
UserType user = new HomeOwner("John123", "1234567890", "John", "Doe");
UserType admin = new Admin();
UserType serviceprovider = new ServiceProvider( "Jane123", "1234567890", "Jane", "Doe",
"testaddress", "8888888888", "companydotcom", true);
"testaddress", "8888888888", "companydotcom", true, "I am a walrus");
boolean userservice = user.equals(serviceprovider);
boolean useradmin = user.equals(admin);
boolean serviceadmin = serviceprovider.equals(admin);