-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial1_Q5.cpp
More file actions
151 lines (125 loc) · 5.11 KB
/
Copy pathtutorial1_Q5.cpp
File metadata and controls
151 lines (125 loc) · 5.11 KB
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
#include <iostream>
#include <string>
using namespace std;
double calculateDamageProtection(int cost){
return cost * 0.01;
}
double calculateTotalCost(double cost, double shipping_fee, double insurance_fee , double discount_price){
return cost + shipping_fee + insurance_fee - discount_price;
}
double calculatePercentage(int number, int percentage){
return (number * percentage) / 100;
}
double calculateTotalItemCost(double cost[], int n){
double sum = 0;
for (int i = 0; i < n; i++){
sum += cost[i];
}
return sum;
}
double calculateAverageTotalTransaction(double trans[]){
double sum = 0,average;
for (int i = 0; i < 5; i++){
sum += trans[i];
}
return average = sum/5;
}
int main(){
double transactions[5], average_trans;
for (int j = 0; j < 5; j++){
int n = 0, i;
double cost[i], shipping_method, damage_protection_cost, discount_applied = 0, insurance_applied = 0, discount_price, total_item_cost;
cout << "\n\n--------------------------This is transaction " << j + 1 << "-------------------------\n";
cout << "How many items do you have? ";
cin >> i;
cout << "Please enter the item(s)' cost (Enter -1 to exit) \n";
do {
cout << "Please input the cost of the item "<< n + 1<< ": $ ";
cin >> cost[n];
if (cost[n] == -1) break;
n++;
} while (n < i);
total_item_cost = calculateTotalItemCost(cost, i);
cout << "Shipping methods:\n 1. Saver ($ 4.90)\n 2. Standard ($ 6.90)\n 3. Courier ($ 10.50)\nPlease select your shipping method (1, 2 or 3): ";
cin >> shipping_method;
while (shipping_method != 1 && shipping_method != 2 && shipping_method != 3){
cout << "Invalid input. Please select your shipping method (1, 2 or 3): ";
cin >> shipping_method;
}
cout << "Please enter the discount code if you have (Enter N to skip): ";
string discount_code;
cin >> discount_code;
if (discount_code != "N"){
if (discount_code == "0909CARNIVAL") {
if (total_item_cost > 50){
cout << "Discount of 5% is applied!";
discount_price = calculatePercentage(total_item_cost, 5);
discount_applied = 1;
} else {
cout << "Sorry you do not meet the minimum spend.";
}
} else if (discount_code == "AUTUMNISHERE"){
if (total_item_cost > 150){
cout << "Discount of $ 15 is applied!";
discount_price = 15;
discount_applied = 1;
} else {
cout << "Sorry you do not meet the minimum spend.";
}
} else if (discount_code == "FREESHIPPING"){
if (total_item_cost > 300 && shipping_method != 3){
cout << "Your shipping fee is free!";
discount_price = (shipping_method == 1 ? 4.90 : shipping_method == 2 ? 6.90: 0);
discount_applied = 1;
} else {
cout << "Sorry you do not meet the minimum spend or this voucher does not apply to your current shipping method.";
}
} else {
cout << "Invalid input.";
}
cout << endl;
}
if (total_item_cost > 250){
damage_protection_cost = calculateDamageProtection(total_item_cost);
cout << "Do you want to purchase an optional insurance ($ " << damage_protection_cost << ") for your item? (Y/N) ";
char damage_protection_choice;
cin >> damage_protection_choice;
if (damage_protection_choice == 'Y') {
cout << "Insurance applied successfully!\n";
insurance_applied = 1;
}
}
cout << "\n----------------------------Checking-------------------------------------\n";
cout << "Subtotal of your item(s) = $ " << total_item_cost << "\n";
double shipping_fee = (shipping_method == 1 ? 4.90 : shipping_method == 2 ? 6.90 : 10.50);
cout << "Shipping Fee = $ " << shipping_fee << "\n";
if (insurance_applied){
cout << "Damage Protection Fee = $ " << damage_protection_cost << "\n";
}
if (discount_applied){
cout << "Discount Voucher = -$ " << discount_price << "\n";
}
double total_cost = calculateTotalCost(total_item_cost, shipping_fee, (insurance_applied ? damage_protection_cost : 0), (discount_applied ? discount_price : 0));
cout << "Total cost for transaction " << j + 1 << " = $ " << total_cost << "\n";
transactions[j] = total_cost;
}
average_trans = calculateAverageTotalTransaction(transactions);
cout << "\nThe average total cost to pay for these 5 transactions is $ " << average_trans << endl;
double transLessThanAverage[5];
int k = 0;
for (int j = 0; j < 5 ; j++){
if (transactions[j] < average_trans){
transLessThanAverage[k] = transactions[j];
k++;
}
}
cout << "The transaction(s) that are less than " << average_trans << " are ";
if (k == 0) {
cout << "none";
} else {
for (int j = 0; j < k; j++) {
cout << transLessThanAverage[j];
if (j + 1 < k) cout << " and ";
}
}
}