-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.java
152 lines (145 loc) · 4.48 KB
/
Cart.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package hust.soict.dsai.aims.cart;
import hust.soict.dsai.aims.disc.DigitalVideoDisc;
public class Cart {
public static final int MAX_NUMBERS_ORDERED = 20;
private int qtyOrdered = 0;
private DigitalVideoDisc itemsOrdered[] = new DigitalVideoDisc[MAX_NUMBERS_ORDERED];
private float totalCost = 0;
public void addDigitalVideoDisc(DigitalVideoDisc disc)
{
if (qtyOrdered == MAX_NUMBERS_ORDERED)
{
System.out.println("The cart is almost full");
}
else {
itemsOrdered[qtyOrdered] = disc;
qtyOrdered+=1;
System.out.println("The disc has been added");
}
return;
}
public void addDigitalVideoDisc(DigitalVideoDisc[] dvdList)
{
if (qtyOrdered == MAX_NUMBERS_ORDERED)
{
System.out.println("The cart is almost full");
}
else {
for (int i = 0; i < dvdList.length; i++)
{
itemsOrdered[qtyOrdered] = dvdList[i];
qtyOrdered += 1;
if (qtyOrdered == MAX_NUMBERS_ORDERED)
{
System.out.println("The cart is almost full");
break;
}
}
}
return;
}
public void addDigitalVideoDisc(DigitalVideoDisc dvd1, DigitalVideoDisc dvd2)
{
if (qtyOrdered == MAX_NUMBERS_ORDERED)
{
System.out.println("The cart is almost full");
}
else {
itemsOrdered[qtyOrdered] = dvd1;
qtyOrdered+=1;
System.out.println("The first disc has been added");
if (qtyOrdered == MAX_NUMBERS_ORDERED)
{
System.out.println("The cart is almost full");
}
else
{
itemsOrdered[qtyOrdered] = dvd2;
qtyOrdered+=1;
System.out.println("The second disc has been added");
}
}
return;
}
public void removeDigitalVideoDisc(DigitalVideoDisc disc)
{
int found = 0;
int index;
if (qtyOrdered == 0)
{
System.out.println("No discs in cart");
return;
}
for (int i = 0; i < qtyOrdered; i++)
{
if (itemsOrdered[i] == disc)
{
index = i;
DigitalVideoDisc tmp[] = new DigitalVideoDisc[qtyOrdered-1];
for (int j = 0; j < index; j++)
{
tmp[j] = itemsOrdered[j];
}
for (int j = index; j < qtyOrdered-1; j++)
{
tmp[j] = itemsOrdered[j+1];
}
itemsOrdered = tmp;
qtyOrdered-=1;
found = 1;
System.out.println("The disc has been removed");
break;
}
}
if (found == 0)
System.out.println("The disc is not found");
}
public float totalCost()
{
float result = 0;
for (int i = 0; i < qtyOrdered; i++)
{
result+=itemsOrdered[i].getCost();
}
return result;
}
public void print()
{
System.out.println("***********************CART***********************");
System.out.println("Ordered Items:");
for (int i = 0; i < qtyOrdered; i++)
{
System.out.println(itemsOrdered[i].getId() + ". DVD - " + itemsOrdered[i].getTitle() + " - "
+ itemsOrdered[i].getCategory() + " - " + itemsOrdered[i].getDirector() + " - "
+ itemsOrdered[i].getLength() + ": " + itemsOrdered[i].getCost() + "$");
}
this.totalCost = totalCost();
System.out.println(this.totalCost);
System.out.println("***************************************************");
}
public void searchID(int x)
{
for (int i = 0; i < qtyOrdered; i++)
{
if (itemsOrdered[i].getId() == x)
{
System.out.println(itemsOrdered[i].toString());
return;
}
}
System.out.println("Not found");
return;
}
public void searchTitle(String title)
{
for (int i = 0; i < qtyOrdered; i++)
{
if (title.compareTo(itemsOrdered[i].getTitle())==0) {
System.out.println(itemsOrdered[i].toString());
return;
}
}
System.out.println("Not found");
return;
}
}