School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,9 @@
# Assignment 06
## Problem Statement
For your solution to the previous assignment (Assignment 04-05), add the following features:
1. Replace the DynamicVector with the STL vector and modify your source code accordingly.
2. Use STL algorithms wherever possible in your application (e.g. in your filter function you could use `copy_if`, `count_if`). Replace all your for loops with STL algorithms. In cases where this is impossible replace your for loops with C++11's ranged-based for loop.
3. Test coverage must be at least 98% for all layers, except the UI. Use a coverage tool to demonstrate this.
@@ -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;
}
@@ -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();
};
@@ -0,0 +1,75 @@
#include "../../repository/repository.h"
#include "../../domain/trench.h"
#include "user_controller.h"
#include <stdio.h>
#include <vector>
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;
}
std::vector<Trench*> UserController::getTrenchCarousel(int size) {
std::vector<Trench*> trenchCarousel;
for(Trench* i : *this->_repo){
if((size == -1||i->size()==size) && i->quantity()>0){
Trench* copy = new Trench(*i);
trenchCarousel.push_back(copy);
}
}
return trenchCarousel;
}
std::vector<Trench*> UserController::getShoppingListTrench(){
std::vector<Trench*> trenchCarousel;
for(Trench* i : *this->_shoppingRepository){
Trench* copy = new Trench(*i);
trenchCarousel.push_back(copy);
}
return trenchCarousel;
}
@@ -0,0 +1,63 @@
#pragma once
#include "../../repository/repository.h"
#include "../../domain/trench.h"
#include <vector>
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
*/
std::vector<Trench*> getTrenchCarousel(int size);
std::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,17 @@
#include "exceptions.h"
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,18 @@
#pragma once
#include <exception>
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,69 @@
#include "repository.h"
#include "../exceptions/exceptions.h"
Repository::Repository(){
this->_data = new std::vector<Trench*>();
}
Repository::~Repository(){
while(this->_data->size()){
this->remove(0);
}
delete this->_data;
}
void Repository::add(Trench* trench){
if(this->get(trench)!=-1)
throw RepoException("Trench already exists");
this->_data->push_back(trench);
}
void Repository::remove(Trench* trench){
int index = this->get(trench);
if(index==-1)
throw RepoException("Trench does not exists");
this->remove(index);
}
void Repository::remove(int index){
if(index<0 || index > (int)(this->_data->size())-1)
throw RepoException("Index out of bounds");
delete (*this->_data)[index];
this->_data->erase(this->_data->begin()+index);
}
int Repository::get(Trench* trench){
for(int i=0;i<(int)(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 > (int)(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 > (int)(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();
}
std::vector<Trench*>::iterator Repository::begin(){
return this->_data->begin();
}
std::vector<Trench*>::iterator Repository::end(){
return this->_data->end();
}
@@ -0,0 +1,95 @@
#pragma once
#include "../domain/trench.h"
#include <vector>
class Repository {
private:
std::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
*/
std::vector<Trench*>::iterator begin();
/*
* Returns an iterator to the end of the repository
* Input: -
* Output: iterator to the end of the repository
*/
std::vector<Trench*>::iterator end();
};
@@ -0,0 +1,16 @@
#include<stdio.h>
#include "tests/domain_tests/domain_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_repo();
test_admin_controller();
test_user_controller();
printf("\nDone\n\n");
return 0;
}
@@ -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;
}
@@ -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);
@@ -0,0 +1,50 @@
#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);
std::vector<Trench *> v = userController->getTrenchCarousel(-1);
assert(v.size()==2);
for(int i=0;i<(int)v.size();i++){
delete v[i];
}
v = userController->getTrenchCarousel(2);
assert(v.size()==0);
for(int i=0;i<(int)v.size();i++){
delete v[i];
}
delete trench1_copy;
delete trench2_copy;
delete userController;
delete repo;
}
@@ -0,0 +1,3 @@
#pragma once
void test_user_controller(void);
@@ -0,0 +1,511 @@
#include "ui.h"
#include "../exceptions/exceptions.h"
#include "../domain/trench.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
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;
std::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.erase(iterator.begin()+index);
if(index==(int)(iterator.size()))
index=0;
}
break;
}
case '2':
{
index++;
if(index==(int)(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;
}
}
}
for (Trench* i : iterator)
delete i;
break;
}
case '2':
{
std::vector<Trench *> iterator = this->_userController->getShoppingListTrench();
if(iterator.size()==0){
printf("Shopping cart is empty!\n");
break;
}
printf("Total price: %d\n",this->_userController->shoppingListTotal());
for(Trench* i:iterator){
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'){
for (Trench* i : iterator)
delete i;
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();
};