-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_1_9_(4)-B.cpp
81 lines (73 loc) · 2.14 KB
/
cpa_lab_5_1_9_(4)-B.cpp
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
#include <iostream>
#include <stdexcept>
using namespace std;
class ShopItemOrder{
private:
string name;
float unitPrice;
int amount;
public:
ShopItemOrder(string name, float unitPrice, int amount)
{
if(name.empty())
throw invalid_argument("Name can't be empty");
if(unitPrice <= 0)
throw invalid_argument("Price can't be <= 0");
if(amount <= 0)
throw invalid_argument("Amount can't be <= 0");
this->name = name;
this->unitPrice = unitPrice;
this->amount = amount;
}
string GetName()
{
return name;
}
void SetName(string name)
{
if(name.empty())
throw invalid_argument("Name can't be empty");
this->name = name;
}
float GetUnitPrice()
{
return unitPrice;
}
void SetUnitPrice(float price)
{
if(price <= 0)
throw invalid_argument("Price can't be <= 0");
unitPrice = price;
}
int GetAmount()
{
return amount;
}
void SetAmount(int amount)
{
if(amount <= 0)
throw invalid_argument("Amount can't be <= 0");
this->amount = amount;
}
float GetTotalPrice()
{
return unitPrice * amount;
}
void PringShopItemOrder()
{
cout << "Order for " << name << " item, which cost " << unitPrice << "$ and has " << amount << " units." << endl;
}
};
int main()
{
ShopItemOrder item1("Item1", 10.50, 5);
item1.PringShopItemOrder();
cout << "Total price is " << item1.GetTotalPrice() << "$" << endl << endl;
item1.SetAmount(4);
item1.PringShopItemOrder();
cout << "Total price is " << item1.GetTotalPrice() << "$" << endl << endl;
item1.SetUnitPrice(9.99);
cout << "Price of " << item1.GetName() << " is " << item1.GetUnitPrice() << "$" << endl;
cout << "Total price is " << item1.GetTotalPrice() << "$" << endl;
return 0;
}