Can I have some help please?

I am trying to write a program but it is not working. This is the exercise that I should write. I am a beginner :)
Thank you for your help :)


Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An invoice should include 6 data members a part number (type string), part description (type string), a quantity of the item beeing purchased (type int), a price per item (type int) a value-added tax (VAT) rate as a decimal (type double) and discount rate as decimal (type double) Your class should have a constructor that initializes the 6 data members. The constructor should initialise the first four data members with values frpm parameters and the last two data members to default values of 0.20 per cent and zero respectively. Provide a set and get functions for each data member. In addition provide a member function named getInvoiceAmount that calculates the invoice amount (i.e multiples the quantity by the price per item and applies the tax and discounts amouns) then returns the amount. Have the set data members perform validity checks on their parameters - if value is not positive it should be left unchanged. Write a driver program to demonstrate Invoice's capabilities

I wrote this, but it is not working as it should work


Header:

#include <string>

class Invoice {
public:
Invoice(std::string partNumber, std::string partDescription, int purchasedQuantity, int itemPrice, double taxRate, double discountRate)
: number{ partNumber } {

if (taxRate > 0) {
trate = taxRate;
}
if (discountRate > 0) {
drate = discountRate;
}
}
void setNumber(std::string partNumber) {
number = partNumber;
}
std::string getNumber() const {
return number;
}
void setDescription(std::string partDescription) {
description = partDescription;
}
std::string getDescription() const {
return description;
}
void setQuantity(int purchasedQuantity) {
quantity = purchasedQuantity;
}
int getQuantity() const {
return quantity;
}
void setPrice(int itemPrice) {
price = itemPrice;
}
int getPrice() const {
return price;
}
double getTrate() const {
return trate;
}
double getDrate() const {
return drate;
}
void setInvoiceAmount(double iamount) {
iamount = ((quantity*price)*trate) + (quantity*price - drate);
}
double getInvoiceAmount() const {
return iamount;
}

private:

std::string number;
std::string description;
int quantity;
int price;
double trate{ 0.20 };
double drate{ 0 };
};


Main:

#include <iostream>
#include <string>
#include "Header.h"

using namespace std;

int main() {
Invoice invoice1{ "NO134 56", "PCIe SSD", 3, 130, 0.50, 0 };

cout << "Purchased part number: " << invoice1.getNumber() << " description: " << invoice1.getDescription() << " quantity: "
<< invoice1.getQuantity() << " price per item: " << invoice1.getPrice() << " tax VAT: " << invoice1.getTrate() << " discount rate: "
<< invoice1.getDrate();
cout << "\nThe invoice amount is: " << invoice1.getInvoiceAmount() << endl;

}
> Invoice(std::string partNumber, std::string partDescription, int purchasedQuantity, int itemPrice, double taxRate,
> double discountRate)
Read the question.

> The constructor should initialise the first four data members with values frpm parameters
> and the last two data members to default values of 0.20 per cent and zero respectively.

The second problem was trying to write the whole thing at once, and then wondering why it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace std;

class Invoice {
public:
  Invoice(std::string partNumber, std::string partDescription, int purchasedQuantity, int itemPrice)
    : number(partNumber), description(partDescription),
      quantity(purchasedQuantity), price(itemPrice)
    {
        trate = 0.20;
        drate = 0.0;
    }
//   void setNumber(std::string partNumber) {
//     number = partNumber;
//   }
//   std::string getNumber()const {
//     return number;
//   }
//   void setDescription(std::string partDescription) {
//     description = partDescription;
//   }
//   std::string getDescription()const {
//     return description;
//   }
//   void setQuantity(int purchasedQuantity) {
//     quantity = purchasedQuantity;
//   }
//   int getQuantity() const {
//     return quantity;
//   }
//   void setPrice(int itemPrice) {
//     price = itemPrice;
//   }
//   int getPrice() const {
//     return price;
//   }
//   double getTrate() const {
//     return trate;
//   }
//   double getDrate() const {
//     return drate;
//   }
//   void setInvoiceAmount(double iamount) {
//     iamount = ((quantity * price) * trate) + (quantity * price - drate);
//   }
//   double getInvoiceAmount() const {
//     return iamount;
//   }

private:
  std::string number;
  std::string description;
  int quantity;
  int price;
  double trate;
  double drate;
};


int main()
{
  Invoice invoice1("NO134 56", "PCIe SSD", 3, 130);
}


Now add in exactly ONE Get/Set pair at once, and make sure each one works before adding in the others.

Start small, compile often.
Thank you for your help.
I tryed to make it as you advised but it is still not working.
we can't help you like that. post what the new code looks like, and explain what is not working, and then we can do something for you.
This is your code, nearly unchanged.
Do read the comments.
Do read the compiler errors.
Ask specific questions only about what you really cannot catch (“is not working” is not a valid description of your problem).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// #ifndef CLASS_INVOICE_HPP
// #define CLASS_INVOICE_HPP
#include <iostream>
#include <string>


// Create a class called Invoice that a hardware store might use to represent
// an invoice for an item sold at the store.
// --> done
class Invoice {
public:
// Your class should have a constructor that initializes the 6 data members.
// --> done
    Invoice(std::string part_number,
            std::string part_description,
            int purchased_quantity,
            int item_price,
            double tax_rate,
            double discount_rate);

// Provide a set and get functions for each data member.
// --> done
    void setNumber(std::string partNumber);
    std::string getNumber() const;

    void setDescription(const std::string& partDescription);
    std::string getDescription() const;

    void setQuantity(int purchasedQuantity);
    int getQuantity() const;

    void setPrice(int itemPrice);
    int getPrice() const;

    double getTrate() const;
    double getDrate() const;

    void setInvoiceAmount(double iamount);
    double getInvoiceAmount() const;

private:
// An invoice should include 6 data members
//     1) a part number (type string), part
//     2) description (type string),
//     3) a quantity of the item beeing purchased (type int),
//     4) a price per item (type int)
//     5) a value-added tax (VAT) rate as a decimal (type double)
//     6) and discount rate as decimal (type double)
// --> done
    std::string number;                     // 1)
    std::string description;                // 2)
    int quantity;                           // 3)
    int price;                              // 4)
    double trate { 0.20 };                  // 5)
    double drate { 0 };                     // 6)
};


// #endif // CLASS_INVOICE_HPP


// The constructor should initialise a) the first four data members with values
// frpm parameters and b) the last two data members to default values of 0.20
// per cent and zero respectively.
// a) --> YET TO BE DONE !!
// b) --> done (but not the way it has been requested)
Invoice::Invoice(std::string part_number,
                 std::string part_description,
                 int purchased_quantity,
                 int item_price,
                 double tax_rate,
                 double discount_rate)
    : number { part_number }
    // something is missing here!!
    {
    if (tax_rate > 0) {
        trate = tax_rate;
    }
    if (discount_rate > 0) {
        drate = discount_rate;
    }
}


// Have the set data members perform validity checks on their parameters - if
// value is not positive it should be left unchanged.
// --> YET TO BE DONE !!
void Invoice::setNumber(std::string partNumber) // --> YET TO BE DONE !!
{
    number = partNumber;
}


std::string Invoice::getNumber() const
{
    return number;
}


void Invoice::setDescription(const std::string& partDescription) // --> YET TO BE DONE !!
{
    description = partDescription;
}


std::string Invoice::getDescription() const
{
    return description;
}


void Invoice::setQuantity(int purchasedQuantity)    // --> YET TO BE DONE !!
{
    quantity = purchasedQuantity;
}


int Invoice::getQuantity() const
{
    return quantity;
}


void Invoice::setPrice(int itemPrice)// --> YET TO BE DONE !!
{
    price = itemPrice;
}


int Invoice::getPrice() const
{
    return price;
}


double Invoice::getTrate() const
{
    return trate;
}


double Invoice::getDrate() const
{
    return drate;
}


void Invoice::setInvoiceAmount(double iamount)
{
    iamount = ((quantity*price)*trate) + (quantity*price - drate);
}


// In addition provide a member function named getInvoiceAmount that calculates
// the invoice amount (i.e multiples the quantity by the price per item and
// applies the tax and discounts amouns) then returns the amount.
// --> YET TO BE DONE!!
double Invoice::getInvoiceAmount() const
{
    return iamount;
}


// Write a driver program to demonstrate Invoice's capabilities
// YET TO BE DONE!!
int main()
{
    // std::string number;
    // std::string description;
    // int quantity;
    // int price;
    // double trate { 0.20 };
    // double drate { 0 };
    Invoice invoice1 { "NO134 56", "PCIe SSD", 3, 130, 0.50, 0 };

    std::cout << "Purchased part number: " << invoice1.getNumber()
              << "; description: "         << invoice1.getDescription()
              << "; quantity: "            << invoice1.getQuantity()
              << "; price per item: "      << invoice1.getPrice()
              << "; tax VAT: "             << invoice1.getTrate()
              << "; discount rate: "       << invoice1.getDrate();
    std::cout << "\nThe invoice amount is: " << invoice1.getInvoiceAmount() << '\n';
}

Thank you.
I am trying to link my new program code as you did. Can somebody tell me how to link the code from cpp.sh please? Thanks
This forum lets you post formatted code by putting the formatting

[code]

TYPE CODE HERE

[/code]

around your code.

1
2
3

TYPE CODE HERE
Last edited on
I have the following problems:

1.) The constructor should initialise the last two data members to default values of 0.20 per cent and zero respectively.

double trate {0.20};
double drate {0};

2.)if value is not positive it should be left unchanged:

if (taxRate > 0) {
trate = taxRate;
}
if (discountRate > 0) {
drate = discountRate;
}

The problem is that when I give invalid value (negative) It doesn't change it to the default value

3.) provide a member function named getInvoiceAmount that calculates the invoice amount.

Does not matter the values for the program, it presents the same unvalid calculation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <string>



class Invoice {
public:
Invoice(std::string partNumber, 
        std::string partDescription,
        int purchasedQuantity,
        int itemPrice,
        double taxRate,
        double discountRate)
        
:number{ partNumber }, 
 description{ partDescription},
 quantity{ purchasedQuantity },
 price { itemPrice }, 
 trate { taxRate },
 drate { discountRate } {
//2.)
    if (taxRate > 0) {
        trate = taxRate;
    }
    if (discountRate > 0) {
        drate = discountRate;
    }
}
void setNumber(std::string partNumber) {
    number = partNumber;
}
std::string getNumber() const {
    return number;
}
void setDescription(std::string partDescription) {
    description = partDescription;
}
std::string getDescription() const {
    return description;
}
void setQuantity(int purchasedQuantity) {
    quantity = purchasedQuantity;
}
int getQuantity() const {
    return quantity;
}
void setPrice(int itemPrice){
    price = itemPrice;
}
int getPrice() const {
    return price;
}

double getTrate() const {
    return trate;
}

double getDrate() const {
    return drate;
}
//3.)
void setInvoiceAmount(double iamount)
{
    iamount = ((quantity*price)*trate) + (quantity*price - drate);

}
double getInvoiceAmount() const
{
    return iamount;
}


private:

std::string number;
std::string description;
int quantity;
int price;
double trate {0.20};  //1.)
double drate {0};      // 1.)
double iamount;

};



int main() {
Invoice invoice1{ "NO134 56", "PCIe SSD", 3, 130, 0.50, 0.2};

std::cout << "Purchased part number: " << invoice1.getNumber() 
 << "\ndescription: " << invoice1.getDescription()
 << "\nquantity: " << invoice1.getQuantity()
 << "\nprice per item: " << invoice1.getPrice()
 << "\ntax VAT: " << invoice1.getTrate()
 << "\ndiscount rate: "<< invoice1.getDrate();
std::cout << "\nInvoice amount is: " << invoice1.getInvoiceAmount();
}


> The constructor should initialise the first four data members with values frpm parameters and the last two data members to default values of 0.20 per cent and zero respectively.
I told you what you needed, and still you persist with the 6-parameter constructor.

I've done the program as you suggested.

The calculation is still invalid.

and how I am supposed to do this? "Have the set data members perform validity checks on their parameters - if value is not positive it should be left unchanged."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <iostream>
#include <string>



class Invoice {
public:
Invoice(std::string partNumber, 
        std::string partDescription,
        int purchasedQuantity,
        int itemPrice,
        double taxRate,
        double discountRate)
        
:number{ partNumber }, 
 description{ partDescription},
 quantity{ purchasedQuantity },
 price { itemPrice }
{

    trate = 0.20;
    drate = 0.0;
}
void setNumber(std::string partNumber) {
    number = partNumber;
}
std::string getNumber() const {
    return number;
}
void setDescription(std::string partDescription) {
    description = partDescription;
}
std::string getDescription() const {
    return description;
}
void setQuantity(int purchasedQuantity) {
    quantity = purchasedQuantity;
}
int getQuantity() const {
    return quantity;
}
void setPrice(int itemPrice){
    price = itemPrice;
}
int getPrice() const {
    return price;
}

double getTrate() const {
    return trate;
}

double getDrate() const {
    return drate;
}

void setInvoiceAmount(double iamount)
{
    iamount = ((quantity*price)*trate) + (quantity*price - drate);

}
double getInvoiceAmount() const
{
    return iamount;
}


private:

std::string number;
std::string description;
int quantity;
int price;
double trate {0.20};
double drate {0};
double iamount;

};



int main() {
Invoice invoice1{ "NO134 56", "PCIe SSD", 3, 130};

std::cout << "Purchased part number: " << invoice1.getNumber() 
 << "\ndescription: " << invoice1.getDescription()
 << "\nquantity: " << invoice1.getQuantity()
 << "\nprice per item: " << invoice1.getPrice()
 << "\ntax VAT: " << invoice1.getTrate()
 << "\ndiscount rate: "<< invoice1.getDrate();
std::cout << "\nInvoice amount is: " << invoice1.getInvoiceAmount();
}

Topic archived. No new replies allowed.