-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartList.java
More file actions
122 lines (95 loc) · 3.01 KB
/
Copy pathCartList.java
File metadata and controls
122 lines (95 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class CartList {
//Atributos
private CartNode first;
//Metodos
//Constructor
public CartList() {
first = null;
}
//Getters
public CartNode getFirst() {
return first;
}
//Setters
public void setFirst(CartNode newFirst) {
first = newFirst;
}
//State
public boolean isEmpty() {
return first == null;
}
//Primera Inserción
public void insertInitNode(Service serviceNode, int cuantityNode) {
CartNode newCart = new CartNode(serviceNode, cuantityNode);
newCart.setNext(first);
setFirst(newCart);
}
//Insertar
public void insert(Service service, int cuantity) {
if (service == null || cuantity <= 0) return;
if (first == null) {
first = new CartNode(service, cuantity);
return;
}
CartNode actual = first;
CartNode last = null;
while (actual != null) {
if (actual.getService().getId() == service.getId()) {
actual.setCuantity(actual.getCuantity() + cuantity);
return;
}
last = actual;
actual = actual.getNext();
}
last.setNext(new CartNode(service, cuantity));
}
//eliminar nodes
public CartNode deleteNode(Service deleteService) {
if (deleteService == null || first == null) {
System.out.println("The node was not Found");
return null;
}
if (first.getService().getId() == deleteService.getId()) {
CartNode deleted = first;
first = first.getNext();
deleted.setNext(null);
System.out.println("The searched node was not found");
return deleted;
}
CartNode before = first;
CartNode actual = first.getNext();
while (actual != null && actual.getService().getId() != deleteService.getId()) {
before = actual;
actual = actual.getNext();
}
if (actual != null) {
before.setNext(actual.getNext());
actual.setNext(null);
System.out.println("The searched node was not found");
return actual;
} else {
System.out.println("The node was not found");
return null;
}
}
public void printCart() {
if (first == null) {
System.out.println("The cart is empty.");
return;
}
CartNode actual = first;
double total = 0;
System.out.println("\nCart");
while (actual != null) {
Service s = actual.getService();
int cuantity = actual.getCuantity();
double subtotal = s.getPrice() * cuantity;
System.out.println("ID " + s.getId() + " | " + s.getName() +
" Cuantity " + cuantity +
" Subtotal " + subtotal);
total += subtotal;
actual = actual.getNext();
}
System.out.println("TOTAL: " + total + "\n");
}
}