Commit c6de924c authored by bryan.quispe's avatar bryan.quispe

Initial commit

parent 079cac85
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
android:theme="@style/Theme.MyApplication" android:theme="@style/Theme.MyApplication"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".LoginActivity" android:name=".HomeActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
......
package com.example.myapplication; package com.example.myapplication;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
...@@ -11,61 +13,86 @@ import java.util.List; ...@@ -11,61 +13,86 @@ import java.util.List;
public class Database extends SQLiteOpenHelper { public class Database extends SQLiteOpenHelper {
// Users table
private static final String TABLE_USERS = "Users";
private static final String COLUMN_UID = "uid";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
// Booking table
private static final String TABLE_BOOKING = "Booking";
private static final String COLUMN_EVENT_ID = "eventID";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_GENRE = "genre";
private static final String COLUMN_TICKET_COUNT = "ticketCount";
// Database constants
private static final String DB_NAME = "MyApp.db"; private static final String DB_NAME = "MyApp.db";
private static final int DB_VERSION = 2; private static final int DB_VERSION = 2;
private Context context;
public Database(Context context) { public Database(Context context) {
super(context, DB_NAME, null, DB_VERSION); super(context, DB_NAME, null, DB_VERSION);
this.context = context;
} }
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
createUsersTable(db); // Create Users table
createBookingTable(db); String createUsersTable = "CREATE TABLE " + TABLE_USERS + "(" +
} COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
private void createUsersTable(SQLiteDatabase db) { COLUMN_USERNAME + " TEXT, " +
String createUsersTable = "CREATE TABLE Users (" + COLUMN_PASSWORD + " TEXT)";
"uid INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"username TEXT, " +
"password TEXT)";
db.execSQL(createUsersTable); db.execSQL(createUsersTable);
}
private void createBookingTable(SQLiteDatabase db) { // Create Booking table
String createBookingTable = "CREATE TABLE Booking (" + String createBookingTable = "CREATE TABLE " + TABLE_BOOKING + "(" +
"eventID TEXT, " + COLUMN_EVENT_ID + " TEXT, " +
"title TEXT, " + COLUMN_TITLE + " TEXT, " +
"genre TEXT, " + COLUMN_GENRE + " TEXT, " +
"ticketCount INTEGER, " + COLUMN_TICKET_COUNT + " INTEGER, " +
"PRIMARY KEY (eventID))"; "PRIMARY KEY (" + COLUMN_EVENT_ID + "))";
db.execSQL(createBookingTable); db.execSQL(createBookingTable);
} }
@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 Booking"); // Drop the existing Booking table
createBookingTable(db); db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKING);
// Create a new Booking table with the updated schema
String createBookingTable = "CREATE TABLE " + TABLE_BOOKING + "(" +
COLUMN_EVENT_ID + " TEXT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_GENRE + " TEXT, " +
COLUMN_TICKET_COUNT + " INTEGER, " +
"PRIMARY KEY (" + COLUMN_EVENT_ID + "))";
db.execSQL(createBookingTable);
} }
public boolean addNewUser(String name, String username, String password) {
SQLiteDatabase db = getWritableDatabase(); // User methods
public void addNewUser(String name, String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("name", name); values.put(COLUMN_NAME, name);
values.put("username", username); values.put(COLUMN_USERNAME, username);
values.put("password", password); values.put(COLUMN_PASSWORD, password);
db.insert("Users", null, values); db.insert(TABLE_USERS, null, values);
db.close(); db.close();
return false;
} }
public boolean checkLogin(String username, String password) { public boolean checkLogin(String username, String password) {
SQLiteDatabase db = getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {"username"}; String query = "SELECT * FROM " + TABLE_USERS +
String selection = "username = ? AND password = ?"; " WHERE " + COLUMN_USERNAME + " = ? AND " + COLUMN_PASSWORD + " = ?";
String[] selectionArgs = {username, password}; Cursor cursor = db.rawQuery(query, new String[]{username, password});
Cursor cursor = db.query("Users", columns, selection, selectionArgs, null, null, null);
boolean result = cursor.getCount() > 0; boolean result = cursor.getCount() > 0;
cursor.close(); cursor.close();
db.close(); db.close();
...@@ -73,87 +100,103 @@ public class Database extends SQLiteOpenHelper { ...@@ -73,87 +100,103 @@ public class Database extends SQLiteOpenHelper {
} }
public boolean checkUsernameExists(String username) { public boolean checkUsernameExists(String username) {
SQLiteDatabase db = getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {"username"}; String query = "SELECT * FROM " + TABLE_USERS +
String selection = "username = ?"; " WHERE " + COLUMN_USERNAME + " = ?";
String[] selectionArgs = {username}; Cursor cursor = db.rawQuery(query, new String[]{username});
Cursor cursor = db.query("Users", columns, selection, selectionArgs, null, null, null);
boolean exists = cursor.getCount() > 0; boolean exists = cursor.getCount() > 0;
cursor.close(); cursor.close();
db.close(); db.close();
return exists; return exists;
} }
public void addBooking(String eventID, String title, String genre, int ticketCount) { // Booking methods
SQLiteDatabase db = getWritableDatabase();
public void addBooking(String eventID, String genre, String title, int ticketCount) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("eventID", eventID); values.put(COLUMN_EVENT_ID, eventID);
values.put("title", title); values.put(COLUMN_TITLE, title);
values.put("genre", genre); values.put(COLUMN_GENRE, genre);
values.put("ticketCount", ticketCount); values.put(COLUMN_TICKET_COUNT, ticketCount);
long rowsAffected = db.replace("Booking", null, values);
if (rowsAffected == -1) {
db.insert("Booking", null, values);
}
db.close();
}
public List<String> searchBooking() { int rowsAffected = db.update(TABLE_BOOKING, values, COLUMN_EVENT_ID + " = ?", new String[]{eventID});
SQLiteDatabase db = getReadableDatabase();
String[] columns = {"eventID", "title", "genre"};
Cursor cursor = db.query("Booking", columns, null, null, null, null, null);
List<String> bookingsList = new ArrayList<>();
while (cursor.moveToNext()) { if (rowsAffected == 0) {
String eventID = cursor.getString(cursor.getColumnIndex("eventID")); // No existing row found with the provided eventID
String title = cursor.getString(cursor.getColumnIndex("title")); db.insert(TABLE_BOOKING, null, values);
String genre = cursor.getString(cursor.getColumnIndex("genre")); }
String booking = "Event ID: " + eventID + "\n" + db.close();
"Title: " + title + "\n" + }
"Genre: " + genre + "\n\n";
bookingsList.add(booking); public String searchBooking(String eventID) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_BOOKING +
" WHERE " + COLUMN_EVENT_ID + " = ?";
Cursor cursor = db.rawQuery(query, new String[]{eventID});
String result = null;
if (cursor.moveToFirst()) {
result = "Event ID: " + cursor.getString(cursor.getColumnIndex(COLUMN_EVENT_ID)) + "\n" +
"Title: " + cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)) + "\n" +
"Genre: " + cursor.getString(cursor.getColumnIndex(COLUMN_GENRE));
} }
cursor.close();
db.close();
return result;
}
public ArrayList<String> getAllBookings() {
ArrayList<String> bookingsList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_BOOKING;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String bookingInfo = "Event ID: " + cursor.getString(cursor.getColumnIndex(COLUMN_EVENT_ID)) + "\n" +
"Title: " + cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)) + "\n" +
"Genre: " + cursor.getString(cursor.getColumnIndex(COLUMN_GENRE));
bookingsList.add(bookingInfo);
} while (cursor.moveToNext());
}
cursor.close(); cursor.close();
db.close(); db.close();
return bookingsList; return bookingsList;
} }
public List<String> getTicketsForEvent(String eventID) {
public void deleteBooking(String eventID) { List<String> ticketsList = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String whereClause = "eventID = ?"; String query = "SELECT * FROM " + TABLE_BOOKING +
String[] whereArgs = {eventID}; " WHERE " + COLUMN_EVENT_ID + " = ?";
db.delete("Booking", whereClause, whereArgs); Cursor cursor = db.rawQuery(query, new String[]{eventID});
if (cursor.moveToFirst()) {
do {
String ticketInfo = "Event ID: " + cursor.getString(cursor.getColumnIndex(COLUMN_EVENT_ID)) + "\n" +
"Title: " + cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)) + "\n" +
"Genre: " + cursor.getString(cursor.getColumnIndex(COLUMN_GENRE)) + "\n" +
"Ticket Count: " + cursor.getInt(cursor.getColumnIndex(COLUMN_TICKET_COUNT));
ticketsList.add(ticketInfo);
} while (cursor.moveToNext());
}
cursor.close();
db.close(); db.close();
return ticketsList;
} }
public String getTicketsForEvent(String eventID) { public void updateBooking(String eventID, String newTitle, String newGenre) {
SQLiteDatabase db = getReadableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
String[] columns = {"eventID", "title", "genre", "ticketCount"}; ContentValues values = new ContentValues();
String selection = "eventID = ?"; values.put(COLUMN_TITLE, newTitle);
String[] selectionArgs = {eventID}; values.put(COLUMN_GENRE, newGenre);
Cursor cursor = db.query("Booking", columns, selection, selectionArgs, null, null, null); db.update(TABLE_BOOKING, values, COLUMN_EVENT_ID + " = ?", new String[]{eventID});
db.close();
}
if (cursor.moveToFirst()) { public void deleteBooking(String eventID) {
String title = cursor.getString(cursor.getColumnIndex("title")); SQLiteDatabase db = this.getWritableDatabase();
String genre = cursor.getString(cursor.getColumnIndex("genre")); db.delete(TABLE_BOOKING, COLUMN_EVENT_ID + " = ?", new String[]{eventID});
int ticketCount = cursor.getInt(cursor.getColumnIndex("ticketCount")); db.close();
String tickets = "Event ID: " + eventID + "\n" +
"Title: " + title + "\n" +
"Genre: " + genre + "\n" +
"Ticket Count: " + ticketCount;
cursor.close();
db.close();
return tickets;
} else {
cursor.close();
db.close();
return null;
}
} }
}
}
package com.example.myapplication; package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
...@@ -19,7 +20,9 @@ public class HomeActivity extends AppCompatActivity { ...@@ -19,7 +20,9 @@ public class HomeActivity extends AppCompatActivity {
private Database dbConnector; private Database dbConnector;
private List<String> bookingsList; // List to store all bookings private List<String> bookingsList; // List to store all bookings
private int ticketCount;
@SuppressLint("MissingInflatedId")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -35,6 +38,7 @@ public class HomeActivity extends AppCompatActivity { ...@@ -35,6 +38,7 @@ public class HomeActivity extends AppCompatActivity {
Button addButton = findViewById(R.id.addButton); Button addButton = findViewById(R.id.addButton);
Button searchButton = findViewById(R.id.searchButton); Button searchButton = findViewById(R.id.searchButton);
Button showAllButton = findViewById(R.id.showAllButton); Button showAllButton = findViewById(R.id.showAllButton);
Button updateButton = findViewById(R.id.updateButton);
Button deleteButton = findViewById(R.id.deleteButton); Button deleteButton = findViewById(R.id.deleteButton);
// Initialize the bookings list // Initialize the bookings list
...@@ -44,13 +48,13 @@ public class HomeActivity extends AppCompatActivity { ...@@ -44,13 +48,13 @@ public class HomeActivity extends AppCompatActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
String eventID = eventIDEditText.getText().toString(); String eventID = eventIDEditText.getText().toString();
String title = titleEditText.getText().toString(); String title = genreEditText.getText().toString(); // Swap genreEditText and titleEditText
String genre = genreEditText.getText().toString(); String genre = titleEditText.getText().toString(); // Swap titleEditText and genreEditText
if (eventID.isEmpty() || title.isEmpty() || genre.isEmpty()) { if (eventID.isEmpty() || title.isEmpty() || genre.isEmpty()) {
Toast.makeText(HomeActivity.this, "Please fill in all fields", Toast.LENGTH_SHORT).show(); Toast.makeText(HomeActivity.this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
} else { } else {
dbConnector.addBooking(eventID, title, genre, 0); dbConnector.addBooking(eventID, title, genre, getTicketCount());
Toast.makeText(HomeActivity.this, "Booking added successfully", Toast.LENGTH_SHORT).show(); Toast.makeText(HomeActivity.this, "Booking added successfully", Toast.LENGTH_SHORT).show();
// Clear the input fields after adding the booking // Clear the input fields after adding the booking
...@@ -76,7 +80,7 @@ public class HomeActivity extends AppCompatActivity { ...@@ -76,7 +80,7 @@ public class HomeActivity extends AppCompatActivity {
showAllButton.setOnClickListener(new View.OnClickListener() { showAllButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
bookingsList = dbConnector.searchBooking(); bookingsList = dbConnector.getAllBookings();
if (bookingsList.isEmpty()) { if (bookingsList.isEmpty()) {
resultTextView.setText("No bookings available"); resultTextView.setText("No bookings available");
} else { } else {
...@@ -89,6 +93,22 @@ public class HomeActivity extends AppCompatActivity { ...@@ -89,6 +93,22 @@ public class HomeActivity extends AppCompatActivity {
} }
}); });
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String eventID = eventIDEditText.getText().toString();
String newTitle = titleEditText.getText().toString();
String newGenre = genreEditText.getText().toString();
dbConnector.updateBooking(eventID, newTitle, newGenre);
Toast.makeText(HomeActivity.this, "Booking updated successfully", Toast.LENGTH_SHORT).show();
// Clear the input fields after updating the booking
eventIDEditText.setText("");
titleEditText.setText("");
genreEditText.setText("");
}
});
deleteButton.setOnClickListener(new View.OnClickListener() { deleteButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
...@@ -100,14 +120,49 @@ public class HomeActivity extends AppCompatActivity { ...@@ -100,14 +120,49 @@ public class HomeActivity extends AppCompatActivity {
eventIDEditText.setText(""); eventIDEditText.setText("");
} }
}); });
EditText filterEditText = findViewById(R.id.filterEditText);
Button filterButton = findViewById(R.id.filterButton);
filterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String keyword = filterEditText.getText().toString();
filterBookings(keyword);
}
});
} }
// Additional method to handle filter/search functionality
private void filterBookings(String keyword) {
List<String> filteredList = new ArrayList<>();
for (String booking : bookingsList) {
// Check if the booking genre contains the keyword
if (booking.toLowerCase().contains(keyword.toLowerCase())) {
filteredList.add(booking);
}
}
if (filteredList.isEmpty()) {
resultTextView.setText("No matching bookings found");
} else {
StringBuilder sb = new StringBuilder();
for (String booking : filteredList) {
sb.append(booking).append("\n\n");
}
resultTextView.setText(sb.toString());
}
}
private void displayTicketsForEvent(String eventID) { private void displayTicketsForEvent(String eventID) {
String tickets = dbConnector.getTicketsForEvent(eventID); List<String> ticketsList = dbConnector.getTicketsForEvent(eventID);
if (tickets == null) { if (ticketsList.isEmpty()) {
resultTextView.setText("No tickets available for this event"); resultTextView.setText("No tickets available for this event");
} else { } else {
resultTextView.setText(tickets); StringBuilder sb = new StringBuilder();
for (String ticket : ticketsList) {
sb.append(ticket).append("\n\n");
}
resultTextView.setText(sb.toString());
} }
} }
...@@ -116,4 +171,13 @@ public class HomeActivity extends AppCompatActivity { ...@@ -116,4 +171,13 @@ public class HomeActivity extends AppCompatActivity {
super.onDestroy(); super.onDestroy();
dbConnector.close(); dbConnector.close();
} }
public void setTicketCount(int ticketCount) {
this.ticketCount = ticketCount;
}
public int getTicketCount() {
return ticketCount;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment