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,69 @@
|
||||
# Assignment 04-05
|
||||
## Requirements
|
||||
- You will be given one of the problems below to solve.
|
||||
- The application must be implemented in C++ and use layered architecture.
|
||||
- Provide tests and specifications for non-trivial functions outside the UI. Test coverage must be at least 98% for all layers, except the UI.
|
||||
- Have at least 10 entities in your memory repository.
|
||||
- Validate the input data.
|
||||
- Handle the following situations:
|
||||
- If an entity that already exists is added, a message is shown and the entity is not stored. You must decide what makes an entity unique.
|
||||
- If the user tries to update/delete an entity that does not exist, a message will be shown and there will be no effect on the list of entities.
|
||||
|
||||
## Week 5
|
||||
- Solve the requirements related to the administrator mode.
|
||||
- Define the `DynamicVector` class which provides the specific operations: `add`, `remove`, `length`, etc. The array of elements must be dynamically allocated.
|
||||
|
||||
## Week 6
|
||||
- Solve all problem requirements.
|
||||
- `DynamicVector` must be a class template.
|
||||
|
||||
## Problem Statements
|
||||
|
||||
### Keep calm and adopt a pet
|
||||
The *“Keep calm and adopt a pet”* shelter needs a software application to help them find adoptive parents for the dogs they are taking care of. The application can be used in two modes: `administrator` and `user`. When the application is started, it will offer the option to select the mode.\
|
||||
**Administrator mode:** The application will have a database that holds all the dogs in the shelter at a given moment. The shelter employees must be able to update the database, meaning: add a new dog, delete a dog (when the dog is adopted) and update the information of a dog. Each **Dog** has a `breed`, a `name`, an `age` and a `photograph`. The photograph is memorised as a link towards an online resource (the photograph on the presentation site of the centre). The administrators will also have the option to see all the dogs in the shelter.\
|
||||
**User mode:** A user can access the application and choose one or more dogs to adopt. The application will allow the user to:
|
||||
- See the dogs in the database, one by one. When the user chooses this option, the data of the first dog (breed, name, age) is displayed, along with its photograph.
|
||||
- Choose to adopt the dog, in which case the dog is added to the user’s adoption list.
|
||||
- Choose not to adopt the dog and to continue to the next. In this case, the information corresponding to the next dog is shown and the user is again offered the possibility to adopt it. This can continue as long as the user wants, as when arriving to the end of the list, if the user chooses next, the application will again show the first dog.
|
||||
- See all the dogs of a given breed, having an age less than a given number. If no breed is provided, then all the dogs will be considered. The functionalities outlined above apply again in this case.
|
||||
- See the adoption list.
|
||||
|
||||
## Local Movie Database
|
||||
So many movies, so little time. To make sure you do not miss any good movies, you absolutely need a software application to help you manage your films and create watch lists. The application can be used in two modes: administrator and user. When the application is started, it will offer the option to choose the mode.\
|
||||
**Administrator mode:** The application will have a database which holds all the movies. You must be able to update the database, meaning: add a new movie, delete a movie and update the information of a movie. Each **Movie** has a `title`, a `genre`, a `year of release`, a `number of likes` and a `trailer`. The trailer is memorised as a link towards an online resource. The administrators will also have the option to see all the movies in the database.\
|
||||
**User mode:** A user can create a watch list with the movies that she wants to watch. The application will allow the user to:
|
||||
- See the movies in the database having a given genre (if the genre is empty, see all the movies), one by one. When the user chooses this option, the data of the first movie (title, genre, year of release, number of likes) is displayed and the trailer is played in the browser.
|
||||
- If the user likes the trailer, she can choose to add the movie to her watch list.
|
||||
- If the trailer is not satisfactory, the user can choose not to add the movie to the watch list and to continue to the next. In this case, the information corresponding to the next movie is shown and the user is again offered the possibility to add it to the watch list. This can continue as long as the user wants, as when arriving to the end of the list of movies with the given genre, if the user chooses next, the application will again show the first movie.
|
||||
- Delete a movie from the watch list, after the user watched the movie. When deleting a movie from the watch list, the user can also rate the movie (with a like), and in this case, the number of likes the movie has in the repository will be increased.
|
||||
- See the watch list.
|
||||
|
||||
## Proper Trench Coats
|
||||
Trench coats are cool. Everyone should own a trench coat. The *“Proper Trench Coats”* store sells fashionable, elegant trench coats and the store needs software to allow their customers to order online. The application can be used in two modes: administrator and user. When the application is started, it will offer the option to choose the mode.\
|
||||
**Administrator mode:** The application will have a database which holds available trench coats at a given moment. The store employees must be able to update the database, meaning: add a new trench coat, delete a trench coat (when it is sold out) and update the information of a trench coat. Each **Trench Coat** has a `size`, a `colour`, a `price`, a `quantity` and a `photograph`. The photograph is memorised as a link towards an online resource (the photograph on the presentation site of the store). The administrators will also have the option to see all the trench coats in the store.\
|
||||
**User mode:** A user can access the application and choose one or more trench coats to buy. The application will allow the user to:
|
||||
- See the trench coats in the database, having a given size, one by one. If the size is empty, then all the trench coats will be considered. When the user chooses this option, the data of the first trench coat (size, colour, price, quantity) is displayed, along with its photograph.
|
||||
- Choose to add the trench to the shopping basket. In this case, the price is added to the total sum the user has to pay. The total sum will be shown after each purchase.
|
||||
- Choose not to add the trench coat to the basket and to continue to the next. In this case, the information corresponding to the next trench coat is shown and the user is again offered the possibility to buy it. This can continue as long as the user wants, as when arriving to the end of the list, if the user chooses next, the application will again show the first trench coat.
|
||||
- See the shopping basket and the total price of the items.
|
||||
|
||||
## Master C++
|
||||
You are very passionate about programing (otherwise you wouldn't be reading this) and C++ is a language close to your heart. On your way to becoming a guru, you study a lot and watch many tutorials. To make sure you do not miss any good tutorials, you absolutely need a software application to help you manage your tutorials and create watch lists. The application can be used in two modes: administrator and user. When the application is started, it will offer the option to choose the mode.\
|
||||
**Administrator mode:** The application will have a database , which holds all the tutorials. You must be able to update the database, meaning: add a new tutorial, delete a tutorial and update the information of a tutorial. Each **Tutorial** has a `title`, a `presenter` (name of the presenter person), a `duration` (minutes and seconds), a number of `likes` and a `link` towards the online resource containing the tutorial. The administrators will also have the option to see all the tutorials in the database.\
|
||||
**User mode:** A user can create a watch list with the tutorials that he/she wants to watch. The application will allow the user to:
|
||||
- See the tutorials in the database having a given presenter (if the presenter name is empty, see all the tutorials), one by one. When the user chooses this option, the data of the first tutorial (title, presenter, duration, number of likes) is displayed and the tutorial is played in the browser.
|
||||
- If the user likes the tutorial, he/she can choose to add it to his/her tutorial watch list.
|
||||
- If the tutorial seems uninteresting, the user can choose not to add it to the watch list and continue to the next. In this case, the information corresponding to the next tutorial is shown and the user is again offered the possibility to add it to the watch list. This can continue as long as the user wants, as when arriving to the end of the list of tutorials with the given presenter, if the user chooses next, the application will again show the first tutorial.
|
||||
- Delete a tutorial from the watch list, after the user watched the tutorial. When deleting a tutorial from the watch list, the user can also rate the tutorial (with a like), and in this case, the number of likes for the tutorial will be increased.
|
||||
- See the watch list.
|
||||
|
||||
## Life After School
|
||||
Lectures, seminars and labs ... school in general must be taken seriously; BUT so must be your social life and leisure time. To manage the latter and be always informed about the interesting events happening in your city you will implement a software application. The application can be used in two modes: administrator and user. When the application is started, it will offer the option to choose the mode.\
|
||||
**Administrator mode:** The application will have a database which holds all the interesting events in your area. You must be able to update the database, meaning: add a new event, delete an event and update the information of an event. Each **Event** has a `title`, a `description`, a `date and time`, a `number of people` who are going and a `link` towards the online resource containing the event. The administrators will also have the option to see all the events in the database.\
|
||||
**User mode:** A user can create a list with the events that he/she is interested in. The application will allow the user to:
|
||||
- See the events in the database for a given month (if there is no month given, see all the events, ordered chronologically), one by one. When the user chooses this option, the data of the first event (title, description, date and time, number of people who are going) is displayed, and the event is opened in the browser (e.g. Facebook event).
|
||||
- If the user wants to participate in the event he/she can choose to add it to his/her events list. When this happens, the number of people who are going to the event increases (for the event in the repository).
|
||||
- If the event seems uninteresting, the user can choose not to add it to the list and continue to the next. In this case, the information corresponding to the next event is shown, and the user is again offered the possibility to add it to his/her list. This can continue as long as the user wants, as when arriving to the end of the list of events for the given month, if the user chooses next, the application will again show the first event.
|
||||
- Delete an event from the list. When the user deletes an event from his/her list, the number of people who are going to the event decreases.
|
||||
- See the list of events the user wants to attend.
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
#include "admin_controller.h"
|
||||
#include "../../exceptions/exceptions.h"
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
|
||||
AdminController::AdminController(Repository* repo){
|
||||
this->_repo = repo;
|
||||
}
|
||||
AdminController::~AdminController(){
|
||||
delete this->_repo;
|
||||
}
|
||||
void AdminController::add(int size,const char* colour,int price,int quantity,const char* photograph){
|
||||
if(size<=0){
|
||||
throw AdminControllerException("Size must be greater than 0");
|
||||
}
|
||||
if(strlen(colour)==0){
|
||||
throw AdminControllerException("Colour must not be empty");
|
||||
}
|
||||
if(price<=0){
|
||||
throw AdminControllerException("Price must be greater than 0");
|
||||
}
|
||||
if(quantity<0){
|
||||
throw AdminControllerException("Quantity must be greater than or equal to 0");
|
||||
}
|
||||
if(strlen(photograph)==0){
|
||||
throw AdminControllerException("Photograph must not be empty");
|
||||
}
|
||||
Trench* trench = new Trench(size,colour,price,quantity,photograph);
|
||||
try{
|
||||
this->_repo->add(trench);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
delete trench;
|
||||
throw AdminControllerException(e.what());
|
||||
}
|
||||
}
|
||||
void AdminController::remove(int size,const char* colour,const char* photograph){
|
||||
if(size<=0){
|
||||
throw AdminControllerException("Size must be greater than 0");
|
||||
}
|
||||
if(strlen(colour)==0){
|
||||
throw AdminControllerException("Colour must not be empty");
|
||||
}
|
||||
if(strlen(photograph)==0){
|
||||
throw AdminControllerException("Photograph must not be empty");
|
||||
}
|
||||
Trench* trench = new Trench(size,colour,0,0,photograph);
|
||||
try{
|
||||
this->_repo->remove(trench);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
delete trench;
|
||||
throw AdminControllerException(e.what());
|
||||
}
|
||||
delete trench;
|
||||
}
|
||||
void AdminController::remove(int index){
|
||||
this->_repo->remove(index);
|
||||
}
|
||||
void AdminController::update(int size,const char* colour,int price,int quantity,const char* photograph){
|
||||
if(size<=0){
|
||||
throw AdminControllerException("Size must be greater than 0");
|
||||
}
|
||||
if(strlen(colour)==0){
|
||||
throw AdminControllerException("Colour must not be empty");
|
||||
}
|
||||
if(price!=-1 && price<=0){
|
||||
throw AdminControllerException("Price must be greater than 0");
|
||||
}
|
||||
if(quantity!=-1 && quantity<0){
|
||||
throw AdminControllerException("Quantity must be greater than or equal to 0");
|
||||
}
|
||||
if(strlen(photograph)==0){
|
||||
throw AdminControllerException("Photograph must not be empty");
|
||||
}
|
||||
Trench* trench = new Trench(size,colour,price,quantity,photograph);
|
||||
int index = this->_repo->get(trench);
|
||||
delete trench;
|
||||
if(index==-1){
|
||||
throw AdminControllerException("Trench does not exist");
|
||||
}
|
||||
this->_repo->update(index,price,quantity);
|
||||
|
||||
}
|
||||
int AdminController::get(int size,const char* colour,const char* photograph){
|
||||
if(size<=0){
|
||||
throw AdminControllerException("Size must be greater than 0");
|
||||
}
|
||||
if(strlen(colour)==0){
|
||||
throw AdminControllerException("Colour must not be empty");
|
||||
}
|
||||
if(strlen(photograph)==0){
|
||||
throw AdminControllerException("Photograph must not be empty");
|
||||
}
|
||||
Trench* trench = new Trench(size,colour,0,0,photograph);
|
||||
int index = this->_repo->get(trench);
|
||||
delete trench;
|
||||
return index;
|
||||
|
||||
}
|
||||
int AdminController::size(){
|
||||
return this->_repo->size();
|
||||
}
|
||||
Trench*& AdminController::operator[](int index){
|
||||
return (*this->_repo)[index];
|
||||
}
|
||||
Repository* AdminController::getAll(){
|
||||
return this->_repo;
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
#include "../../repository/repository.h"
|
||||
|
||||
class AdminController {
|
||||
private:
|
||||
Repository* _repo;
|
||||
public:
|
||||
|
||||
/*
|
||||
* Constructor for the AdminController class
|
||||
* Input: repo - Repository object
|
||||
* Output: adminController object
|
||||
*/
|
||||
AdminController(Repository* repo);
|
||||
|
||||
/*
|
||||
* Destructor for the AdminController class
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
~AdminController();
|
||||
|
||||
/*
|
||||
* Adds a trench coat to the repository
|
||||
* Input: size - integer
|
||||
* colour - string
|
||||
* price - integer
|
||||
* quantity - integer
|
||||
* photograph - string
|
||||
* Output: -
|
||||
* Throws: AdminControllerException if: size<=0
|
||||
* colour is empty
|
||||
* price<=0
|
||||
* quantity<0
|
||||
* photograph is empty
|
||||
* trench coat already exists
|
||||
*/
|
||||
void add(int size,const char* colour,int price,int quantity,const char* photograph);
|
||||
|
||||
/*
|
||||
* Removes a trench coat from the repository
|
||||
* Input: size - integer
|
||||
* colour - string
|
||||
* photograph - string
|
||||
* Output: -
|
||||
* Throws: AdminControllerException if: size<=0
|
||||
* colour is empty
|
||||
* photograph is empty
|
||||
* trench coat does not exist
|
||||
*/
|
||||
void remove(int size,const char* colour,const char* photograph);
|
||||
|
||||
/*
|
||||
* Removes a trench coat from the repository
|
||||
* Input: index - integer
|
||||
* Output: -
|
||||
* Throws: RepoException if: index is out of bounds
|
||||
*/
|
||||
void remove(int index);
|
||||
|
||||
/*
|
||||
* Updates a trench coat from the repository. If the price or quantity is -1, it will not be updated
|
||||
* Input: size - integer
|
||||
* colour - string
|
||||
* price - integer
|
||||
* quantity - integer
|
||||
* photograph - string
|
||||
* Output: -
|
||||
* Throws: AdminControllerException if: size<=0
|
||||
* colour is empty
|
||||
* price!=-1 and price<=0
|
||||
* quantity!=-1 and quantity<0
|
||||
* photograph is empty
|
||||
* trench coat does not exist
|
||||
*/
|
||||
void update(int size,const char* colour,int price,int quantity,const char* photograph);
|
||||
|
||||
/*
|
||||
* Returns the index of a trench coat in the repository. If it does not exist, it returns -1
|
||||
* Input: size - integer
|
||||
* colour - string
|
||||
* photograph - string
|
||||
* Output: index - integer
|
||||
*/
|
||||
int get(int size,const char* colour,const char* photograph);
|
||||
|
||||
/*
|
||||
* Returns the number of trench coats in the repository
|
||||
* Input: -
|
||||
* Output: size - integer
|
||||
*/
|
||||
int size();
|
||||
|
||||
/*
|
||||
* Returns the trench coat at the given index
|
||||
* Input: index - integer
|
||||
* Output: trench coat - Trench object
|
||||
* Throws: RepoException if: index is out of bounds
|
||||
*/
|
||||
Trench*& operator[](int index);
|
||||
|
||||
/*
|
||||
* Returns an iterator for all the trench coats in the repository
|
||||
* Input: -
|
||||
* Output: iterator
|
||||
*/
|
||||
Repository* getAll();
|
||||
|
||||
};
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../vector/vector.h"
|
||||
#include "user_controller.h"
|
||||
#include <stdio.h>
|
||||
|
||||
UserController::UserController(Repository* repo,Repository* shoppingRepository) {
|
||||
this->_repo = repo;
|
||||
this->_shoppingRepository = shoppingRepository;
|
||||
}
|
||||
|
||||
UserController::~UserController() {
|
||||
delete this->_shoppingRepository;
|
||||
}
|
||||
|
||||
void UserController::addProductToShoppingList(Trench* trench) {
|
||||
int index = this->_shoppingRepository->get(trench);
|
||||
int repo_index = this->_repo->get(trench);
|
||||
if(index==-1) {
|
||||
Trench* trenchCopy = new Trench(trench->size(),trench->colour(),trench->price(),1,trench->photograph());
|
||||
this->_shoppingRepository->add(trenchCopy);
|
||||
trench->quantity(trench->quantity()-1);
|
||||
(*this->_repo)[repo_index]->quantity((*this->_repo)[repo_index]->quantity()-1);
|
||||
return;
|
||||
}
|
||||
(*this->_shoppingRepository)[index]->quantity((*this->_shoppingRepository)[index]->quantity()+1);
|
||||
trench->quantity(trench->quantity()-1);
|
||||
(*this->_repo)[repo_index]->quantity((*this->_repo)[repo_index]->quantity()-1);
|
||||
|
||||
}
|
||||
|
||||
void UserController::emptyShoppingList() {
|
||||
for(Trench* i : *this->_shoppingRepository) {
|
||||
int index = this->_repo->get(i);
|
||||
(*this->_repo)[index]->quantity((*this->_repo)[index]->quantity()+i->quantity());
|
||||
}
|
||||
while(this->_shoppingRepository->size()){
|
||||
this->_shoppingRepository->remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
void UserController::buyShoppingList() {
|
||||
while(this->_shoppingRepository->size()){
|
||||
this->_shoppingRepository->remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
int UserController::shoppingListTotal() {
|
||||
int total = 0;
|
||||
for(Trench* i : *this->_shoppingRepository) {
|
||||
total+=i->quantity()*i->price();
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
Vector<Trench*> UserController::getTrenchCarousel(int size) {
|
||||
Vector<Trench*> trenchCarousel;
|
||||
for(Trench* i : *this->_repo){
|
||||
if((size == -1||i->size()==size) && i->quantity()>0){
|
||||
Trench* copy = new Trench(*i);
|
||||
trenchCarousel.add(copy);
|
||||
}
|
||||
}
|
||||
return trenchCarousel;
|
||||
}
|
||||
|
||||
Vector<Trench*> UserController::getShoppingListTrench(){
|
||||
Vector<Trench*> trenchCarousel;
|
||||
for(Trench* i : *this->_shoppingRepository){
|
||||
Trench* copy = new Trench(*i);
|
||||
trenchCarousel.add(copy);
|
||||
}
|
||||
return trenchCarousel;
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../vector/vector.h"
|
||||
|
||||
class UserController {
|
||||
private:
|
||||
Repository* _repo;
|
||||
Repository* _shoppingRepository;
|
||||
public:
|
||||
|
||||
/*
|
||||
* Constructor for the UserController class
|
||||
* Input: repo - Repository object
|
||||
* Output: userController object
|
||||
*/
|
||||
UserController(Repository* repo,Repository* shoppingRepository);
|
||||
|
||||
/*
|
||||
* Destructor for the UserController class
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
~UserController();
|
||||
|
||||
/*
|
||||
* Adds a trench coat to the shopping list
|
||||
* Input: trench - Trench object
|
||||
* Output: -
|
||||
*/
|
||||
void addProductToShoppingList(Trench* trench);
|
||||
|
||||
/*
|
||||
* Empties the shopping list
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
void emptyShoppingList();
|
||||
|
||||
/*
|
||||
* Buys the shopping list
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
void buyShoppingList();
|
||||
|
||||
/*
|
||||
* Returns the total price of the shopping list
|
||||
* Input: -
|
||||
* Output: total - integer
|
||||
*/
|
||||
int shoppingListTotal();
|
||||
|
||||
|
||||
/*
|
||||
* Returns a vector of trench coats that are in the shopping list
|
||||
* Input: size - integer
|
||||
* Output: trenchCarousel - Vector<Trench*> object
|
||||
*/
|
||||
Vector<Trench*> getTrenchCarousel(int size);
|
||||
|
||||
Vector<Trench*> getShoppingListTrench();
|
||||
};
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "trench.h"
|
||||
#include <string.h>
|
||||
#include <exception>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
|
||||
Trench::Trench(int size, const char *colour, int price, int quantity, const char *photograph){
|
||||
this->_size = size;
|
||||
this->_colour = new char[255];
|
||||
strcpy(this->_colour,colour);
|
||||
this->_price = price;
|
||||
this->_quantity = quantity;
|
||||
this->_photograph=new char[255];
|
||||
strcpy(this->_photograph,photograph);
|
||||
}
|
||||
|
||||
Trench::~Trench(){
|
||||
delete[] this->_colour;
|
||||
delete[] this->_photograph;
|
||||
}
|
||||
|
||||
Trench::Trench(const Trench &trench){
|
||||
_size = trench.size();
|
||||
_colour = new char[255];
|
||||
strcpy(_colour,trench.colour());
|
||||
_price = trench.price();
|
||||
_quantity = trench.quantity();
|
||||
_photograph = new char[255];
|
||||
strcpy(_photograph,trench.photograph());
|
||||
}
|
||||
|
||||
int Trench::size() const {
|
||||
return this->_size;
|
||||
}
|
||||
|
||||
void Trench::size(int newSize){
|
||||
this->_size = newSize;
|
||||
}
|
||||
|
||||
char* Trench::colour() const {
|
||||
return this->_colour;
|
||||
}
|
||||
|
||||
void Trench::colour(const char *newColour){
|
||||
strcpy(this->_colour,newColour);
|
||||
}
|
||||
|
||||
int Trench::price() const {
|
||||
return this->_price;
|
||||
}
|
||||
|
||||
void Trench::price(int newPrice){
|
||||
this->_price = newPrice;
|
||||
}
|
||||
|
||||
int Trench::quantity() const{
|
||||
return this->_quantity;
|
||||
}
|
||||
|
||||
void Trench::quantity(int newQuantity){
|
||||
this->_quantity = newQuantity;
|
||||
}
|
||||
|
||||
char* Trench::photograph() const {
|
||||
return this->_photograph;
|
||||
}
|
||||
|
||||
void Trench::photograph(const char *newPhotograph){
|
||||
strcpy(this->_photograph, newPhotograph);
|
||||
}
|
||||
|
||||
bool Trench::operator==(const Trench& t){
|
||||
if(this->_size==t.size() && strcmp(this->_colour,t.colour())==0 && strcmp(this->_photograph,t.photograph())==0){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* Trench::toString(){
|
||||
char* string = new char[256];
|
||||
sprintf(string,"Size: %d | Colour: %s | Price: %d | Quantity: %d | Photograph Link: %s \n",this->_size,this->_colour,this->_price,this->_quantity,this->_photograph);
|
||||
return string;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
class Trench {
|
||||
|
||||
private:
|
||||
|
||||
int _size;
|
||||
char* _colour;
|
||||
int _price;
|
||||
int _quantity;
|
||||
char* _photograph;
|
||||
|
||||
public:
|
||||
/*
|
||||
* Constructor for the Trench class
|
||||
* Input: size - integer
|
||||
* colour - string
|
||||
* price - integer
|
||||
* quantity - integer
|
||||
* photograph - string
|
||||
* Output: trench object
|
||||
*/
|
||||
Trench(int size, const char *colour, int price, int quantity, const char *photograph);
|
||||
|
||||
|
||||
/*
|
||||
* Destructor for the Trench class
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
~Trench();
|
||||
|
||||
/*
|
||||
* Copy constructor for the Trench class
|
||||
* Input: trench - trench object
|
||||
* Output: trench object
|
||||
*/
|
||||
Trench(const Trench &trench);
|
||||
|
||||
/*
|
||||
* Getter for the size attribute
|
||||
* Input: -
|
||||
* Output: size - integer
|
||||
*/
|
||||
int size() const;
|
||||
|
||||
/*
|
||||
* Setter for the size attribute
|
||||
* Input: newSize - integer
|
||||
* Output: -
|
||||
*/
|
||||
void size(int newSize);
|
||||
|
||||
/*
|
||||
* Getter for the colour attribute
|
||||
* Input: -
|
||||
* Output: colour - string
|
||||
*/
|
||||
char* colour() const;
|
||||
|
||||
/*
|
||||
* Setter for the colour attribute
|
||||
* Input: newColour - string
|
||||
* Output: -
|
||||
*/
|
||||
void colour(const char *newColour);
|
||||
|
||||
/*
|
||||
* Getter for the price attribute
|
||||
* Input: -
|
||||
* Output: price - integer
|
||||
*/
|
||||
int price() const;
|
||||
|
||||
/*
|
||||
* Setter for the price attribute
|
||||
* Input: newPrice - integer
|
||||
* Output: -
|
||||
*/
|
||||
void price(int newPrice);
|
||||
|
||||
/*
|
||||
* Getter for the quantity attribute
|
||||
* Input: -
|
||||
* Output: quantity - integer
|
||||
*/
|
||||
int quantity() const;
|
||||
|
||||
/*
|
||||
* Setter for the quantity attribute
|
||||
* Input: newQuantity - integer
|
||||
* Output: -
|
||||
*/
|
||||
void quantity(int newQuantity);
|
||||
|
||||
/*
|
||||
* Getter for the photograph attribute
|
||||
* Input: -
|
||||
* Output: photograph - string
|
||||
*/
|
||||
char* photograph() const;
|
||||
|
||||
/*
|
||||
* Setter for the photograph attribute
|
||||
* Input: newPhotograph - string
|
||||
* Output: -
|
||||
*/
|
||||
void photograph(const char *newPhotograph);
|
||||
|
||||
/*
|
||||
* Overload of the == operator
|
||||
* Input: trench - trench object
|
||||
* Output: true - if the trench objects are the same
|
||||
* false - otherwise
|
||||
*/
|
||||
bool operator==(const Trench& t);
|
||||
|
||||
/*
|
||||
* Converts the trench object into a string
|
||||
* Input: -
|
||||
* Output: trench - char*
|
||||
*/
|
||||
char* toString();
|
||||
|
||||
};
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "exceptions.h"
|
||||
|
||||
VectorException::VectorException(const char* message){
|
||||
this->_message = message;
|
||||
}
|
||||
|
||||
const char* VectorException::what() const noexcept {
|
||||
return this->_message;
|
||||
}
|
||||
|
||||
RepoException::RepoException(const char* message){
|
||||
this->_message = message;
|
||||
}
|
||||
|
||||
const char* RepoException::what() const noexcept {
|
||||
return this->_message;
|
||||
}
|
||||
|
||||
AdminControllerException::AdminControllerException(const char* message){
|
||||
this->_message = message;
|
||||
}
|
||||
|
||||
const char* AdminControllerException::what() const noexcept {
|
||||
return this->_message;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <exception>
|
||||
|
||||
class VectorException : public std::exception {
|
||||
public:
|
||||
VectorException(const char* message);
|
||||
const char* what() const noexcept override;
|
||||
private:
|
||||
const char* _message;
|
||||
};
|
||||
|
||||
class RepoException : public std::exception {
|
||||
public:
|
||||
RepoException(const char* message);
|
||||
const char* what() const noexcept override;
|
||||
private:
|
||||
const char* _message;
|
||||
};
|
||||
|
||||
class AdminControllerException : public std::exception {
|
||||
public:
|
||||
AdminControllerException(const char* message);
|
||||
const char* what() const noexcept override;
|
||||
private:
|
||||
const char* _message;
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "repository/repository.h"
|
||||
#include "controllers/admin_controller/admin_controller.h"
|
||||
#include "ui/ui.h"
|
||||
|
||||
void fillRepo(AdminController* controller){
|
||||
controller->add(38,"Bej",55,30,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/71zZoa1e-xL._AC_UX466_.jpg");
|
||||
controller->add(40,"Bej",55,25,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/71zZoa1e-xL._AC_UX466_.jpg");
|
||||
controller->add(36,"Black",60,5,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/41RBzLubByL._AC_UY550_.jpg");
|
||||
controller->add(40,"Black",60,50,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/41RBzLubByL._AC_UY550_.jpg");
|
||||
controller->add(38,"Army Green",60,40,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/61Qptn0CKQL._AC_UX342_.jpg");
|
||||
controller->add(42,"Army Green",60,40,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/61Qptn0CKQL._AC_UX342_.jpg");
|
||||
controller->add(40,"Black",100,3,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/71YLqJCurtL._AC_UY550_.jpg");
|
||||
controller->add(38,"Red",33,20,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/816lGcU-fWL._AC_UX425_.jpg");
|
||||
controller->add(40,"Red",33,33,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/816lGcU-fWL._AC_UX425_.jpg");
|
||||
controller->add(38,"Blue",51,15,"https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/51zHT88C6zL._AC_UY550_.jpg");
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
Repository* repo = new Repository();
|
||||
Repository* shoppingRepo = new Repository();
|
||||
AdminController* controller = new AdminController(repo);
|
||||
fillRepo(controller);
|
||||
UserController* userController = new UserController(repo,shoppingRepo);
|
||||
UI* ui = new UI(controller,userController);
|
||||
ui->run();
|
||||
delete ui;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "repository.h"
|
||||
#include "../exceptions/exceptions.h"
|
||||
|
||||
Repository::Repository(){
|
||||
this->_data = new Vector<Trench*>();
|
||||
}
|
||||
Repository::~Repository(){
|
||||
delete this->_data;
|
||||
}
|
||||
void Repository::add(Trench* trench){
|
||||
if(this->get(trench)!=-1)
|
||||
throw RepoException("Trench already exists");
|
||||
this->_data->add(trench);
|
||||
|
||||
}
|
||||
void Repository::remove(Trench* trench){
|
||||
int index = this->get(trench);
|
||||
if(index==-1)
|
||||
throw RepoException("Trench does not exists");
|
||||
this->_data->remove(index);
|
||||
}
|
||||
void Repository::remove(int index){
|
||||
if(index<0 || index > this->_data->size()-1)
|
||||
throw RepoException("Index out of bounds");
|
||||
this->_data->remove(index);
|
||||
}
|
||||
|
||||
int Repository::get(Trench* trench){
|
||||
for(int i=0;i<this->_data->size();i++){
|
||||
if(*trench == *(*this->_data)[i])
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
//return this->_data->get(trench);
|
||||
}
|
||||
|
||||
Trench*& Repository::operator[](int index){
|
||||
if(index<0 || index > this->_data->size()-1)
|
||||
throw RepoException("Index out of bounds");
|
||||
return (*this->_data)[index];
|
||||
}
|
||||
void Repository::update(int index, int price, int quantity){
|
||||
if(index<0 || index > this->_data->size()-1)
|
||||
throw RepoException("Index out of bounds");
|
||||
Trench* trench = (*this->_data)[index];
|
||||
if(price!=-1 && price<=0)
|
||||
throw RepoException("Invalid price");
|
||||
if(quantity!=-1 && quantity<0)
|
||||
throw RepoException("Invalid quantity");
|
||||
if(price>0)
|
||||
trench->price(price);
|
||||
if(quantity>=0)
|
||||
trench->quantity(quantity);
|
||||
}
|
||||
|
||||
int Repository::size(){
|
||||
return this->_data->size();
|
||||
}
|
||||
|
||||
Trench** Repository::begin(){
|
||||
return this->_data->begin();
|
||||
}
|
||||
Trench** Repository::end(){
|
||||
return this->_data->end();
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include "../vector/vector.h"
|
||||
#include "../domain/trench.h"
|
||||
|
||||
class Repository {
|
||||
private:
|
||||
Vector<Trench*>* _data;
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
* Constructor for the Repository class
|
||||
* Input: -
|
||||
* Output: repository object
|
||||
*/
|
||||
Repository();
|
||||
|
||||
/*
|
||||
* Destructor for the Repository class
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
~Repository();
|
||||
|
||||
/*
|
||||
* Adds a trench to the repository
|
||||
* Input: trench - trench object
|
||||
* Output: -
|
||||
* Throws: RepoException if trench already exists
|
||||
*/
|
||||
void add(Trench* trench);
|
||||
|
||||
/*
|
||||
* Removes a trench from the repository
|
||||
* Input: trench - trench object
|
||||
* Output: -
|
||||
* Throws: RepoException if trench does not exist
|
||||
*/
|
||||
void remove(Trench* trench);
|
||||
|
||||
/*
|
||||
* Removes a trench from the repository
|
||||
* Input: index - index of the trench
|
||||
* Output: -
|
||||
* Throws: RepoException if index is out of bounds
|
||||
*/
|
||||
void remove(int index);
|
||||
|
||||
/*
|
||||
* Returns the index of a trench in the repository
|
||||
* Input: trench - trench object
|
||||
* Output: index of the trench. -1 if trench does not exist
|
||||
*/
|
||||
int get(Trench* trench);
|
||||
|
||||
/*
|
||||
* Returns a trench from the repository
|
||||
* Input: index - index of the trench
|
||||
* Output: trench object
|
||||
* Throws: RepoException if index is out of bounds
|
||||
*/
|
||||
Trench*& operator[](int index);
|
||||
|
||||
/*
|
||||
* Updates a trench from the repository
|
||||
* Input: index - index of the trench
|
||||
* price - new price
|
||||
* quantity - new quantity
|
||||
* Output: -
|
||||
* Throws: RepoException if index is out of bounds
|
||||
*/
|
||||
void update(int index, int price, int quantity);
|
||||
|
||||
/*
|
||||
* Returns the size of the repository
|
||||
* Input: -
|
||||
* Output: size of the repository
|
||||
*/
|
||||
int size();
|
||||
|
||||
/*
|
||||
* Returns an iterator to the beginning of the repository
|
||||
* Input: -
|
||||
* Output: iterator to the beginning of the repository
|
||||
*/
|
||||
Trench** begin();
|
||||
|
||||
/*
|
||||
* Returns an iterator to the end of the repository
|
||||
* Input: -
|
||||
* Output: iterator to the end of the repository
|
||||
*/
|
||||
Trench** end();
|
||||
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
#include<stdio.h>
|
||||
#include "tests/domain_tests/domain_tests.h"
|
||||
#include "tests/vector_tests/vector_tests.h"
|
||||
#include "tests/repo_tests/repo_tests.h"
|
||||
#include "tests/admin_controller_tests/admin_controller_tests.h"
|
||||
#include "tests/user_controller_tests/user_controller_tests.h"
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
test_domain();
|
||||
test_vector();
|
||||
test_repo();
|
||||
test_admin_controller();
|
||||
test_user_controller();
|
||||
printf("\nDone\n\n");
|
||||
return 0;
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
#include "admin_controller_tests.h"
|
||||
#include "../../controllers/admin_controller/admin_controller.h"
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../exceptions/exceptions.h"
|
||||
|
||||
#include<assert.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
void test_admin_controller(void){
|
||||
Repository* repo = new Repository();
|
||||
AdminController* controller = new AdminController(repo);
|
||||
int check = 0;
|
||||
|
||||
try{
|
||||
controller->add(0,"1",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Size must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->add(1,"",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Colour must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->add(1,"1",0,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Price must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->add(1,"1",1,-1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Quantity must be greater than or equal to 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->add(1,"1",1,1,"");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Photograph must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
controller->add(1,"1",1,1,"1");
|
||||
try{
|
||||
controller->add(1,"1",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Trench already exists")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->remove(0,"1","1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Size must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->remove(1,"","1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Colour must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->remove(1,"1","");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Photograph must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
controller->remove(1,"1","1");
|
||||
try{
|
||||
controller->remove(1,"1","1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Trench does not exists")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
controller->add(1,"1",1,1,"1");
|
||||
controller->remove(0);
|
||||
try{
|
||||
controller->remove(1);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
controller->add(1,"1",1,1,"1");
|
||||
|
||||
|
||||
try{
|
||||
controller->update(0,"1",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Size must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->update(1,"",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Colour must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->update(1,"1",0,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Price must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->update(1,"1",1,-2,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Quantity must be greater than or equal to 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->update(1,"1",1,1,"");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Photograph must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
controller->update(1,"1",2,2,"1");
|
||||
try{
|
||||
controller->update(2,"1",1,1,"1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Trench does not exist")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->get(0,"1","1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Size must be greater than 0")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->get(1,"","1");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Colour must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
controller->get(1,"1","");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
assert(strcmp(e.what(),"Photograph must not be empty")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
assert(controller->get(1,"1","1")==0);
|
||||
assert(controller->size()==1);
|
||||
assert((*controller)[0]->size()==1);
|
||||
assert(controller->getAll()==repo);
|
||||
|
||||
delete controller;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void test_admin_controller(void);
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "domain_tests.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
void test_domain(void){
|
||||
Trench trench(1,"red",100,10,"photo");
|
||||
assert(trench.size()==1);
|
||||
assert(strcmp(trench.colour(),"red")==0);
|
||||
assert(trench.price()==100);
|
||||
assert(trench.quantity()==10);
|
||||
assert(strcmp(trench.photograph(),"photo")==0);
|
||||
trench.size(2);
|
||||
assert(trench.size()==2);
|
||||
trench.colour("blue");
|
||||
assert(strcmp(trench.colour(),"blue")==0);
|
||||
trench.price(200);
|
||||
assert(trench.price()==200);
|
||||
trench.quantity(20);
|
||||
assert(trench.quantity()==20);
|
||||
trench.photograph("photo2");
|
||||
assert(strcmp(trench.photograph(),"photo2")==0);
|
||||
Trench trench2(trench);
|
||||
assert(trench2.size()==2);
|
||||
assert(strcmp(trench2.colour(),"blue")==0);
|
||||
assert(trench2.price()==200);
|
||||
assert(trench2.quantity()==20);
|
||||
assert(strcmp(trench2.photograph(),"photo2")==0);
|
||||
trench2.size(3);
|
||||
assert(trench2.size()==3);
|
||||
assert(trench.size()==2);
|
||||
trench2.colour("green");
|
||||
assert(strcmp(trench2.colour(),"green")==0);
|
||||
assert(strcmp(trench.colour(),"blue")==0);
|
||||
trench2.price(300);
|
||||
assert(trench2.price()==300);
|
||||
assert(trench.price()==200);
|
||||
trench2.quantity(30);
|
||||
assert(trench2.quantity()==30);
|
||||
assert(trench.quantity()==20);
|
||||
trench2.photograph("photo3");
|
||||
assert(strcmp(trench2.photograph(),"photo3")==0);
|
||||
assert(strcmp(trench.photograph(),"photo2")==0);
|
||||
Trench trench3 = trench2;
|
||||
assert(trench2==trench3);
|
||||
assert(!(trench==trench3));
|
||||
trench3.price(100);
|
||||
assert(trench2.price()==300);
|
||||
assert(trench3.price()==100);
|
||||
char* string = trench.toString();
|
||||
assert(strcmp(string,"Size: 2 | Colour: blue | Price: 200 | Quantity: 20 | Photograph Link: photo2 \n")==0);
|
||||
delete[] string;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void test_domain(void);
|
||||
@@ -0,0 +1,109 @@
|
||||
#include "repo_tests.h"
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../exceptions/exceptions.h"
|
||||
|
||||
#include<assert.h>
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
void test_repo(void){
|
||||
Repository* repo = new Repository();
|
||||
|
||||
Trench* t1 = new Trench(1,"Colour1",1,1,"Photo1");
|
||||
Trench* t2 = new Trench(2,"Colour2",2,2,"Photo2");
|
||||
Trench* t3 = new Trench(3,"Colour1",3,3,"Photo3");
|
||||
|
||||
int check=0;
|
||||
|
||||
repo->add(t1);
|
||||
repo->add(t2);
|
||||
repo->add(t3);
|
||||
assert(repo->size()==3);
|
||||
assert(repo->get(t1)==0);
|
||||
assert(repo->get(t2)==1);
|
||||
assert(repo->get(t3)==2);
|
||||
assert((*repo)[0]==t1);
|
||||
repo->remove(t2);
|
||||
repo->remove(1);
|
||||
assert(repo->size()==1);
|
||||
repo->update(0,2,-1);
|
||||
assert((*repo)[0]->price()==2);
|
||||
assert((*repo)[0]->quantity()==1);
|
||||
repo->update(0,-1,2);
|
||||
assert((*repo)[0]->price()==2);
|
||||
assert((*repo)[0]->quantity()==2);
|
||||
repo->update(0,3,3);
|
||||
assert((*repo)[0]->price()==3);
|
||||
assert((*repo)[0]->quantity()==3);
|
||||
assert(*(repo->begin())==t1);
|
||||
assert(*(repo->end()-1)==t1);
|
||||
|
||||
Trench* t4 = new Trench(4,"Colour4",4,4,"Photo4");
|
||||
try{
|
||||
repo->add(t1);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Trench already exists")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
repo->remove(t4);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Trench does not exists")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
repo->remove(-6);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
(*repo)[54];
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
repo->update(-6,1,1);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
repo->update(0,-3,1);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Invalid price")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
repo->update(0,3,-5);
|
||||
}
|
||||
catch(RepoException& e){
|
||||
assert(strcmp(e.what(),"Invalid quantity")==0);
|
||||
check=1;
|
||||
}
|
||||
assert(check);
|
||||
delete t4;
|
||||
delete repo;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void test_repo(void);
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
#include "user_controller_tests.h"
|
||||
#include "../../controllers/user_controller/user_controller.h"
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../exceptions/exceptions.h"
|
||||
|
||||
#include<assert.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
void test_user_controller(void){
|
||||
Repository* repo = new Repository();
|
||||
Repository* shoppingRepo = new Repository();
|
||||
UserController* userController = new UserController(repo,shoppingRepo);
|
||||
Trench* trench1 = new Trench(1,"red",100,100,"photo");
|
||||
Trench* trench2 = new Trench(2,"blue",200,2,"photo");
|
||||
Trench* trench3 = new Trench(3,"green",300,100,"photo");
|
||||
Trench* trench1_copy = new Trench(1,"red",100,100,"photo");
|
||||
Trench* trench2_copy = new Trench(2,"blue",200,100,"photo");
|
||||
repo->add(trench1);
|
||||
repo->add(trench2);
|
||||
repo->add(trench3);
|
||||
userController->addProductToShoppingList(trench1_copy);
|
||||
userController->addProductToShoppingList(trench1_copy);
|
||||
userController->addProductToShoppingList(trench1_copy);
|
||||
assert(userController->shoppingListTotal()==300);
|
||||
userController->emptyShoppingList();
|
||||
assert(userController->shoppingListTotal()==0);
|
||||
userController->addProductToShoppingList(trench2_copy);
|
||||
userController->addProductToShoppingList(trench2_copy);
|
||||
assert(userController->shoppingListTotal()==400);
|
||||
userController->buyShoppingList();
|
||||
assert(userController->shoppingListTotal()==0);
|
||||
assert(userController->getTrenchCarousel(-1).size()==2);
|
||||
assert(userController->getTrenchCarousel(2).size()==0);
|
||||
delete trench1_copy;
|
||||
delete trench2_copy;
|
||||
delete userController;
|
||||
delete repo;
|
||||
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void test_user_controller(void);
|
||||
@@ -0,0 +1,93 @@
|
||||
#include "vector_tests.h"
|
||||
#include "../../domain/trench.h"
|
||||
#include "../../vector/vector.h"
|
||||
#include "../../exceptions/exceptions.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void test_vector(void){
|
||||
Vector<Trench*> *v = new Vector<Trench*>();
|
||||
Trench *t1 = new Trench(1,"Blue",10,10,"Photo1");
|
||||
int check=0;
|
||||
v->add(t1);
|
||||
assert(v->size() == 1);
|
||||
assert(v->get(t1)==0);
|
||||
Trench *t2 = new Trench(2,"Red",20,20,"Photo2");
|
||||
assert(v->get(t2)==-1);
|
||||
v->add(t2);
|
||||
assert(v->size() == 2);
|
||||
assert(v->get(t2)==1);
|
||||
v->remove(t1);
|
||||
t1 = new Trench(1,"Blue",10,10,"Photo1");
|
||||
assert(v->size() == 1);
|
||||
assert(v->get(t1)==-1);
|
||||
assert(v->get(t2)==0);
|
||||
v->remove(0);
|
||||
t2 = new Trench(2,"Red",20,20,"Photo2");
|
||||
assert(v->size() == 0);
|
||||
assert(v->get(t2)==-1);
|
||||
v->add(t1);
|
||||
(*v)[0]=t2;
|
||||
assert(v->get(t2)==0);
|
||||
assert((*v)[0]==t2);
|
||||
assert(v->capacity()==10);
|
||||
assert(*(v->begin())==t2);
|
||||
assert(*(v->end()-1)==t2);
|
||||
Trench* t3 = new Trench(3,"Yellow",30,30,"Photo3");
|
||||
try{
|
||||
v->remove(t3);
|
||||
}
|
||||
catch(VectorException& e){
|
||||
check=1;
|
||||
assert(strcmp(e.what(),"Element not found")==0);
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
v->remove(-4);
|
||||
}
|
||||
catch(VectorException& e){
|
||||
check=1;
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
}
|
||||
assert(check);
|
||||
check=0;
|
||||
try{
|
||||
(*v)[44];
|
||||
}
|
||||
catch(VectorException& e){
|
||||
check=1;
|
||||
assert(strcmp(e.what(),"Index out of bounds")==0);
|
||||
}
|
||||
assert(check);
|
||||
v->add(t1);
|
||||
v->add(t3);
|
||||
Trench* t4 = new Trench(13,"Yellow",31,30,"Photo3");
|
||||
Trench* t5 = new Trench(4,"Yellow",32,30,"Photo3");
|
||||
Trench* t6 = new Trench(5,"Yellow",33,30,"Photo3");
|
||||
Trench* t7 = new Trench(6,"Yellow",34,30,"Photo3");
|
||||
Trench* t8 = new Trench(7,"Yellow",35,30,"Photo3");
|
||||
Trench* t9 = new Trench(8,"Yellow",36,30,"Photo3");
|
||||
Trench* t10 = new Trench(9,"Yellow",37,30,"Photo3");
|
||||
Trench* t11 = new Trench(10,"Yellow",38,30,"Photo3");
|
||||
v->add(t4);
|
||||
v->add(t5);
|
||||
v->add(t6);
|
||||
v->add(t7);
|
||||
v->add(t8);
|
||||
v->add(t9);
|
||||
v->add(t10);
|
||||
v->add(t11);
|
||||
Vector<Trench*> *v2 = new Vector<Trench*>(23);
|
||||
assert(v2->capacity()==23);
|
||||
Trench* t12 = new Trench(10,"Yellow",38,30,"Photo3");
|
||||
Trench* t13 = new Trench(10,"Yellow",38,30,"Photo3");
|
||||
v2->add(t12);
|
||||
Vector<Trench*> v3 = *v2;
|
||||
assert(v3.capacity()==23);
|
||||
v3[0]=t13;
|
||||
delete v2;
|
||||
delete v;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void test_vector(void);
|
||||
@@ -0,0 +1,504 @@
|
||||
#include "ui.h"
|
||||
#include "../exceptions/exceptions.h"
|
||||
#include "../domain/trench.h"
|
||||
#include "../vector/vector.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
UI::UI(AdminController* adminController, UserController* userController){
|
||||
this->_adminController = adminController;
|
||||
this->_userController = userController;
|
||||
}
|
||||
|
||||
UI::~UI(){
|
||||
delete this->_adminController;
|
||||
delete this->_userController;
|
||||
}
|
||||
|
||||
void UI::run(){
|
||||
char option = 0;
|
||||
while(1){
|
||||
printf("1. Run as admin\n");
|
||||
printf("2. Run as user\n");
|
||||
printf("3. Exit\n");
|
||||
printf("Option: ");
|
||||
option = getchar();
|
||||
if(option == '\n' || getchar() != '\n'){
|
||||
while(getchar()!='\n');
|
||||
printf("Invalid option!\n");
|
||||
continue;
|
||||
}
|
||||
switch(option){
|
||||
case '1':
|
||||
this->runAsAdmin();
|
||||
break;
|
||||
case '2':
|
||||
this->runAsUser();
|
||||
break;
|
||||
case '3':
|
||||
return;
|
||||
default:
|
||||
printf("Invalid option!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UI::runAsAdmin(){
|
||||
char option = 0;
|
||||
while(1){
|
||||
printf("1. Add trench\n");
|
||||
printf("2. Remove trench\n");
|
||||
printf("3. Update trench\n");
|
||||
printf("4. List all trenches\n");
|
||||
printf("5. Exit\n");
|
||||
printf("Option: ");
|
||||
option = getchar();
|
||||
if(option == '\n' || getchar() != '\n'){
|
||||
while(getchar()!='\n');
|
||||
printf("Invalid option!\n");
|
||||
continue;
|
||||
}
|
||||
switch(option){
|
||||
case '1':
|
||||
{
|
||||
int check = 0;
|
||||
char* buffer = new char[255];
|
||||
int size;
|
||||
char* colour = new char[255];
|
||||
int price;
|
||||
int quantity;
|
||||
char* photograph = new char[255];
|
||||
printf("Size: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid price!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
size = atoi(buffer);
|
||||
printf("Colour: ");
|
||||
fgets(colour,255,stdin);
|
||||
colour[strlen(colour)-1] = '\0';
|
||||
if(strlen(colour)==0){
|
||||
printf("Invalid colour!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
printf("Price: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid price!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
price = atoi(buffer);
|
||||
printf("Quantity: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid quantity!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
quantity = atoi(buffer);
|
||||
printf("Photograph: ");
|
||||
fgets(photograph,255,stdin);
|
||||
photograph[strlen(photograph)-1] = '\0';
|
||||
if(strlen(photograph)==0){
|
||||
printf("Invalid photograph!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
try{
|
||||
this->_adminController->add(size,colour,price,quantity,photograph);
|
||||
printf("Trench added!\n");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
catch(RepoException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
|
||||
}
|
||||
break;
|
||||
case '2':
|
||||
{
|
||||
int check = 0;
|
||||
char* buffer = new char[255];
|
||||
char size;
|
||||
char* colour = new char[255];
|
||||
char* photograph = new char[255];
|
||||
printf("Size: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid price!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
size = atoi(buffer);
|
||||
printf("Colour: ");
|
||||
fgets(colour,255,stdin);
|
||||
colour[strlen(colour)-1] = '\0';
|
||||
if(strlen(colour)==0){
|
||||
printf("Invalid colour!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
printf("Photograph: ");
|
||||
fgets(photograph,255,stdin);
|
||||
photograph[strlen(photograph)-1] = '\0';
|
||||
if(strlen(photograph)==0){
|
||||
printf("Invalid photograph!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
try{
|
||||
this->_adminController->remove(size,colour,photograph);
|
||||
printf("Trench removed!\n");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
catch(RepoException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
}
|
||||
break;
|
||||
case '3':
|
||||
{
|
||||
int check = 0;
|
||||
char* buffer = new char[255];
|
||||
char size;
|
||||
char* colour = new char[255];
|
||||
int price;
|
||||
int quantity;
|
||||
char* photograph = new char[255];
|
||||
printf("Size: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid price!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
size = atoi(buffer);
|
||||
printf("Colour: ");
|
||||
fgets(colour,255,stdin);
|
||||
colour[strlen(colour)-1] = '\0';
|
||||
if(strlen(colour)==0){
|
||||
printf("Invalid colour!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
printf("Price: (type enter to not change it) ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid price!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
if (strlen(buffer)!=0)
|
||||
price = atoi(buffer);
|
||||
else
|
||||
price = -1;
|
||||
printf("Quantity: (type enter to not change it) ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid quantity!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
check = 0;
|
||||
if (strlen(buffer)!=0)
|
||||
quantity = atoi(buffer);
|
||||
else
|
||||
quantity = -1;
|
||||
printf("Photograph: ");
|
||||
fgets(photograph,255,stdin);
|
||||
photograph[strlen(photograph)-1] = '\0';
|
||||
if(strlen(photograph)==0){
|
||||
printf("Invalid photograph!\n");
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
continue;
|
||||
}
|
||||
try{
|
||||
this->_adminController->update(size,colour,price,quantity,photograph);
|
||||
printf("Trench updated!\n");
|
||||
}
|
||||
catch(AdminControllerException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
catch(RepoException& e){
|
||||
printf("%s\n",e.what());
|
||||
}
|
||||
delete[] buffer;
|
||||
delete[] colour;
|
||||
delete[] photograph;
|
||||
}
|
||||
break;
|
||||
case '4':
|
||||
for(Trench* i:*this->_adminController->getAll()){
|
||||
char* s = i->toString();
|
||||
printf("%s\n",s);
|
||||
delete[] s;
|
||||
}
|
||||
break;
|
||||
case '5':
|
||||
return;
|
||||
default:
|
||||
printf("Invalid option!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UI::runAsUser(){
|
||||
char option;
|
||||
while(1){
|
||||
printf("1. Show all trench coats in a given size\n");
|
||||
printf("2. Show shopping cart\n");
|
||||
printf("3. Exit\n");
|
||||
printf("Option: ");
|
||||
option = getchar();
|
||||
if(option == '\n' || getchar() != '\n'){
|
||||
while(getchar()!='\n');
|
||||
printf("Invalid option!\n");
|
||||
continue;
|
||||
}
|
||||
switch(option){
|
||||
case '1':
|
||||
{
|
||||
int check = 0;
|
||||
char* buffer = new char[255];
|
||||
int size;
|
||||
printf("Size: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
for(size_t i=0;i<strlen(buffer);i++){
|
||||
if(buffer[i]<'0' || buffer[i]>'9'){
|
||||
check=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(check==1){
|
||||
printf("Invalid size!\n");
|
||||
delete[] buffer;
|
||||
continue;
|
||||
}
|
||||
if (strlen(buffer)!=0)
|
||||
size = atoi(buffer);
|
||||
else
|
||||
size = -1;
|
||||
delete[] buffer;
|
||||
Vector<Trench*> iterator = this->_userController->getTrenchCarousel(size);
|
||||
if(iterator.size()==0){
|
||||
printf("No trench coats found!\n");
|
||||
continue;
|
||||
}
|
||||
int exit = 1;
|
||||
int index = 0;
|
||||
while(exit && iterator.size()){
|
||||
char* s = iterator[index]->toString();
|
||||
printf("%s\n",s);
|
||||
printf("Total price: %d\n",this->_userController->shoppingListTotal());
|
||||
delete[] s;
|
||||
char option;
|
||||
|
||||
printf("1. Add to shopping cart\n");
|
||||
printf("2. Next trench coat\n");
|
||||
printf("3. Show picture\n");
|
||||
printf("4. Exit\n");
|
||||
printf("Option: ");
|
||||
option = getchar();
|
||||
if(option == '\n' || getchar() != '\n'){
|
||||
while(getchar()!='\n');
|
||||
printf("Invalid option!\n");
|
||||
continue;
|
||||
}
|
||||
switch(option){
|
||||
case '1':
|
||||
{
|
||||
this->_userController->addProductToShoppingList(iterator[index]);
|
||||
if (iterator[index]->quantity() == 0){
|
||||
iterator.remove(index);
|
||||
if(index==iterator.size())
|
||||
index=0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '2':
|
||||
{
|
||||
index++;
|
||||
if(index==iterator.size())
|
||||
index=0;
|
||||
break;
|
||||
}
|
||||
case '3':
|
||||
{
|
||||
char* photograph = iterator[index]->photograph();
|
||||
char* command = new char[1000];
|
||||
strcpy(command,"open ");
|
||||
strcat(command,photograph);
|
||||
strcat(command," > /dev/null 2> /dev/null");
|
||||
system(command);
|
||||
delete[] command;
|
||||
break;
|
||||
}
|
||||
case '4':
|
||||
{
|
||||
exit=0;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '2':
|
||||
{ if(this->_userController->getShoppingListTrench().size()==0){
|
||||
printf("Shopping cart is empty!\n");
|
||||
break;
|
||||
}
|
||||
printf("Total price: %d\n",this->_userController->shoppingListTotal());
|
||||
|
||||
for(Trench* i:this->_userController->getShoppingListTrench()){
|
||||
char* s = i->toString();
|
||||
printf("%s",s);
|
||||
delete[] s;
|
||||
}
|
||||
char option;
|
||||
while(1){
|
||||
printf("1. Buy trench coats\n");
|
||||
printf("2. Empty shopping cart\n");
|
||||
printf("3. Exit\n");
|
||||
printf("Option: ");
|
||||
option = getchar();
|
||||
if(option == '\n' || getchar() != '\n'){
|
||||
while(getchar()!='\n');
|
||||
printf("Invalid option!\n");
|
||||
continue;
|
||||
}
|
||||
switch(option){
|
||||
case '1':
|
||||
{
|
||||
this->_userController->buyShoppingList();
|
||||
break;
|
||||
}
|
||||
case '2':
|
||||
{
|
||||
this->_userController->emptyShoppingList();
|
||||
break;
|
||||
}
|
||||
case '3':
|
||||
{
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
printf("Invalid option!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if('1'<=option && option<='3')
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '3':
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "../controllers/admin_controller/admin_controller.h"
|
||||
#include "../controllers/user_controller/user_controller.h"
|
||||
|
||||
|
||||
class UI {
|
||||
private:
|
||||
AdminController* _adminController;
|
||||
UserController* _userController;
|
||||
public:
|
||||
UI(AdminController* adminController,UserController* userController);
|
||||
~UI();
|
||||
void run();
|
||||
void runAsAdmin();
|
||||
void runAsUser();
|
||||
};
|
||||
@@ -0,0 +1,122 @@
|
||||
#include "vector.h"
|
||||
#include "../domain/trench.h"
|
||||
#include <exception>
|
||||
#include "../exceptions/exceptions.h"
|
||||
|
||||
template<class T>
|
||||
Vector<T>::Vector(){
|
||||
this->_capacity = 10;
|
||||
this->_size = 0;
|
||||
this->_data = new T[10];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>::Vector(int capacity){
|
||||
this->_capacity = capacity;
|
||||
this->_size = 0;
|
||||
this->_data = new T[capacity];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>::~Vector(){
|
||||
for(int i=0;i<this->_size;i++){
|
||||
delete this->_data[i];
|
||||
}
|
||||
this->_size=0;
|
||||
delete[] this->_data;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>::Vector(const Vector<T> &vector){
|
||||
_capacity = vector._capacity;
|
||||
_size = vector._size;
|
||||
_data = new T[_capacity];
|
||||
for(int i=0;i<_size;i++){
|
||||
_data[i]=vector._data[i];
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T& Vector<T>::operator[](int index){
|
||||
if(index<0 || index>this->_size-1)
|
||||
{
|
||||
throw VectorException("Index out of bounds");
|
||||
}
|
||||
return this->_data[index];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Vector<T>::add(T elem){
|
||||
if(this->_size==this->_capacity)
|
||||
this->_resize();
|
||||
this->_data[this->_size]=elem;
|
||||
this->_size+=1;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Vector<T>::remove(T elem){
|
||||
for(int i=0;i<this->_size;i++){
|
||||
if(elem == this->_data[i]){
|
||||
this->remove(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw VectorException("Element not found");
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Vector<T>::remove(int index){
|
||||
if(index<0 || index>this->_size-1)
|
||||
{
|
||||
throw VectorException("Index out of bounds");
|
||||
}
|
||||
delete this->_data[index];
|
||||
for(int i=index;i<this->_size-1;i++){
|
||||
this->_data[i]=this->_data[i+1];
|
||||
}
|
||||
this->_size--;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int Vector<T>::get(T elem){
|
||||
for(int i=0;i<this->_size;i++){
|
||||
if(elem == this->_data[i]){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int Vector<T>::size(){
|
||||
return this->_size;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int Vector<T>::capacity(){
|
||||
return this->_capacity;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Vector<T>::_resize(){
|
||||
this->_capacity*=2;
|
||||
T* newData = new T[this->_capacity];
|
||||
for(int i=0;i<this->_size;i++){
|
||||
newData[i]=this->_data[i];
|
||||
}
|
||||
delete[] this->_data;
|
||||
this->_data=newData;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* Vector<T>::begin(){
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* Vector<T>::end(){
|
||||
return this->_data+this->_size;
|
||||
}
|
||||
|
||||
|
||||
template class Vector<Trench*>;
|
||||
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
|
||||
template <class T> class Vector{
|
||||
private:
|
||||
T* _data;
|
||||
int _size;
|
||||
int _capacity;
|
||||
|
||||
void _resize();
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
* Constructor for the Vector class
|
||||
* Input: -
|
||||
* Output: vector object with default capacity (10)
|
||||
*/
|
||||
Vector();
|
||||
|
||||
/*
|
||||
* Constructor for the Vector class
|
||||
* Input: capacity - integer
|
||||
* Output: vector object with given capacity
|
||||
*/
|
||||
Vector(int capacity);
|
||||
|
||||
/*
|
||||
* Destructor for the Vector class
|
||||
* Input: -
|
||||
* Output: -
|
||||
*/
|
||||
~Vector();
|
||||
|
||||
/*
|
||||
* Copy constructor for the Vector class
|
||||
* Input: vector - vector object
|
||||
* Output: vector object
|
||||
*/
|
||||
Vector(const Vector<T> &vector);
|
||||
|
||||
|
||||
/*
|
||||
* Overload of the [] operator
|
||||
* Input: index - integer
|
||||
* Output: element at the given index
|
||||
* Throws: VectorException if index is out of bounds
|
||||
*/
|
||||
T& operator[](int index);
|
||||
|
||||
/*
|
||||
* Adds an element to the vector
|
||||
* Input: elem - element to be added
|
||||
* Output: -
|
||||
*/
|
||||
void add(T elem);
|
||||
|
||||
/*
|
||||
* Removes an element from the vector
|
||||
* Input: elem - element to be removed
|
||||
* Output: -
|
||||
* Throws: VectorException if element is not found
|
||||
*/
|
||||
void remove(T elem);
|
||||
|
||||
/*
|
||||
* Removes an element from the vector
|
||||
* Input: index - index of the element to be removed
|
||||
* Output: -
|
||||
* Throws: VectorException if index is out of bounds
|
||||
*/
|
||||
void remove(int index);
|
||||
|
||||
/*
|
||||
* Returns the index of an element
|
||||
* Input: elem - element to be searched
|
||||
* Output: index of the element. -1 if not found
|
||||
*/
|
||||
int get(T elem);
|
||||
|
||||
/*
|
||||
* Returns the size of the vector
|
||||
* Input: -
|
||||
* Output: size of the vector
|
||||
*/
|
||||
int size();
|
||||
|
||||
/*
|
||||
* Returns the capacity of the vector
|
||||
* Input: -
|
||||
* Output: capacity of the vector
|
||||
*/
|
||||
int capacity();
|
||||
|
||||
/*
|
||||
* Returns the begin iterator
|
||||
* Input: -
|
||||
* Output: pointer to the first element
|
||||
*/
|
||||
T* begin();
|
||||
|
||||
/*
|
||||
* Returns the end iterator
|
||||
* Input: -
|
||||
* Output: pointer to the last element
|
||||
*/
|
||||
T* end();
|
||||
};
|
||||
Reference in New Issue
Block a user