Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drink input #99

Merged
merged 9 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
android:exported="false"
android:label="Graphs"
android:parentActivityName=".Views.HomePage" />
<activity
android:name=".Views.DrinkInputActivity"
android:exported="false"
android:label="Drink Input"
android:parentActivityName=".Views.HomePage"/>
<activity
android:name=".Views.BarGraphActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.util.Log;
import android.widget.Toast;

import com.example.drinkingbuddy.Models.Breathalyzer;
import com.example.drinkingbuddy.Models.Config;
import com.example.drinkingbuddy.Models.Drink;
import com.example.drinkingbuddy.Models.Profile;

import java.util.ArrayList;
Expand All @@ -34,42 +36,41 @@ public class DBHelper extends SQLiteOpenHelper {
String CREATE_TABLE_RESULTS;
SQLiteDatabase db;


public DBHelper(Context context)
{
public DBHelper(Context context) {
super(context, Config.DATABASE_NAME, null, Config.DATABASE_VERSION );
this.context = context;
db = getWritableDatabase();
}

//simply creates database to hold breathalyzer entries
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
CREATE_TABLE_RESULTS = "CREATE TABLE " + Config.TABLE_NAME
+ " (" + Config.Result + " TEXT NOT NULL,"
+ Config.TimeStamp + " TEXT NOT NULL,"
public void onCreate(SQLiteDatabase sqLiteDatabase) {// might need id autoincrement (?)
CREATE_TABLE_RESULTS = "CREATE TABLE " + Config.TABLE_NAME_SENSOR
+ " (" + Config.SENSOR_RESULT + " TEXT NOT NULL,"
+ Config.TIME_STAMP_SENSOR + " TEXT NOT NULL,"
+ Config.DAY_OF_WEEK + " TEXT NOT NULL)";

CREATE_TABLE_PROFILE = "CREATE TABLE " + Config.TABLE_NAME_PROFILE
+ " (" + Config.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " (" + Config.PROFILE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Config.USERNAME + " TEXT NOT NULL,"
+ Config.PASSWORD + " TEXT NOT NULL,"
+ Config.DEVICE_NAME + " TEXT NOT NULL,"
+ Config.DEVICE_CODE + " TEXT NOT NULL)";

CREATE_TABLE_TYPE_OF_DRINK = "CREATE TABLE " + Config.TABLE_NAME_DRINK_TYPE
+ " (" + Config.TYPE_OF_DRINK + " TEXT NOT NULL)";
+ " (" + Config.DRINK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Config.TIME_STAMP_DRINK + " TEXT NOT NULL,"
+ Config.TYPE_OF_DRINK + " TEXT NOT NULL,"
+ Config.DRINK_QUANTITY + " INTEGER NOT NULL,"
+ Config.DAY_OF_WEEK + " TEXT NOT NULL)";

Log.d(TAG, "db created");

sqLiteDatabase.execSQL(CREATE_TABLE_RESULTS);
sqLiteDatabase.execSQL(CREATE_TABLE_PROFILE);
sqLiteDatabase.execSQL(CREATE_TABLE_TYPE_OF_DRINK);

}



//not currently needed but can be implemented in the future
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
Expand All @@ -94,8 +95,6 @@ public String TimeStamp()
//REFERENCE: https://howtodoinjava.com/java/date-time/convert-date-time-to-est-est5edt/
}



//Method to add new Profile
public void insertNewProfile(Profile profile)
{
Expand All @@ -113,7 +112,6 @@ public void insertNewProfile(Profile profile)
{
Log.d(TAG, "EXCEPTION" + e);
Toast.makeText(context, "Operation Failed!: " + e, Toast.LENGTH_LONG).show();

}
finally {
db.close();
Expand All @@ -130,7 +128,7 @@ public boolean update(Profile profile, int id)
contentValues.put(Config.PASSWORD, profile.getPassword());
contentValues.put(Config.DEVICE_NAME, profile.getDeviceName());
contentValues.put(Config.DEVICE_CODE, profile.getDeviceCode());
db.update(Config.TABLE_NAME_PROFILE, contentValues, Config.ID + "=?", new String[] {String.valueOf(id)});
db.update(Config.TABLE_NAME_PROFILE, contentValues, Config.PROFILE_ID + "=?", new String[] {String.valueOf(id)});
return true;
}

Expand All @@ -139,14 +137,13 @@ public void insertNewResult(String result)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String currentStamp = TimeStamp();
String dayOfWeek = DayOfWeek();
contentValues.put(Config.Result, result);
contentValues.put(Config.TimeStamp, currentStamp);
contentValues.put(Config.DAY_OF_WEEK, dayOfWeek);

contentValues.put(Config.SENSOR_RESULT, result);
contentValues.put(Config.TIME_STAMP_SENSOR, TimeStamp());
contentValues.put(Config.DAY_OF_WEEK, DayOfWeek());

try{
db.insertOrThrow(Config.TABLE_NAME, null, contentValues);
db.insertOrThrow(Config.TABLE_NAME_SENSOR, null, contentValues);
Log.d(TAG, "db value added");
} catch(SQLException e)
{
Expand All @@ -158,26 +155,25 @@ public void insertNewResult(String result)
}
}

public void SaveDrinkType(String type_of_drink) {
public void saveDrinkType(String typeOfDrink, int quantity) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put(Config.TYPE_OF_DRINK, type_of_drink);
contentValues.put(Config.TIME_STAMP_DRINK, TimeStamp());
contentValues.put(Config.TYPE_OF_DRINK, typeOfDrink);
contentValues.put(Config.DRINK_QUANTITY, quantity);
contentValues.put(Config.DAY_OF_WEEK, DayOfWeek());

try {
db.insertOrThrow(Config.TABLE_NAME_DRINK_TYPE, null, contentValues);
Log.d(TAG, type_of_drink);


Log.d(TAG, typeOfDrink);
} catch (SQLException e)
{
Log.d(TAG, "EXCEPTION: " + e);
Toast.makeText(context, "Operation Failed!: " + e, Toast.LENGTH_LONG).show();

}
finally {
db.close();

}
}

Expand All @@ -200,7 +196,7 @@ public int checkProfile(String user, String pass)
Log.d(TAG, "User " + Username + "Password " + Password);
if(user.equals(Username) && pass.equals(Password)) //if the user and password in database matches passed parameters
{
return Cursor.getInt(Cursor.getColumnIndexOrThrow(Config.ID));
return Cursor.getInt(Cursor.getColumnIndexOrThrow(Config.PROFILE_ID));
}
} while(Cursor.moveToNext());
}
Expand All @@ -227,7 +223,7 @@ public Profile getProfileById(int id) {
Cursor cursor = null;

try {
cursor = db.query(Config.TABLE_NAME_PROFILE, null, Config.ID + "= ?", new String[]{Integer.toString(id)}, null, null, null);
cursor = db.query(Config.TABLE_NAME_PROFILE, null, Config.PROFILE_ID + "= ?", new String[]{Integer.toString(id)}, null, null, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
Expand Down Expand Up @@ -257,7 +253,7 @@ public String getDeviceCode(int id) {
Cursor cursor = null;

try {
cursor = db.query(Config.TABLE_NAME_PROFILE, null, Config.ID + "= ?", new String[]{Integer.toString(id)}, null, null, null);
cursor = db.query(Config.TABLE_NAME_PROFILE, null, Config.PROFILE_ID + "= ?", new String[]{Integer.toString(id)}, null, null, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
Expand All @@ -280,12 +276,12 @@ public List<Breathalyzer> getAllResults() {
List<Breathalyzer> breathalyzer_values = new ArrayList<>();
SQLiteDatabase userDatabase = this.getReadableDatabase();

Cursor userTableCursor = userDatabase.query(Config.TABLE_NAME, null, null, null, null, null, null);
Cursor userTableCursor = userDatabase.query(Config.TABLE_NAME_SENSOR, null, null, null, null, null, null);
if(userTableCursor != null) {
if(userTableCursor.moveToFirst()) {
do {
String bloodAlcohol = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.Result));
String timeStamp = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.TimeStamp));
String bloodAlcohol = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.SENSOR_RESULT));
String timeStamp = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.TIME_STAMP_SENSOR));
String dayOfWeek = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.DAY_OF_WEEK));
breathalyzer_values.add(new Breathalyzer(bloodAlcohol, String.valueOf(timeStamp), dayOfWeek));

Expand Down Expand Up @@ -353,15 +349,16 @@ else if(TypeofValue == "device_name")
return false;
}

public ArrayList<String> ReturnDrinkTypes(){
ArrayList<String> drink_types = new ArrayList<>();
public ArrayList<Drink> ReturnDrinkTypes(){
ArrayList<Drink> drink_types = new ArrayList<>();
SQLiteDatabase userDatabase = this.getReadableDatabase();
@SuppressLint("Recycle") Cursor userTableCursor = userDatabase.query(Config.TABLE_NAME_DRINK_TYPE, null, null, null, null, null, null);
if(userTableCursor != null) {
if(userTableCursor.moveToFirst()) {
do {
String drink_type = userTableCursor.getString(userTableCursor.getColumnIndexOrThrow(Config.TYPE_OF_DRINK));
drink_types.add(drink_type);
Integer quantity = userTableCursor.getInt(userTableCursor.getColumnIndexOrThrow(Config.DRINK_QUANTITY));
drink_types.add(new Drink(drink_type, quantity));

} while(userTableCursor.moveToNext());
}
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/com/example/drinkingbuddy/Models/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ public class Config {

public static final String DATABASE_NAME = "BreathalyzerResult";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "ResultsTable";
public static final String Result = "_ResultOfMeasurement";
public static final String TimeStamp = "TimeStamp";

public static final String TABLE_NAME_SENSOR = "ResultsTable";
public static final String SENSOR_RESULT = "ResultOfMeasurement";
public static final String TIME_STAMP_SENSOR = "TimeStampSensor";

public static final String TABLE_NAME_PROFILE = "profileTable";
public static final String ID = "_profileId";
public static final String PROFILE_ID = "profileId";
public static final String USERNAME = "profileUser";
public static final String PASSWORD = "profilePassword";
public static final String DEVICE_NAME = "profileDevice";
public static final String DEVICE_CODE = "profileCode";
public static final String DAY_OF_WEEK = "dayOfWeek";

public static final String TYPE_OF_DRINK = "typeOfDrink";
public static final String TABLE_NAME_DRINK_TYPE = "drinkTypeTable";
public static final String DRINK_ID = "drinkId";
public static final String TIME_STAMP_DRINK = "TimeStampDrink";
public static final String TYPE_OF_DRINK = "typeOfDrink";
public static final String DRINK_QUANTITY = "drinkQuantity";
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/example/drinkingbuddy/Models/Drink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.drinkingbuddy.Models;

public class Drink {

protected String drinkName;
protected Integer quantity;

public Drink(String drinkName, Integer quantity) {
this.drinkName = drinkName;
this.quantity = quantity;
}

public String getDrinkName() {
return drinkName;
}

public void setDrinkName(String drinkName) {
this.drinkName = drinkName;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
Loading