School Commit Init
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
@@ -0,0 +1,18 @@
|
||||
[](https://classroom.github.com/a/ga0xn55N)
|
||||
# Lab Test 1
|
||||
|
||||
Solve the provided problem, using object oriented programming and C++.
|
||||
|
||||
Input at least 5 entities in your repository.
|
||||
|
||||
The application must use layered architecture in order for functionalities to be graded.
|
||||
|
||||
Do not forget to add the required tests and specifications.
|
||||
|
||||
Using existing code is forbidden, you must start your application from scratch.
|
||||
|
||||
For function documentation you may use:
|
||||
- https://en.cppreference.com/w/
|
||||
- https://www.cplusplus.com/
|
||||
|
||||
Time: 60 minutes.
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "domain.h"
|
||||
#include "repository.h"
|
||||
#include "controller.h"
|
||||
#include "UI.h"
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
UI::UI(){
|
||||
this->_controller.add("Orange","123",100,true);
|
||||
this->_controller.add("Digi","124",200,false);
|
||||
this->_controller.add("Vodafone","125",300,true);
|
||||
}
|
||||
|
||||
UI::~UI(){
|
||||
|
||||
}
|
||||
|
||||
void UI::run(){
|
||||
char option;
|
||||
while(true){
|
||||
printf("1. Add bill\n");
|
||||
printf("2. Show all bills\n");
|
||||
printf("3. Show all paid bills\n");
|
||||
printf("4. Exit\n");
|
||||
option=fgetc(stdin);
|
||||
while (fgetc(stdin)!='\n');
|
||||
switch(option){
|
||||
case '1':{
|
||||
char company_name[100];
|
||||
char serial_number[100];
|
||||
int sum;
|
||||
bool isPaid;
|
||||
printf("Company name: ");
|
||||
fgets(company_name,100,stdin);
|
||||
company_name[strlen(company_name)-1] = '\0';
|
||||
printf("Serial number: ");
|
||||
fgets(serial_number,100,stdin);
|
||||
serial_number[strlen(serial_number)-1] = '\0';
|
||||
printf("Sum: ");
|
||||
scanf("%d",&sum);
|
||||
printf("Is paid: ");
|
||||
std::cin>>isPaid;
|
||||
bool check = this->_controller.add(company_name,serial_number,sum,isPaid);
|
||||
if(check)
|
||||
printf("Bill added\n");
|
||||
else
|
||||
printf("Bill not added\n");
|
||||
break;
|
||||
}
|
||||
case '2':{
|
||||
auto v = this->_controller.getBills();
|
||||
for (Bill bill : v)
|
||||
std::cout<<bill.company_name()<<";"<<bill.serial_number()<<";"<<bill.sum()<<";"<<bill.isPaid()<<std::endl;
|
||||
break;
|
||||
}
|
||||
case '3':{
|
||||
auto v = this->_controller.getPaidBills();
|
||||
int sum=0;
|
||||
for (Bill bill : v){
|
||||
sum+=bill.sum();
|
||||
std::cout<<bill.company_name()<<";"<<bill.serial_number()<<";"<<bill.sum()<<";"<<bill.isPaid()<<std::endl;
|
||||
}
|
||||
std::cout<<"Sum: "<<sum<<std::endl;
|
||||
break;
|
||||
}
|
||||
case '4':
|
||||
return;
|
||||
default:
|
||||
printf("Invalid option");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "domain.h"
|
||||
#include "repository.h"
|
||||
#include "controller.h"
|
||||
|
||||
class UI{
|
||||
private:
|
||||
Controller _controller;
|
||||
public:
|
||||
UI();
|
||||
~UI();
|
||||
void run();
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "domain.h"
|
||||
#include "repository.h"
|
||||
#include "controller.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
Controller::Controller(){
|
||||
|
||||
}
|
||||
|
||||
Controller::~Controller(){
|
||||
|
||||
}
|
||||
|
||||
bool Controller::add(char* company_name,char* serial_number,int sum,bool isPaid){
|
||||
return this->_repository.add(Bill(company_name,serial_number,sum,isPaid));
|
||||
}
|
||||
|
||||
std::vector<Bill>::iterator Controller::begin(){
|
||||
return this->_repository.begin();
|
||||
}
|
||||
|
||||
std::vector<Bill>::iterator Controller::end(){
|
||||
return this->_repository.end();
|
||||
}
|
||||
|
||||
std::vector<Bill> Controller::getBills(){
|
||||
std::vector<Bill> copy_bills = this->_repository.bills();
|
||||
std::sort(copy_bills.begin(),copy_bills.end(),[](Bill bill1,Bill bill2){return strcmp(bill1.company_name(),bill2.company_name()) <= 0;});
|
||||
return copy_bills;
|
||||
}
|
||||
|
||||
std::vector<Bill> Controller::getPaidBills(){
|
||||
std::vector<Bill> copy_bills = this->_repository.bills();
|
||||
std::vector<Bill> paid_bills(copy_bills.size());
|
||||
auto it = std::copy_if(copy_bills.begin(),copy_bills.end(),paid_bills.begin(),[](Bill bill){return bill.isPaid();});
|
||||
paid_bills.resize(std::distance(paid_bills.begin(),it));
|
||||
return paid_bills;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "domain.h"
|
||||
#include "repository.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
class Controller{
|
||||
private:
|
||||
Repository _repository;
|
||||
public:
|
||||
Controller();
|
||||
~Controller();
|
||||
/*
|
||||
Add function
|
||||
Input: company_name - char*
|
||||
serial_number - char*
|
||||
sum - int
|
||||
isPaid - bool
|
||||
Output: true - if the bill was added
|
||||
false - if the bill was not added
|
||||
*/
|
||||
bool add(char* company_name,char* serial_number,int sum,bool isPaid);
|
||||
std::vector<Bill>::iterator begin();
|
||||
std::vector<Bill>::iterator end();
|
||||
std::vector<Bill> getBills();
|
||||
/*
|
||||
Get paid bills function
|
||||
Output: paid_bills - std::vector<Bill>
|
||||
*/
|
||||
std::vector<Bill> getPaidBills();
|
||||
};
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "domain.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
|
||||
Bill::Bill(){
|
||||
|
||||
}
|
||||
|
||||
Bill::Bill(char* company_name,char* serial_number,int sum,bool isPaid){
|
||||
this->_company_name = new char[strlen(company_name)+1];
|
||||
strcpy(this->_company_name,company_name);
|
||||
this->_serial_number = new char[strlen(serial_number)+1];
|
||||
strcpy(this->_serial_number,serial_number);
|
||||
this->_sum = sum;
|
||||
this->_isPaid = isPaid;
|
||||
}
|
||||
|
||||
Bill::~Bill(){
|
||||
delete[] this->_company_name;
|
||||
delete[] this->_serial_number;
|
||||
}
|
||||
|
||||
Bill::Bill(const Bill& bill){
|
||||
_company_name = new char[strlen(bill.company_name())+1];
|
||||
strcpy(_company_name,bill.company_name());
|
||||
_serial_number = new char[strlen(bill.serial_number())+1];
|
||||
strcpy(_serial_number,bill.serial_number());
|
||||
_sum = bill.sum();
|
||||
_isPaid = bill.isPaid();
|
||||
}
|
||||
|
||||
char* Bill::company_name() const{
|
||||
return this->_company_name;
|
||||
}
|
||||
|
||||
char* Bill::serial_number() const{
|
||||
return this->_serial_number;
|
||||
}
|
||||
|
||||
int Bill::sum() const{
|
||||
return this->_sum;
|
||||
}
|
||||
|
||||
bool Bill::isPaid() const{
|
||||
return this->_isPaid;
|
||||
}
|
||||
|
||||
void Bill::company_name(char* company_name){
|
||||
delete[] this->_company_name;
|
||||
this->_company_name = new char[strlen(company_name)+1];
|
||||
strcpy(this->_company_name,company_name);
|
||||
}
|
||||
|
||||
void Bill::serial_number(char* serial_number){
|
||||
delete[] this->_serial_number;
|
||||
this->_serial_number = new char[strlen(serial_number)+1];
|
||||
strcpy(this->_serial_number,serial_number);
|
||||
}
|
||||
|
||||
void Bill::sum(int sum){
|
||||
this->_sum = sum;
|
||||
}
|
||||
|
||||
void Bill::isPaid(bool check){
|
||||
this->_isPaid = check;
|
||||
}
|
||||
|
||||
bool Bill::operator==(Bill bill){
|
||||
return strcmp(this->_serial_number,bill.serial_number()) == 0;
|
||||
}
|
||||
|
||||
Bill& Bill::operator=(const Bill& bill){
|
||||
this->_company_name = new char[strlen(bill.company_name())+1];
|
||||
strcpy(this->_company_name,bill.company_name());
|
||||
this->_serial_number = new char[strlen(bill.serial_number())+1];
|
||||
strcpy(this->_serial_number,bill.serial_number());
|
||||
this->_sum = bill.sum();
|
||||
this->_isPaid = bill.isPaid();
|
||||
return *this;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
class Bill{
|
||||
private:
|
||||
char* _company_name;
|
||||
char* _serial_number;
|
||||
int _sum;
|
||||
bool _isPaid;
|
||||
public:
|
||||
Bill();
|
||||
Bill(char* company_name,char* serial_number,int sum,bool isPaid);
|
||||
~Bill();
|
||||
Bill(const Bill& bill);
|
||||
char* company_name() const;
|
||||
char* serial_number() const;
|
||||
int sum() const;
|
||||
bool isPaid() const;
|
||||
void company_name(char* company_name);
|
||||
void serial_number(char* serial_number);
|
||||
void sum(int sum);
|
||||
void isPaid(bool check);
|
||||
bool operator==(Bill bill);
|
||||
Bill& operator=(const Bill& bill);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "UI.h"
|
||||
#include "repository.h"
|
||||
|
||||
int main(){
|
||||
test_repository();
|
||||
UI ui;
|
||||
ui.run();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "repository.h"
|
||||
#include "domain.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
void test_repository(){
|
||||
Repository repository;
|
||||
assert(repository.add(Bill("company1","serial1",100,true)) == true);
|
||||
assert(repository.add(Bill("company1","serial1",200,false)) == false);
|
||||
}
|
||||
|
||||
Repository::Repository(){
|
||||
|
||||
}
|
||||
|
||||
Repository::~Repository(){
|
||||
|
||||
}
|
||||
|
||||
bool Repository::add(Bill bill){
|
||||
if(std::find(this->_bills.begin(),this->_bills.end(),bill) != this->_bills.end()){
|
||||
return false;
|
||||
}
|
||||
this->_bills.push_back(bill);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<Bill>::iterator Repository::begin(){
|
||||
return this->_bills.begin();
|
||||
}
|
||||
|
||||
std::vector<Bill>::iterator Repository::end(){
|
||||
return this->_bills.end();
|
||||
}
|
||||
|
||||
std::vector<Bill> Repository::bills(){
|
||||
return this->_bills;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "domain.h"
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
|
||||
class Repository{
|
||||
private:
|
||||
std::vector<Bill> _bills;
|
||||
public:
|
||||
Repository();
|
||||
~Repository();
|
||||
/*
|
||||
Add function
|
||||
Input: bill - Bill
|
||||
Output: true - if the bill was added
|
||||
false - if the bill was not added
|
||||
*/
|
||||
bool add(Bill bill);
|
||||
std::vector<Bill>::iterator begin();
|
||||
std::vector<Bill>::iterator end();
|
||||
std::vector<Bill> bills();
|
||||
};
|
||||
|
||||
void test_repository();
|
||||
Reference in New Issue
Block a user