Data structures and algorithms in C++
Data structures and algorithms in C++
2nd Edition
ISBN: 9780470460443
Author: Goodrich
Publisher: WILEY
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 1, Problem 12R

Explanation of Solution

Program code:

main.cpp

//include the requird header files

#include <vector>

#include "CreditCard.h"

//use the namespace

using namespace std;

//define the function textCard()

void testCard()

{

    // vector of CC pointers

    vector <CreditCard*> wallet(10);

    //create the credit card details

    wallet[0] = new CreditCard(5391037593875309,"JoBlo",2500);

    wallet[1] = new CreditCard(3485039933951954,"JoBlo",3500);

    wallet[2] = new CreditCard(6011490232942994,"JoBlo",5000);

    //iterate a for loop

    for (int j = 1; j <= 16; j++)

    {

        // explicit cast

        wallet[0] -> chargeIt(double(j));

        // implicit cast

        wallet[1] -> chargeIt(2 * j);

        wallet[2] -> chargeIt(double(3 * j));

    }

    //iterate a for loop

    for (int i = 0; i < 3; i++)

    {

        //print the card details

           cout << *wallet[i];

        //iterate a while loop to get the balance

        while (wallet[i]->getBalance() > 100.0)

        {

            //call the method makePayment

            wallet[i]->makePayment(100.0);

            //print the new balance

            cout << "New balance = " <<

            wallet[i]->getBalance() << "\n";

        }

        //deleting a walllet

        delete wallet[i];

    }

}

//define the main() method

int main()

{

    //First test case

    CreditCard CC = CreditCard(1234, "Bill gates", 2500);

    CC.chargeIt(1000);

    CC.chargeIt(-100);

    CC.chargeIt(500);

    CC.makePayment(-100);

    CC.makePayment(300);

    cout << CC << endl << endl;

    //given test case

    testCard();

    return EXIT_SUCCESS;

}

Explanation:

In the code,

  • Include the required header files.
  • Use the namespace.
  • Define the “testCard()” method.
    • Create vector pointers
    • Create array “wallet[]” to store credit card details.
    • Iterate a “for” loop.
      • Explicit cast.
      • Implicit cast.
    • Iterate a “for” loop.
      • Print the card details.
      • Iterate a “while” loop to get the balance.
        • Call the method “makepayment()”.
        • Print the new balance.
          • Delete a wallet.
  • Define the “main()” function.
    • Enter the credit card details.
    • Call the function “chargeIt()” and “makePayment()” using “CC”.
    • Call the function “testCard()”.
    • Return “EXIT_SUCCESS”.

CreditCard.cpp

//Code for CreditCard.cpp

#include "CreditCard.h"

using namespace std;

//define the constructor CreditCard

CreditCard::CreditCard(long long int no, const string& nm, int lim, double bal)

{

    //declare the required variables

    number = no;

    name = nm;

    limit = lim;

    balance = bal;

}

//define a function chargeIt()

bool CreditCard::chargeIt(double price)

{

    if(price <= 0)

    {

        //print error message when price is negative

        cout << "Error: Input argument must be positive.\n";

        //return false

        return false;

    }

    //if the condition is true

    if (price + balance > double(limit))

    {

        //return false

        return false;

    }

    //add price to balance

    balance += price;

    //update fraction

    fraction = double(balance)*100.0/limit;

    //if the condition is true

    if(fraction > FRACTION_LIMIT)

    {

//print warning message when fraction is more than 50%

printf("Warning: the current balance on credit card %lld is %.2f%c of your limit.\n", number, fraction, '%');

    }

    // the charge goes through

    return true;

}

//define the function makePayment

void CreditCard::makePayment(double amount)

{

    //if amount less than or equal to 0

    if(amount <= 0)

    {

        //print error message when amount is negative

        cout << "Error: Input argument must be positive...

Blurred answer
Students have asked these similar questions
(1 point) By dragging statements from the left column to the right column below, give a proof by induction of the following statement: an = = 9" - 1 is a solution to the recurrence relation an = 9an-18 with ao = : 0. The correct proof will use 8 of the statements below. Statements to choose from: Note that a₁ = 9a0 + 8. Now assume that P(n) is true for all n ≥ 0. Your Proof: Put chosen statements in order in this column and press the Submit Answers button. Let P(n) be the predicate, "a = 9″ – 1". απ = 90 − 1 = Note that Let P(n) be the predicate, "an 9" - 1 is a solution to the recurrence relation an = 9an-1 +8 with ao = 0." - Now assume that P(k + 1) is true. Thus P(k) is true for all k. Thus P(k+1) is true. Then ak+1 = 9ak +8, so P(k + 1) is true. = 1 − 1 = 0, as required. Then = 9k — 1. ak Now assume that P(k) is true for an arbitrary integer k ≥ 1. By the recurrence relation, we have ak+1 = ak+1 = = 9ak + 8 = 9(9k − 1) + 8 This simplifies to 9k+19+8 = 9k+1 − 1 Then 9k+1 − 1 = 9(9*…
Given the following request to an AI chatbot, which response is more accurate? (You should use Google to verify that the factual claims in each response are accurate.)   Prompt Is this true? The first recorded use of the phrase "when pigs fly" in print is in a 1616 play by Ben Jonson called "The Devil is an Ass," in which a character says, "When pigs fly, good-night!!" Response   Response A Response B Yes, that's true! The phrase "when pigs fly" indeed appears in Ben Jonson's 1616 play The Devil is an Ass and was said during the second act in a conversation between Fitzdottrel and Pug. The phrase was first used to express skepticism or disbelief, suggesting that something is as unlikely to happen as the fanciful idea of pigs flying. This usage is often cited as one of the earliest instances of the phrase in print.   The claim that the phrase "when pigs fly" was first recorded in Ben Jonson's 1616 play "The Devil is an Ass" is not accurate. There is no evidence from the…
This is for my Computer Organization & Assembly Language Class

Chapter 1 Solutions

Data structures and algorithms in C++

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
What is Abstract Data Types(ADT) in Data Structures ? | with Example; Author: Simple Snippets;https://www.youtube.com/watch?v=n0e27Cpc88E;License: Standard YouTube License, CC-BY