Skip to content

Commit c5c5fd4

Browse files
authored
Merge pull request #15 from Steva0/modifica-Test-e-mainNode
Modifica test e main node
2 parents 0d82d99 + 73940c4 commit c5c5fd4

File tree

6 files changed

+127
-114
lines changed

6 files changed

+127
-114
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ set(SOURCES
1212
src/CreaDispositivo.cpp
1313
src/Dispositivo.cpp
1414
src/RicercaDispositivo.cpp
15-
src/main.cpp
15+
src/mainNode.cpp
16+
src/LinkedList.cpp
1617
)
1718

1819
set(HEADERS
1920
include/CreaDispositivo.h
2021
include/Dispositivo.h
2122
include/ListaDispositivi.h
2223
include/RicercaDispositivo.h
24+
include/LinkedList.h
2325
)
2426

2527
# Crea l'eseguibile

include/DispositivoNonTrovatoException.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

include/LinkedList.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#ifndef LINKED_LIST_H
22
#define LINKED_LIST_H
33
#include "CreaDispositivo.h"
4-
#include "DispositivoNonTrovatoException.h"
5-
#include "ListaVuotaException.h"
64
#include <iostream>
75
#include <ostream>
8-
6+
#include <exception>
97

108
class LinkedList
119
{
@@ -18,22 +16,18 @@ class LinkedList
1816
Dispositivo* disp;
1917
Node* prev;
2018
Node* next;
19+
2120
Node(Dispositivo& data);
2221

23-
//copy constructor
24-
Node(const Node& data);
25-
26-
//copy assignment
27-
Node& operator=(const Node& data);
28-
2922
~Node();
3023
};
3124

3225
Node* head;
3326
Node* tail;
3427

3528
//Funzioni private
36-
Node* searchDispositivo(const std::string nome) const; //cerca un dispositivo nella lista
29+
Node* searchDispositivoName(const std::string nome) const; //cerca un dispositivo nella lista by NAME
30+
Node* searchDispositivoId(const int id) const; //cerca un dispositivo nella lista by ID
3731
void connectBefore(Node* p, Node* q); //connette prima di p il nodo q
3832

3933
public:
@@ -43,7 +37,8 @@ class LinkedList
4337

4438
//Funzioni membro utili
4539
void insert(Dispositivo& dispositivo); //inserisce un dispositivo in coda
46-
Dispositivo* removeDispositivo(const std::string nome); //rimuove un dispositivo dalla lista accettando il nome del dispositivo
40+
Dispositivo* removeDispositivoName(const std::string nome); //rimuove un dispositivo dalla lista accettando il nome del dispositivo by NAME
41+
Dispositivo* removeDispositivoId(const int id); //rimuove un dispositivo dalla lista accettando il nome del dispositivo by ID
4742
std::vector<Dispositivo*> removeAllDispositiviOff(const int currentTime); //rimuove tutti i dispositivi spenti (la cui ora e' prima dell'orario indicato)
4843
void removeTimer(const std::string nome); //rimuove il timer di un dispositivo
4944
void removeAllTimers(); //rimuove tutti i timer

include/ListaVuotaException.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/LinkedList.cpp

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
#include "../include/LinkedList.h"
2-
using namespace std;
32

43
LinkedList::Node::Node(Dispositivo& data): prev{nullptr}, next{nullptr}, disp{&data}
54
{ }
65

7-
LinkedList::Node::Node(const Node& data): prev{data.prev}, next{data.next}, disp{data.disp}
6+
LinkedList::LinkedList(): head{nullptr}, tail{nullptr}
87
{ }
98

10-
LinkedList::Node& LinkedList::Node::operator=(const Node& data)
11-
{
12-
prev = data.prev;
13-
next = data.next;
14-
disp = data.disp;
15-
return *this;
16-
}
17-
189
LinkedList::Node::~Node()
1910
{
20-
delete disp;
11+
prev = nullptr;
12+
next = nullptr;
2113
}
2214

23-
LinkedList::LinkedList(): head{nullptr}, tail{nullptr}
24-
{ }
25-
2615
LinkedList::LinkedList(Dispositivo& dispositivo)
2716
{
2817
head = tail = new Node(dispositivo);
@@ -62,71 +51,98 @@ void LinkedList::insert(Dispositivo& dispositivo)
6251
}
6352
}
6453

65-
Dispositivo* LinkedList::removeDispositivo(const string nome)
54+
Dispositivo* LinkedList::removeDispositivoName(const std::string nome)
55+
{
56+
if(isEmpty())
57+
{
58+
throw std::out_of_range("Lista vuota!");
59+
}
60+
61+
Node* current = searchDispositivoName(nome);
62+
63+
if(current == head)
64+
{
65+
head = head->next;
66+
head->prev = nullptr;
67+
}
68+
else if(current == tail)
69+
{
70+
tail = tail->prev;
71+
tail->next = nullptr;
72+
}
73+
else
74+
{
75+
current->prev->next = current->next;
76+
current->next->prev = current->prev;
77+
}
78+
79+
Dispositivo* disp = current->disp;
80+
delete current;
81+
return disp;
82+
}
83+
84+
Dispositivo* LinkedList::removeDispositivoId(const int id)
6685
{
6786
if(isEmpty())
6887
{
69-
throw ListaVuotaException();
88+
throw std::out_of_range("Lista vuota!");
7089
}
7190

72-
Node* current = searchDispositivo(nome);
91+
Node* current = searchDispositivoId(id);
7392

7493
if(current == head)
7594
{
7695
head = head->next;
7796
head->prev = nullptr;
78-
return current->disp;
7997
}
8098
else if(current == tail)
8199
{
82100
tail = tail->prev;
83101
tail->next = nullptr;
84-
return current->disp;
85102
}
86103
else
87104
{
88105
current->prev->next = current->next;
89106
current->next->prev = current->prev;
90-
return current->disp;
91107
}
108+
109+
Dispositivo* disp = current->disp;
110+
delete current;
111+
return disp;
92112
}
93113

94-
vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
114+
std::vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
95115
{
96116
if(isEmpty())
97117
{
98-
throw ListaVuotaException();
118+
throw std::out_of_range("Lista vuota!");
99119
}
100120

101-
vector<Dispositivo*> dispositiviSpenti;
121+
std::vector<Dispositivo*> dispositiviSpenti;
102122
Node* current = head;
103123
while(current)
104124
{
105125
if(current->disp->getOrarioSpegnimento() <= currentTime)
106126
{
107127
dispositiviSpenti.push_back(current->disp);
108128
Node* temp = current;
109-
current = current->next;
110-
removeDispositivo(temp->disp->getNome());
111-
}
112-
else
113-
{
114-
current = current->next;
129+
removeDispositivoName(temp->disp->getNome());
115130
}
131+
current = current->next;
116132
}
117133

118134
return dispositiviSpenti;
119135
}
120136

121137

122-
void LinkedList::removeTimer(const string nome)
138+
void LinkedList::removeTimer(const std::string nome)
123139
{
124140
if(isEmpty())
125141
{
126-
throw ListaVuotaException();
142+
throw std::out_of_range("Lista vuota!");;
127143
}
128144

129-
Node* current = searchDispositivo(nome);
145+
Node* current = searchDispositivoName(nome);
130146

131147
current->disp->setTimerOff();
132148
}
@@ -135,7 +151,7 @@ void LinkedList::removeAllTimers()
135151
{
136152
if(isEmpty())
137153
{
138-
throw ListaVuotaException();
154+
throw std::out_of_range("Lista vuota!");;
139155
}
140156

141157
Node* current = head;
@@ -151,7 +167,7 @@ bool LinkedList::isEmpty() const
151167
return (head == nullptr);
152168
}
153169

154-
ostream& operator<<(ostream& os, const LinkedList& list)
170+
std::ostream& operator<<(std::ostream& os, const LinkedList& list)
155171
{
156172
if(list.isEmpty())
157173
{
@@ -180,11 +196,11 @@ void LinkedList::connectBefore(Node* p, Node* q)
180196
p->prev = q;
181197
}
182198

183-
LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const
199+
LinkedList::Node* LinkedList::searchDispositivoName(const std::string nome) const
184200
{
185201
if(isEmpty())
186202
{
187-
throw ListaVuotaException();
203+
throw std::out_of_range("Lista vuota!");;
188204
}
189205

190206
Node* current = head;
@@ -195,7 +211,28 @@ LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const
195211

196212
if(current == nullptr)
197213
{
198-
throw DispositivoNonTrovatoException();
214+
throw std::invalid_argument("Dispositivo non trovato!");;
215+
}
216+
217+
return current;
218+
}
219+
220+
LinkedList::Node* LinkedList::searchDispositivoId(const int id) const
221+
{
222+
if(isEmpty())
223+
{
224+
throw std::out_of_range("Lista vuota!");;
225+
}
226+
227+
Node* current = head;
228+
while(current && current->disp->getId() != id)
229+
{
230+
current = current->next;
231+
}
232+
233+
if(current == nullptr)
234+
{
235+
throw std::invalid_argument("Dispositivo non trovato!");;
199236
}
200237

201238
return current;

src/mainNode.cpp

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,48 @@ using namespace std;
66

77
int main()
88
{
9-
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
10-
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
11-
Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale
12-
Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min
13-
Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min
14-
Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min
15-
16-
LinkedList list = LinkedList();
17-
18-
list.insert(Frigorifero);
19-
cout << list << endl;
20-
21-
list.insert(Lavatrice);
22-
cout << list << endl;
23-
24-
list.insert(ScaldaBagno);
25-
cout << list << endl;
26-
27-
list.insert(Televisore);
28-
cout << list << endl;
29-
30-
list.insert(FornoMicroonde);
31-
cout << list << endl;
32-
33-
list.insert(Asciugatrice);
34-
cout << list << endl;
35-
36-
list.removeDispositivo("Scaldabagno");
37-
cout << list << endl;
38-
39-
cout << Televisore.getOrarioSpegnimento() << endl;
40-
41-
list.removeAllDispositiviOff(5);
42-
cout << list << endl;
43-
44-
list.removeTimer("Asciugatrice");
45-
cout << list << endl;
46-
cout << Asciugatrice.getOrarioSpegnimento() << endl;
47-
48-
9+
try {
10+
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
11+
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
12+
Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale
13+
Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min
14+
Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min
15+
Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min
16+
17+
LinkedList list = LinkedList();
18+
19+
list.insert(Frigorifero);
20+
cout << list << endl;
21+
22+
list.insert(Lavatrice);
23+
cout << list << endl;
24+
25+
list.insert(ScaldaBagno);
26+
cout << list << endl;
27+
28+
list.insert(Televisore);
29+
cout << list << endl;
30+
31+
list.insert(FornoMicroonde);
32+
cout << list << endl;
33+
34+
list.insert(Asciugatrice);
35+
cout << list << endl;
36+
37+
list.removeDispositivoName("Scaldabagno");
38+
cout << list << endl;
39+
40+
cout << Televisore.getOrarioSpegnimento() << endl;
41+
42+
list.removeAllDispositiviOff(5);
43+
cout << list << endl;
44+
45+
list.removeTimer("Asciugatrice");
46+
cout << list << endl;
47+
cout << Asciugatrice.getOrarioSpegnimento() << endl;
48+
}
49+
catch (exception& e) {
50+
return 1;
51+
}
52+
return 0;
4953
}

0 commit comments

Comments
 (0)