![Data structures and algorithms in C++](https://www.bartleby.com/isbn_cover_images/9780470460443/9780470460443_largeCoverImage.jpg)
Concept explainers
Explanation of Solution
Program code:
main.cpp
//include the requird header files
#include <
#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 i = 0; i < 3; i++)
{
//iterate a for loop
for (int j = 1; j <= 16; j++)
{
// explicit cast
wallet[0] -> chargeIt(double(i));
// implicit cast
wallet[1] -> chargeIt(2.0 * i);
wallet[1] -> chargeIt(2.0 * 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.
- Iterate a “for” loop.
- Explicit cast.
- Implicit cast.
- Print the card details.
- Iterate a “while” loop to get the balance.
- Call the method “makepayment()”.
- Print the new balance.
- Delete a wallet.
- Iterate a “for” loop.
- 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)
{
//create a variable
const double interestRate = 0.10;
//if amount less than or equal to 0
if(amount <= 0)
{
//return
return;
}
//set the value of balance
balance -= amount * (1 + interestRate);
}
ostream& operator<<(ostream& out, const CreditCard& c)
{
//print method
out << "Number = " << c.getNumber() << "\n"
<< "Name = " << c.getName() << "\n"
<< "Balance = " << c.getBalance() << "\n"
<< "Limit = " << c.getLimit() << "\n";
return out;
}
Explanation:
In the code,
- Include the required header files.
- Use the namespace.
- Define the constructor “creditCard()”.
- Declare the required variables.
- Define the function “chargeIt()”.
- If the value of the “price” is less than or equal to 0.
- Print an error message that the price must be positive.
- Return a value “false”.
- If the condition is true,
- Return false.
- Add the value of “price” to “balance”.
- Update the value of “fraction”.
- If the condition “fraction > FRACTION_LIMIT” is true.
- Print a warning message.
- Return true.
- If the condition is true,
- If the value of the “price” is less than or equal to 0.
- Define the function “makePayment()”.
- Create a variable “interestrate”.
- If the value of the “amount” is less than or equal to 0.
- Return.
- Set the value of “balance”.
- Return.
- Print the details.
- Enter the credit card details.
- Call the function “getNumber()” and “getName()”,“getBalance()” ,and “getLimit()”.
- Call the function “testCard()”.
- Return “out”.
CreditCard.h
//Code for CreditCard.h:
#ifndef CreditCard_H
#define CreditCard_H
#include<iostream>
//interest rate
#define INTEREST 5
//fraction limit
#define FRACTION_LIMIT 50.00
//define a class
class CreditCard
{
//public access specifier
public:
//class member functions
CreditCard(long long int no, const std::string& nm,
int lim, double bal = 0);
long long int getNumber() const
{
return number;
}
std::string getName() const
{
return name;
}
double getBalance() const
{
return balance;
}
int getLimit() const
{
return limit;
}
// make a charge
bool chargeIt(double price);
// make a payment
void makePayment(double amount);
//private access specifier
private:
//class members
// credit card number
long long int number;
// card owner's name
std::string name;
// the current balance
double balance;
// the credit limit
int limit;
//maintains the fraction
double fraction;
};
// print card information
std::ostream& operator<<(std::ostream& out, const CreditCard& c);
#endif
Explanation:
In the code,
- Include the required header files...
![Check Mark](/static/check-mark.png)
Want to see the full answer?
Check out a sample textbook solution![Blurred answer](/static/blurred-answer.jpg)
Chapter 1 Solutions
Data structures and algorithms in C++
- Consider the following program that counts the number of spaces in a user-supplied string. Modify the program to define and use a function, countSpaces, instead. def main() : userInput = input("Enter a string: ") spaces = 0 for char in userInput : if char == " " : spaces = spaces + 1 print(spaces) main()arrow_forwardWhat is the python code for the function def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Here is a typical usage: salary = readFloat("Please enter your salary:") percentageRaise = readFloat("What percentage raise would you like?")arrow_forwardassume python does not define count method that can be applied to a string to determine the number of occurances of a character within a string. Implement the function numChars that takes a string and a character as arguments and determined and returns how many occurances of the given character occur withing the given stringarrow_forward
- Consider the ER diagram of online sales system above. Based on the diagram answer the questions below, a) Based on the ER Diagram, determine the Foreign Key in the Product Table. Just mention the name of the attribute that could be the Foreign Key. b) Mention the relationship between the Order and Customer Entities. You can use the following: 1:1, 1:M, M:1, 0:1, 1:0, M:0, 0:M c) Is there a direct relationship that exists between Store and Customer entities? Answer Yes/No? d) Which of the 4 Entities mention in the diagram can have a recursive relationship? e) If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type?arrow_forwardNo aiarrow_forwardGiven the dependency diagram of attributes {C1,C2,C3,C4,C5) in a table shown in the following figure, (the primary key attributes are underlined)arrow_forward
- What are 3 design techniques that enable data representations to be effective and engaging? What are some usability considerations when designing data representations? Provide examples or use cases from your professional experience.arrow_forward2D array, Passing Arrays to Methods, Returning an Array from a Method (Ch8) 2. Read-And-Analyze: Given the code below, answer the following questions. 2 1 import java.util.Scanner; 3 public class Array2DPractice { 4 5 6 7 8 9 10 11 12 13 14 15 16 public static void main(String args[]) { 17 } 18 // Get an array from the user int[][] m = getArray(); // Display array elements System.out.println("You provided the following array "+ java.util.Arrays.deepToString(m)); // Display array characteristics int[] r = findCharacteristics(m); System.out.println("The minimum value is: " + r[0]); System.out.println("The maximum value is: " + r[1]); System.out.println("The average is: " + r[2] * 1.0/(m.length * m[0].length)); 19 // Create an array from user input public static int[][] getArray() { 20 21 PASSTR2222322222222222 222323 F F F F 44 // Create a Scanner to read user input Scanner input = new Scanner(System.in); // Ask user to input a number, and grab that number with the Scanner…arrow_forwardGiven the dependency diagram of attributes C1,C2,C3,C4,C5 in a table shown in the following figure, the primary key attributes are underlined Make a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NFarrow_forward
- Consider the ER diagram of online sales system above. Based on the diagram answer the questions below, 1. Based on the ER Diagram, determine the Foreign Key in the Product Table. Just mention the name of the attribute that could be the Foreign Key 2. Is there a direct relationship that exists between Store and Customer entities? AnswerYes/No?arrow_forwardConsider the ER diagram of online sales system above. Based on the diagram answer thequestions below, 1. Mention the relationship between the Order and Customer Entities. You can use the following: 1:1, 1:M, M:1, 0:1, 1:0, M:0, 0:M 2. Which one of the 4 Entities mention in the diagram can have a recursive relationship? 3. If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type (ID or Non-ID, also Justify why)? NO AI use pencil and paperarrow_forwardSTEP 1: The skeleton Let's start by creating a skeleton for some of the classes you will need. • Write a class called Tile. You can think of a tile as a square on the board on which the game will be played. We will come back to this class later. For the moment you can leave it empty while you work on creating classes that represents characters in the game. • Write an abstract class Fighter which has the following private fields: - A Tile field named position, representing the fighter's position in the game. A double field named health, representing the fighter's health points (HP). An int field named weaponType, representing the type of weapon the fighter is using. This value is used to rank different weapon types: higher values indicate higher weapon ranks. -An int field named attackDamage, representing the fighter's attack power. The class must also have the following public methods: 3 A constructor that takes as input a Tile indicating the position of the fighter, a double…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
![Text book image](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)