//Create a new customer and return that object to the calling function Customer *Database::createCustomer(string firstName, string lastName, int pin, string emailAddress) { // create a record in the database for the customer // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); //Insert query pStmt->Sql("INSERT INTO Customer (lastName, firstName, emailAddress, PIN) VALUES(?, ?, ?, ?);"); pStmt->BindString(1, lastName); pStmt->BindString(2, firstName); pStmt->BindString(3, emailAddress); pStmt->BindInt(4, pin); // executes the INSERT statement and cleans-up automatically pStmt->ExecuteAndFree(); // get the customer ID (the last row id) pStmt->Sql("SELECT last_insert_rowid();"); pStmt->Execute(); int customerId = pStmt->GetColumnInt(0); // get the int value at the zeroth column pStmt->FreeQuery(); // return the customer to the calling function return new Customer(customerId, firstName, lastName, emailAddress, pin); }
// Account: Create, Update, Retrieve Account *Database::createAccount(int customerId, double balance,string accountStatus, string accountType){ // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); pStmt->Sql("INSERT INTO Account (customerId, balance, accountStatus, accountType) VALUES(?, ?, ?, ?);"); pStmt->BindInt(1, customerId); pStmt->BindDouble(2, balance); pStmt->BindString(3, accountStatus); pStmt->BindString(4, accountType); // executes the INSERT statement and cleans-up automatically pStmt->ExecuteAndFree(); // get the customer ID (the last row id) pStmt->Sql("SELECT last_insert_rowid();"); pStmt->Execute(); int accountId = pStmt->GetColumnInt(0); // get the int value at the zeroth column pStmt->FreeQuery(); // return the account to the calling function Database *dbObject = new Database(); //Write this transaction to the database dbObject->createTransaction(accountId, balance, "Deposit", "Time", "Opening Account Deposit"); //Build and return an account object to the calling function return new Account(accountId, customerId, balance,accountStatus, accountType); }
//Updates account status to closed Account *Database::CloseAccount(int customerId,string time) { // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); Account *acct = getAccount(customerId); int acctId = acct->getAccountNumber(); //Close account by accountId4 pStmt->Sql("UPDATE Account SET accountStatus = ? where accountId = ?"); pStmt->BindString(1, "Closed"); pStmt->BindInt(2, acctId); pStmt->Execute(); pStmt->FreeQuery(); //Close out Account, set Balance to Zero. Database::createTransaction(acctId, acct->getAccountBalance(), "Withdraw", time, "Account Closed"); Database::updateAccount(0, acctId); //Uses getAccount to return the updated customer object return getAccount(customerId); }
Transaction *Database::createTransaction(int accountId, double amount, string tType, string tDate, string tDescript){ //Open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); //Build new transaction query pStmt->Sql("INSERT INTO Transactions1 (accountId, amount, transactionType, transactionDate, transactionDescription) VALUES(?, ?, ?, ?, ?);"); pStmt->BindInt(1, accountId); pStmt->BindDouble(2, amount); pStmt->BindString(3, tType); pStmt->BindString(4, tDate); pStmt->BindString(5, tDescript); // executes the INSERT statement and cleans-up automatically pStmt->ExecuteAndFree(); return new Transaction(); }
//Return a customer from the database based on email address Customer *Database::getCustomer(string email) { // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); //Query the database for a customer with this specific email address pStmt->Sql("SELECT customerId, firstName, lastName, emailAddress, PIN FROM Customer where emailAddress = ?"); pStmt->BindString(1, email); pStmt->Execute(); //Parse customer attributes from returned query int customerId = pStmt->GetColumnInt(0); string firstName = pStmt->GetColumnString(1); string lastName = pStmt->GetColumnString(2); string emailAddress = pStmt->GetColumnString(3); int pin = pStmt->GetColumnInt(4); pStmt->FreeQuery(); //Build and return a customer object return new Customer(customerId, firstName, lastName, emailAddress, pin); }