consolidate all repos to one for archive
This commit is contained in:
60
semester_2/programiranje_2/primeri/Example12/Date.cpp
Normal file
60
semester_2/programiranje_2/primeri/Example12/Date.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "Date.h"
|
||||
|
||||
Date::Date() : day(1), month(1), year(1971) {}
|
||||
|
||||
//Date::Date(const Date& dat) : day(dat.day), month(dat.month), year(dat.year) {}
|
||||
|
||||
Date::Date(int d, int m, int y) : day(d), month(m), year(y) {
|
||||
}
|
||||
|
||||
Date::~Date() {}
|
||||
|
||||
int Date::getDay() const {
|
||||
return day;
|
||||
}
|
||||
|
||||
void Date::setDay(int day) {
|
||||
if (day < 1 || day > 31) {
|
||||
std::cout << "Wrong day: " << day << std::endl;
|
||||
this->day = 1;
|
||||
}
|
||||
this->day = day;
|
||||
}
|
||||
|
||||
int Date::getMonth() const {
|
||||
return month;
|
||||
}
|
||||
|
||||
void Date::setMonth(int month) {
|
||||
if (month < 1 || month > 12) {
|
||||
std::cout << "Wrong month: " << month << std::endl;
|
||||
this->month = 1;
|
||||
}
|
||||
this->month = month;
|
||||
}
|
||||
|
||||
int Date::getYear() const {
|
||||
return year;
|
||||
}
|
||||
|
||||
void Date::setYear(int year) {
|
||||
this->year = year;
|
||||
}
|
||||
|
||||
const std::string Date::toString() const {
|
||||
// A stringstream associates a string object with a stream allowing you to read
|
||||
// from the string as if it were a stream (like cin).
|
||||
std::stringstream ss;
|
||||
ss << day << "." << month << "." << year;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool Date::isEqual(const Date& second) const {
|
||||
if (day==second.day && month == second.month && year == second.year)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
32
semester_2/programiranje_2/primeri/Example12/Date.h
Normal file
32
semester_2/programiranje_2/primeri/Example12/Date.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef EXAMPLE12_DATE_H
|
||||
#define EXAMPLE12_DATE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Date {
|
||||
private:
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
public:
|
||||
Date();
|
||||
// Date(const Date& date);
|
||||
Date(int d, int m, int l);
|
||||
virtual ~Date();
|
||||
|
||||
int getDay() const;
|
||||
void setDay(int day);
|
||||
int getMonth() const;
|
||||
void setMonth(int month);
|
||||
int getYear() const;
|
||||
void setYear(int year);
|
||||
|
||||
// A std::string is a class which defines objects that be represented as stream of characters.
|
||||
// Size of the character array has to allocated statically, more memory cannot be allocated
|
||||
// at run time if required. Unused allocated memory is wasted in case of character array.
|
||||
// In case of strings, memory is allocated dynamically. More memory can be allocated at run time
|
||||
// on demand. As no memory is preallocated, no memory is wasted.
|
||||
const std::string toString() const;
|
||||
bool isEqual(const Date& date) const;
|
||||
};
|
||||
#endif //EXAMPLE12_DATE_H
|
138
semester_2/programiranje_2/primeri/Example12/FlightTicket.cpp
Normal file
138
semester_2/programiranje_2/primeri/Example12/FlightTicket.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <sstream>
|
||||
#include "FlightTicket.h"
|
||||
|
||||
int FlightTicket::numberOfFirstClass = 0;
|
||||
|
||||
FlightTicket::FlightTicket() :
|
||||
price(0.0), flightFrom("From?"), flightTo("To?"), addBaggage(false),
|
||||
ticketType(FlightTicketType::Economy) {
|
||||
ptrFlightDate = new Date(1, 1, 1971);
|
||||
}
|
||||
|
||||
FlightTicket::FlightTicket(const FlightTicket& t) :
|
||||
price(t.price), flightFrom(t.flightFrom), flightTo(t.flightTo),
|
||||
addBaggage(t.addBaggage), ticketType(t.ticketType), ptrFlightDate(t.ptrFlightDate) {
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
FlightTicket::FlightTicket(double price, std::string ff, std::string ft,
|
||||
bool bag, FlightTicketType type, Date* date1) :
|
||||
price(price), flightFrom(ff), flightTo(ft), addBaggage(bag),
|
||||
ticketType(type), ptrFlightDate(date1) {
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
FlightTicket::~FlightTicket() {
|
||||
}
|
||||
|
||||
double FlightTicket::getTotalPrice() const {
|
||||
double sum = price;
|
||||
|
||||
if (FlightTicketType::Business == ticketType)
|
||||
sum *= 1.20;
|
||||
else if (FlightTicketType::FirstClass == ticketType)
|
||||
sum *= 1.50;
|
||||
if (addBaggage == true)
|
||||
sum += 50;
|
||||
return sum;
|
||||
}
|
||||
|
||||
std::string FlightTicket::toString() const {
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "Flight ticket details: " << "\n";
|
||||
ss << "\tPrice: " << price << "\n";
|
||||
ss << "\tAdditional baggage: " << (addBaggage ? "Yes" : "No") << "\n";
|
||||
ss << "\tFrom: " << flightFrom << "\n";
|
||||
ss << "\tTo: " << flightTo << "\n";
|
||||
ss << "\tTicket type: " << getTicketTypeString()
|
||||
<< "\n";
|
||||
ss << "\tDeparture: " << this->ptrFlightDate->toString() << "\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
double FlightTicket::getPrice() const {
|
||||
return price;
|
||||
}
|
||||
|
||||
void FlightTicket::setPrice(double price) {
|
||||
this->price = price;
|
||||
}
|
||||
bool FlightTicket::isAddBaggage() const {
|
||||
return addBaggage;
|
||||
}
|
||||
|
||||
void FlightTicket::setAddBaggage(bool addBaggage) {
|
||||
this->addBaggage = addBaggage;
|
||||
}
|
||||
|
||||
Date* FlightTicket::getFlightDate() const {
|
||||
return ptrFlightDate;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightDate(Date* flightDate) {
|
||||
this->ptrFlightDate = flightDate;
|
||||
}
|
||||
|
||||
const std::string& FlightTicket::getFlightFrom() const {
|
||||
return flightFrom;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightFrom(const std::string& flightFrom) {
|
||||
this->flightFrom = flightFrom;
|
||||
}
|
||||
|
||||
const std::string& FlightTicket::getFlightTo() const {
|
||||
return flightTo;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightTo(const std::string& flightTo) {
|
||||
this->flightTo = flightTo;
|
||||
}
|
||||
|
||||
FlightTicketType FlightTicket::getTicketType() const {
|
||||
return ticketType;
|
||||
}
|
||||
|
||||
void FlightTicket::setTicketType(FlightTicketType ticketType) {
|
||||
if (ticketType < FlightTicketType::Economy
|
||||
|| ticketType > FlightTicketType::FirstClass) {
|
||||
std::cout << "Wrong flight ticket type ";
|
||||
this->ticketType = FlightTicketType::Economy;
|
||||
} else
|
||||
this->ticketType = ticketType;
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
std::string FlightTicket::getTicketTypeString() const {
|
||||
switch (ticketType) {
|
||||
case FlightTicketType::Economy:
|
||||
return "Economy";
|
||||
break;
|
||||
case FlightTicketType::Business:
|
||||
return "Business";
|
||||
break;
|
||||
case FlightTicketType::FirstClass:
|
||||
return "First Class";
|
||||
break;
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
int FlightTicket::GetNumberOfFirstClass() {
|
||||
return numberOfFirstClass;
|
||||
}
|
||||
|
||||
bool FlightTicket::isEqual(const FlightTicket& drugi) const {
|
||||
if (this->price == drugi.price && this->addBaggage == drugi.addBaggage
|
||||
&& this->getTicketType() == drugi.getTicketType()
|
||||
&& this->flightFrom == drugi.flightFrom
|
||||
&& this->flightTo == drugi.flightTo
|
||||
&& this->ptrFlightDate->isEqual(*drugi.ptrFlightDate))
|
||||
return true;
|
||||
return false;
|
||||
}
|
58
semester_2/programiranje_2/primeri/Example12/FlightTicket.h
Normal file
58
semester_2/programiranje_2/primeri/Example12/FlightTicket.h
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
#ifndef EXAMPLE12_FLIGHTTICKET_H
|
||||
#define EXAMPLE12_FLIGHTTICKET_H
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "Date.h"
|
||||
|
||||
//C++11 has introduced enum classes (also called scoped enumerations),
|
||||
//that makes enumerations both strongly typed and strongly scoped.
|
||||
//Class enum doesn't allow implicit conversion to int, and also doesn't compare enumerators
|
||||
//from different enumerations.
|
||||
enum class FlightTicketType {
|
||||
Economy = 0,
|
||||
Business = 1,
|
||||
FirstClass = 2
|
||||
};
|
||||
|
||||
class FlightTicket {
|
||||
protected:
|
||||
double price;
|
||||
std::string flightFrom;
|
||||
std::string flightTo;
|
||||
bool addBaggage;
|
||||
FlightTicketType ticketType;
|
||||
Date* ptrFlightDate;
|
||||
static int numberOfFirstClass;
|
||||
public:
|
||||
FlightTicket();
|
||||
FlightTicket(const FlightTicket& t);
|
||||
FlightTicket(double price, std::string ff,std::string ft, bool bag, FlightTicketType type, Date* p_date);
|
||||
virtual ~FlightTicket();
|
||||
|
||||
//get/set methods
|
||||
double getPrice() const;
|
||||
void setPrice(double price);
|
||||
const std::string& getFlightFrom() const;
|
||||
void setFlightFrom(const std::string& flightFrom);
|
||||
const std::string& getFlightTo() const;
|
||||
void setFlightTo(const std::string& flightTo);
|
||||
bool isAddBaggage() const;
|
||||
void setAddBaggage(bool addBaggage);
|
||||
FlightTicketType getTicketType() const;
|
||||
void setTicketType(FlightTicketType ticketType);
|
||||
std::string getTicketTypeString() const;
|
||||
Date* getFlightDate() const;
|
||||
void setFlightDate(Date* flightDate);
|
||||
|
||||
//methods
|
||||
virtual double getTotalPrice() const;
|
||||
virtual std::string toString() const;
|
||||
virtual bool isEqual(const FlightTicket& drugi) const;
|
||||
|
||||
//static method
|
||||
static int GetNumberOfFirstClass();
|
||||
};
|
||||
|
||||
#endif //EXAMPLE12_FLIGHTTICKET_H
|
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "ReturnFlightTicket.h"
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket() : FlightTicket() {
|
||||
this->ptrReturnDate = new Date(1, 1, 1971);
|
||||
}
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket(const ReturnFlightTicket& rft) :
|
||||
FlightTicket(rft), ptrReturnDate(rft.ptrReturnDate) {
|
||||
}
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket(double price, std::string ff,
|
||||
std::string ft, bool bag, FlightTicketType type, Date* p_d1,
|
||||
Date* p_d2) :
|
||||
FlightTicket(price, ff, ft, bag, type, p_d1), ptrReturnDate(p_d2) {
|
||||
}
|
||||
|
||||
ReturnFlightTicket::~ReturnFlightTicket() {
|
||||
}
|
||||
|
||||
std::string ReturnFlightTicket::toString() const {
|
||||
std::stringstream ss;
|
||||
ss << FlightTicket::toString();
|
||||
ss << "\tReturn On: " << this->ptrReturnDate->toString() << "\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool ReturnFlightTicket::isEqual(const ReturnFlightTicket& second) const {
|
||||
if (FlightTicket::isEqual(second)
|
||||
&& this->ptrReturnDate->isEqual(*second.ptrReturnDate))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
double ReturnFlightTicket::getTotalPrice() const {
|
||||
return FlightTicket::getTotalPrice() * 2;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,25 @@
|
||||
|
||||
#ifndef EXAMPLE12_RETURNFLIGHTTICKET_H
|
||||
#define EXAMPLE12_RETURNFLIGHTTICKET_H
|
||||
|
||||
#include "FlightTicket.h"
|
||||
#include "Date.h"
|
||||
|
||||
class ReturnFlightTicket: public FlightTicket {
|
||||
private:
|
||||
Date* ptrReturnDate;
|
||||
public:
|
||||
ReturnFlightTicket();
|
||||
ReturnFlightTicket(const ReturnFlightTicket& rft);
|
||||
ReturnFlightTicket(double price, std::string ff, std::string ft, bool bag, FlightTicketType type, Date* p_d1, Date* p_d2);
|
||||
~ReturnFlightTicket();
|
||||
|
||||
//methods
|
||||
std::string toString() const;
|
||||
double getTotalPrice() const;
|
||||
bool isEqual(const ReturnFlightTicket& second) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLE12_RETURNFLIGHTTICKET_H
|
28
semester_2/programiranje_2/primeri/Example12/main.cpp
Normal file
28
semester_2/programiranje_2/primeri/Example12/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include "FlightTicket.h"
|
||||
#include "ReturnFlightTicket.h"
|
||||
|
||||
int main() {
|
||||
Date* ptrDate1 = new Date(17, 3, 2020);
|
||||
Date* ptrDate2 = new Date(18, 3, 2020);
|
||||
Date* ptrDate3 = new Date(19, 3, 2020);
|
||||
Date* ptrDate4 = new Date(29, 3, 2020);
|
||||
Date* ptrDate5 = new Date(31, 3, 2020);
|
||||
FlightTicket* ptrFlights[3];
|
||||
ptrFlights[0] = new FlightTicket(400, "Moskva", "Ljubljana", false, FlightTicketType::Economy, ptrDate1);
|
||||
ptrFlights[1] = new ReturnFlightTicket(1000, "Hong Kong", "Washington", true,
|
||||
FlightTicketType::FirstClass, ptrDate2, ptrDate4);
|
||||
ptrFlights[2] = new ReturnFlightTicket(800, "Vienna", "Chicago",
|
||||
false, FlightTicketType::Economy, ptrDate3, ptrDate5);
|
||||
for (int i=0; i<3; i++)
|
||||
std::cout << ptrFlights[i]->toString() << std::endl;
|
||||
delete ptrDate1;
|
||||
delete ptrDate2;
|
||||
delete ptrDate3;
|
||||
delete ptrDate4;
|
||||
delete ptrDate5;
|
||||
for (int i=0; i<3; i++)
|
||||
delete ptrFlights[i];
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user