Question

I have updated my previously posted C++ question to ensure that I have included all of...

I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that follow. I hope that this helps! Thank you!!!

Create a Bank Account program in C++ that meets the following requirements. Be sure to write unit tests for each public method before you implement it! Also, remember to include an interactive demo under the filename account_demo.cpp.

C++ - Pointers & Dynamic Memory Allocation:

1 Introduction
The functions in the following subsections can all go in one big file called pointer practice.cpp.

1.1 Basics
• Write a function, int square_1( int* p), that takes a pointer to an int and returns the square of the int that it points to.
• Write a function, void square_2( int* p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its square.
• Write a function, void square_3( int& p), that takes an int by reference and replaces it with its square.
• Write a function void display_square () that reads a number from the user and displays its square three times, once using each of the above functions. • Write a main that calls display_square as a simple “test”.

1.2 Arrays
• Write a function, void replace_with_increment ( int* p, size_t n) that takes the base pointer of an array of size n as an argument and increments each of the items in the array.
• Write a function, void display_increment_array (), that makes an array with 10, 20, and 30 in it, passes it to replace_with_increment, and prints its elements after that call. Modify main to call this function and test it.

1.3 Dynamic Allocation
• Write a function, counter* make_broken_counter(), that creates a counter as a local variable and returns its address. Compile your program with all warnings enabled (-Wall for command line folks, otherwise look in compiler settings in your IDE). Note the warnings/errors.
• Write a function, void display_broken_counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesn’t matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens?
• Write a function, counter* make_counter(), that creates a counter using new and returns the resulting pointer.

• Write a function, void display_counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesn’t matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens?
• Write a function, counter* make_counter (int start), that creates a counter using new, passing start to its constructor, and returns the resulting pointer.
• Write a function, void display_counter_2 (), that calls this function and displays the resulting counter’s count. Modify main to call this function...
• Write a function, counter* bunch_of_counters (size_t number), that uses new to create an array of number counters. Return the base pointer of the array.
• Write a function, void display_counters (), that uses the function above to make an array of 25 counters, increments them each 5 times and then prints each of their counts. Modify main to call this function.

** NOTE: Keep all previously written code, but comment the code out of the program using //, or /* and */, before beginning the following section of the lab!! The exercises in the previous section are designed to help you become familiar with pointers & dynamically allocated memory. REMEMBER to save your code from this section & submit it with the rest of your lab assignment!!!!!

2 Bank Account
In the following subsections you will create an account class that manages a dynamically-sized collection of transactions. As we have not learned about dealing with some aspects of dynamic memory allocation in classes, your code will leak memory. Do not use this class as a reference for future projects (please put a note in it saying just
that)
. The goal is to learn the basics before cluttering your mind and code with C++-isms.

2.1 The transaction class
Create a class called transaction with the following instance variables:
• int amount
• string type
Create a two-argument constructor that allows the caller to specify values for all of these variables. Create (const) “getter” methods for each of these variables.

2.2 Test first!
Remember to write unit tests for each public method before you implement it.

2.3 The account class basics
Create an account class with the following instance variables:
int balance – balance in cents
• size_t transactions_cap – capacity of transactions list

• size_t transactions_size – number of “used” slots in transaction array
• transaction ** transactions – base pointer to dynamically allocated array of pointers to transactions

Write a constructor that initializes balance to zero, transactions size to zero, transactions cap to 30 and dynamically allocates a collection of transaction cap transaction pointers.

Write methods deposit and withdraw that simply update the balance variable for now. Include assertions to make sure that the amount deposited/withdrawn was valid.

2.4 Managing the transactions array

Implement the following private methods:
void add_transaction ( transaction *t ) that:
– Checks if there is enough room in the transactions array and, if not, calls resize_transactions ()
– Adds the transaction *t to the “end” of the partially filled transactions array.
– Increments transactions_size
void resize_transactions() – doubles the capacity of the transactions array, copying old elements over to the new array and updating instance variables as appropriate.

Implement the following public methods:
size_t get_transactions_size() const – returns the number of elements in the transactions array
transaction * get_transaction (size_t i) const – (Precondition: i < transactions cap ) returns the transaction at index i in the transactions collect

Modify withdraw and deposit so that they create transaction objects describing the transaction and add them to the transaction list (use type ” deposit ” or ”withdraw” as appropriate). Add unit tests for withdraw and deposit to make sure that these transactions are created and that they contain the correct data.

2.5 Interactive demo
Create a demo that works as the example shows below.
---
Current balance: 0
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: d
amount to deposit: 1000
---
Current balance: 1000
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v
There are 1 transactions. Give transaction you wish to view: 1
Transaction 1: amount = 1000, type = deposit
---
Current balance: 1000
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: w
amount to withdraw: 50
---
Current balance: 950
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v
There are 2 transactions. Give transaction you wish to view: 9
Invalid transaction number. Enter a number between 1 and 2.
There are 2 transactions. Give transaction you wish to view: 2
Transaction 2: amount = 50, type = withdraw
---
Current balance: 950
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: x
Invalid command.
---
Current balance: 950
Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: e
Bye!

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

bank_demo.cpp

#include <iostream>
#include "account.h"
#include "transaction.h"
using namespace std;
using namespace csis3700;
int main(void){
string action;
int amount, count=0, index;
account a;
bool end =false;
do{
    cout << "Enter the action you want to perform: deposit, withdraw, view transaction, exit: ";
    cin >> action;
    if (action == "deposit"){
      cout << "Please enter the amount: ";
      cin >> amount;
      while(amount <= 0){
   cout << "Please input a number greater than 0 or -1 to quit: ";
   cin >> amount;
   if(amount == -1){
      return 0;
   }
      }
      count++;
      cout << "Transaction " << count << " type: " << action << " amount: " << amount << endl;
      a.deposit(amount);
      cout << "Balance: " << a.get_balance() << endl;
  
      }
    else if (action =="withdraw"){
      cout << "Please enter the amount: ";
      cin >> amount;
      while(amount==0 || amount > a.get_balance()){
   cout << "The amount to withdraw should be between zero and should not exceed your current balance. Please enter your amount again or -1 to quit: ";
   cin >> amount;
   if (amount == -1){
      return 0;
   }
      }
      count++;
      cout << "Transaction " << count << " type: " << action << " amount: " << amount << endl;
      a.withdraw(amount);
      cout << "Balance: " << a.get_balance() << endl;
    }
    else if (action == "view"){
      cout << "There are " << count << " transactions. Which transaction do you want to view? Just enter the number: ";
      cin >> index;
      transaction *t = a.get_transaction(index-1);
      cout << "Transaction " << index << " amount: " << t -> get_amount() << " type: " << t -> get_type() <<endl;
     }
    else if (action == "exit"){
      end=true;
    }
}while(!end);
cout << "Bye !" <<endl;
return 0;
}
  

account.cpp

#include "account.h"
#include "transaction.h"
using namespace std;
namespace csis3700{
account::account(){
    balance =0;
    transactions_size=0;
    transactions_cap=30;
    transactions = new transaction*[transactions_cap];
}
void account::deposit(int a){
    transaction *t = new transaction(a, "deposit");
    add_transaction(t);
    balance+=a;
}
void account::withdraw(int b){
    transaction *t = new transaction(b, "withdraw");
    add_transaction(t);
    if(b <= balance){
      balance-=b;
    }
}
void account::add_transaction(transaction *t){
   if(get_transactions_size() == transactions_cap){
       resize_transactions();
   }
              transactions[get_transactions_size()] = t;
       transactions_size++;
}
void account::resize_transactions(){
   transactions_cap *= 2;
   transaction** transactions_2 = new transaction*[transactions_cap];
   for(int i =0; i < int(get_transactions_size()); i++){
       transactions_2[i] = transactions[i];
   }
   delete transactions;
   transactions = transactions_2;
}
size_t account::get_transactions_size() const{
   return transactions_size;
}
transaction* account::get_transaction(size_t i) const{
   return transactions[i];
}
int account::get_balance(){
    return balance;
}
}

  
account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "transaction.h"
namespace csis3700{
   class account{
       public:
          
                    account();
           void deposit(int b);
           void withdraw(int b);
           size_t get_transactions_size() const;
           transaction* get_transaction(size_t i) const;
           int get_balance();
       private:
           int balance;
           size_t transactions_cap;
           size_t transactions_size;
           transaction** transactions;
           void add_transaction(transaction *t);
           void resize_transactions();
          
   };  
}
#endif

transaction.cpp

#include "transaction.h"
#include <cstring>
using namespace std;
namespace csis3700{
transaction::transaction(int a, string t){
    amount = a;
    type = t;
}
int transaction::get_amount() const{
    return amount;
}
string transaction::get_type() const{
    return type;
}
}

transaction.h

#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <iostream>
#include <cstring>
using namespace std;
namespace csis3700{
   class transaction{
       public:
           transaction(int a, string t);
           int get_amount() const;
           string get_type() const;
       private:
           int amount;
           string type;
   };  
}
#endif

Default Term +Browser sh-4.2$ gtt o main *.cpp |sh-4.25 main Enter the action you want to perform: deposit, withdraw, view tr

Add a comment
Know the answer?
Add Answer to:
I have updated my previously posted C++ question to ensure that I have included all of...
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
  • Design a bank account class named Account that has the following private member variables:

    Need help please!.. C++ programDesign a bank account class named Account that has the following private member variables: accountNumber of type int ownerName of type string balance of type double transactionHistory of type pointer to Transaction structure (structure is defined below) numberTransactions of type int totalNetDeposits of type static double.The totalNetDeposits is the cumulative sum of all deposits (at account creation and at deposits) minus the cumulative sum of all withdrawals. numberAccounts of type static intand the following public member...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below....

    I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below. If you could leave comments pointing out where pointers are used it would be much appreciated! ----------------------------------------------------------------------------------------------------------------------------------- Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, howeve...

    In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, however the image has been distorted. The famous object is in the red values, however the red values have all been divided by 10, so they are too small by a factor of 10. The blue and green values are all just meaningless random values ("noise") added to obscure the real image. If you were to create a grayscale image out of just the red...

  • Here's an assignment that'll give you some practice with pointers. All you have to do is...

    Here's an assignment that'll give you some practice with pointers. All you have to do is write a program that allows the user to populate an array of doubles (5 values) by calling the function InitArray. After the user has entered all the values, then call the function DispRevArray to display the array in reverse. Main will create and allocate space for an array of type double for 5 elements. Then you will create and allocate a pointer that will...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only....

    Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only. Thank you. Pointers and Dynamic Memory – Chapter 11 1. Write code using pointers and the Accounts class from week 1 assignment as follows – create a program in which you a. Create 10 accounts using an array with id’s 0 to 9 and refer to each account using pointer notation b. Add an initial balance of...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

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