diff options
Added files from project 1 (mirror)
8 files changed, 580 insertions, 0 deletions
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Event.cpp b/Year 1/Object-oriented Programming/Project 1/src/Models/Event.cpp new file mode 100644 index 0000000..c4557b7 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Event.cpp @@ -0,0 +1,153 @@ +#include "Event.h" +#include "Reservation.h" + +Event::Event() : hall(), name(), date() {} + +Event::Event(const Hall& hall, String name, Date date) : tickets(), reservations() { + this->hall = hall; + this->name = name; + this->date = date; +} + +OrderedList<Ticket>& Event::get_tickets() { + return tickets; +} + +const OrderedList<Ticket>& Event::get_tickets() const { + return tickets; +} + +OrderedList<Reservation>& Event::get_reservations() { + return reservations; +} + +const OrderedList<Reservation>& Event::get_reservations() const { + return reservations; +} + +const Hall& Event::get_hall() const { + return hall; +} + +const String& Event::get_name() const { + return name; +} + +const Date& Event::get_date() const { + return date; +} + +/*! \param istr An input stream + * + * Directly reads bytes from stream (calls read() function). + * Stored types must have defined a read() function. + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ifstream + */ +void Event::read(std::istream& istr) { + hall.read(istr); + name.read(istr); + date.read(istr); + tickets.read(istr); + reservations.read(istr); +} + +/*! \param ostr An output stream + * + * Directly writes bytes to stream (calls write() function). + * Stored types must have defined a read() function. + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ofstream + */ +void Event::write(std::ostream& ostr) { + hall.write(ostr); + name.write(ostr); + date.write(ostr); + tickets.write(ostr); + reservations.write(ostr); +} + +/*! \return compare of halls, or if they are equal, compare of dates + */ +int Event::compare(const Event& other) { + if (hall.compare(other.hall) == 0) + return date.compare(other.date); + return hall.compare(other.hall); +} + +/*! + * Uses the stream's >> operator to read and parse all internal values. + * Stored types must have defined operator >>. + * + * \note Best used with std::cin or text std::ifstream + */ +std::istream& operator>>(std::istream& istr, Event& event) { + Hall newHall; + String newString; + Date newDate; + OrderedList<Ticket> newTickets; + OrderedList<Reservation> newReservation; + + istr >> newHall >> newString >> newDate >> newTickets >> newReservation; + event = Event(newHall, newString, newDate); + event.get_tickets() = newTickets; + event.get_reservations() = newReservation; + + return istr; +} + +unsigned insertionOpSet = 0; + +/*! \param setting A number, defining the different options. It's best if it is passed as binary number. + * + * Sets a configuration setting for how operator<< should behave. Setting is stored as a global variable in the cpp file, so it's independent of Event instance. + * + * Each bit in the number is a certain setting. Each bit from left to right represents one of these options, from top to bottom: + * 1. Write count of bought tickets (available only if 5. is set) + * 2. Write count of reserved tickets (available only if 5. is set) + * 3. Write reservations + * 4. Write bought tickets + * 5. Write Hall information + * + * \par Example + * To configure printing the Hall information, bought tickets count and the bought tickets themselves, you would do: + * \code{.cpp} + * configureEventInsertionOp(0b10011); + * \endcode + */ +void configureEventInsertionOp(unsigned setting) { + insertionOpSet = setting; +} + +/*! + * Uses the stream's << operator to write all internal data, following setting from configureEventInsertionOp() + * Stored types must have defined operator <<. + * + * \note Best used with std::cout or text std::ofstream + */ +std::ostream& operator<<(std::ostream& ostr, const Event& event) { + if (insertionOpSet & 1) { + ostr << "| Hall: " << event.get_hall().get_number() << " | Name: " << event.get_name() << " | Date: " << event.get_date(); + + if (insertionOpSet & 8) + ostr << " || Tickets Count: " << event.get_tickets().get_count(); + if (insertionOpSet & 16) + ostr << " || Reservations Count: " << event.get_reservations().get_count(); + ostr << std::endl; + } + if (insertionOpSet & 2) { + ostr << "Bought tickets: "; + for (unsigned i = 0; i < event.get_tickets().get_count(); i++) + ostr << "[" << event.get_tickets()[i] << "] "; + ostr << std::endl; + } + if(insertionOpSet & 4) { + ostr << "Reserved tickets: "; + for (unsigned i = 0; i < event.get_reservations().get_count(); i++) + ostr << "[" << event.get_reservations()[i] << "] "; + ostr << std::endl; + } + return ostr; +} diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Event.h b/Year 1/Object-oriented Programming/Project 1/src/Models/Event.h new file mode 100644 index 0000000..1162b8f --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Event.h @@ -0,0 +1,63 @@ +#ifndef HEADER_MODEL_EVENT +#define HEADER_MODEL_EVENT + +/*! \file Event.h + * \brief Stores the declaration of class Event + */ + +#include "../Generic/List/OrderedList.hpp" +#include "../Generic/Date/Date.h" +#include "../Generic/String/String.h" +#include "Hall.h" +#include "Ticket.h" +#include "Reservation.h" +#include <istream> +#include <ostream> + +/*! \brief Stores an event's hall, name, date, bought tickets and reservations + */ +class Event { + Hall hall; + String name; + Date date; + OrderedList<Ticket> tickets; + OrderedList<Reservation> reservations; + +public: + //! Calls default constructors on data + Event(); + //! Copies given hall and sets name and date + Event(const Hall& hall, String name, Date date); + + //! Returns constant reference Hall in which event will be held + const Hall& get_hall() const; + //! Returns constant reference to name of event + const String& get_name() const; + //! Returns constant reference to date on which event will be held + const Date& get_date() const; + + //! Returns reference to bought tickets + OrderedList<Ticket>& get_tickets(); + //! Returns constant reference to bought tickets + const OrderedList<Ticket>& get_tickets() const; + //! Returns reference to reservations + OrderedList<Reservation>& get_reservations(); + //! Returns constant reference to reservations + const OrderedList<Reservation>& get_reservations() const; + + //! Reads Event from stream + void read(std::istream& istr); + //! Writes Event to stream + void write(std::ostream& ostr); + //! Compares two events + int compare(const Event& other); +}; + +//! Reads Event from stream with >> operator +std::istream& operator>>(std::istream& istr, Event& event); +//! Configures how Event should be shown, when written to stream with << operator +void configureEventInsertionOp(unsigned setting); +//! Writes Event to stream with << operator +std::ostream& operator<<(std::ostream& ostr, const Event& event); + +#endif diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.cpp b/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.cpp new file mode 100644 index 0000000..feacdac --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.cpp @@ -0,0 +1,76 @@ +#include "Hall.h" + +Hall::Hall() : Hall(0, 0, 0) {} + +Hall::Hall(int number) : Hall(number, 0, 0) {} + +Hall::Hall(int number, unsigned rows, unsigned seatsPerRow) { + this->number = number; + this->rows = rows; + this->seatsPerRow = seatsPerRow; +} + +int Hall::get_number() const { + return number; +} +unsigned Hall::get_rows() const { + return rows; +} + +unsigned Hall::get_seatsPerRow() const { + return seatsPerRow; +} + +/*! \param istr An input stream + * + * Directly reads bytes from stream (calls read() function). + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ifstream + */ +void Hall::read(std::istream& istr) { + istr.read((char*)&number, sizeof(number)); + istr.read((char*)&rows, sizeof(rows)); + istr.read((char*)&seatsPerRow, sizeof(seatsPerRow)); +} + +/*! \param ostr An output stream + * + * Directly writes bytes to stream (calls write() function). + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ofstream + */ +void Hall::write(std::ostream& ostr) const { + ostr.write((char*)&number, sizeof(number)); + ostr.write((char*)&rows, sizeof(rows)); + ostr.write((char*)&seatsPerRow, sizeof(seatsPerRow)); +} + +/*! \return -1 if number < other.number, 1 if number > other.number, 0 if number == other.number + */ +int Hall::compare(const Hall& other) const { + return (number > other.get_number()) - (number < other.get_number()); +} + +/*! + * Uses the stream's >> operator to read and parse all internal values. + * + * \note Best used with std::cin or text std::ifstream + */ +std::istream& operator>>(std::istream& istr, Hall& hall) { + int number; + unsigned rows, seatsPerRow; + istr >> number >> rows >> seatsPerRow; + hall = Hall(number, rows, seatsPerRow); + return istr; +} + +/*! + * Uses the stream's << operator to write all internal data. + * + * \note Best used with std::cout or text std::ofstream + */ +std::ostream& operator<<(std::ostream& ostr, const Hall& hall) { + return ostr << hall.get_number() << " " << hall.get_rows() << " "<< hall.get_seatsPerRow(); +} diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.h b/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.h new file mode 100644 index 0000000..c567c75 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Hall.h @@ -0,0 +1,47 @@ +#ifndef HEADER_MODEL_HALL +#define HEADER_MODEL_HALL + +/*! \file Hall.h + * \brief Stores the declaration of class Hall + */ + +#include <istream> +#include <ostream> + +/*! \brief Each hall contains a number, rows and seats per row + */ +class Hall { + int number; + unsigned rows; + unsigned seatsPerRow; + +public: + //! Sets values to 0 + Hall(); + //! Sets values, except number, to 0 + Hall(int number); + //! Sets values + Hall(int number, unsigned rows, unsigned seatsPerRow); + + //! Returns a Hall's number + int get_number() const; + //! Returns a Hall's row count + unsigned get_rows() const; + //! Return a Hall's seats per row count + unsigned get_seatsPerRow() const; + + //! Reads Hall from stream + void read(std::istream& istr); + //! Writes Hall to steam + void write(std::ostream& ostr) const; + //! Compare two Halls + int compare(const Hall& other) const; + +}; + +//! Reads Hall from stream with >> operator +std::istream& operator>>(std::istream& istr, Hall& hall); +//! Writes Hall to stream with << operator +std::ostream& operator<<(std::ostream& ostr, const Hall& hall); + +#endif diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.cpp b/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.cpp new file mode 100644 index 0000000..002ba29 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.cpp @@ -0,0 +1,79 @@ +#include "Reservation.h" +#include <cstring> + +Reservation::Reservation() : Reservation(Ticket(), "", "") {} + +Reservation::Reservation(const Ticket& ticket) : Reservation(ticket, "", "") {} + +Reservation::Reservation(const Ticket& ticket, const char* password, const char* note) { + this->ticket = ticket; + strncpy(this->password, password, PASSWORD_LEN); + strncpy(this->note, note, NOTE_LEN); +} + +const Ticket& Reservation::get_ticket() const { + return ticket; +} + +const char* Reservation::get_password() const { + return password; +} + +const char* Reservation::get_note() const { + return note; +} + +/*! \param istr An input stream + * + * Directly reads bytes from stream (calls read() function). + * Uses ticket's read() function. + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ifstream + */ +void Reservation::read(std::istream& istr) { + ticket.read(istr); + istr.read(password, sizeof(password)); + istr.read(note, sizeof(note)); +} + +/*! \param ostr An output stream + * + * Directly writes bytes to stream (calls write() function). + * Uses ticket's write() function. + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ofstream + */ +void Reservation::write(std::ostream& ostr) const { + ticket.write(ostr); + ostr.write(password, sizeof(password)); + ostr.write(note, sizeof(note)); +} + +/*! \return Compares tickets + */ +int Reservation::compare(const Reservation& other) const { + return ticket.compare(other.ticket); +} + +/*! + * Uses the stream's >> operator to read and parse the ticket. + * Password and note are left blank. + * + * \note Best used with std::cin or text std::ifstream + */ +std::istream& operator>>(std::istream& istr, Reservation& reservation) { + strncpy(reservation.password, "", PASSWORD_LEN); + strncpy(reservation.note, "", NOTE_LEN); + return istr >> reservation.ticket; +} + +/*! + * Uses the stream's << operator to write the ticket only. + * + * \note Best used with std::cout or text std::ofstream + */ +std::ostream& operator<<(std::ostream& ostr, const Reservation& reservation) { + return ostr << reservation.ticket; +} diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.h b/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.h new file mode 100644 index 0000000..10035b9 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Reservation.h @@ -0,0 +1,50 @@ +#ifndef HEADER_MODEL_RESERVATION +#define HEADER_MODEL_RESERVATION + +/*! \file Reservation.h + * \brief Stores the declaration of class Reservation + */ + +#include "Ticket.h" +#include <istream> +#include <ostream> + +#define PASSWORD_LEN 8 +#define NOTE_LEN 32 + +/*! \brief Each Reservations contains a ticket, a password and a note + */ +class Reservation { + Ticket ticket; + char password[PASSWORD_LEN]; + char note[NOTE_LEN]; + +public: + //! Sets ticket as default value and leaves password and note blank + Reservation(); + //! Sets ticket and leaves password and note blank + Reservation(const Ticket& ticket); + //! Sets ticket, password and note + Reservation(const Ticket& ticket, const char* password, const char* note); + + //! Returns constant reference to ticket + const Ticket& get_ticket() const; + //! Returns constant C-style string to password + const char* get_password() const; + //! Returns constant C-style string to note + const char* get_note() const; + + //! Reads Reservation from stream + void read(std::istream& istr); + //! Writes Reservation to stream + void write(std::ostream& ostr) const; + //! Compares two Reservations + int compare(const Reservation& other) const; + + //! Reads Reservation from stream with >> operator + friend std::istream& operator>>(std::istream& istr, Reservation& reservation); + //! Writes Reservation to stream with << operator + friend std::ostream& operator<<(std::ostream& ostr, const Reservation& reservation); +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp new file mode 100644 index 0000000..bc8ef84 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp @@ -0,0 +1,71 @@ +#include "Ticket.h" + +Ticket::Ticket() : Ticket(0, 0) {} + +Ticket::Ticket(unsigned row, unsigned seat) { + this->row = row; + this->seat = seat; +} + +unsigned Ticket::get_row() const { + return row; +} + +unsigned Ticket::get_seat() const { + return seat; +} + +/*! \param istr An input stream + * + * Directly reads bytes from stream (calls read() function). + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ifstream + */ +void Ticket::read(std::istream& istr) { + istr.read((char*)&row, sizeof(row)); + istr.read((char*)&seat, sizeof(seat)); +} + +/*! \param ostr An output stream + * + * Directly writes bytes to stream (calls write() function). + * + * \remark Doesn't alter the stream in any other way. + * \note Best used with binary ofstream + */ +void Ticket::write(std::ostream& ostr) const { + ostr.write((const char*)&row, sizeof(row)); + ostr.write((const char*)&seat, sizeof(seat)); +} + +/*! \return If rows are equal, -1 if seat < other.seat, 1 if seat > other.seat, 0 if seat == other.seat, else the same, but for rows + */ +int Ticket::compare(const Ticket& other) const { + if (row == other.row) + return (seat > other.seat) - (seat < other.seat); + return (row > other.row) - (row < other.row); +} + +/* Outside of class */ + +/*! + * Uses the stream's >> operator to read and parse row and seat. + * + * \note Best used with std::cin or text std::ifstream + */ +std::istream& operator>>(std::istream& istr, Ticket& ticket) { + unsigned row, seat; + istr >> row >> seat; + ticket = Ticket(row, seat); + return istr; +} + +/*! + * Uses the stream's << operator to write row and seat, separated by a space. + * + * \note Best used with std::cout or text std::ofstream + */ +std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket) { + return ostr << ticket.get_row() << " " << ticket.get_seat(); +} diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.h b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.h new file mode 100644 index 0000000..06b4571 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.h @@ -0,0 +1,41 @@ +#ifndef HEADER_HEADER_TICKET +#define HEADER_HEADER_TICKET + +/*! \file Ticket.h + * \brief Stores the declaration of class Ticket + */ + +#include <istream> +#include <ostream> + +/*! \brief Each Ticket contains a row and a seat number + */ +class Ticket { + unsigned row; + unsigned seat; + +public: + //! Sets row and seat to zeroes + Ticket(); + //! Sets row and seat + Ticket(unsigned row, unsigned seat); + + //! Returns row + unsigned get_row() const; + //! Returns seat + unsigned get_seat() const; + + //! Reads Ticket from stram + void read(std::istream& istr); + //! Writes Ticket to stream + void write(std::ostream& ostr) const; + //! Compares two Tickets + int compare(const Ticket& other) const; +}; + +//! Reads Ticket from stream with >> operator +std::istream& operator>>(std::istream& istr, Ticket& ticket); +//! Writes Ticket to stream with << operator +std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket); + +#endif |
