Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models
diff options
context:
space:
mode:
Commitb844597fd23bc57aa97e231fc90eecfd6322cecc (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-07 08:28:27 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-07 08:28:27 +0300
Files86a4112ecfa1e3a54dbcf44a7ddf37575ba4aa39 /src/Models
FMI-OOP-P1_TicketSystem-b844597fd23bc57aa97e231fc90eecfd6322cecc.tar
FMI-OOP-P1_TicketSystem-b844597fd23bc57aa97e231fc90eecfd6322cecc.tar.gz
FMI-OOP-P1_TicketSystem-b844597fd23bc57aa97e231fc90eecfd6322cecc.zip
Parent0910afb91c8f9672be1d6184f47fb75947ea14f1 (diff)
Reorganized whole architecture
8 files changed, 313 insertions, 0 deletions
-rw-r--r--src/Models/Event.cpp76
-rw-r--r--src/Models/Event.h41
-rw-r--r--src/Models/Hall.cpp34
-rw-r--r--src/Models/Hall.h25
-rw-r--r--src/Models/Reservation.cpp37
-rw-r--r--src/Models/Reservation.h29
-rw-r--r--src/Models/Ticket.cpp45
-rw-r--r--src/Models/Ticket.h26
diff --git a/src/Models/Event.cpp b/src/Models/Event.cpp
new file mode 100644
index 0000000..e6105eb
--- /dev/null
+++ b/src/Models/Event.cpp
@@ -0,0 +1,76 @@
+#include "Event.h"
+#include "Reservation.h"
+#include "StatusCodes.h"
+
+Event::Event() : Event(nullptr, "", Date()) {}
+
+Event::Event(const Hall* hall, String name, Date date) : tickets(), reservations() {
+ this->hall = hall;
+ this->name = name;
+ this->date = date;
+}
+
+const OrderedList<Ticket>& Event::get_tickets() const {
+ return tickets;
+}
+
+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;
+}
+
+StatusCode Event::reserveTicket(const Ticket& ticket, const char* password, const char* note) {
+ if (tickets.contain(ticket)) return E_TicketAlreadyBought;
+ if (reservations.contain(ticket)) return E_TicketAlreadyReserved;
+
+ reservations.insert(Reservation(ticket, password, note));
+ return Success;
+}
+
+StatusCode Event::cancelReservation(unsigned row, unsigned seat) {
+ return cancelReservation(Ticket(row, seat));
+}
+
+StatusCode Event::cancelReservation(const Ticket& ticket) {
+ unsigned ind = reservations.findIndex(ticket);
+ if (ind == reservations.get_count())
+ return W_TicketHadNotBeenReserved;
+
+ reservations.removeAt(ind);
+ return Success;
+}
+
+StatusCode Event::buyTicket(const Ticket& ticket) {
+ if (tickets.contain(ticket)) return E_TicketAlreadyBought;
+ if (reservations.contain(ticket)) return E_TicketAlreadyReserved;
+
+ tickets.insert(ticket);
+ return Success;
+}
+
+StatusCode Event::buyTicketFromReservation(const Reservation& reservation, const char* password) {
+ unsigned resInd = reservations.findIndex(reservation);
+ if (resInd == reservations.get_count()) return E_ReservationDoesNotExist;
+
+ if (!reservations[resInd].validPassword(password)) return E_WrongReservationPassword;
+
+ reservations.removeAt(resInd);
+ tickets.insert(reservation.get_ticket());
+ return Success;
+}
+
+int Event::compare(const Event& other) {
+ // TODO: improve
+ return name.compare(other.name);
+}
diff --git a/src/Models/Event.h b/src/Models/Event.h
new file mode 100644
index 0000000..43ad5d9
--- /dev/null
+++ b/src/Models/Event.h
@@ -0,0 +1,41 @@
+#ifndef HEADER_EVENTSYSTEM_EVENT
+#define HEADER_EVENTSYSTEM_EVENT
+
+#include "../List/OrderedList.hpp"
+#include "../Date/Date.h"
+#include "../String/String.h"
+#include "../Venue/Hall.h"
+#include "Ticket.h"
+#include "Reservation.h"
+#include "StatusCodes.h"
+
+class Event {
+ const Hall* hall;
+ String name;
+ Date date;
+ OrderedList<Ticket> tickets;
+ OrderedList<Reservation> reservations;
+
+public:
+ Event();
+ Event(const Hall* hall, String name, Date date);
+
+ const Hall& get_hall() const;
+ const String& get_name() const;
+ const Date& get_date() const;
+ const OrderedList<Ticket>& get_tickets() const;
+ const OrderedList<Reservation>& get_reservations() const;
+
+ StatusCode reserveTicket(const Ticket& ticket, const char* password, const char* note);
+
+ StatusCode cancelReservation(unsigned row, unsigned seat);
+ StatusCode cancelReservation(const Ticket& ticket);
+
+ StatusCode buyTicket(const Ticket& ticket);
+
+ StatusCode buyTicketFromReservation(const Reservation& reservation, const char* password);
+
+ int compare(const Event& other);
+};
+
+#endif
diff --git a/src/Models/Hall.cpp b/src/Models/Hall.cpp
new file mode 100644
index 0000000..98b8cbb
--- /dev/null
+++ b/src/Models/Hall.cpp
@@ -0,0 +1,34 @@
+#include "Hall.h"
+
+Hall::Hall() : Hall(0, 0, 0) {}
+
+Hall::Hall(int index, unsigned rowCount, unsigned seatsPerRow) {
+ this->index = index;
+ this->rowCount = rowCount;
+ this->seatsPerRow = seatsPerRow;
+}
+
+int Hall::get_index() const {
+ return index;
+}
+void Hall::set_index(int index) {
+ this->index = index;
+}
+
+unsigned Hall::get_rowCount() const {
+ return rowCount;
+}
+void Hall::set_rowCount(unsigned rowCount) {
+ this->rowCount = rowCount;
+}
+
+unsigned Hall::get_seatsPerRow() const {
+ return seatsPerRow;
+}
+void Hall::set_seatsPerRow(unsigned seatsPerRow) {
+ this->seatsPerRow = seatsPerRow;
+}
+
+int Hall::compare(const Hall& other) {
+ return (index > other.get_index()) - (index < other.get_index());
+}
diff --git a/src/Models/Hall.h b/src/Models/Hall.h
new file mode 100644
index 0000000..bd2f82a
--- /dev/null
+++ b/src/Models/Hall.h
@@ -0,0 +1,25 @@
+#ifndef HEADER_HALL
+#define HEADER_HALL
+
+class Hall {
+ int index;
+ unsigned rowCount;
+ unsigned seatsPerRow;
+
+public:
+ Hall();
+ Hall(int index, unsigned rowCount, unsigned seatsPerRow);
+
+ int get_index() const;
+ void set_index(int index);
+
+ unsigned get_rowCount() const;
+ void set_rowCount(unsigned rowCount);
+
+ unsigned get_seatsPerRow() const;
+ void set_seatsPerRow(unsigned seatsPerRow);
+
+ int compare(const Hall& other);
+};
+
+#endif
diff --git a/src/Models/Reservation.cpp b/src/Models/Reservation.cpp
new file mode 100644
index 0000000..26278b2
--- /dev/null
+++ b/src/Models/Reservation.cpp
@@ -0,0 +1,37 @@
+#include "Reservation.h"
+#include <cstring>
+#include <ostream>
+
+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;
+ strcpy(this->password, password);
+ strcpy(this->note, note);
+}
+
+bool Reservation::validPassword(const char* password) const {
+ return !strcmp(this->password, password);
+}
+
+const Ticket& Reservation::get_ticket() const {
+ return ticket;
+}
+
+void Reservation::read(std::istream& istr) {
+ ticket.read(istr);
+ istr.read(password, sizeof(password));
+ istr.read(note, sizeof(note));
+}
+
+void Reservation::write(std::ostream& ostr) const {
+ ticket.write(ostr);
+ ostr.write(password, sizeof(password));
+ ostr.write(note, sizeof(note));
+}
+
+int Reservation::compare(const Reservation& other) const {
+ return ticket.compare(other.ticket);
+}
diff --git a/src/Models/Reservation.h b/src/Models/Reservation.h
new file mode 100644
index 0000000..1a415b5
--- /dev/null
+++ b/src/Models/Reservation.h
@@ -0,0 +1,29 @@
+#ifndef HEADER_EVENTSYSTEM_RESERVATION
+#define HEADER_EVENTSYSTEM_RESERVATION
+
+#include "Ticket.h"
+#include <istream>
+#include <ostream>
+
+#define PASSWORD_LEN 8
+#define NOTE_LEN 32
+
+class Reservation {
+ Ticket ticket;
+ char password[PASSWORD_LEN];
+ char note[NOTE_LEN];
+
+public:
+ Reservation();
+ Reservation(const Ticket& ticket);
+ Reservation(const Ticket& ticket, const char* password, const char* note);
+
+ bool validPassword(const char* password) const;
+ const Ticket& get_ticket() const;
+
+ void read(std::istream& istr);
+ void write(std::ostream& ostr) const;
+ int compare(const Reservation& other) const;
+};
+
+#endif
diff --git a/src/Models/Ticket.cpp b/src/Models/Ticket.cpp
new file mode 100644
index 0000000..e37cccf
--- /dev/null
+++ b/src/Models/Ticket.cpp
@@ -0,0 +1,45 @@
+#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;
+}
+
+void Ticket::read(std::istream& istr) {
+ istr.read((char*)&row, sizeof(row));
+ istr.read((char*)&row, sizeof(seat));
+}
+
+void Ticket::write(std::ostream& ostr) const {
+ ostr.write((const char*)&row, sizeof(row));
+ ostr.write((const char*)&seat, sizeof(seat));
+}
+
+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);
+}
+
+/* General */
+
+std::istream& operator>>(std::istream& istr, Ticket& ticket) {
+ unsigned row, seat;
+ istr >> row >> seat;
+ ticket = Ticket(row, seat);
+ return istr;
+}
+
+std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket) {
+ return ostr << ticket.get_row() << " " << ticket.get_seat();
+}
diff --git a/src/Models/Ticket.h b/src/Models/Ticket.h
new file mode 100644
index 0000000..5d26e35
--- /dev/null
+++ b/src/Models/Ticket.h
@@ -0,0 +1,26 @@
+#ifndef HEADER_EVENTSYSTEM_TICKET
+#define HEADER_EVENTSYSTEM_TICKET
+
+#include <istream>
+#include <ostream>
+
+class Ticket {
+ unsigned row;
+ unsigned seat;
+
+public:
+ Ticket();
+ Ticket(unsigned row, unsigned seat);
+
+ unsigned get_row() const;
+ unsigned get_seat() const;
+
+ void read(std::istream& istr);
+ void write(std::ostream& ostr) const;
+ int compare(const Ticket& other) const;
+};
+
+std::istream& operator>>(std::istream& istr, Ticket& ticket);
+std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket);
+
+#endif