-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1.cpp
1057 lines (876 loc) · 34.2 KB
/
a1.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <stack>
using namespace std;
// Average consumption is in kWh
int avg_cons_person = 200;
int avg_cons_sqm = 9;
int avg_water_heat = 550;
const int column_width = 35;
enum Use {
once, daily, mo_fr, sa_su, weekly
};
string use_to_string(Use use) {
switch (use) {
case once:
return "once";
case daily:
return "daily";
case mo_fr:
return "Monday to Friday";
case sa_su:
return "Saturday and Sunday";
case weekly:
return "weekly";
}
return "";
}
Use findFrequencyOfUse(const string &type) {
Use use;
if (type == "once")
use = once;
else if (type == "daily")
use = daily;
else if (type == "Monday to Friday")
use = mo_fr;
else if (type == "Saturday and Sunday")
use = sa_su;
else if (type == "weekly")
use = weekly;
return use;
}
Use input_use(const string &message) {
cout << message << endl;
cout << "daily (d)" << endl;
cout << "mo_fr (m)" << endl;
cout << "sa_su (s)" << endl;
cout << "weekly (w)? ";
char useType;
cin >> useType;
if (useType == 'w')
return weekly;
else if (useType == 'd')
return daily;
else if (useType == 'm')
return mo_fr;
else if (useType == 'o')
return once;
else if (useType == 's')
return sa_su;
else {
cout << "invalid option enter again." << endl;
return input_use(message);
}
}
class Device {
private:
string description;
Device *next;
public:
Device(Device *next = nullptr) : next(next) {}
virtual ~Device() {
cout << "Device " << description << " at address " << this << " deleted" << endl;
}
string get_description() {
return description;
}
Device *get_next() {
return next;
}
void set_description(string description) {
this->description = description;
}
void set_next(Device *next) {
this->next = next;
}
virtual void input() {
cout << "what is the description of the power consumer? ";
getline(cin >> ws, description);
}
virtual void print(int index, float price_in_KWh) = 0;
virtual float annual_kWh() = 0;
virtual Device *get_a_copy() = 0;
virtual string get_string_for_file(char separator) = 0;
};
class Consumer : public Device {
private:
Use use;
float watt;
static int total_number;
public:
// Default constructor
Consumer(Device *next = nullptr) : Device(next) { total_number++; }
virtual ~Consumer() {
total_number--;
cout << "Consumer " << get_description() << " at address " << this << " deleted" << endl;
}
static int get_total_consumers() {
return total_number;
}
float get_watt() const {
return watt;
}
Use get_use() const {
return use;
}
void set_watt(float w) {
this->watt = w;
}
void set_use(Use usage) {
this->use = usage;
}
virtual void input() {
Device::input();
cout << "how many watt it will have? ";
cin >> watt;
use = input_use("how often it will be used?");
}
};
int Consumer::total_number = 0; // has to be static in order to be initialized outside the class
class Immobile : public Consumer {
private:
float watt_standby;
float hours;
public:
Immobile(Device *next = nullptr) : Consumer(next) {}
virtual ~Immobile() {
cout << "Immobile " << get_description() << " at address " << this << " deleted" << endl;
}
float get_watt_standby() const {
return watt_standby;
}
float get_hours() const {
return hours;
}
void set_watt_standby(float w_s) {
this->watt_standby = w_s;
}
void set_hours(float hour) {
this->hours = hour;
}
float annual_hours_of_use() {
switch (get_use()) {
case once:
return hours;
case daily:
return hours * 365;
case mo_fr:
return hours * 260;
case sa_su:
return hours * 104;
case weekly:
return hours * 52;
default:
return 0;
}
}
float annual_hours_of_standby() {
return 8760 - annual_hours_of_use();
}
float annual_kWh() {
return (annual_hours_of_use() * get_watt() + annual_hours_of_standby() * watt_standby) / 1000.0f;
}
void print(int index, float price_in_KWh) {
cout << right << setw(column_width - 2) << index << ": " << fixed << setprecision(2)
<< get_description() << endl;
cout << right << setw(column_width) << "(this: " << this << ")" << endl;
cout << right << setw(column_width) << "power consumption: " << fixed << setprecision(2)
<< get_watt() << " W" << endl;
cout << right << setw(column_width) << "power consumption standby: " << fixed << setprecision(2)
<< watt_standby << " W" << endl;
cout << right << setw(column_width) << "annual hours of use: " << fixed << setprecision(2)
<< annual_hours_of_use() << " h" << endl;
cout << right << setw(column_width) << "annual hours of standby: " << fixed << setprecision(2)
<< annual_hours_of_standby() << " h" << endl;
cout << right << setw(column_width) << "annual consumption: " << fixed << setprecision(1)
<< annual_kWh() << " kWh" << endl;
cout << right << setw(column_width) << "annual costs: " << fixed << setprecision(2)
<< annual_kWh() * price_in_KWh << " EUR" << endl;
}
void input() {
Consumer::input();
cout << "how many watt standby it will have? ";
cin >> watt_standby;
cout << "how many hours it will be operating then? ";
cin >> hours;
}
virtual Device *get_a_copy() {
Immobile *obj = new Immobile();
*obj = *this;
return obj;
}
virtual string get_string_for_file(char separator) {
stringstream ss;
ss << "Immobile" << separator << get_description() << separator << get_watt()
<< separator << use_to_string(get_use()) << separator << watt_standby << separator << hours;
return ss.str();
}
};
class Mobile : public Consumer {
private:
float km;
public:
Mobile(Device *next = nullptr) : Consumer(next) {}
virtual ~Mobile() {
cout << "Mobile " << get_description() << " at address " << this << " deleted" << endl;
}
float get_km() const {
return km;
}
void set_km(float km) {
Mobile::km = km;
}
void input() {
Consumer::input();
cout << "how many kilometer will be driven? ";
cin >> km;
}
float frequency_of_use() {
if (get_use() == once)
return 1;
else if (get_use() == daily)
return 365.0f;
else if (get_use() == mo_fr)
return 260.0f;
else if (get_use() == sa_su)
return 104.0f;
else if (get_use() == weekly)
return 52.0f;
else
return 0;
}
virtual float annual_kWh() {
return frequency_of_use() * (km / 100.0f) * (get_watt() / 1000.0f);
}
virtual void print(int index, float price_in_KWh) {
cout << right << setw(column_width - 2) << index << ": " << fixed << setprecision(2)
<< get_description() << endl;
cout << right << setw(column_width) << "(this: " << this << ")" << endl;
cout << right << setw(column_width) << "power consumption 100 km: " << fixed << setprecision(2)
<< (get_watt() / 1000.0f) << " kW" << endl;
cout << right << setw(column_width) << "kilometer driven: " << fixed << setprecision(2)
<< km << " km " << use_to_string(get_use()) << endl;
cout << right << setw(column_width) << "annual consumption: " << fixed << setprecision(1)
<< annual_kWh() << " kWh" << endl;
cout << right << setw(column_width) << "annual costs: " << fixed << setprecision(2)
<< annual_kWh() * price_in_KWh << " EUR" << endl;
}
virtual Device *get_a_copy() {
Mobile *obj = new Mobile();
*obj = *this;
return obj;
}
virtual string get_string_for_file(char separator) {
stringstream ss;
ss << "Mobile" << separator << get_description() << separator << km
<< separator << use_to_string(get_use()) << separator << get_watt();
return ss.str();
}
};
class Producer : public Device {
public:
Producer(Device *next = nullptr) : Device(next) {}
virtual ~Producer() {
cout << "Producer " << get_description() << " at address " << this << " deleted" << endl;
}
void input() {
Device::input();
}
};
class Solar : public Producer {
private:
float watt_peak;
int year;
public:
Solar(Device *next = nullptr) : Producer(next) {}
virtual ~Solar() {
cout << "Solar " << get_description() << " at address " << this << " deleted" << endl;
}
float get_watt_peak() const {
return watt_peak;
}
void set_watt_peak(float wattPeak) {
watt_peak = wattPeak;
}
int get_year() const {
return year;
}
void set_year(int year) {
Solar::year = year;
}
void input() {
Producer::input();
cout << "how many watt peak have been installed? ";
cin >> watt_peak;
cout << "in which year the solar modules are installed? ";
cin >> year;
}
void print(int index, float price_in_KWh) {
cout << right << setw(column_width - 2) << index << ": " << fixed << setprecision(2)
<< get_description() << endl;
cout << right << setw(column_width) << "(this: " << this << ")" << endl;
cout << right << setw(column_width) << "solar cells installed power: " << fixed << setprecision(2)
<< watt_peak << " watt_peek" << endl;
cout << right << setw(column_width) << "Age of solar plant: " << fixed << setprecision(2)
<< (2024 - year) << " years" << endl;
cout << right << setw(column_width) << "annual production: " << fixed << setprecision(1)
<< annual_kWh() << " kWh" << endl;
cout << right << setw(column_width) << "annual savings: " << fixed << setprecision(2)
<< annual_kWh() * price_in_KWh << " EUR" << endl;
}
virtual float annual_kWh() {
return -1 * watt_peak * (1 - (2024 - year) * 0.005);
}
virtual Device *get_a_copy() {
Solar *obj = new Solar();
*obj = *this;
return obj;
}
virtual string get_string_for_file(char separator) {
stringstream ss;
ss << "Solar" << separator << get_description() << separator << year << separator << watt_peak;
return ss.str();
}
};
class Household {
private:
float price_kWh;
string power_supplier;
int persons;
float square_meters;
bool electric_water_heating;
Device *devices; // represents the devices of the devices list, it represents the connection between the households and consumer class in the class diagram
public:
Household() : devices(nullptr) {};
~Household() {
Device *current = get_devices();
Device *to_be_deleted;
while (current != nullptr) {
to_be_deleted = current;
Device *next = current->get_next();
current->set_next(nullptr);
delete to_be_deleted;
current = next;
}
cout << "Household at address " << this << " deleted " << endl;
}
float get_price_kWh() {
return price_kWh;
}
string get_power_supplier() {
return power_supplier;
}
int get_persons() {
return persons;
}
float get_square_meters() {
return square_meters;
}
bool get_electric_water_heating() {
return electric_water_heating;
}
Device *get_devices() {
return this->devices;
}
void set_price_kWh(float power_price) {
price_kWh = power_price;
}
void set_power_supplier(string supplier) {
power_supplier = supplier;
}
void set_persons(int person) {
persons = person;
}
void set_square_meters(float meters) {
square_meters = meters;
}
void set_electric_water_heating(bool heating) {
electric_water_heating = heating;
}
void set_devices(Device *consumer) {
this->devices = consumer;
}
// add device on front
void add_device_to_household(Device *pDevice) {
if (pDevice == nullptr) {
cerr << "Invalid" << endl;
}
pDevice->set_next(devices);
devices = pDevice;
}
// add device on last
void add_bk_device_to_household(Device *nDevice) {
if (nDevice == nullptr) {
cerr << "Invalid" << endl;
}
if (devices == nullptr) {
devices = nDevice;
return;
}
Device *current = devices;
while (current->get_next() != nullptr)
current = current->get_next();
current->set_next(nDevice);
}
Device *move_up(int k) {
if (devices == nullptr || k <= 1) {
return devices;
}
Device *current = devices;
Device *prev = nullptr;
Device *prev_2 = nullptr;
int currentPosition = 1;
while (current != nullptr && currentPosition < k) {
prev_2 = prev;
prev = current;
current = current->get_next();
currentPosition++;
}
if (current == nullptr)
return devices;
if (prev != nullptr) {
prev->set_next(current->get_next());
current->set_next(prev);
if (prev_2 != nullptr) {
prev_2->set_next(current);
} else {
return current;
}
}
return devices;
}
static void copy_devices(Household *from_household, Household *to_Household) {
Device *current = from_household->devices;
if (current == nullptr)
return;
Device *copyDevices = nullptr;
Device *copyCurrent = nullptr;
while (current != nullptr) {
Device *copy = current->get_a_copy();
if (copyDevices == nullptr)
copyDevices = copy;
else
copyCurrent->set_next(copy);
copyCurrent = copy;
current = current->get_next();
}
copyCurrent->set_next(to_Household->devices);
to_Household->set_devices(copyDevices);
}
void input() {
cout << "how many square metres does the households have? ";
cin >> square_meters;
cout << "how many persons live in this households? ";
cin >> persons;
char ishotwateruse;
cout << "Is hot water produced electrically? (y(es) or n(o)): ";
cin >> ishotwateruse;
electric_water_heating = ishotwateruse == 'y';
cout << "what is the price for one kWh in EUR? ";
cin >> price_kWh;
cout << "who is the power supplier? ";
getline(cin >> ws, power_supplier);
devices = nullptr;
}
void print(int household_number) {
cout << "H O U S E H O L D N O " << household_number << " P O W E R C O N S U M P T I O N" << endl;
cout << "----------------------------------------------------------------------" << endl;
cout << right << setw(column_width) << "(this: " << this << ")" << endl;
cout << right << setw(column_width) << "price for one kWh: " << fixed << setprecision(2)
<< get_price_kWh() * 100 << " ct/kWh" << endl;
cout << right << setw(column_width) << "power supplier: " << fixed << setprecision(2)
<< get_power_supplier() << endl;
cout << right << setw(column_width) << "square meters: " << fixed << setprecision(2)
<< get_square_meters() << " qm" << endl;
cout << right << setw(column_width) << "persons: " << get_persons() << endl;
cout << right << setw(column_width) << "water heated using electricity: "
<< (get_electric_water_heating() ? "yes" : "no") << endl;
cout << right << setw(column_width - 2) << "list of devices" << endl;
cout << "----------------------------------------------------------------------" << endl;
int index = 1;
Device *current = get_devices();
while (current) {
current->print(index, price_kWh);
current = current->get_next();
index++;
}
if (index > 1)
cout << "----------------------------------------------------------------------" << endl;
float totalPowerConsumptionPerSquareMeter;
float totalPowerConsumptionPerPerson;
float totalPowerConsumption;
if (get_electric_water_heating()) {
totalPowerConsumptionPerSquareMeter = get_square_meters() * avg_cons_sqm;
totalPowerConsumptionPerPerson = get_persons() * avg_water_heat;
totalPowerConsumption = totalPowerConsumptionPerSquareMeter + totalPowerConsumptionPerPerson;
} else {
totalPowerConsumptionPerSquareMeter = get_square_meters() * avg_cons_sqm;
totalPowerConsumptionPerPerson = get_persons() * avg_cons_person;
totalPowerConsumption = totalPowerConsumptionPerSquareMeter + totalPowerConsumptionPerPerson;
}
current = get_devices();
while (current) {
totalPowerConsumption += current->annual_kWh();
current = current->get_next();
}
cout << right << setw(column_width) << "power consumption square meters: " << fixed << setprecision(1)
<< totalPowerConsumptionPerSquareMeter << " kWh" << endl;
cout << right << setw(column_width) << "power consumption all persons: " << fixed << setprecision(1)
<< totalPowerConsumptionPerPerson << " kWh" << endl;
cout << right << setw(column_width) << "total annual power consumption: " << fixed << setprecision(1)
<< totalPowerConsumption << " kWh" << endl;
cout << right << setw(column_width) << "total annual power costs: " << fixed << setprecision(2)
<< totalPowerConsumption * get_price_kWh() << " EUR" << endl;
cout << endl;
}
};
class Address {
private:
string street;
string no;
string zip;
string city;
public:
// Parametrized constructor
Address(string street, string no, string zip, string city) : street(std::move(street)), no(std::move(no)),
zip(std::move(zip)), city(std::move(city)) {}
// Default constructor
Address() : street(""), no(""), zip(""), city("") {}
string get_street() {
return street;
}
string get_no() {
return no;
}
string get_zip() {
return zip;
}
string get_city() {
return city;
}
string to_string() {
string myAddress;
myAddress.append(street).append(" ").append(no).append(", ").append(zip).append(" ").append(city);
return myAddress;
}
void input() {
cout << "what is the street name? ";
getline(cin >> ws, street);
cout << "what is house number? ";
cin >> no;
cout << "what is zip code? ";
cin >> zip;
cout << "what is the city name? ";
getline(cin >> ws, city);
}
~Address() {
cout << "Address " << to_string() << " at address " << this << " deleted " << endl;
}
};
class House {
private:
Household **households;
int number_of_households;
Address address;
public:
// Parametrized constructor
House(int n_households, const Address &addres) : number_of_households(n_households), address(addres) {
households = new Household *[number_of_households];
for (int i = 0; i < number_of_households; ++i) {
households[i] = nullptr;
}
}
Household *get_household(int household_number) {
if (household_number >= 0 && household_number < number_of_households) {
return households[household_number];
}
return nullptr;
}
Household *operator[](int household_number) {
if (household_number >= 0 && household_number < number_of_households) {
return households[household_number];
}
return nullptr;
}
void set_household(Household *household, int number_of_household) {
Household *this_household = get_household(number_of_household);
if (this_household != nullptr) {
cout << "Household already exists! " << endl;
} else if (number_of_household > number_of_households) {
cout << "Invalid number of Household, the available number of households in this House: "
<< number_of_households << endl;
} else {
households[number_of_household] = household;
}
}
void print_all() {
cout << "======================================================================" << endl;
cout << right << setw(column_width) << " House " << endl;
cout << "======================================================================" << endl;
cout << right << setw(column_width) << "(this: " << &households << ") " << endl;
cout << right << setw(column_width) << "address: " << address.to_string() << " " << endl;
cout << right << setw(column_width) << "number of households: " << number_of_households << " " << endl;
cout << right << setw(column_width) << "total numbers of all devices: " << Consumer::get_total_consumers()
<< endl;
cout << "======================================================================" << endl;
for (int i = 0; i < number_of_households; i++) {
if (households[i] != nullptr) {
households[i]->print(i);
}
}
cout << "======================================================================" << endl;
}
void write_to_file(const string &file_name, char delim) {
ofstream file;
file.open(file_name);
file << "A6" << delim << number_of_households << delim << address.get_street() << delim
<< address.get_no() << delim << address.get_zip() << delim << address.get_city() << endl;
for (int i = 0; i < number_of_households; i++) {
if (households[i] != nullptr) {
file << "Household" << delim << i << delim
<< (households[i]->get_electric_water_heating() ? "true" : "false") << delim
<< households[i]->get_persons() << delim << households[i]->get_square_meters() << delim
<< households[i]->get_price_kWh() << delim << households[i]->get_power_supplier() << endl;
if (households[i]->get_devices()) {
Device *current = households[i]->get_devices();
while (current) {
file << current->get_string_for_file(delim) << endl;
current = current->get_next();
}
}
}
}
file.close();
}
static void read_from_file(House *&house, const string &file_name, char delim) {
ifstream file(file_name); // open file in read
string line;
int current_household_number;
while (getline(file, line)) {
if (line.find("A6") != string::npos) {
istringstream stream(line);
string element;
vector<string> elements;
while (getline(stream, element, delim)) {
elements.push_back(element);
}
int num_households = stoi(elements[1]);
house = new House(num_households, (Address) {elements[2], elements[3], elements[4], elements[5]});
} else if (line.find("Household") != string::npos) {
istringstream stream(line);
string element;
vector<string> elements;
while (getline(stream, element, delim)) {
elements.push_back(element);
}
int num_household = stoi(elements[1]);
current_household_number = num_household;
if (num_household >= 0) {
if (house->get_household(num_household) == nullptr) {
house->set_household(new Household(), num_household);
}
}
Household *nHousehold = house->get_household(num_household);
nHousehold->set_electric_water_heating(elements[2] == "true");
nHousehold->set_persons(stoi(elements[3]));
nHousehold->set_square_meters(stof(elements[4]));
nHousehold->set_price_kWh(stof(elements[5]));
nHousehold->set_power_supplier(elements[6]);
} else if (line.find("Mobile") != string::npos) {
istringstream stream(line);
string element;
vector<string> elements;
while (getline(stream, element, delim)) {
elements.push_back(element);
}
Mobile *nMobile = new Mobile();
nMobile->set_description(elements[1]);
nMobile->set_km(stof(elements[2]));
nMobile->set_use(findFrequencyOfUse(elements[3]));
nMobile->set_watt(stof(elements[4]));
nMobile->set_next(nullptr);
house->get_household(current_household_number)->add_bk_device_to_household(nMobile);
} else if (line.find("Immobile") != string::npos) {
istringstream stream(line);
string element;
vector<string> elements;
while (getline(stream, element, delim)) {
elements.push_back(element);
}
Immobile *nImmobile = new Immobile();
nImmobile->set_description(elements[1]);
nImmobile->set_hours(stof(elements[2]));
nImmobile->set_use(findFrequencyOfUse(elements[3]));
nImmobile->set_watt(stof(elements[4]));
nImmobile->set_watt_standby(stof(elements[5]));
nImmobile->set_next(nullptr);
house->get_household(current_household_number)->add_bk_device_to_household(nImmobile);
} else if (line.find("Solar") != string::npos) {
istringstream stream(line);
string element;
vector<string> elements;
while (getline(stream, element, delim)) {
elements.push_back(element);
}
Solar *nSolar = new Solar();
nSolar->set_description(elements[1]);
nSolar->set_year(stoi(elements[2]));
nSolar->set_watt_peak(stof(elements[3]));
nSolar->set_next(nullptr);
house->get_household(current_household_number)->add_bk_device_to_household(nSolar);
}
}
file.close();
}
~House() {
for (int i = 0; i < number_of_households; i++) {
if (households[i] != nullptr) {
delete households[i];
}
}
delete[] households;
cout << "House at address " << this << "deleted" << endl;
}
};
int main() {
House *myHouse = nullptr;
cout << " CALCULATION OF AVERAGE POWER COSTS FOR A HOUSEHOLD! - Class Version \n" << endl;
char opt = ' ';
while (opt != 'q') {
cout << "h house initialisation" << endl;
cout << "d delete house" << endl;
cout << "m input mobile consumer" << endl;
cout << "i input immobile consumer" << endl;
cout << "s input solar producer" << endl;
cout << "u move up power consumer" << endl;
cout << "n new household" << endl;
cout << "p print household" << endl;
cout << "a print all households" << endl;
cout << "c copy all devices (added to already existing ones)" << endl;
cout << "r read data from file" << endl;
cout << "w write data into file" << endl;
cout << "q quit" << endl;
cout << ">> ";
cin >> opt;
cin.ignore();
if (opt == 'p') { // print households
int number_of_household;
cout << "number of household? ";
cin >> number_of_household;
Household *household = myHouse->get_household(number_of_household);
if (household) {
household->print(number_of_household);
}
} else if (opt == 'h') { // initialize the House
int number_of_households;
cout << "how many households does the house have? ";
cin >> number_of_households;
Address addr;
addr.input();
myHouse = new House(number_of_households, addr);
} else if (opt == 'r') { // read from file
string file_name;
char delim;
cout << "input file name: ";
cin >> file_name;
cout << "input separator character: ";
cin >> delim;
cout << "input file " << file_name << " opened..." << endl;
House::read_from_file(myHouse, file_name, delim);
cout << "input file " << file_name << " closed" << endl;
} else if (opt == 'w') { // write to file
string file_name;
cout << "input file name: ";
cin >> file_name;
cout << "input separator character: ";
char delim;
cin >> delim;
cout << "output file " << file_name << " opened..." << endl;
myHouse->write_to_file(file_name, delim);
cout << "output file " << file_name << " closed" << endl;
} else if (opt == 'm') { // input mobile
int number_of_household;
cout << "number of household? ";
cin >> number_of_household;
if (myHouse) {
Household *household = myHouse->get_household(number_of_household);
if (household) {
Mobile *nMobile = new Mobile();
nMobile->input();
household->add_device_to_household(nMobile);
}
} else {
cout << "house is a nullptr, please first choose h to initialise a new house" << endl;
}
} else if (opt == 's') { // input solar
int number_of_household;
cout << "number of household? ";
cin >> number_of_household;
if (myHouse) {
Household *household = myHouse->get_household(number_of_household);
if (household) {
Solar *nSolar = new Solar();
nSolar->input();
household->add_device_to_household(nSolar);
}
} else {
cout << "house is a nullptr, please first choose h to initialise a new house" << endl;
}
} else if (opt == 'u') { // move up a device
int number_of_household;