this should be done in c++ Target gift card tracker Your task is to write a simp
ID: 3756772 • Letter: T
Question
this should be done in c++
Target gift card tracker
Your task is to write a simple app that allows me to keep track of my gift cards from target store. Here are the features of this program:
Feature #1:dd a gift card: I should be able to add a gift card to the program. I will enter a gift card code which is a number. The value of the gift card (the amount of money in it) can be determined by this number.
If the card code is above (or equal) 50000. Then it has $5 value in it. the cards with code at 40000s (4000 to 49999) has $4 in it. the cards with code at 30000s (30000 to 39999) has $3 and so on all the way down to 10000 (10000 to 19999) has $1 in it. any code less than 10000 is invalid code number – let user know with a message.
Feature #2: remove a gift card: sometimes, I may give my gift card away or spend it, in which case, I need to be able to remove the card from the system. I will enter the card code and you will be determined what value the card has from the code (just like in feature #1) and remove from the system.
Feature #3: report: I should be able to get a detailed report from the program displaying how many of each type cards ($1, $2, $3, $4 or $5 cards) I have and the total number of cards and as well as the average value of cards. This report changes as I add/ remove cards from the system.
Feature #0: I should be able to exit out of the program when I am done.
User will pick one of the 4 features—if user makes an invalid choice (enters a number other than these options), let the user know that it is an invalid entry
Explanation / Answer
#include <iostream>
#include <cstdlib>
#define MAX 100
using namespace std;
// Defines class GiftCard
class GiftCard
{
// Data member to store card information
int cardNo;
double price;
public:
// Default constructor definition
GiftCard()
{
cardNo = 0;
price = 0.0;
}// End of default constructor
// Method to generate a card
void generateCard(int no)
{
// Assigns parameter number to data member cardNo
cardNo = no;
// Calls the function to set the price
foundPrice(no);
}// End of function
// Function to return card number
int getCardNo()
{
return cardNo;
}// End of function
// Function to return price
double getPrice()
{
return price;
}// End of function
// Function to set price
double foundPrice(int no)
{
// Checks if card number is greater than or equals to 50000
if(no >= 50000)
// Assigns 5 to price
price = 5;
// Otherwise checks if card number is greater than or equals to 40000
else if(no >= 40000)
// Assigns 4 to price
price = 4;
// Otherwise checks if card number is greater than or equals to 30000
else if(no >= 30000)
// Assigns 3 to price
price = 3;
// Otherwise checks if card number is greater than or equals to 20000
else if(no >= 20000)
// Assigns 2 to price
price = 2;
// Otherwise card number is less than 20000
else
// Assigns 1 to price
price = 1;
}// End of function
// Function to display card information
void displayCard()
{
cout<<" Card Number: "<<cardNo<<" Price: "<<price;
}// End of function
};// End of class
// Defines class CardStore
class CardStore
{
// Declares an array of object of class GiftCard of size MAX
GiftCard cards[MAX];
// To store number of cards
int counter;
public:
// Default constructor definition to initialize counter to zero
CardStore()
{
counter = 0;
}// End of default constructor
// Function to add a card to the list
void addCard(GiftCard g)
{
// Assigns a GiftCard class object at counter index position of array of object cards
// Increase the counter by one
cards[counter++] = g;
}// End of function
// Function to remove a card from the list
void removeCard(int cardNo)
{
// Initially found status is -1
int found = -1;
// Loops till number of cards
for(int x = 0; x < counter; x++)
{
// Checks if the parameter card number is equals to current index position card number
if(cardNo == cards[x].getCardNo())
{
// Update the found status to found record position i.e., x
found = x;
// Loops from found index position till end
for(int y = found; y < counter; y++)
// Move each card to its previous position
cards[y] = cards[y + 1];
// Decrease the card counter by one
counter--;
}// End of if condition
}// End of for loop
// Checks if found status is -1 display error message
if(found == -1)
cout<<" ERROR: No such card number found: "<<cardNo;
}// End of function
// Function to display report
void printReport()
{
// Declares a array to store card frequency and initializes to zero
int cardFrequency[5] = {0};
// To store the total amount
double totalAmt = 0;
// Loops till number of cards
for(int x = 0; x < counter; x++)
{
// Calls the function getPrice() to get the price for the card at x index position
// Checks if the price is equals to 1
if(cards[x].getPrice() == 1)
// Increase the frequency counter for 0 index position
cardFrequency[0]++;
// Calls the function getPrice() to get the price for the card at x index position
// Checks if the price is equals to 2
else if(cards[x].getPrice() == 2)
// Increase the frequency counter for 1 index position
cardFrequency[1]++;
// Calls the function getPrice() to get the price for the card at x index position
// Checks if the price is equals to 3
else if(cards[x].getPrice() == 3)
// Increase the frequency counter for 2 index position
cardFrequency[2]++;
// Calls the function getPrice() to get the price for the card at x index position
// Checks if the price is equals to 4
else if(cards[x].getPrice() == 4)
// Increase the frequency counter for 3 index position
cardFrequency[3]++;
// Otherwise price is 5
else
// Increase the frequency counter for 4 index position
cardFrequency[4]++;
// Calculates the total amount
totalAmt += cards[x].getPrice();
}// End of for loop
// Displays the heading
cout<<" ****************** Cards Report ****************** ";
cout<<" Available cards: ";
// Loops till number of cards
for(int x = 0; x < counter; x++)
// Calls the function to display each card information
cards[x].displayCard();
// Loops 5 times for card frequency array
for(int x = 0; x < 5; x++)
// Displays the frequency for each price
cout<<" Number of cards of type $"<<(x + 1)<<" = "<<cardFrequency[x];
// Displays total number of cards
cout<<" Total number of cards = "<<counter;
// Displays total amount
cout<<" Total value of cards = $"<<totalAmt;
// Displays average amount
cout<<" Average value of cards = $"<<(totalAmt / counter);
}// End of function
};// End of class
// Function to display the menu, accepts user choice, and returns choice
int menu()
{
// To store choice
int ch;
// Displays menu
cout<<" 1 - Add a card.";
cout<<" 2 - Remove a card.";
cout<<" 3 - Display report.";
cout<<" 4 - Exit.";
// Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns choice
return ch;
}// End of function
// main function definition
int main()
{
// To store card number entered by the user
int no;
// Creates an object of the class CardStore
CardStore cs;
// Creates an object of the class GiftCard
GiftCard g;
// Loops till user choice is not 4
do
{
// Calls the function menu() to accepts user choice
// Calls the appropriate function based on the user choice
switch(menu())
{
case 1:
// Loops till valid card number entered by the user
do
{
// Accepts a card number to add
cout<<" Enter a card number to add: ";
cin>>no;
// Checks if card number is greater than or equals to 10000 then valid card number
if(no >= 10000)
{
// Cass the function to create a card
g.generateCard(no);
// Calls the function to add the card
cs.addCard(g);
// Come out of the loop
break;
}// End of if condition
// Otherwise invalid card number display error message
else
cout<<" ERROR: Invalid card number (Must >= 10000):"<<no<<" Try again.";
}while(1);// End of do - while
break;
case 2:
// Loops till valid card number entered by the user
do
{
// Accepts a card number to delete
cout<<" Enter a card number to remove: ";
cin>>no;
// Checks if card number is greater than or equals to 10000 then valid card number
if(no >= 10000)
{
// Calls the function to delete card number
cs.removeCard(no);
// Come out of the loop
break;
}// End of if condition
// Otherwise invalid card number display error message
else
cout<<" ERROR: Invalid card number (Must >= 10000):"<<no<<" Try again.";
}while(1);// End of do - while loop
break;
case 3:
// Calls the function to display report
cs.printReport();
break;
case 4:
cout<<" Thanks.";
exit(0);
default:
cout<<" Invalid choice.";
}// End of switch - case
}while(1); // End of outer do - while loop
}// End of main function
Sample Output:
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 6
Invalid choice.
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 100
ERROR: Invalid card number (Must >= 10000):100
Try again.
Enter a card number to add: 10000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 10001
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 11000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 30000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 50000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 3
****************** Cards Report ******************
Available cards:
Card Number: 10000 Price: 1
Card Number: 10001 Price: 1
Card Number: 11000 Price: 1
Card Number: 30000 Price: 3
Card Number: 50000 Price: 5
Number of cards of type $1 = 3
Number of cards of type $2 = 0
Number of cards of type $3 = 1
Number of cards of type $4 = 0
Number of cards of type $5 = 1
Total number of cards = 5
Total value of cards = $11
Average value of cards = $2.2
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 1
Enter a card number to add: 20000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 5
Invalid choice.
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 3
****************** Cards Report ******************
Available cards:
Card Number: 10000 Price: 1
Card Number: 10001 Price: 1
Card Number: 11000 Price: 1
Card Number: 30000 Price: 3
Card Number: 50000 Price: 5
Card Number: 20000 Price: 2
Number of cards of type $1 = 3
Number of cards of type $2 = 1
Number of cards of type $3 = 1
Number of cards of type $4 = 0
Number of cards of type $5 = 1
Total number of cards = 6
Total value of cards = $13
Average value of cards = $2.16667
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 2
Enter a card number to remove: 200
ERROR: Invalid card number (Must >= 10000):200
Try again.
Enter a card number to remove: 22000
ERROR: No such card number found: 22000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 2
Enter a card number to remove: 20000
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 3
****************** Cards Report ******************
Available cards:
Card Number: 10000 Price: 1
Card Number: 10001 Price: 1
Card Number: 11000 Price: 1
Card Number: 30000 Price: 3
Card Number: 50000 Price: 5
Number of cards of type $1 = 3
Number of cards of type $2 = 0
Number of cards of type $3 = 1
Number of cards of type $4 = 0
Number of cards of type $5 = 1
Total number of cards = 5
Total value of cards = $11
Average value of cards = $2.2
1 - Add a card.
2 - Remove a card.
3 - Display report.
4 - Exit.
Enter your choice: 4
Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.