Question

Can anyone help me with this lab? Thanks

Exceptions Lab In this lab you will get practice with exceptions by adding them to a class that represents bank accounts. I have provided two files, BankAccount.h and BankAccount.cpp, which have the code for the class itself. Your task is to modify the code to have it throw exceptions under the conditions listed below, then write a main which uses try/catch blocks to display error messages when the bank account is used in ways that trigger exceptions. The bank account should throw exceptions under the following circumstances withdrawing a negative amount depositing a negative amount withdrawing more money than is present in the account setting a negative starting balance . . To practice throwing different kinds of exceptions, when withdrawing or depositing negative amounts of money, the code should throw an integer that is the negative amount. When withdrawing more money than it present the code should throw a string, and when setting a negative starting balance the code should throw a domain_error exception.

The BankAccount.h:

The BankAccount.cpp:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BankAccount.h

#ifndef BANKACCOUNT_H

#define BANKACCOUNT_H

#include <iostream>

using namespace std;

class BankAccount {

public:

BankAccount(string name, double initialBalance);

string getName();

unsigned int getAccountNumber();

double getBalance();

double getInterestRate();

string getStatementString();

void deposit(double amount);

void deposit(double amount, string statement);

void withdraw(double amount);

void withdraw(double amount, string statement);

virtual void printStatement() = 0;

protected:

void addStatementLine(string statement, double amount);

private:

string name;

unsigned int accountNumber;

double balance;

static unsigned int nextAccountNumber;

string statementString;

};

#endif /* BANKACCOUNT_H */

BankAccount.cpp

#include "BankAccount.h"

#include <iostream>

#include <sstream>

#include <iomanip>

using namespace std;

unsigned int BankAccount::nextAccountNumber = 1;

BankAccount::BankAccount(string name, double initialBalance)

{

stringstream output;

this->name = name;

balance = initialBalance;

accountNumber = nextAccountNumber++;

output << setw(60) << left << "Transaction" << setw(10) << "Amount" << " " << "Balance" << endl;

statementString = output.str();

addStatementLine("Initial Deposit", initialBalance);

}

string BankAccount::getName()

{

return name;

}

unsigned int BankAccount::getAccountNumber()

{

return accountNumber;

}

double BankAccount::getBalance()

{

return balance;

}

void BankAccount::addStatementLine(string statement, double amount)

{

stringstream output;

  

output << setw(60) << left << statement << setw(10) << amount << " " << getBalance() << endl;

//.append(statement);

statementString.append(output.str());

}

void BankAccount::deposit(double amount)

{

deposit(amount, "Deposit");

}

void BankAccount::deposit(double amount, string statement)

{

balance += amount;

addStatementLine(statement, amount);

}

void BankAccount::withdraw(double amount)

{

withdraw(amount, "Withdrawal");

}

void BankAccount::withdraw(double amount, string statement)

{

if (balance >= amount)

{

balance -= amount;

addStatementLine(statement, amount);

}

else

{

addStatementLine(statement.append(" Overdraft"), amount);

}

}

string BankAccount::getStatementString()

{

return statementString;

}

Add a comment
Know the answer?
Add Answer to:
Can anyone help me with this lab? Thanks The BankAccount.h: The BankAccount.cpp: Exceptions Lab In this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would I alter this code to have the output to show the exceptions for not...

    How would I alter this code to have the output to show the exceptions for not just the negative starting balance and negative interest rate but a negative deposit as well? Here is the class code for BankAccount: /** * This class simulates a bank account. */ public class BankAccount { private double balance; // Account balance private double interestRate; // Interest rate private double interest; // Interest earned /** * The constructor initializes the balance * and interestRate fields...

  • MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab...

    MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab is as follows: Objectives Familiarize the student with: using exceptions in real programs; the simplification of exception throwing; throwing exceptions in constructors. Scenario IP header – imagine you have a class or struct (choose one) describing an IP header which holds (apart from other fields) two string fields that contain Source IP Address and Destination IP Address. Check these two fields and throw an...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as...

    MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as follows: Objectives Familiarize the student with: situations when exceptions are thrown; handling file-related exceptions. Scenario Write a class that holds a (2×2) matrix, and add two methods to work with files: one method to load the matrix from a file (in any format) and one method to save the matrix to a file (in the same format). Add code to handle exceptional situations (file...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • The program needs to be in python : First, create a BankAccount class. Your class should...

    The program needs to be in python : First, create a BankAccount class. Your class should support the following methods: class BankAccount (object): """Bank Account protected by a pin number.""" def (self, pin) : init "n"Initial account balance is 0 and pin is 'pin'."" self.balance - 0 self.pin pin def deposit (self, pin, amount): """Increment account balance by amount and return new balance.""" def withdraw (self, pin, amount): """Decrement account balance by amount and return amount withdrawn.""" def get balance...

  • For this lab you must write a complete class in C++. This will be a class...

    For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables: 1. accountHolderName : string 2. balance : double 3. interestRate: double This class must have the following public constructor: 1. BancAccount(name : string, balance : double, rate : double) This class must have the following public member functions: 1. getAccountHolderName() : string a. returns the account holders name 2. getBalance() : double a. returns...

  • Please help me with the code. Thank you! Description James Vond has delivered us some intel...

    Please help me with the code. Thank you! Description James Vond has delivered us some intel that the enemy perpetrators have been tricked into going to a bank that we've wired and booby-trapped. However, we have not gotten a bank system in place and you are the only person who is capable of handling such a task. We need this plan to go off without a hitch because the money we'll be receiving will give us insight on the hideout's...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT