School Commit Init
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
.vscode
|
||||
@@ -0,0 +1,15 @@
|
||||
# Lab Test 2
|
||||
|
||||
Solve the provided problem, using object oriented programming and C++.
|
||||
|
||||
Input at least 3 entities in your initial list (from the source code).
|
||||
|
||||
In order for functionalities to be graded the application must use layered architecture, as shown in the given UML diagram (a UI layer will also be necessary).
|
||||
|
||||
Using existing code is forbidden, you must start your application from scratch.
|
||||
|
||||
For function documentation you may use:
|
||||
- https://en.cppreference.com/w/
|
||||
- https://www.cplusplus.com/
|
||||
|
||||
Time: 60 minutes.
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "block.h"
|
||||
|
||||
Block::Block(int constructionYear, int totalApartments, int occupiedApartments){
|
||||
this->constructionYear = constructionYear;
|
||||
this->totalApartments = totalApartments;
|
||||
this->occupiedApartments = occupiedApartments;
|
||||
}
|
||||
|
||||
bool Block::mustBeRestored(){
|
||||
return (this->constructionYear < 1987) && (this->occupiedApartments/this->totalApartments>0.8);
|
||||
}
|
||||
|
||||
bool Block::canBeDemolished(){
|
||||
return (this->occupiedApartments/this->totalApartments<0.05);
|
||||
}
|
||||
|
||||
std::string Block::toString(){
|
||||
return "Block built in: " + std::to_string(this->constructionYear) + " with " + std::to_string(this->totalApartments) + " apartments, of which " + std::to_string(this->occupiedApartments) + " are occupied.";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "building.h"
|
||||
|
||||
class Block : public Building{
|
||||
private:
|
||||
int totalApartments;
|
||||
int occupiedApartments;
|
||||
public:
|
||||
Block(int constructionYear, int totalApartments, int occupiedApartments);
|
||||
~Block()=default;
|
||||
bool mustBeRestored() override;
|
||||
bool canBeDemolished() override;
|
||||
std::string toString() override;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Building{
|
||||
protected:
|
||||
int constructionYear;
|
||||
public:
|
||||
~Building()=default;
|
||||
int getConstructionYear(){return constructionYear;};
|
||||
virtual bool mustBeRestored()=0;
|
||||
virtual bool canBeDemolished()=0;
|
||||
virtual std::string toString()=0;
|
||||
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "house.h"
|
||||
|
||||
House::House(int constructionYear, bool isHistorical){
|
||||
this->constructionYear = constructionYear;
|
||||
this->isHistorical = isHistorical;
|
||||
}
|
||||
|
||||
bool House::mustBeRestored(){
|
||||
return (this->constructionYear < 1923);
|
||||
}
|
||||
|
||||
bool House::canBeDemolished(){
|
||||
return !this->isHistorical;
|
||||
}
|
||||
|
||||
std::string House::toString(){
|
||||
return "House built in: " + std::to_string(this->constructionYear) + " which is " + (this->isHistorical ? "" : "not ") + "historical.";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "building.h"
|
||||
#include "string"
|
||||
|
||||
class House : public Building{
|
||||
private:
|
||||
bool isHistorical;
|
||||
public:
|
||||
House(int constructionYear, bool isHistorical);
|
||||
~House()=default;
|
||||
bool mustBeRestored() override;
|
||||
bool canBeDemolished() override;
|
||||
std::string toString() override;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "ui.h"
|
||||
|
||||
int main(){
|
||||
UI ui;
|
||||
ui.run();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "service.h"
|
||||
#include <fstream>
|
||||
|
||||
void Service::addBuilding(Building* building){
|
||||
this->buildings.push_back(building);
|
||||
}
|
||||
|
||||
std::vector<Building*> Service::getAllBuildings(){
|
||||
return this->buildings;
|
||||
}
|
||||
|
||||
std::vector<Building*> Service::getAllToBeRestored(){
|
||||
std::vector<Building*> toBeRestored;
|
||||
for(auto building : this->buildings){
|
||||
if(building->mustBeRestored()){
|
||||
toBeRestored.push_back(building);
|
||||
}
|
||||
}
|
||||
return toBeRestored;
|
||||
}
|
||||
|
||||
std::vector<Building*> Service::getAllToBeDemolished(){
|
||||
std::vector<Building*> toBeDemolished;
|
||||
for(auto building : this->buildings){
|
||||
if(building->canBeDemolished()){
|
||||
toBeDemolished.push_back(building);
|
||||
}
|
||||
}
|
||||
return toBeDemolished;
|
||||
}
|
||||
|
||||
void Service::writeToFile(std::string fileName, std::vector<Building*> buildings){
|
||||
std::ofstream file(fileName);
|
||||
for(auto building : buildings){
|
||||
file << building->toString() << "\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "building.h"
|
||||
|
||||
class Service{
|
||||
private:
|
||||
std::vector<Building*> buildings;
|
||||
public:
|
||||
Service()=default;
|
||||
~Service()=default;
|
||||
void addBuilding(Building* building);
|
||||
std::vector<Building*> getAllBuildings();
|
||||
std::vector<Building*> getAllToBeRestored();
|
||||
std::vector<Building*> getAllToBeDemolished();
|
||||
void writeToFile(std::string fileName, std::vector<Building*> buildings);
|
||||
};
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "ui.h"
|
||||
#include "service.h"
|
||||
#include "building.h"
|
||||
#include "house.h"
|
||||
#include "block.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
UI::UI(){
|
||||
this->service.addBuilding(new House(1900, true));
|
||||
this->service.addBuilding(new House(1900, false));
|
||||
this->service.addBuilding(new House(2000, false));
|
||||
this->service.addBuilding(new Block(2000, 100, 2));
|
||||
this->service.addBuilding(new Block(1900, 100, 100));
|
||||
}
|
||||
|
||||
void UI::run(){
|
||||
while(true){
|
||||
std::cout << "1. Add building\n";
|
||||
std::cout << "2. Show all buildings\n";
|
||||
std::cout << "3. Show all buildings to be restored built in a given year\n";
|
||||
std::cout << "4. Write to file\n";
|
||||
std::cout << "0. Exit\n";
|
||||
|
||||
char option;
|
||||
std::cout << "Choose an option: ";
|
||||
std::cin >> option;
|
||||
|
||||
switch(option){
|
||||
case '1':{
|
||||
Building* building;
|
||||
char type;
|
||||
std::cout << "Choose the type of building (h - house, b - block): ";
|
||||
std::cin >> type;
|
||||
if(type == 'h'){
|
||||
int constructionYear;
|
||||
bool isHistorical;
|
||||
std::cout << "Construction year: ";
|
||||
std::cin >> constructionYear;
|
||||
std::cout << "Is historical (0 - no, 1 - yes): ";
|
||||
std::cin >> isHistorical;
|
||||
building = new House(constructionYear, isHistorical);
|
||||
}
|
||||
if(type == 'b'){
|
||||
int constructionYear;
|
||||
int totalApartments;
|
||||
int occupiedApartments;
|
||||
std::cout << "Construction year: ";
|
||||
std::cin >> constructionYear;
|
||||
std::cout << "Total apartments: ";
|
||||
std::cin >> totalApartments;
|
||||
std::cout << "Occupied apartments: ";
|
||||
std::cin >> occupiedApartments;
|
||||
building = new Block(constructionYear, totalApartments, occupiedApartments);
|
||||
}
|
||||
if(type != 'h' && type != 'b'){
|
||||
std::cout << "Invalid type!\n";
|
||||
break;
|
||||
}
|
||||
this->service.addBuilding(building);
|
||||
break;
|
||||
}
|
||||
case '2':{
|
||||
std::vector<Building*> buildings = this->service.getAllBuildings();
|
||||
for(auto building : buildings){
|
||||
std::cout << building->toString() << "\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '3':{
|
||||
int year;
|
||||
std::cout << "Year: ";
|
||||
std::cin >> year;
|
||||
std::vector<Building*> buildings = this->service.getAllToBeRestored();
|
||||
for(auto building : buildings){
|
||||
if(building->getConstructionYear() == year){
|
||||
std::cout << building->toString() << "\n";
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '4':{
|
||||
std::string fileName_restore;
|
||||
std::cout << "File name: (for restoring)";
|
||||
std::cin >> fileName_restore;
|
||||
std::vector<Building*> buildings = this->service.getAllToBeRestored();
|
||||
this->service.writeToFile(fileName_restore, buildings);
|
||||
std::string fileName_demolish;
|
||||
std::cout << "File name: (for demolishing)";
|
||||
std::cin >> fileName_demolish;
|
||||
buildings = this->service.getAllToBeDemolished();
|
||||
this->service.writeToFile(fileName_demolish, buildings);
|
||||
break;
|
||||
}
|
||||
case '0':{
|
||||
exit(0);
|
||||
}
|
||||
default:{
|
||||
std::cout << "Invalid option!\n";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "service.h"
|
||||
|
||||
class UI{
|
||||
private:
|
||||
Service service;
|
||||
public:
|
||||
UI();
|
||||
~UI()=default;
|
||||
void run();
|
||||
};
|
||||
Reference in New Issue
Block a user