Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

campus.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "campus.h"
  2. #include "salaried.h"
  3. #include "hourly.h"
  4. #include "faculty.h"
  5. #include "administrator.h"
  6. #include "util.h"
  7. #include <iostream>
  8. #include <vector>
  9. #include <cstdlib>
  10. #include <fstream>
  11. bool campus::contains(string name) { //function used to verify if the string is already included
  12. employee * f = NULL;
  13. for(unsigned int i = 0; i < workers.size(); i++) {
  14. f = dynamic_cast<employee *>(workers[i]);
  15. if(f->get_name() == name) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. void campus::add_worker_from_parse(string name, string type, double pay_rate) { //add worker for parsed input
  22. bool passed = false;
  23. employee * h = NULL;
  24. int hours = 0;
  25. if(type == "H") {
  26. h = new hourly(name, pay_rate, hours, type);
  27. passed = true;
  28. }
  29. else if(type == "S") {
  30. h = new salaried(name, pay_rate, hours, type);
  31. passed = true;
  32. }
  33. else if(type == "F") {
  34. h = new faculty(name, pay_rate, hours, type);
  35. passed = true;
  36. }
  37. else if(type == "A") {
  38. h = new administrator(name, pay_rate, hours, type);
  39. passed = true;
  40. }
  41. if(passed == true) {
  42. workers.push_back(h);
  43. }
  44. }
  45. void campus::add_worker() { //general add worker function which it gets the input
  46. employee * h = NULL;
  47. bool passed;
  48. string info;
  49. string name, type;
  50. int hours = 0;
  51. double pay_rate = 0;
  52. cout << "Enter info in following format" << endl;
  53. cout << "type pay_rate name" << endl;;
  54. getline(cin, info);
  55. passed = parse_n_load(info, name, type, pay_rate);
  56. if(contains(name) == true) {
  57. cout << "already exists" << endl;
  58. return;
  59. }
  60. if(passed == false) {
  61. cout << "incorrect entry" << endl;
  62. }
  63. if(info[0] == ' ') {
  64. cout << "incorrect entry" << endl;
  65. passed = false;
  66. }
  67. else if(type == "H") {
  68. h = new hourly(name, pay_rate, hours, type);
  69. }
  70. else if(type == "S") {
  71. h = new salaried(name, pay_rate, hours, type);
  72. }
  73. else if(type == "F") {
  74. h = new faculty(name, pay_rate, hours, type);
  75. }
  76. else if(type == "A") {
  77. h = new administrator(name, pay_rate, hours, type);
  78. }
  79. if(passed == true) {
  80. workers.push_back(h);
  81. }
  82. }
  83. void campus::delete_worker() {
  84. string name;
  85. employee * h = NULL;
  86. cout << "Enter name of worker to delete" << endl;
  87. getline(cin, name);
  88. for(unsigned int i = 0; i < workers.size(); i++) {
  89. h = dynamic_cast<employee *>(workers[i]);
  90. if(h->get_name() == name) {
  91. delete h;
  92. workers.erase(workers.begin() + i);
  93. }
  94. }
  95. }
  96. void campus::display_worker() { //allows for display of all employees or selected
  97. string input;
  98. employee * h = NULL;
  99. cout << "(a)ll workers or (o)ne worker?" << endl;
  100. cin >> input;
  101. cin.ignore();
  102. cout << "type // payrate // name // hours // salary" << endl;
  103. if(input == "a") {
  104. for(unsigned int i = 0; i < workers.size(); i++){
  105. h = dynamic_cast<employee *>(workers[i]);
  106. cout << *workers[i] << " $" << h->get_salary() << endl;
  107. }
  108. }
  109. if(input == "o") {
  110. cout << "name: ";
  111. employee * h = NULL;
  112. getline(cin, input);
  113. for(unsigned int i = 0; i < workers.size(); i++) {
  114. h = dynamic_cast<employee *>(workers[i]);
  115. if(h->get_name() == input) {
  116. cout << *h << " $" << h->get_salary() << endl;
  117. }
  118. }
  119. }
  120. }
  121. void campus::sort_name() {
  122. bool ready = true;
  123. employee * h = NULL;
  124. employee * j = NULL;
  125. while(ready) {
  126. for(unsigned int i = 0; i < workers.size() -1; i++) {
  127. h = dynamic_cast<employee *>(workers[i]);
  128. j = dynamic_cast<employee *>(workers[i+1]);
  129. ready = false;
  130. if(h->get_name() > j->get_name()) {
  131. workers.push_back(workers[i]);
  132. workers[i] = workers[i+1];
  133. workers[i+1] = workers[workers.size()-1];
  134. workers.pop_back();
  135. ready = true;
  136. }
  137. }
  138. }
  139. }
  140. void campus::sort_pay() {
  141. bool ready = true;
  142. employee * h = NULL;
  143. employee * j = NULL;
  144. while(ready) {
  145. for(unsigned int i = 0; i < workers.size() -1; i++) {
  146. h = dynamic_cast<employee *>(workers[i]);
  147. j = dynamic_cast<employee *>(workers[i+1]);
  148. ready = false;
  149. if(h->get_payrate() > j->get_payrate()) {
  150. workers.push_back(workers[i]);
  151. workers[i] = workers[i+1];
  152. workers[i+1] = workers[workers.size()-1];
  153. workers.pop_back();
  154. ready = true;
  155. }
  156. }
  157. }
  158. }
  159. void campus::sort_worker() { //allows for either sort by name or by pay
  160. string input;
  161. cout << "sort by (n)ame or by (p)ay?" << endl;
  162. cin >> input;
  163. if(input == "n") {
  164. sort_name();
  165. }
  166. if(input == "p") {
  167. sort_pay();
  168. }
  169. }
  170. void campus::advance_month() {
  171. employee * h = NULL;
  172. for(unsigned int i = 0; i < workers.size(); i++) {
  173. h = dynamic_cast<employee *>(workers[i]);
  174. h->reset_hours();
  175. }
  176. }
  177. void campus::set_hours() {
  178. string name;
  179. employee * h = NULL;
  180. cout << "enter name of employee" << endl;
  181. getline(cin, name);
  182. for(unsigned int i = 0; i < workers.size(); i++) {
  183. h = dynamic_cast<employee *>(workers[i]);
  184. if(h->get_name() == name) {
  185. int input;
  186. cout << "enter hours" << endl;
  187. cin >> input;
  188. cin.ignore();
  189. h->set_hours(input);
  190. }
  191. }
  192. }
  193. void campus::add_files(campus c) { //function for i/o. can either write to a file or take a file in
  194. string add_or_write;
  195. cout << "(a)dd or (w)rite to file?" << endl;
  196. cin >> add_or_write;
  197. cin.ignore();
  198. if(add_or_write == "a") {
  199. bool file_correct = false;
  200. string input, input_file_name;
  201. while(!file_correct) {
  202. cout << "please enter file name: ";
  203. getline(cin, input);
  204. ifstream inputs(input.c_str());
  205. if(inputs.good()) {
  206. input_file_name = input;
  207. file_correct = true;
  208. string lineread;
  209. while(getline(inputs, lineread)) {
  210. bool worked;
  211. string name, type;
  212. double pay_rate;
  213. worked = parse_n_load(lineread, name, type, pay_rate);
  214. if(worked == true) {
  215. add_worker_from_parse(name, type, pay_rate);
  216. }
  217. lineread = "";
  218. }
  219. }
  220. else {
  221. cerr << "incorrect file name" << endl;
  222. }
  223. }
  224. }
  225. if(add_or_write == "w") {
  226. bool file_correct = false;
  227. string input, input_file_name;
  228. string output_file_name;
  229. file_correct = false;
  230. while(!file_correct) {
  231. cout << "please enter file name: ";
  232. getline(cin, output_file_name);
  233. ofstream out(output_file_name.c_str());
  234. if(out.good()) {
  235. out << c;
  236. file_correct = true;
  237. }
  238. else {
  239. cerr << "incorrect file name" << endl;
  240. }
  241. }
  242. }
  243. }
  244. ostream & operator<<(ostream & os, campus & t) { //makes writing out to files much easier
  245. employee * h = NULL;
  246. for(unsigned int i = 0; i < t.workers.size(); i++) {
  247. h = dynamic_cast<employee *>(t.workers[i]);
  248. os << h->get_type() << " " << h->get_payrate() << " " << h->get_name() << "\n";
  249. }
  250. return os;
  251. }