School Commit Init
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
.vscode/
|
||||
@@ -0,0 +1,70 @@
|
||||
# Assignment 02-03
|
||||
|
||||
## Requirements
|
||||
- Each student will be given one of the problems below.
|
||||
- The solution must use the **C language**.
|
||||
- The problem should be solved over 2 iterations, due `Week 3` and `Week 4`:
|
||||
|
||||
### Week 3
|
||||
- Solve at least requirements **a** and **b**.
|
||||
- The vector used in the repository can be statically allocated.
|
||||
|
||||
### Week 4
|
||||
- Solve all problem requirements.
|
||||
- Define a vector structure with specific operations using a dynamically allocated array.
|
||||
- Use modular programming.
|
||||
- Source code must be specified and include tests for all non-UI functions
|
||||
- The program must not leak memory!
|
||||
- Use a layered architecture for your application (domain, repository, controller, UI). User interface, domain and data access elements will be stored in different modules. The user interface module will only contain the user interface part.
|
||||
- Have at least 10 entries available at application startup.
|
||||
- Handle user input errors gracefully (replace program crashes with nice error messages :blush:).
|
||||
|
||||
## Bonus possibilities
|
||||
1. Implement the following requirements using function pointers **[deadline: week 4, bonus: 0.1p]**:
|
||||
- For requirement **b**, add a different type of filtering (of your choice).
|
||||
- For requirement **c**, add descending sorting. The user should choose the type of sort and the program will show the list of entities accordingly.
|
||||
2. Provide 2 different implementations for the undo/redo functionality: one using a list of operations (this approach is a precursor of the [Command design pattern](https://en.wikipedia.org/wiki/Command_pattern)) and one where each state of the repository is recorded (this approach is not unlike the [Memento design pattern](https://en.wikipedia.org/wiki/Memento_pattern)). Implement your dynamic array generically, such that it can contain any type of elements (use void*). Use this structure for your repository, as well as to implement both undo/redo functionalities **[deadline: week 5, bonus: 0.1p]**.
|
||||
|
||||
## Problem Statements
|
||||
|
||||
### Pharmacy
|
||||
John is the administrator of the *“Smiles”* Pharmacy. He needs a software application to help him manage his pharmacy's medicine stocks. Each **Medicine** has the following attributes: `name`, `concentration`, `quantity` and `price`. John wants the application to help him in the following ways:\
|
||||
**(a)** Add, delete or update a medicine. A medicine is uniquely identified by its name and concentration. If a product that already exists is added, its quantity will be updated (the new quantity is added to the existing one).\
|
||||
**(b)** See all available medicines containing a given string (if the string is empty, all the available medicines will be considered), sorted ascending by medicine name.\
|
||||
**(c)** See only those medicines that are in short supply (quantity less than `X` items, where the value of `X` is user-provided).\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
|
||||
### Bakery
|
||||
Mary runs her family's bakery, *“Bread'n Bagel”*. Every day she struggles with keeping up to date with available stocks of raw materials and would like a program to help her manage the business more effectively. Each **Material** used in the bakery must have: a `name`, a `supplier`, a `quantity` and the `expiration date`. Mary wants a software application that helps her in the following ways:\
|
||||
**(a)** Add, delete and update a material. A raw material is uniquely identified by its name, supplier and expiration date. If a material that already exists is added, its quantity will be modified (the new quantity is added to the existing one).\
|
||||
**(b)** See all available materials past their expiration date, containing a given string (if the string is empty, all materials past their expiration date will be considered).\
|
||||
**(c)** Display all materials from a given supplier, which are in short supply (quantity less than a user-provided value), sorted ascending by their quantities.\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
|
||||
### Tourism Agency
|
||||
The employees of *“Happy Holidays”* need an application to manage the offers that the agency has. Each **Offer** has a `type` (one of `seaside, mountain or city break`), a `destination`, a `departure date` and a `price`. The employees need the application to help them in the following ways:\
|
||||
**(a)** Add, delete and update an offer. An offer is uniquely identified by its destination and departure dates.\
|
||||
**(b)** Display all tourism offers whose destinations contain a given string (if the string is empty, all destinations are considered), and show them sorted ascending by price.\
|
||||
**(c)** Display all offers of a given type, having their departure after a given date.\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
|
||||
### Real Estate Agency
|
||||
Evelyn owns a real estate agency. Being also the only employee, she needs an application to help her manage all the real estates of her clients. Each **Estate** has a type (one of `house, apartment or penthouse`), an `address`, a `surface` and a `price`. Evelyn needs the application to help her in the following ways:\
|
||||
**(a)** Add, delete or update an estate. An estate is uniquely identified by its address.\
|
||||
**(b)** Display all estates whose address contains a given string (if the string is empty, all estates will be considered), sorted ascending by their price.\
|
||||
**(c)** See all estates of a given type, having the surface greater than a user provided value.\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
|
||||
### Intelligent Refrigerator
|
||||
The company *“Home SmartApps”* have decided to design a new intelligent refrigerator. Besides the hardware, they need a software application to manage the refrigerator. Each **Product** that the fridge can store has a `name`, a `category` (one of `dairy, sweets, meat or fruit`), a `quantity` and an `expiration date`. The software application will provide the following functionalities:\
|
||||
**(a)** Add, delete or update a product. A product is uniquely identified by name and category. If a product that already exists is added, its quantity will be updated (the new quantity is added to the existing one).\
|
||||
**(b)** Display all products whose name contains a given string (if the string is empty, all products from the refrigerator are considered), and show them sorted ascending by the existing quantity.\
|
||||
**(c)** Display all products of a given category (if the category is empty, all types of food will be considered) whose expiration dates are close (expire in the following `X` days, where the value of `X` is user-provided).\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
|
||||
### World Population Monitoring
|
||||
The World Population Monitoring Organisation needs an application to help keep track of countries’ populations. Each **Country** has a unique `name`, the `continent` it belongs to (one of `Europe, America, Africa, Australia or Asia`), and a population (stored in millions). The employees of the organisation need the application to help them in the following ways:\
|
||||
**(a)** Add, delete or update a country. Updating must also consider the case of migration: a given number of people leave one country to migrate to another.\
|
||||
**(b)** Display all countries whose name contains a given string (if the string is empty, all the countries should be considered).\
|
||||
**(c)** Display all countries on a given continent (if the continent is empty, all countries will be considered), whose populations are greater than a given value, sorted ascending by population.\
|
||||
**(d)** Provide multiple undo and redo functionality. Each step will undo/redo the previous operation performed by the user.
|
||||
@@ -0,0 +1,364 @@
|
||||
#include "UI.h"
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include "../controller/controller.h"
|
||||
|
||||
UI _UI_Constructor(Controller *controller){
|
||||
UI ui;
|
||||
ui.controller=controller;
|
||||
return ui;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _UI_Destructor(UI* ui){
|
||||
_Controller_Destructor(ui->controller);
|
||||
}
|
||||
|
||||
|
||||
void _UI_start(UI* ui ){
|
||||
while(1){
|
||||
printf("\n");
|
||||
printf("1. Add a material\n");
|
||||
printf("2. Delete a material\n");
|
||||
printf("3. Update a material\n");
|
||||
printf("4. List all materials\n");
|
||||
printf("5. See all available materials past their expiration date\n");
|
||||
printf("6. See all materials with a given supplier which are in short supply\n");
|
||||
printf("7. Undo\n");
|
||||
printf("8. Redo\n");
|
||||
printf("9. Exit\n");
|
||||
printf("\n");
|
||||
printf("Enter command: ");
|
||||
char command = getchar();
|
||||
char buffer;
|
||||
if (command!='\n'&&(buffer=getchar())!='\n'&&buffer!=EOF){
|
||||
while(getchar()!='\n'&&buffer!=EOF);
|
||||
printf("Invalid Command\n");
|
||||
continue;
|
||||
}
|
||||
switch (command){
|
||||
case '1':
|
||||
{
|
||||
char* name = (char*)malloc(255);
|
||||
char* supplier=(char*)malloc(255);
|
||||
int quantity;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
char* buffer = (char*)malloc(255);
|
||||
printf("Enter name: ");
|
||||
fgets(name,255,stdin);
|
||||
name[strlen(name)-1]='\0';
|
||||
if(strlen(name)==0){
|
||||
printf("Invalid name\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter supplier: ");
|
||||
fgets(supplier,255,stdin);
|
||||
supplier[strlen(supplier)-1]='\0';
|
||||
if(strlen(supplier)==0){
|
||||
printf("Invalid supplier\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter quantity: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
quantity = atoi(buffer);
|
||||
if (quantity==0){
|
||||
printf("Invalid quantity\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter day: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
day = atoi(buffer);
|
||||
if (day==0 || day>31){
|
||||
printf("Invalid day\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter month: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
month = atoi(buffer);
|
||||
if (month==0 || month>12){
|
||||
printf("Invalid month\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter year: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
year = atoi(buffer);
|
||||
if (year==0 || year<1900){
|
||||
printf("Invalid year\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
free(buffer);
|
||||
int response = _Controller_add(ui->controller,name,supplier,quantity,day,month,year);
|
||||
if(response==0){
|
||||
printf("Material added successfully\n");
|
||||
}
|
||||
else if(response==1){
|
||||
printf("Material not added\n");
|
||||
}
|
||||
else{
|
||||
printf("Material already exists. Updated quantity\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '2':
|
||||
{
|
||||
char* name = (char*)malloc(255);
|
||||
char* supplier=(char*)malloc(255);
|
||||
int quantity;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
char* buffer = (char*)malloc(255);
|
||||
printf("Enter name: ");
|
||||
fgets(name,255,stdin);
|
||||
name[strlen(name)-1]='\0';
|
||||
if(strlen(name)==0){
|
||||
printf("Invalid name\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter supplier: ");
|
||||
fgets(supplier,255,stdin);
|
||||
supplier[strlen(supplier)-1]='\0';
|
||||
if(strlen(supplier)==0){
|
||||
printf("Invalid supplier\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter day: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
day = atoi(buffer);
|
||||
if (day==0 || day>31){
|
||||
printf("Invalid day\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter month: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
month = atoi(buffer);
|
||||
if (month==0 || month>12){
|
||||
printf("Invalid month\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter year: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
year = atoi(buffer);
|
||||
if (year==0 || year<1900){
|
||||
printf("Invalid year\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
free(buffer);
|
||||
int response = _Controller_delete(ui->controller,name,supplier,day,month,year);
|
||||
if(response==0){
|
||||
printf("Material deleted successfully\n");
|
||||
}
|
||||
else{
|
||||
printf("Material not deleted\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '3':
|
||||
{
|
||||
char* name = (char*)malloc(255);
|
||||
char* supplier=(char*)malloc(255);
|
||||
int quantity;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
char* buffer = (char*)malloc(255);
|
||||
printf("Enter name: ");
|
||||
fgets(name,255,stdin);
|
||||
name[strlen(name)-1]='\0';
|
||||
if (strlen(name)==0){
|
||||
printf("Invalid name\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter supplier: ");
|
||||
fgets(supplier,255,stdin);
|
||||
supplier[strlen(supplier)-1]='\0';
|
||||
if (strlen(supplier)==0){
|
||||
printf("Invalid supplier\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter quantity: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
quantity = atoi(buffer);
|
||||
if (quantity==0){
|
||||
printf("Invalid quantity\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter day: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
day = atoi(buffer);
|
||||
if (day==0 || day>31){
|
||||
printf("Invalid day\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter month: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
month = atoi(buffer);
|
||||
if (month==0 || month>12){
|
||||
printf("Invalid month\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
printf("Enter year: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
year = atoi(buffer);
|
||||
if (year==0 || year<1900){
|
||||
printf("Invalid year\n");
|
||||
free(buffer);
|
||||
free(name);
|
||||
free(supplier);
|
||||
break;
|
||||
}
|
||||
free(buffer);
|
||||
int response = _Controller_update(ui->controller,name,supplier,quantity,day,month,year);
|
||||
if(response==0){
|
||||
printf("Material updated successfully\n");
|
||||
}
|
||||
else{
|
||||
printf("Material not updated\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '4':
|
||||
{
|
||||
for(int i=0;i<_Controller_getLenght(ui->controller);i++){
|
||||
char* s =_Material_stringify(_Controller_getItem(ui->controller,i));
|
||||
printf("%s",s);
|
||||
free(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '5':
|
||||
{
|
||||
char string[255];
|
||||
printf("Enter string to compare: ");
|
||||
fgets(string,255,stdin);
|
||||
int lenght=0;
|
||||
Material** vector_material=_Controller_getExpiredMaterial(ui->controller,string,&lenght);
|
||||
for(int i=0;i<lenght;i++){
|
||||
char* s =_Material_stringify(vector_material[i]);
|
||||
printf("%s",s);
|
||||
free(s);
|
||||
}
|
||||
free(vector_material);
|
||||
break;
|
||||
}
|
||||
case '6':
|
||||
{
|
||||
int quantity;
|
||||
char* buffer = (char*)malloc(255);
|
||||
printf("Enter quantity: ");
|
||||
fgets(buffer,255,stdin);
|
||||
buffer[strlen(buffer)-1]='\0';
|
||||
quantity = atoi(buffer);
|
||||
if (quantity==0){
|
||||
printf("Invalid quantity\n");
|
||||
free(buffer);
|
||||
break;
|
||||
}
|
||||
free(buffer);
|
||||
Vector* vector = _Controller_getShortSupplyItems(ui->controller,quantity);
|
||||
for(int i=0;i<_Vector_getLength(vector);i++){
|
||||
char* s =_Material_stringify((Material*)_Vector_getItem(vector,i));
|
||||
printf("%s",s);
|
||||
free(s);
|
||||
}
|
||||
_Vector_Destructor(vector);
|
||||
break;
|
||||
}
|
||||
case '7':
|
||||
{
|
||||
int response = _Controller_undo(ui->controller);
|
||||
if(response==0){
|
||||
printf("Undo successful\n");
|
||||
}
|
||||
else{
|
||||
printf("Undo not successful\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '8':
|
||||
{
|
||||
int response = _Controller_redo(ui->controller);
|
||||
if(response==0){
|
||||
printf("Redo successful\n");
|
||||
}
|
||||
else{
|
||||
printf("Redo not successful\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '9':
|
||||
{
|
||||
_UI_Destructor(ui);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
printf("Invalid Command\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "../controller/controller.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Controller *controller;
|
||||
} UI;
|
||||
|
||||
UI _UI_Constructor(Controller *controller);
|
||||
|
||||
void _UI_Destructor(UI* ui);
|
||||
|
||||
void _UI_start(UI* ui);
|
||||
@@ -0,0 +1,226 @@
|
||||
#include "controller.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Controller* _Controller_Constructor(Repository* repository,UndoController* undo_controller){
|
||||
Controller* controller = (Controller*)malloc(sizeof(Controller));
|
||||
if (controller==NULL)
|
||||
return NULL;
|
||||
controller->repository=repository;
|
||||
controller->undo_controller=undo_controller;
|
||||
return controller;
|
||||
}
|
||||
|
||||
void _Controller_Destructor(Controller* controller){
|
||||
if (controller==NULL)
|
||||
return;
|
||||
_Repository_Destructor(controller->repository);
|
||||
_UndoController_Destructor(controller->undo_controller);
|
||||
controller->repository=NULL;
|
||||
free(controller);
|
||||
}
|
||||
|
||||
int _Controller_add(Controller* controller, char *name, char *supplier, int quantity, int day, int month, int year){
|
||||
if (controller==NULL)
|
||||
return 1;
|
||||
if (name==NULL)
|
||||
return 1;
|
||||
if (supplier==NULL)
|
||||
return 1;
|
||||
Material* material = _Material_Constructor(name,supplier,quantity,day,month,year);
|
||||
int response = _Repository_add(controller->repository,material);
|
||||
switch (response)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
Vector* argsDo = _Vector_Constructor(2);
|
||||
_Vector_add(argsDo,controller->repository);
|
||||
_Vector_add(argsDo,material);
|
||||
FunctionCall* doFunction = _FunctionCall_Constructor(_Repository_add,argsDo);
|
||||
Vector* argsUndo = _Vector_Constructor(2);
|
||||
_Vector_add(argsUndo,controller->repository);
|
||||
char* copy_name=(char*)malloc(255);
|
||||
strcpy(copy_name,name);
|
||||
char* copy_supplier = (char*)malloc(255);
|
||||
strcpy(copy_supplier,supplier);
|
||||
Material* copy_material = _Material_Constructor(copy_name,copy_supplier,quantity,day,month,year);
|
||||
_Vector_add(argsUndo,copy_material);
|
||||
FunctionCall* undoFunction = _FunctionCall_Constructor(_Repository_delete,argsUndo);
|
||||
Operation* operation = _Operation_Constructor(doFunction,undoFunction);
|
||||
_UndoController_recordOperation(controller->undo_controller,operation);
|
||||
return 0;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
Vector* argsDo = _Vector_Constructor(2);
|
||||
_Vector_add(argsDo,controller->repository);
|
||||
_Vector_add(argsDo,material);
|
||||
FunctionCall* doFunction = _FunctionCall_Constructor(_Repository_add,argsDo);
|
||||
Vector* argsUndo = _Vector_Constructor(2);
|
||||
_Vector_add(argsUndo,controller->repository);
|
||||
char* copy_name=(char*)malloc(255);
|
||||
strcpy(copy_name,name);
|
||||
char* copy_supplier = (char*)malloc(255);
|
||||
strcpy(copy_supplier,supplier);
|
||||
Material* negativeMaterial = _Material_Constructor(copy_name,copy_supplier,-_Material_GetQuantity(material),_Material_GetDate(material).tm_mday,_Material_GetDate(material).tm_mon+1,_Material_GetDate(material).tm_year);
|
||||
_Vector_add(argsUndo,negativeMaterial);
|
||||
FunctionCall* undoFunction = _FunctionCall_Constructor(_Repository_add,argsUndo);
|
||||
Operation* operation = _Operation_Constructor(doFunction,undoFunction);
|
||||
_UndoController_recordOperation(controller->undo_controller,operation);
|
||||
return 2;
|
||||
}
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int _Controller_delete(Controller* controller, char *name, char *supplier, int day, int month, int year){
|
||||
if (controller==NULL)
|
||||
return 1;
|
||||
if (name==NULL)
|
||||
return 1;
|
||||
if (supplier==NULL)
|
||||
return 1;
|
||||
Material* material = _Material_Constructor(name,supplier,0,day,month,year);
|
||||
int response = _Repository_delete(controller->repository,material);
|
||||
switch (response)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
Vector* argsDo = _Vector_Constructor(2);
|
||||
_Vector_add(argsDo,controller->repository);
|
||||
_Vector_add(argsDo,material);
|
||||
FunctionCall* doFunction = _FunctionCall_Constructor(_Repository_delete,argsDo);
|
||||
Vector* argsUndo = _Vector_Constructor(2);
|
||||
_Vector_add(argsUndo,controller->repository);
|
||||
char* copy_name=(char*)malloc(255);
|
||||
strcpy(copy_name,name);
|
||||
char* copy_supplier = (char*)malloc(255);
|
||||
strcpy(copy_supplier,supplier);
|
||||
Material* copy_material = _Material_Constructor(copy_name,copy_supplier,_Material_GetQuantity(material),day,month,year);
|
||||
_Vector_add(argsUndo,copy_material);
|
||||
FunctionCall* undoFunction = _FunctionCall_Constructor(_Repository_add,argsUndo);
|
||||
Operation* operation = _Operation_Constructor(doFunction,undoFunction);
|
||||
_UndoController_recordOperation(controller->undo_controller,operation);
|
||||
return 0;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int _Controller_update(Controller* controller,char *name, char *supplier, int quantity, int day, int month, int year){
|
||||
if (controller==NULL)
|
||||
return 1;
|
||||
if (name==NULL)
|
||||
return 1;
|
||||
if (supplier==NULL)
|
||||
return 1;
|
||||
Material* material = _Material_Constructor(name,supplier,quantity,day,month,year);
|
||||
int response = _Repository_update(controller->repository,material);
|
||||
switch (response)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
Vector* argsUndo = _Vector_Constructor(2);
|
||||
_Vector_add(argsUndo,controller->repository);
|
||||
_Vector_add(argsUndo,material);
|
||||
FunctionCall* undoFunction = _FunctionCall_Constructor(_Repository_update,argsUndo);
|
||||
Vector* argsDo = _Vector_Constructor(2);
|
||||
_Vector_add(argsDo,controller->repository);
|
||||
char* copy_name=(char*)malloc(255);
|
||||
strcpy(copy_name,name);
|
||||
char* copy_supplier = (char*)malloc(255);
|
||||
strcpy(copy_supplier,supplier);
|
||||
Material* new_material = _Material_Constructor(copy_name,copy_supplier,quantity,day,month,year);
|
||||
_Vector_add(argsDo,new_material);
|
||||
FunctionCall* doFunction = _FunctionCall_Constructor(_Repository_update,argsDo);
|
||||
Operation* operation = _Operation_Constructor(doFunction,undoFunction);
|
||||
_UndoController_recordOperation(controller->undo_controller,operation);
|
||||
return 0;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int _Controller_getLenght(Controller* controller){
|
||||
if (controller==NULL)
|
||||
return 0;
|
||||
return _Repository_getLenght(controller->repository);
|
||||
}
|
||||
|
||||
Material* _Controller_getItem(Controller* controller,int index){
|
||||
if (controller==NULL)
|
||||
return NULL;
|
||||
return _Repository_getItem(controller->repository,index);
|
||||
}
|
||||
|
||||
Material** _Controller_getExpiredMaterial(Controller* controller,char* string,int* lenght){
|
||||
int len=_Controller_getLenght(controller);
|
||||
Material** vector = (Material**)malloc(sizeof(Material)*len);
|
||||
time_t t = time(NULL);
|
||||
struct tm current_time = *localtime(&t);
|
||||
current_time.tm_year+=1900;
|
||||
string[strlen(string)-1]='\0';
|
||||
for(int i=0;i<len;i++){
|
||||
Material* material=_Controller_getItem(controller,i);
|
||||
if ((strstr(material->name,string)!=NULL||strlen(string)==0||string[0]=='\n')&&(material->expiration_date.tm_year<current_time.tm_year ||(material->expiration_date.tm_year==current_time.tm_year && material->expiration_date.tm_mon<current_time.tm_mon)||(material->expiration_date.tm_year==current_time.tm_year && material->expiration_date.tm_mon==current_time.tm_mon && material->expiration_date.tm_mday<current_time.tm_mday))){
|
||||
vector[*lenght]=material;
|
||||
(*lenght)++;
|
||||
}
|
||||
}
|
||||
return vector;
|
||||
}
|
||||
|
||||
Vector* _Controller_getShortSupplyItems(Controller* controller, int quantity){
|
||||
int len = _Controller_getLenght(controller);
|
||||
Vector* vector = _Vector_Constructor(5);
|
||||
for(int i=0;i<len;i++){
|
||||
Material* material=_Controller_getItem(controller,i);
|
||||
if(_Material_GetQuantity(material)<quantity)
|
||||
_Vector_add(vector,material);
|
||||
}
|
||||
for(int i=0;i<_Vector_getLength(vector)-1;i++){
|
||||
for(int j=i;j<_Vector_getLength(vector);j++){
|
||||
if(_Material_GetQuantity((Material*)_Vector_getItem(vector,i)) > _Material_GetQuantity((Material*)_Vector_getItem(vector,j))){
|
||||
TElem aux = vector->elems[i];
|
||||
vector->elems[i] = vector->elems[j];
|
||||
vector->elems[j] = aux;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
int _Controller_undo(Controller* controller){
|
||||
if (controller==NULL)
|
||||
return 1;
|
||||
return _UndoController_undo(controller->undo_controller);
|
||||
}
|
||||
|
||||
int _Controller_redo(Controller* controller){
|
||||
if (controller==NULL)
|
||||
return 1;
|
||||
return _UndoController_redo(controller->undo_controller);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
#include "../repository/repository.h"
|
||||
#include "../undo_module/undo_controller/undo_controller.h"
|
||||
|
||||
typedef struct{
|
||||
Repository* repository;
|
||||
UndoController* undo_controller;
|
||||
} Controller;
|
||||
|
||||
/*
|
||||
Constructor for the Controller
|
||||
Input: repository - Repository*
|
||||
Output: a pointer to the newly created controller
|
||||
*/
|
||||
Controller* _Controller_Constructor(Repository* repository,UndoController* undo_controller);
|
||||
|
||||
/*
|
||||
Destructor for the Controller
|
||||
Input: controller - Controller*
|
||||
Output: -
|
||||
*/
|
||||
void _Controller_Destructor(Controller* controller);
|
||||
|
||||
/*
|
||||
Adds an element to the repository
|
||||
Input: controller - Controller*
|
||||
name - char* - the name of the material
|
||||
supplier - char* - the supplier of the material
|
||||
quantity - int - the quantity of the material
|
||||
day - int - the day of the expiration date
|
||||
month - int - the month of the expiration date
|
||||
year - int - the year of the expiration date
|
||||
Output: 1 - if the element was added
|
||||
0 - if the element was not added
|
||||
2 - if the element was not added because it already exists
|
||||
*/
|
||||
int _Controller_add(Controller* controller, char *name, char *supplier, int quantity, int day, int month, int year);
|
||||
|
||||
/*
|
||||
Deletes an element from the repository
|
||||
Input: controller - Controller*
|
||||
name - char* - the name of the material
|
||||
supplier - char* - the supplier of the material
|
||||
day - int - the day of the expiration date
|
||||
month - int - the month of the expiration date
|
||||
year - int - the year of the expiration date
|
||||
Output: 1 - if the element was deleted
|
||||
0 - if the element was not deleted
|
||||
*/
|
||||
int _Controller_delete(Controller* controller, char *name, char *supplier, int day, int month, int year);
|
||||
|
||||
/*
|
||||
Updates an element from the repository
|
||||
Input: controller - Controller*
|
||||
name - char* - the name of the material
|
||||
supplier - char* - the supplier of the material
|
||||
quantity - int - the quantity of the material
|
||||
day - int - the day of the expiration date
|
||||
month - int - the month of the expiration date
|
||||
year - int - the year of the expiration date
|
||||
Output: 1 - if the element was updated
|
||||
0 - if the element was not updated
|
||||
*/
|
||||
int _Controller_update(Controller* controller,char *name, char *supplier, int quantity, int day, int month, int year);
|
||||
|
||||
/*
|
||||
Gets the lenght of the repository
|
||||
Input: controller - Controller*
|
||||
Output: int - the lenght of the repository
|
||||
*/
|
||||
int _Controller_getLenght(Controller* controller);
|
||||
|
||||
/*
|
||||
Gets an element from the repository
|
||||
Input: controller - Controller*
|
||||
index - int - the index of the element to be returned
|
||||
Output: Material* - the element at the given index
|
||||
NULL - if the index is invalid
|
||||
*/
|
||||
Material* _Controller_getItem(Controller* controller,int index);
|
||||
|
||||
/*
|
||||
Gets the expired material matching the given string
|
||||
Input: controller - Controller*
|
||||
string - char* - the string to be matched
|
||||
lenght - int* - the lenght of the retruned array
|
||||
Output: Material** - the array of expired material matching the given string
|
||||
*/
|
||||
Material** _Controller_getExpiredMaterial(Controller* controller,char* string,int* lenght);
|
||||
|
||||
/*
|
||||
Gets the material with quantity less than the given quantity
|
||||
Input: controller - Controller*
|
||||
quantity - int - the quantity to be matched
|
||||
Output: Vector* - the vector of material with the given quantity
|
||||
*/
|
||||
Vector* _Controller_getShortSupplyItems(Controller* controller, int quantity);
|
||||
|
||||
/*
|
||||
Undoes the last operation
|
||||
Input: controller - Controller*
|
||||
Output: 1 - if the operation was undone
|
||||
0 - if the operation was not undone
|
||||
*/
|
||||
int _Controller_undo(Controller* controller);
|
||||
|
||||
/*
|
||||
Redoes the last operation
|
||||
Input: controller - Controller*
|
||||
Output: 1 - if the operation was redone
|
||||
0 - if the operation was not redone
|
||||
*/
|
||||
int _Controller_redo(Controller* controller);
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "domain.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Material* _Material_Constructor(char *name, char *supplier, int quantity, int day, int month, int year)
|
||||
{
|
||||
Material* m =(Material*)malloc(sizeof(Material));
|
||||
m->name=name;
|
||||
m->supplier=supplier;
|
||||
m->quantity = quantity;
|
||||
m->expiration_date.tm_mday = day;
|
||||
m->expiration_date.tm_mon = month-1;
|
||||
m->expiration_date.tm_year = year;
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _Material_Destructor(Material *material){
|
||||
free(material->name);
|
||||
free(material->supplier);
|
||||
free(material);
|
||||
}
|
||||
|
||||
char* _Material_GetName(Material *material){
|
||||
return material->name;
|
||||
}
|
||||
|
||||
char* _Material_GetSupplier(Material *material){
|
||||
return material->supplier;
|
||||
}
|
||||
|
||||
int _Material_GetQuantity(Material *material){
|
||||
return material->quantity;
|
||||
}
|
||||
|
||||
struct tm _Material_GetDate(Material *material){
|
||||
return material->expiration_date;
|
||||
}
|
||||
|
||||
void _Material_SetQuantity(Material *material, int quantity){
|
||||
material->quantity = quantity;
|
||||
}
|
||||
|
||||
char* _Material_stringify(Material *material){
|
||||
char* string =(char*)malloc(sizeof(char)*255);
|
||||
sprintf(string,"Name: %s | Supplier: %s | Quantity: %d | Expiration Date: %02d/%02d/%d\n",material->name,material->supplier,material->quantity,material->expiration_date.tm_mday,material->expiration_date.tm_mon+1,material->expiration_date.tm_year);
|
||||
return string;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
#include<time.h>
|
||||
|
||||
typedef struct{
|
||||
char *name;
|
||||
char *supplier;
|
||||
int quantity;
|
||||
struct tm expiration_date;
|
||||
} Material;
|
||||
|
||||
/*
|
||||
Constructor for Material
|
||||
Input: name - char*
|
||||
supplier - char*
|
||||
quantity - int
|
||||
day - int
|
||||
month - int
|
||||
year - int
|
||||
Output: Material* - pointer to the newly created Material
|
||||
*/
|
||||
Material* _Material_Constructor(char *name, char *supplier, int quantity, int day, int month, int year);
|
||||
|
||||
/*
|
||||
Destructor for Material
|
||||
Input: material - Material*
|
||||
Output: -
|
||||
*/
|
||||
void _Material_Destructor(Material *material);
|
||||
|
||||
/*
|
||||
Getter for name
|
||||
Input: material - Material*
|
||||
Output: char* - name
|
||||
*/
|
||||
char* _Material_GetName(Material *material);
|
||||
|
||||
/*
|
||||
Getter for supplier
|
||||
Input: material - Material*
|
||||
Output: char* - supplier
|
||||
*/
|
||||
char* _Material_GetSupplier(Material *material);
|
||||
|
||||
/*
|
||||
Getter for quantity
|
||||
Input: material - Material*
|
||||
Output: int - quantity
|
||||
*/
|
||||
int _Material_GetQuantity(Material *material);
|
||||
|
||||
/*
|
||||
Getter for expiration date
|
||||
Input: material - Material*
|
||||
Output: struct tm - expiration date
|
||||
*/
|
||||
struct tm _Material_GetDate(Material *material);
|
||||
|
||||
/*
|
||||
Setter for name
|
||||
Input: material - Material*
|
||||
name - char*
|
||||
Output: -
|
||||
*/
|
||||
void _Material_SetQuantity(Material *material, int quantity);
|
||||
|
||||
/*
|
||||
Setter for supplier
|
||||
Input: material - Material*
|
||||
supplier - char*
|
||||
Output: -
|
||||
*/
|
||||
char* _Material_stringify(Material *material);
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include "domain/domain.h"
|
||||
#include "vector/vector.h"
|
||||
#include "UI/UI.h"
|
||||
#include "repository/repository.h"
|
||||
#include "controller/controller.h"
|
||||
#include "undo_module/undo_controller/undo_controller.h"
|
||||
#include "undo_module/function_call/functioncall.h"
|
||||
#include "undo_module/operation/operation.h"
|
||||
#include<string.h>
|
||||
#include "tests/tests.h"
|
||||
|
||||
|
||||
void add_materials(Controller* controller){
|
||||
char* name;
|
||||
char* supplier;
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Bucovina");
|
||||
_Controller_add(controller,name,supplier,20,2,3,2025);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Dorna");
|
||||
_Controller_add(controller,name,supplier,5,16,10,2026);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Flour");
|
||||
strcpy(supplier,"Boromir");
|
||||
_Controller_add(controller,name,supplier,25,16,10,2023);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Flour");
|
||||
strcpy(supplier,"Titan");
|
||||
_Controller_add(controller,name,supplier,25,4,1,2023);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Sugar");
|
||||
strcpy(supplier,"Margarita");
|
||||
_Controller_add(controller,name,supplier,25,7,11,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Sugar");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,125,23,10,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Rum Extract");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,65,23,10,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Vanilla Extract");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,45,23,10,2025);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Apples");
|
||||
strcpy(supplier,"Eco Fruits");
|
||||
_Controller_add(controller,name,supplier,95,4,9,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Plums");
|
||||
strcpy(supplier,"Eco Fruits");
|
||||
_Controller_add(controller,name,supplier,15,22,11,2021);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
testAll();
|
||||
Repository* repository = _Repository_Constructor();
|
||||
UndoController* undo_controller = _UndoController_Constructor();
|
||||
Controller* controller = _Controller_Constructor(repository,undo_controller);
|
||||
UI ui = _UI_Constructor(controller);
|
||||
add_materials(controller);
|
||||
_UI_start(&ui);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "repository.h"
|
||||
#include "../vector/vector.h"
|
||||
#include <string.h>
|
||||
#include<time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Repository* _Repository_Constructor(){
|
||||
Repository* repository = (Repository*)malloc(sizeof(Repository));
|
||||
if (repository==NULL)
|
||||
return NULL;
|
||||
repository->elements=_Vector_Constructor(10);
|
||||
return repository;
|
||||
}
|
||||
|
||||
void _Repository_Destructor(Repository* repository){
|
||||
if (repository==NULL)
|
||||
return;
|
||||
_Vector_Destructor(repository->elements);
|
||||
repository->elements=NULL;
|
||||
free(repository);
|
||||
|
||||
}
|
||||
|
||||
int _Repository_add(Repository* repository, Material* material){
|
||||
if (repository==NULL)
|
||||
return 1;
|
||||
if (material==NULL)
|
||||
return 1;
|
||||
for(int i=0;i<_Vector_getLength(repository->elements);i++){
|
||||
Material* item =_Vector_getItem(repository->elements,i);
|
||||
if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
|
||||
_Material_SetQuantity(item,_Material_GetQuantity(item)+_Material_GetQuantity(material));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
_Vector_add(repository->elements,material);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _Repository_delete(Repository* repository, Material* material){
|
||||
if (repository==NULL)
|
||||
return 1;
|
||||
if (material==NULL)
|
||||
return 1;
|
||||
for(int i=0;i<_Vector_getLength(repository->elements);i++){
|
||||
Material* item =_Vector_getItem(repository->elements,i);
|
||||
if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
|
||||
int quantity = _Material_GetQuantity(item);
|
||||
_Vector_delete(repository->elements,i);
|
||||
_Material_SetQuantity(material,quantity);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _Repository_update(Repository* repository, Material* material){
|
||||
if (repository==NULL)
|
||||
return 1;
|
||||
if (material==NULL)
|
||||
return 1;
|
||||
for(int i=0;i<_Vector_getLength(repository->elements);i++){
|
||||
Material* item =_Vector_getItem(repository->elements,i);
|
||||
if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
|
||||
int quantity = _Material_GetQuantity(item);
|
||||
_Material_SetQuantity(item,_Material_GetQuantity(material));
|
||||
_Material_SetQuantity(material,quantity);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _Repository_getLenght(Repository* repository){
|
||||
return _Vector_getLength(repository->elements);
|
||||
}
|
||||
|
||||
Material* _Repository_getItem(Repository* repository,int index){
|
||||
return (Material*)_Vector_getItem(repository->elements,index);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include "../vector/vector.h"
|
||||
#include "../domain/domain.h"
|
||||
|
||||
typedef struct {
|
||||
Vector* elements;
|
||||
} Repository;
|
||||
|
||||
/*
|
||||
Constructor for the Repository
|
||||
Input: -
|
||||
Output: a pointer to the newly created repository
|
||||
*/
|
||||
Repository* _Repository_Constructor();
|
||||
|
||||
/*
|
||||
Destructor for the Repository
|
||||
Input: repository - Repository*
|
||||
Output: -
|
||||
*/
|
||||
void _Repository_Destructor(Repository* repository);
|
||||
|
||||
/*
|
||||
Adds an element to the repository
|
||||
Input: repository - Repository*
|
||||
material - Material* - the element to be added
|
||||
Output: 1 - if the element was added
|
||||
0 - if the element was not added
|
||||
*/
|
||||
int _Repository_add(Repository* repository, Material* material);
|
||||
|
||||
/*
|
||||
Deletes an element from the repository
|
||||
Input: repository - Repository*
|
||||
material - Material* - the element to be deleted
|
||||
Output: 1 - if the element was deleted
|
||||
0 - if the element was not deleted
|
||||
*/
|
||||
int _Repository_delete(Repository* repository, Material* material);
|
||||
|
||||
/*
|
||||
Updates an element from the repository
|
||||
Input: repository - Repository*
|
||||
material - Material* - the element to be updated
|
||||
Output: 1 - if the element was updated
|
||||
0 - if the element was not updated
|
||||
*/
|
||||
int _Repository_update(Repository* repository, Material* material);
|
||||
|
||||
/*
|
||||
Gets the lenght of the repository
|
||||
Input: repository - Repository*
|
||||
Output: int - the lenght of the repository
|
||||
*/
|
||||
int _Repository_getLenght(Repository* repository);
|
||||
|
||||
/*
|
||||
Gets an element from the repository
|
||||
Input: repository - Repository*
|
||||
index - int - the index of the element to be returned
|
||||
Output: Material* - the element at the given index
|
||||
NULL - if the element was not found
|
||||
*/
|
||||
Material* _Repository_getItem(Repository* repository,int index);
|
||||
@@ -0,0 +1,134 @@
|
||||
#include "controller_tests.h"
|
||||
#include "../controller/controller.h"
|
||||
#include "../domain/domain.h"
|
||||
#include "../repository/repository.h"
|
||||
#include "../undo_module/undo_controller/undo_controller.h"
|
||||
#include "../undo_module/function_call/functioncall.h"
|
||||
#include "../undo_module/operation/operation.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void controller_tests(void){
|
||||
printf("Running controller tests\n");
|
||||
Repository* repository = _Repository_Constructor();
|
||||
UndoController* undo_controller = _UndoController_Constructor();
|
||||
Controller* controller = _Controller_Constructor(repository,undo_controller);
|
||||
char* name;
|
||||
char* supplier;
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Bucovina");
|
||||
_Controller_add(controller,name,supplier,20,2,3,2025);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Dorna");
|
||||
_Controller_add(controller,name,supplier,5,16,10,2026);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Flour");
|
||||
strcpy(supplier,"Boromir");
|
||||
_Controller_add(controller,name,supplier,25,16,10,2023);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Flour");
|
||||
strcpy(supplier,"Titan");
|
||||
_Controller_add(controller,name,supplier,25,4,1,2023);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Sugar");
|
||||
strcpy(supplier,"Margarita");
|
||||
_Controller_add(controller,name,supplier,25,7,11,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Sugar");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,125,23,10,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Rum Extract");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,65,23,10,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Vanilla Extract");
|
||||
strcpy(supplier,"Diamant");
|
||||
_Controller_add(controller,name,supplier,45,23,10,2025);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Apples");
|
||||
strcpy(supplier,"Eco Fruits");
|
||||
_Controller_add(controller,name,supplier,95,4,9,2021);
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Plums");
|
||||
strcpy(supplier,"Eco Fruits");
|
||||
_Controller_add(controller,name,supplier,15,22,11,2021);
|
||||
|
||||
assert(_Controller_getLenght(controller) == 10);
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,0)), "Water") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,0)), "Bucovina") == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,0)) == 20);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mday == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mon == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_year == 2025);
|
||||
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,9)),"Plums" ) == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,9)),"Eco Fruits" ) == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,9)) == 15);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_mday == 22);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_mon == 10);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_year == 2021);
|
||||
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Bucovina");
|
||||
|
||||
_Controller_update(controller,name,supplier,30,2,3,2025);
|
||||
assert(_Controller_getLenght(controller) == 10);
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,0)), "Water") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,0)), "Bucovina") == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,0)) == 30);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mday == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mon == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_year == 2025);
|
||||
|
||||
name = (char*)malloc(255);
|
||||
supplier = (char*)malloc(255);
|
||||
strcpy(name,"Water");
|
||||
strcpy(supplier,"Bucovina");
|
||||
_Controller_delete(controller,name,supplier,2,3,2025);
|
||||
assert(_Controller_getLenght(controller) == 9);
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,0)), "Water") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,0)), "Dorna") == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,0)) == 5);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mday == 16);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mon == 9);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_year == 2026);
|
||||
|
||||
_Controller_undo(controller);
|
||||
assert(_Controller_getLenght(controller) == 10);
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,9)), "Water") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,9)), "Bucovina") == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,9)) == 30);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_mday == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_mon == 2);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,9)).tm_year == 2025);
|
||||
|
||||
_Controller_redo(controller);
|
||||
assert(_Controller_getLenght(controller) == 9);
|
||||
assert(strcmp(_Material_GetName(_Controller_getItem(controller,0)), "Water") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Controller_getItem(controller,0)), "Dorna") == 0);
|
||||
assert(_Material_GetQuantity(_Controller_getItem(controller,0)) == 5);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mday == 16);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_mon == 9);
|
||||
assert(_Material_GetDate(_Controller_getItem(controller,0)).tm_year == 2026);
|
||||
|
||||
_Controller_Destructor(controller);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void controller_tests(void);
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "domain_tests.h"
|
||||
#include "../domain/domain.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void domain_tests(void){
|
||||
printf("Running domain tests\n");
|
||||
char *name = (char *)malloc(20);
|
||||
char *supplier = (char *)malloc(20);
|
||||
strcpy(name, "name");
|
||||
strcpy(supplier, "supplier");
|
||||
Material* material = _Material_Constructor(name,supplier,10,1,2,2005);
|
||||
assert(strcmp(_Material_GetName(material), "name") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(material), "supplier") == 0);
|
||||
assert(_Material_GetQuantity(material) == 10);
|
||||
assert(_Material_GetDate(material).tm_mday == 1);
|
||||
assert(_Material_GetDate(material).tm_mon == 1);
|
||||
assert(_Material_GetDate(material).tm_year == 2005);
|
||||
_Material_Destructor(material);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
void domain_tests(void);
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "repo_tests.h"
|
||||
#include "../repository/repository.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
void repo_tests(){
|
||||
printf("Running repository tests\n");
|
||||
Repository* repository = _Repository_Constructor();
|
||||
assert(_Repository_getLenght(repository) == 0);
|
||||
char *name = (char *)malloc(20);
|
||||
char *supplier = (char *)malloc(20);
|
||||
strcpy(name, "name");
|
||||
strcpy(supplier, "supplier");
|
||||
Material* material = _Material_Constructor(name,supplier,10,1,2,2005);
|
||||
_Repository_add(repository, material);
|
||||
assert(_Repository_getLenght(repository) == 1);
|
||||
assert(strcmp(_Material_GetName(_Repository_getItem(repository,0)), "name") == 0);
|
||||
assert(strcmp(_Material_GetSupplier(_Repository_getItem(repository,0)), "supplier") == 0);
|
||||
assert(_Material_GetQuantity(_Repository_getItem(repository,0)) == 10);
|
||||
assert(_Material_GetDate(_Repository_getItem(repository,0)).tm_mday == 1);
|
||||
assert(_Material_GetDate(_Repository_getItem(repository,0)).tm_mon == 1);
|
||||
assert(_Material_GetDate(_Repository_getItem(repository,0)).tm_year == 2005);
|
||||
_Repository_Destructor(repository);
|
||||
_Material_Destructor(material);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
void repo_tests();
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "domain_tests.h"
|
||||
#include "vector_tests.h"
|
||||
#include "repo_tests.h"
|
||||
#include "controller_tests.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void testAll(void){
|
||||
domain_tests();
|
||||
vector_tests();
|
||||
repo_tests();
|
||||
controller_tests();
|
||||
printf("All tests passed\n");
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
void testAll(void);
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "vector_tests.h"
|
||||
#include "../vector/vector.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
void vector_tests(void){
|
||||
printf("Running vector tests\n");
|
||||
Vector* vector = _Vector_Constructor(10);
|
||||
assert(_Vector_getLength(vector) == 0);
|
||||
assert(_Vector_getCapacity(vector) == 10);
|
||||
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9, j = 10, k = 11;
|
||||
_Vector_add(vector, &a);
|
||||
_Vector_add(vector, &b);
|
||||
_Vector_add(vector, &c);
|
||||
assert(_Vector_getLength(vector) == 3);
|
||||
assert(_Vector_getCapacity(vector) == 10);
|
||||
_Vector_add(vector, &d);
|
||||
_Vector_add(vector, &e);
|
||||
_Vector_add(vector, &f);
|
||||
_Vector_add(vector, &g);
|
||||
_Vector_add(vector, &h);
|
||||
_Vector_add(vector, &i);
|
||||
_Vector_add(vector, &j);
|
||||
_Vector_add(vector, &k);
|
||||
assert(_Vector_getLength(vector) == 11);
|
||||
assert(_Vector_getCapacity(vector) == 20);
|
||||
assert(*((int*)_Vector_getItem(vector, 0)) == 1);
|
||||
assert(*((int*)_Vector_getItem(vector, 1)) == 2);
|
||||
_Vector_delete(vector, 0);
|
||||
assert(_Vector_getLength(vector) == 10);
|
||||
assert(*((int*)_Vector_getItem(vector, 0)) == 2);
|
||||
_Vector_Destructor(vector);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
void vector_tests(void);
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#include<stdlib.h>
|
||||
#include "functioncall.h"
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/domain.h"
|
||||
#include "../../vector/vector.h"
|
||||
|
||||
FunctionCall* _FunctionCall_Constructor(int (*function)(Repository*, Material*), Vector* args){
|
||||
FunctionCall* functionCall = (FunctionCall*)malloc(sizeof(FunctionCall));
|
||||
functionCall->function = function;
|
||||
functionCall->args = args;
|
||||
return functionCall;
|
||||
}
|
||||
|
||||
void _FunctionCall_Destructor(FunctionCall* functionCall){
|
||||
_Material_Destructor((Material*)functionCall->args->elems[1]);
|
||||
_Vector_Destructor(functionCall->args);
|
||||
free(functionCall);
|
||||
}
|
||||
|
||||
void _FunctionCall_Execute(FunctionCall* functionCall){
|
||||
functionCall->function((Repository*)functionCall->args->elems[0], (Material*)functionCall->args->elems[1]);
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "../../repository/repository.h"
|
||||
#include "../../domain/domain.h"
|
||||
#include "../../vector/vector.h"
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int (*function)(Repository*, Material*);
|
||||
Vector* args;
|
||||
} FunctionCall;
|
||||
|
||||
/*
|
||||
Constructor for the FunctionCall
|
||||
Input: function - int (*)(Repository*, Material*) - the function to be called
|
||||
args - Vector* - the arguments of the function
|
||||
Output: a pointer to the newly created FunctionCall
|
||||
*/
|
||||
FunctionCall* _FunctionCall_Constructor(int (*function)(Repository*, Material*), Vector* args);
|
||||
|
||||
/*
|
||||
Destructor for the FunctionCall
|
||||
Input: functionCall - FunctionCall*
|
||||
Output: -
|
||||
*/
|
||||
void _FunctionCall_Destructor(FunctionCall* functionCall);
|
||||
|
||||
/*
|
||||
Executes the function
|
||||
Input: functionCall - FunctionCall*
|
||||
Output: -
|
||||
*/
|
||||
void _FunctionCall_Execute(FunctionCall* functionCall);
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#include "operation.h"
|
||||
#include "../function_call/functioncall.h"
|
||||
#include<stdlib.h>
|
||||
|
||||
Operation* _Operation_Constructor(FunctionCall* functionDo, FunctionCall* functionUndo){
|
||||
Operation* operation = (Operation*)malloc(sizeof(Operation));
|
||||
operation->functionDo = functionDo;
|
||||
operation->functionUndo = functionUndo;
|
||||
return operation;
|
||||
}
|
||||
|
||||
void _Operation_Destructor(Operation* operation){
|
||||
_FunctionCall_Destructor(operation->functionDo);
|
||||
_FunctionCall_Destructor(operation->functionUndo);
|
||||
free(operation);
|
||||
}
|
||||
|
||||
void _Operation_Do(Operation* operation){
|
||||
_FunctionCall_Execute(operation->functionDo);
|
||||
}
|
||||
|
||||
void _Operation_Undo(Operation* operation){
|
||||
_FunctionCall_Execute(operation->functionUndo);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include "../function_call/functioncall.h"
|
||||
|
||||
typedef struct{
|
||||
FunctionCall* functionDo;
|
||||
FunctionCall* functionUndo;
|
||||
} Operation;
|
||||
|
||||
/*
|
||||
Constructor for the Operation
|
||||
Input: functionDo - FunctionCall* - the function to be called when the operation is done
|
||||
functionUndo - FunctionCall* - the function to be called when the operation is undone
|
||||
Output: a pointer to the newly created Operation
|
||||
*/
|
||||
Operation* _Operation_Constructor(FunctionCall* functionDo, FunctionCall* functionUndo);
|
||||
|
||||
/*
|
||||
Destructor for the Operation
|
||||
Input: operation - Operation*
|
||||
Output: -
|
||||
*/
|
||||
void _Operation_Destructor(Operation* operation);
|
||||
|
||||
/*
|
||||
Executes the Do function
|
||||
Input: operation - Operation*
|
||||
Output: -
|
||||
*/
|
||||
void _Operation_Do(Operation* operation);
|
||||
|
||||
/*
|
||||
Executes the Undo function
|
||||
Input: operation - Operation*
|
||||
Output: -
|
||||
*/
|
||||
void _Operation_Undo(Operation* operation);
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
#include<stdlib.h>
|
||||
#include "undo_controller.h"
|
||||
#include "../operation/operation.h"
|
||||
|
||||
|
||||
|
||||
UndoController* _UndoController_Constructor(){
|
||||
UndoController* undo_controller = (UndoController*)malloc(sizeof(UndoController));
|
||||
undo_controller->operations = _Vector_Constructor(10);
|
||||
undo_controller->index=-1;
|
||||
return undo_controller;
|
||||
}
|
||||
|
||||
void _UndoController_Destructor(UndoController* undo_controller){
|
||||
for(int i=0;i<_Vector_getLength(undo_controller->operations);i++)
|
||||
_Operation_Destructor((Operation*)(_Vector_getItem(undo_controller->operations,i)));
|
||||
_Vector_Destructor(undo_controller->operations);
|
||||
undo_controller->operations=NULL;
|
||||
free(undo_controller);
|
||||
}
|
||||
|
||||
void _UndoController_recordOperation(UndoController* undo_controller,Operation* operation){
|
||||
for(int i = _Vector_getLength(undo_controller->operations)-1;i>undo_controller->index;i--){
|
||||
_Operation_Destructor((Operation*)(_Vector_getItem(undo_controller->operations,i)));
|
||||
_Vector_delete(undo_controller->operations,i);
|
||||
}
|
||||
_Vector_add(undo_controller->operations,operation);
|
||||
undo_controller->index+=1;
|
||||
}
|
||||
|
||||
int _UndoController_undo(UndoController* undo_controller){
|
||||
if(undo_controller->index<0)
|
||||
return 1;
|
||||
_Operation_Undo((Operation*)(_Vector_getItem(undo_controller->operations,undo_controller->index)));
|
||||
undo_controller->index--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _UndoController_redo(UndoController* undo_controller){
|
||||
if(undo_controller->index + 1 >= _Vector_getLength(undo_controller->operations))
|
||||
return 1;
|
||||
undo_controller->index++;
|
||||
_Operation_Do((Operation*)(_Vector_getItem(undo_controller->operations,undo_controller->index)));
|
||||
return 0;
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../vector/vector.h"
|
||||
#include "../operation/operation.h"
|
||||
|
||||
typedef struct {
|
||||
Vector* operations;
|
||||
int index;
|
||||
} UndoController;
|
||||
|
||||
/*
|
||||
Constructor for the UndoController
|
||||
Input: -
|
||||
Output: a pointer to the newly created UndoController
|
||||
*/
|
||||
UndoController* _UndoController_Constructor();
|
||||
|
||||
/*
|
||||
Destructor for the UndoController
|
||||
Input: undo_controller - UndoController*
|
||||
Output: -
|
||||
*/
|
||||
void _UndoController_Destructor(UndoController* undo_controller);
|
||||
|
||||
/*
|
||||
Adds an operation to the UndoController
|
||||
Input: undo_controller - UndoController*
|
||||
operation - Operation* - the operation to be added
|
||||
Output: -
|
||||
*/
|
||||
void _UndoController_recordOperation(UndoController* undo_controller,Operation* operation);
|
||||
|
||||
/*
|
||||
Undoes the last operation
|
||||
Input: undo_controller - UndoController*
|
||||
Output: 1 - if the operation was undone
|
||||
0 - if the operation was not undone
|
||||
*/
|
||||
int _UndoController_undo(UndoController* undo_controller);
|
||||
|
||||
/*
|
||||
Redoes the last operation
|
||||
Input: undo_controller - UndoController*
|
||||
Output: 1 - if the operation was redone
|
||||
0 - if the operation was not redone
|
||||
*/
|
||||
int _UndoController_redo(UndoController* undo_controller);
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <stdlib.h>
|
||||
#include "vector.h"
|
||||
|
||||
|
||||
Vector* _Vector_Constructor(int capacity){
|
||||
Vector* vector = (Vector*)malloc(sizeof(Vector));
|
||||
if (vector == NULL)
|
||||
return NULL;
|
||||
vector->capacity=capacity;
|
||||
vector->lenght=0;
|
||||
vector->elems=(TElem*)malloc(capacity*(sizeof(TElem)));
|
||||
if (vector->elems==NULL)
|
||||
return NULL;
|
||||
return vector;
|
||||
}
|
||||
|
||||
void _Vector_Destructor(Vector* vector){
|
||||
if (vector==NULL)
|
||||
return;
|
||||
free(vector->elems);
|
||||
vector->elems=NULL;
|
||||
free(vector);
|
||||
|
||||
}
|
||||
|
||||
int _Vector_add(Vector* vector, TElem t){
|
||||
if (vector == NULL)
|
||||
return 1;
|
||||
if (vector->elems==NULL)
|
||||
return 1;
|
||||
|
||||
if (vector->lenght==vector->capacity)
|
||||
_Vector_resize(vector);
|
||||
vector->elems[vector->lenght]=t;
|
||||
vector->lenght++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _Vector_delete(Vector* vector,int index){
|
||||
if (vector == NULL)
|
||||
return 1;
|
||||
if (vector->elems==NULL)
|
||||
return 1;
|
||||
if (vector->lenght<=index)
|
||||
return 1;
|
||||
|
||||
TElem return_value=vector->elems[index];
|
||||
for(int i=index;i<vector->lenght-1;i++)
|
||||
vector->elems[i]=vector->elems[i+1];
|
||||
vector->lenght--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _Vector_resize(Vector* vector){
|
||||
if (vector == NULL)
|
||||
return;
|
||||
vector->capacity*=2;
|
||||
TElem* aux = (TElem*)realloc(vector->elems,vector->capacity*sizeof(TElem));
|
||||
if (aux==NULL)
|
||||
return;
|
||||
vector->elems=aux;
|
||||
}
|
||||
|
||||
TElem _Vector_getItem(Vector* vector,int index){
|
||||
if (vector==NULL)
|
||||
return NULL;
|
||||
if (index<0 || index >= vector->lenght)
|
||||
return NULL;
|
||||
return vector->elems[index];
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _Vector_getLength(Vector* vector){
|
||||
if (vector==NULL)
|
||||
return 0;
|
||||
return vector->lenght;
|
||||
}
|
||||
|
||||
int _Vector_getCapacity(Vector* vector){
|
||||
if (vector==NULL)
|
||||
return 0;
|
||||
return vector->capacity;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
#include "../domain/domain.h"
|
||||
|
||||
typedef void* TElem;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TElem* elems;
|
||||
int lenght;
|
||||
int capacity;
|
||||
} Vector;
|
||||
|
||||
/*
|
||||
Constructor for the Vector
|
||||
Input: capacity - the initial capacity of the vector
|
||||
Output: a pointer to the newly created vector
|
||||
*/
|
||||
Vector* _Vector_Constructor(int capacity);
|
||||
|
||||
/*
|
||||
Destructor for the Vector
|
||||
Input: vector - Vector*
|
||||
Output: -
|
||||
*/
|
||||
void _Vector_Destructor(Vector* vector);
|
||||
|
||||
/*
|
||||
Adds an element to the vector
|
||||
Input: vector - Vector*
|
||||
t - TElem - the element to be added
|
||||
Output: 1 - if the element was added
|
||||
0 - if the element was not added
|
||||
*/
|
||||
int _Vector_add(Vector* vector, TElem t);
|
||||
|
||||
/*
|
||||
Deletes an element from the vector
|
||||
Input: vector - Vector*
|
||||
index - int - the index of the element to be deleted
|
||||
Output: 1 - if the element was deleted
|
||||
0 - if the element was not deleted
|
||||
*/
|
||||
int _Vector_delete(Vector* vector,int index);
|
||||
|
||||
/*
|
||||
Gets an element from the vector
|
||||
Input: vector - Vector*
|
||||
index - int - the index of the element to be returned
|
||||
Output: TElem - the element at the given index
|
||||
NULL - if the element was not found
|
||||
*/
|
||||
TElem _Vector_getItem(Vector* vector,int index);
|
||||
|
||||
/*
|
||||
Resizes the vector, doubling its capacity
|
||||
Input: vector - Vector*
|
||||
Output: -
|
||||
*/
|
||||
void _Vector_resize(Vector* vector);
|
||||
|
||||
/*
|
||||
Gets the length of the vector
|
||||
Input: vector - Vector*
|
||||
Output: int - the length of the vector
|
||||
*/
|
||||
int _Vector_getLength(Vector* vector);
|
||||
|
||||
/*
|
||||
Gets the capacity of the vector
|
||||
Input: vector - Vector*
|
||||
Output: int - the capacity of the vector
|
||||
*/
|
||||
int _Vector_getCapacity(Vector* vector);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user