Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Implement a base class, BankAccount , with two derived classes: BuyerAccount and RetailerAccount . The BuyerAccount will represent the account of someone who

C++ Programming

Implement a base class, BankAccount, with two derived classes: BuyerAccount and RetailerAccount. The BuyerAccount will represent the account of someone who can make purchases. The RetailerAccount will represent the account of some sort of seller of a product.

The BankAccount will provide the basic necessities of an account. It has a protected member balance, which will represent an integer number of dollars (ignoring cents to keep it simple), and a private string name. The name length should be between 2 and 50 characters, and if a name hasn't been set, the object should default to "(undefined)". Neither the balance nor any of the values passed to methods should be below 0. Instead of a setBalance, we use a withdraw and a deposit. Both must check if the value passed is valid, and the withdraw must also make sure there is enough in the account to go through with the withdrawal. The class also has a toString() method that prints the name and balance as shown in the run below.

The BuyerAccount will inherit the BankAccount class. We will also include its own constructor (here's a good opportunity to practice constructor chaining), an overloaded toString() method that will print similarly to the BankAccount toString() but specify that this is a Buyer. We also introduce the bool purchase(int price, RetailerAccount& seller) method. This allows the buyer object to "purchase" from another account: it will withdraw from the buyer's account and deposit the same value into the seller's account. Notice that we have passed a RetailerAccount by reference. Remember this allows us to make lasting changes to that RetailerAccount object. Here's the prototype for the BankAccount and BuyerAccount. You can add whatever static constants you feel are necessary:

#include  #include  #include  using namespace std; // prototypes ---------------------------------------------------- class BankAccount { public: BankAccount(int balance = MIN_BALANCE, string name = DEFAULT_NAME); string getName() const; int getBalance() const; bool setName(const string& name); bool deposit(int amount); bool withdraw(int amount); string toString(); protected: int balance; bool isValidName(const string& name); bool isValidAmount(int amount); private: string name; }; class RetailerAccount; //This is a forward declaration, which allows us to call // an instance of this class in BuyerAccount before we have declared the // RetailerAccount class, whose prototype you should provide AFTER the BuyerAccount // prototype class BuyerAccount : public BankAccount { public: BuyerAccount(int balance = MIN_BALANCE, string name = DEFAULT_NAME); bool purchase(int price, RetailerAccount& seller); string toString(); }; // Put your RetailerAccount prototype here // ------------------------------------------------------------

From this, you should be able to write the prototype of the RetailerAccount class, which will also be derived from BankAccount. This class has an additional private data member, string product, which will hold the name of the product this retailer sells. It will contain the following methods: -- RetailerAccount(int balance = MIN_BALANCE, string name = DEFAULT_NAME, string product = DEFAULT NAME): a constructor for initializing the data members -- string getProduct(): return the name of the product -- bool setProduct(string product): If the length of the product name is between 2 and 50 characters, change it, otherwise return false without changing anything. -- bool sale(int price, BuyerAccount& buyer): This functions as the RetailerAccount's version of the BuyerAccount's purchase() method. If the price is valid and the buyer can afford the sale, this method will deposit to the RetailerAccount and withdraw from the BuyerAccount. (Would we be able to do that if the BuyerAccount was not passed by reference?) -- string toString(): This will print the RetailerAccount's name, what they sell, and their balance.

Here is a sample client with the expected output. This does not cover all cases, so make sure to test your edge cases.

BankAccount b1(35); cout << b1.getName() << endl; cout << b1.getBalance() << endl; b1.setName("Carlos"); b1.deposit(15); b1.withdraw(5); cout << b1.toString() << endl << endl; BuyerAccount buyer(500, "Phuong"); cout << buyer.toString() << endl << endl; RetailerAccount retailer(0, "Chocolate Shop", "chocolate"); cout << retailer.toString() << endl << endl; buyer.purchase(50, retailer); cout << "After a purchase: " << endl; cout << buyer.toString() << endl; cout << retailer.toString() << endl << endl; retailer.sale(100, buyer); cout << "After a sale: " << endl; cout << buyer.toString() << endl; cout << retailer.toString() << endl << endl; buyer.withdraw(350); cout << "After withdrawal: " << endl; cout << buyer.toString() << endl << endl; retailer.sale(50, buyer); cout << "After attempted sale: " << endl; cout << buyer.toString() << endl; cout << retailer.toString() << endl; /* ---------------- output ----------------------------- (undefined) 35 My name is Carlos and my balance is 45 dollars. Buyer: My name is Phuong and my balance is 500 dollars. Retailer: The name of our business is Chocolate Shop, we sell chocolate and our balance is 0 dollars. After a purchase: Buyer: My name is Phuong and my balance is 450 dollars. Retailer: The name of our business is Chocolate Shop, we sell chocolate and our balance is 50 dollars. After a sale: Buyer: My name is Phuong and my balance is 350 dollars. Retailer: The name of our business is Chocolate Shop, we sell chocolate and our balance is 150 dollars. After withdrawal: Buyer: My name is Phuong and my balance is 0 dollars. After attempted sale: Buyer: My name is Phuong and my balance is 0 dollars. Retailer: The name of our business is Chocolate Shop, we sell chocolate and our balance is 150 dollars. */ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions

Question

Find 5 stereochemistry examples with R or S. Show work !!

Answered: 1 week ago