In: Computer Science
Using dev c++
I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues:
The [REDACTED] Phone Store needs a program to compute phone
charges for some phones sold in the store. There are two different
pricing systems, depending on the phone purchased. Tax must be
added on after the phone charge is computed. Each customer gets a
neatly formatted receipt displayed to the screen.
You must write a driver and a class using separate files for full
credit.
Learning Objectives
In this assignment, you will practice:
Using selection control structures
Creating a driver program and a class
Calling member functions.
Displaying neatly formatted output to the screen using C++
syntax
Requirements for the Phone Class
Data Members
Phone price (in dollars)
bool data member to indicate if phone is an iPhone or not
AppleCare years
Be sure to:
use the appropriate data type for each data member
use the appropriate access modifier for each data member
initialize each data member using an in-class initializer
Member Functions
One 3-parameter constructor – The constructor uses three parameters
representing the phone price, whether or not the phone is an
iPhone, and the AppleCare years. Use a member initializer
list.
Getter and setter for each data member
getTotalPurchase function
This function must call the appropriate getter member functions
where necessary. Do not access the data members directly.
This function calculates and returns the total amount of the
phone.
If the phone is an iPhone, calculate additional charges for
AppleCare as follows:
If the user chooses one year, the charge for AppleCare is 12% of
the phone’s price.
If the user chooses more than one year, the charge for AppleCare is
10% of the phone’s price multiplied by the number of years.
The user cannot choose less than 1 year of AppleCare if the phone
is an iPhone.
Compute the purchase subtotal by adding the AppleCare charges to
the phone’s price.
Use a tax rate of 5% of the purchase subtotal to compute the sales
tax amount in dollars. Add the sales tax amount to the purchase
subtotal to determine the total purchase amount.
Return the total purchase amount to the calling code.
Requirements for the PhoneDriver Program
main Function
The user must be prompted appropriately.
All values related to money may include values after a decimal
point. All values displayed to the screen must display with 2
places after the decimal.
The user must indicate whether or not the phone is an iPhone by
typing a single character (y for yes, and n for no). Your program
must be able to handle an upper or lowercase letter: Y, y, N,
n.
Optional: Handle any other characters here by displaying an error
message.
If the phone is an iPhone, prompt the user for the number of
AppleCare years.
Instantiate a Phone object using the 3-parameter constructor.
Note that the parameter that indicates if the phone is an iPhone is
a bool data type. The user must type a single character. You will
have to use selection to instantiate a Phone object with the
correct data type for this parameter.
Display the values in the output by calling the appropriate member
function of the Phone object. The output must line up at the
decimal point, as displayed in the sample runs.
Sample Run 1
Enter the price of the phone> 500.00
Is the phone an iPhone (Y/N)?> N
Price of phone
$500.00
Total purchase
$525.00
Sample Run 2
Enter the price of the phone> 600
Is the phone an iPhone (Y/N)?> y
Enter the number of years of AppleCare> 3
Price of phone
$600.00
Total purchase
$819.00
Sample Run 3
Enter the price of the phone> 800
Is the phone an iPhone (Y/N)?> Y
Enter the number of years of AppleCare> 1
Price of phone
$800.00
Total purchase
$940.80
Requirements for Full Credit on This Project
COMPLETE AND ACCURATE – Your program must compile, execute, and
give accurate output.
-----------------------------------------------------------------------------------------------------------------------------------------------
My program: PhoneDriver.cp
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#include
#include
#include "Phone.h"
using namespace std;
int main(){
Phone myPhone{0, false, 1};
int year;
cout << "Enter the price of the phone> ";
//prompt for price
double price; //creating variable
cin >> price; //obtaining user input
myPhone.setPrice(price);
cout << "\nIs the phone an iPhone (Y/N)? ";
//prompt for if iphone or not
string temp;
cin >> temp;
bool android; //creating variable
//cin >> android; //obtaining user input
temp == "Y" ? android = true : android = false;
myPhone.setAndroid(android);
if (android = 1){
//getting the years of apple care
cout << "Enter the number of
years of AppleCare> ";
cin >> year;
myPhone.setYear(year);
}
printf("\nPhone price is $%.2f\n",
myPhone.getPrice());
printf("\nTotal purchase $%.2f\n",
myPhone.getTotalPurchase());
//cout << ("\nPhone price is $" <<
myPhone.getPrice();
//cout << "\nTotal purchase $" <<
myPhone.getTotal();
//cout << "\nPhone is apple: " <<
myPhone.getAndroid();
//cout << "\nAppleCare years: " <<
myPhone.getYear();
}
----------------------------------------------------------------------------------------------------------------
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
My other program file: Phone.h
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#include
#include
class Phone{
public:
//Phone(double phonePrice, bool phoneAndroid, int
appleCare)
Phone(double phonePrice, bool phoneAndroid, int
phoneYear)
: price{phonePrice}{
if (phoneAndroid
!= 1){
droid = phoneAndroid;
}
}
void setPrice(double phonePrice){
price = phonePrice;
}
double getPrice() const{
return price;
}
void setAndroid(bool phoneAndroid){
droid = phoneAndroid;
}
bool getAndroid() const{
return droid;
}
void setYear(double phoneYear){
yrs = phoneYear;
}
int getYear() const{
return yrs;
}
double getTotalPurchase() const{
//commented out because it doesnt work and dont know how to fix,
might finally ask the internet
//if (droid != 0){
// if (yrs == 1){
//total = price + (price * 1.12);
// }
//total = price
+ (yrs * (price * 1.1));
//}
//return total;
}
private:
double price {0};
bool droid {false};
int yrs {1};
double total {0};
};
Here is the updated code:
/////////////////////////////// PhoneDriver.cpp //////////////////////////////
#include <iostream>
#include <string>
#include "Phone.h"
using namespace std;
int main(){
Phone myPhone{0, false, 1};
int year;
cout << "Enter the price of the phone> "; //prompt for price
double price; //creating variable
cin >> price; //obtaining user input
myPhone.setPrice(price);
cout << "Is the phone an iPhone (Y/N)? "; //prompt for if iphone or not
string temp;
cin >> temp;
bool android;
// its an Android phone if the user did not enter y or Y
android = (temp != "Y") && (temp != "y");
myPhone.setAndroid(android);
if (!android){
//getting the years of apple care
cout << "Enter the number of years of AppleCare> ";
cin >> year;
myPhone.setYear(year);
}
printf("Phone price is $%.2f\n", myPhone.getPrice());
printf("Total purchase $%.2f\n", myPhone.getTotalPurchase());
}
And the phone.h file is below:
/////////////////////////////// Phone.h //////////////////////////////
class Phone{
public:
// Three parameter constructor with member initializer
Phone(double phonePrice, bool phoneAndroid, int phoneYear)
: price(phonePrice), droid (phoneAndroid), yrs (phoneYear) {}
void setPrice(double phonePrice){
price = phonePrice;
}
double getPrice() const{
return price;
}
void setAndroid(bool phoneAndroid){
droid = phoneAndroid;
}
bool getAndroid() const{
return droid;
}
void setYear(double phoneYear){
yrs = phoneYear;
}
int getYear() const{
return yrs;
}
double getTotalPurchase() const{
if (droid) {
double tax = price *0.05;
return (price + tax);
}
else {
double appleCare, tax;
appleCare = (yrs == 1 ? 0.12 * price : 0.1 * price * yrs);
tax = (price + appleCare) *0.05;
return (price + appleCare + tax);
}
}
private:
double price {0.0};
bool droid {false};
int yrs {1};
};