-
Notifications
You must be signed in to change notification settings - Fork 0
/
workoutenvironment.cpp
125 lines (119 loc) · 4.5 KB
/
workoutenvironment.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
//
// workoutenvironment.cpp
// 2012PA1
//
// Created by TONY WAI SUM JI on 2/9/2019.
// Copyright © 2019 Tony Wai Sum JI. All rights reserved.
//
#include <iostream>
#include "workoutenvironment.h"
WorkoutEnvironment::WorkoutEnvironment(string name, int entry_fee, int MAX_NUM_OF_WORKOUTS) :
name(name),
entry_fee(entry_fee),
available_workouts(new Workout[MAX_NUM_OF_WORKOUTS]),
MAX_NUM_OF_WORKOUTS(MAX_NUM_OF_WORKOUTS),
current_num_of_workouts(0),
participants(NULL),
current_num_of_participants(0)
{}
WorkoutEnvironment::~WorkoutEnvironment(){
delete [] available_workouts;
/*for(int i=0; i<current_num_of_participants; i++){
delete participants[i];
}*/
delete [] participants;
participants = NULL;
}
string WorkoutEnvironment::get_name() const{
return name;
}
int WorkoutEnvironment::get_entry_fee() const{
return entry_fee;
}
bool WorkoutEnvironment::add_workout(const Workout& workout, int add_index){
//check if its a valid add_index, and check that array is not full
if(add_index >= 0
&& add_index <= current_num_of_workouts
&& current_num_of_workouts < MAX_NUM_OF_WORKOUTS){
//check if the array element at add_index is empty or not
if(available_workouts[add_index].get_name() == "")
//if empty put workout at add_index
available_workouts[add_index] = workout;
else{
//else (not empty) move current element to next index, and next element to next next index...
for(int i=current_num_of_workouts; i>add_index; i--){
available_workouts[i] = available_workouts[i-1];
}
//after moving all other elements, add workout at add_index
available_workouts[add_index] = workout;
}
//add = increment current_num_of_workouts
current_num_of_workouts++;
return true;
}
return false;
}
bool WorkoutEnvironment::remove_workout(int remove_index){
//check if its a valid add_index, and check that array is not full
if(remove_index >= 0
&& remove_index < current_num_of_workouts
&& current_num_of_workouts > 0){
//at remove_index, replace element with next element, and so on for the next ones
for(int i=remove_index; i<current_num_of_workouts-1; i++)
available_workouts[i] = available_workouts[i+1];
//remove = decrement current_num_of_workouts
current_num_of_workouts--;
return true;
}
return false;
}
int WorkoutEnvironment::participant_index(const Buddy* buddy) const{
//look through the whole participants array
for(int i=0; i<current_num_of_participants; i++){
//check if address is the same
if(participants[i] == buddy){
return i;
}
}
return -1;
}
bool WorkoutEnvironment::register_participant(Buddy* buddy){
if(participant_index(buddy) > -1)
return false;
if(participant_index(buddy) == -1 && entry_fee <= buddy->get_money()){
//deduct money
buddy->set_money(buddy->get_money() - entry_fee);
{
//make dynamic array of pointers with size current_num_of_participants+1
Buddy **array = new Buddy*[current_num_of_participants+1];
//copy allelements of old dynamic array (participants) into new array.
for(int i=0; i<current_num_of_participants; i++){
array[i] = participants[i];
}
//add buddy to the new array
array[current_num_of_participants] = buddy;
//make participants point to array
participants = array;
}
//increment current_num_of_participants
current_num_of_participants++;
return true;
}
//catch all remaining edge cases
return false;
}
bool WorkoutEnvironment::start_workout(int participant_index, int workout_index) const{
//check if participant_index and workout_index are valid, and check that participant has enough energy
if(participant_index >= 0
&& participant_index <= current_num_of_participants
&& workout_index >= 0
&& workout_index <= current_num_of_workouts
&& participants[participant_index]->get_energy() >= -1*available_workouts[workout_index].get_energy_change()){
//seting participants fat, muscle and energy
participants[participant_index]->set_fat(participants[participant_index]->get_fat() + available_workouts[workout_index].get_fat_change());
participants[participant_index]->set_muscle(participants[participant_index]->get_muscle() + available_workouts[workout_index].get_muscle_change());
participants[participant_index]->set_energy(participants[participant_index]->get_energy() + available_workouts[workout_index].get_energy_change());
return true;
}
return false;
}