-
Notifications
You must be signed in to change notification settings - Fork 0
/
FruitBox.cpp
175 lines (150 loc) · 2.84 KB
/
FruitBox.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
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
#include <iostream>
using namespace std;
//Fruit
class Fruit {
public:
int weight;
int lose;
Fruit();
~Fruit();
virtual void loseWeight();
virtual Fruit* GetClassType(void);
};
//Apple
class Apple : public Fruit {
public:
Apple();
void loseWeight();
Apple* GetClassType(void);
};
//Orange
class Orange : public Fruit {
public:
Orange();
void loseWeight();
Orange* GetClassType(void);
};
//Box
class Box {
private:
Fruit* box[8];
int order;
public:
Box();
~Box();
void passOneDay();
bool putFruit(Fruit* fruit);
int showApple();
int showOrange();
int showReducing();
int showSum();
};
Fruit::Fruit():weight(0),lose(0) {
}
void Fruit::loseWeight() {
}
Fruit* Fruit::GetClassType(void) {
return this;
}
Apple::Apple() {
weight = 50;
}
void Apple::loseWeight() {
if (weight > 30) {
weight -= 4;
lose = 4;
} else {
lose = 0;
}
}
Apple* Apple::GetClassType(void) {
return this;
}
Orange::Orange() {
weight = 30;
}
void Orange::loseWeight() {
if (weight > 18) {
weight -= 3;
lose = 3;
} else {
lose = 0;
}
}
Orange* Orange::GetClassType(void) {
return this;
}
Box::Box():order(0){
for (int i = 0; i < 8; i++) {
box[i] = NULL;
}
}
void Box::passOneDay(){
int i = 0;
while (box[i] != NULL && i < 8) {
box[i]->loseWeight();
i++;
}
}
bool Box::putFruit(Fruit* fruit){
if (order < 8) {
box[order] = fruit;
order++;
return true;
} else {
return false;
}
}
int Box::showApple(){
int i = 0;
int count = 0;
while (box[i] != NULL && i < 8) {
if (typeid(*(box[i]->GetClassType()))== typeid(Apple)) {
count++;
}
i++;
}
return count;
}
int Box::showOrange(){
int i = 0;
int count = 0;
while (box[i] != NULL && i < 8) {
if (typeid(*(box[i]->GetClassType()))== typeid(Orange)) {
count++;
}
i++;
}
return count;
}
int Box::showReducing(){
int i = 0;
int count = 0;
while (box[i] != NULL && i < 8) {
count += box[i]->lose;
i++;
}
return count;
}
int Box::showSum(){
int i = 0;
int count = 0;
while (box[i] != NULL && i < 8) {
count += box[i]->weight;
i++;
}
return count;
}
int main(int argc, const char * argv[]) {
Box* testbox = new Box();
for (int i = 0; i < 4; i++) {
testbox->putFruit(new Apple());
testbox->putFruit(new Orange());
testbox->passOneDay();
cout << "Apple:" << testbox->showApple() << endl;
cout << "Orange:" << testbox->showOrange() << endl;
cout << "Reducing:" << testbox->showReducing() << endl;
cout << "Sum:" << testbox->showSum() << endl;
}
return 0;
}