Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit71c57cde42e99301aeb079be4ef368ff155f99c5 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 14:05:56 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 14:05:56 +0300
Filesb8d0c07bd7514077d4595cac1b0704cf05b96ebd
uni-projects-71c57cde42e99301aeb079be4ef368ff155f99c5.tar
uni-projects-71c57cde42e99301aeb079be4ef368ff155f99c5.tar.gz
uni-projects-71c57cde42e99301aeb079be4ef368ff155f99c5.zip
Parentff171c3bc508e4a82e2329c550532f60a8be2e38 (diff)
Added my prep work for exam2
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.cpp
new file mode 100644
index 0000000..9eb6cc4
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.cpp
@@ -0,0 +1,12 @@
+#include "Automobile.h"
+
+Automobile::Automobile(String brand, unsigned maxSpeed) : Vehicle(brand, (maxSpeed >= 180 && maxSpeed <= 240) ? maxSpeed : 180)
+{}
+
+unsigned Automobile::getSpeedOnRoad(Road road) const {
+ return Vehicle::getMaxSpeed();
+}
+
+Automobile* Automobile::clone() const {
+ return new Automobile(*this);
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.h
new file mode 100644
index 0000000..a468f2d
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Automobile.h
@@ -0,0 +1,9 @@
+#include "Vehicle.h"
+
+class Automobile : public Vehicle {
+public:
+ Automobile(String brand, unsigned maxSpeed);
+
+ unsigned getSpeedOnRoad(Road road) const override;
+ Automobile* clone() const override;
+};
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp
new file mode 100644
index 0000000..3490293
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp
@@ -0,0 +1,59 @@
+#include "Garage.h"
+
+void Garage::copyFrom(const Garage& other) {
+ vehicles = new Vehicle*[other.maxCapacity];
+ for (unsigned i = 0; i < other.count; i++)
+ vehicles[i] = other.vehicles[i]->clone();
+ count = other.count;
+ maxCapacity = other.maxCapacity;
+}
+
+void Garage::free() {
+ for (unsigned i = 0; i < count; i++)
+ delete vehicles[i];
+ delete[] vehicles;
+}
+
+Garage::Garage(unsigned maxCapacity) {
+ if (maxCapacity == 0)
+ maxCapacity = 1;
+
+ vehicles = new Vehicle*[maxCapacity];
+ this->maxCapacity = maxCapacity;
+ count = 0;
+}
+
+bool Garage::parkVehicle(const Vehicle& vehicle) {
+ if (count == maxCapacity)
+ return false;
+
+ vehicles[count++] = vehicle.clone();
+ return true;
+}
+
+unsigned Garage::countOfCapableVehiclesForOrder(Road road, double roadLength, double time) {
+ unsigned counter = 0;
+ for (unsigned i = 0; i < count; i++) {
+ if (vehicles[i]->getSpeedOnRoad(road) >= roadLength / time)
+ counter++;
+ }
+ return counter;
+}
+
+Garage::Garage() : Garage(1) {}
+
+Garage::Garage(const Garage& other) {
+ copyFrom(other);
+}
+
+Garage& Garage::operator=(const Garage& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Garage::~Garage() {
+ free();
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.h
new file mode 100644
index 0000000..83781a4
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.h
@@ -0,0 +1,20 @@
+#include "Vehicle.h"
+
+class Garage {
+ Vehicle** vehicles;
+ unsigned count;
+ unsigned maxCapacity;
+
+ void copyFrom(const Garage& other);
+ void free();
+
+public:
+ Garage(unsigned maxCapacity);
+ bool parkVehicle(const Vehicle& vehicle);
+ unsigned countOfCapableVehiclesForOrder(Road road, double roadLength, double time);
+
+ Garage();
+ Garage(const Garage& other);
+ Garage& operator=(const Garage& other);
+ ~Garage();
+};
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.cpp
new file mode 100644
index 0000000..ee43100
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.cpp
@@ -0,0 +1,16 @@
+#include "Minivan.h"
+
+Minivan::Minivan(String brand, unsigned maxSpeed) : Vehicle(brand, (maxSpeed >= 120 && maxSpeed <= 160) ? maxSpeed : 120)
+{}
+
+unsigned Minivan::getSpeedOnRoad(Road road) const {
+ switch (road) {
+ case Speedy: return Vehicle::getMaxSpeed() / 2;
+ case ThirdClass: return Vehicle::getMaxSpeed() / 3;
+ default: return Vehicle::getMaxSpeed();
+ }
+}
+
+Minivan* Minivan::clone() const {
+ return new Minivan(*this);
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.h
new file mode 100644
index 0000000..97f4031
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Minivan.h
@@ -0,0 +1,9 @@
+#include "Vehicle.h"
+
+class Minivan : public Vehicle {
+public:
+ Minivan(String brand, unsigned maxSpeed);
+
+ unsigned getSpeedOnRoad(Road road) const override;
+ Minivan* clone() const override;
+};
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Road.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Road.h
new file mode 100644
index 0000000..860e5c7
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Road.h
@@ -0,0 +1,10 @@
+#ifndef HEADER_ROAD
+#define HEADER_ROAD
+
+enum Road {
+ ThirdClass,
+ Speedy,
+ Highway,
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp
new file mode 100644
index 0000000..84c0046
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp
@@ -0,0 +1,36 @@
+#include "String.h"
+#include <cstring>
+
+void String::copyFrom(const String& other) {
+ length = other.length;
+ str = new char[length];
+ strncpy(str, other.str, length);
+}
+
+void String::free() {
+ delete[] str;
+}
+
+String::String(const char* str) {
+ length = strlen(str);
+ this->str = new char[length + 1];
+ strncpy(this->str, str, length + 1);
+}
+
+String::String() : String("") {}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::~String() {
+ free();
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/String.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.h
new file mode 100644
index 0000000..55be00c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.h
@@ -0,0 +1,19 @@
+#ifndef HEADER_STRING
+#define HEADER_STRING
+
+class String {
+ char *str;
+ unsigned length;
+
+ void copyFrom(const String& other);
+ void free();
+public:
+ String(const char* str);
+
+ String();
+ String(const String& other);
+ String& operator=(const String& other);
+ ~String();
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.cpp
new file mode 100644
index 0000000..a023979
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.cpp
@@ -0,0 +1,18 @@
+#include "Truck.h"
+#include "Vehicle.h"
+
+Truck::Truck(String brand, unsigned maxSpeed) : Vehicle(brand, (maxSpeed >= 80 && maxSpeed <= 140) ? maxSpeed : 80)
+{}
+
+unsigned Truck::getSpeedOnRoad(Road road) const {
+ switch (road) {
+ case Speedy: return Vehicle::getMaxSpeed() / 4;
+ case ThirdClass: return 10;
+ case Highway: return Vehicle::getMaxSpeed() / 2;
+ }
+ return Vehicle::getMaxSpeed();
+}
+
+Truck* Truck::clone() const {
+ return new Truck(*this);
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.h
new file mode 100644
index 0000000..914a479
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Truck.h
@@ -0,0 +1,9 @@
+#include "Vehicle.h"
+
+class Truck : public Vehicle {
+public:
+ Truck(String brand, unsigned maxSpeed);
+
+ unsigned getSpeedOnRoad(Road road) const override;
+ Truck* clone() const override;
+};
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.cpp
new file mode 100644
index 0000000..2d06f14
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.cpp
@@ -0,0 +1,12 @@
+#include "Vehicle.h"
+
+Vehicle::Vehicle(const String& brand, unsigned maxSpeed) {
+ this->brand = brand;
+ this->maxSpeed = maxSpeed;
+}
+
+unsigned Vehicle::getMaxSpeed() const {
+ return maxSpeed;
+}
+
+Vehicle::~Vehicle() {}
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.h b/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.h
new file mode 100644
index 0000000..d052162
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Vehicle.h
@@ -0,0 +1,21 @@
+#ifndef HEADER_VEHICLE
+#define HEADER_VEHICLE
+
+#include "String.h"
+#include "Road.h"
+
+class Vehicle {
+ String brand;
+ unsigned maxSpeed;
+
+public:
+ Vehicle(const String& brand, unsigned maxSpeed);
+
+ unsigned getMaxSpeed() const;
+ virtual unsigned getSpeedOnRoad(Road road) const = 0;
+
+ virtual Vehicle* clone() const = 0;
+ virtual ~Vehicle();
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/main.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/main.cpp
new file mode 100644
index 0000000..b75084d
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/main.cpp
@@ -0,0 +1,15 @@
+#include <iostream>
+#include "Garage.h"
+#include "Automobile.h"
+#include "Minivan.h"
+#include "Truck.h"
+
+int main() {
+ Garage myGarage(5);
+ myGarage.parkVehicle(Automobile("Audi", 200));
+ myGarage.parkVehicle(Minivan("Volkswagen", 126));
+ myGarage.parkVehicle(Truck("Ford", 140));
+
+ std::cout << myGarage.countOfCapableVehiclesForOrder(Highway, 200, 1)
+ << myGarage.countOfCapableVehiclesForOrder(Highway, 200, 2);
+}