forked from khanaaziz1/Hacktoberfest_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheShoppingBasket.java
73 lines (60 loc) · 1.55 KB
/
TheShoppingBasket.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
import java.util.*;
/**
* Write a description of class TheShoppingBasket here.
*
* @author (Oarabile Mwiya)
* @version (Final)
*/
public class TheShoppingBasket
{
// instance variables
private ArrayList<Items> items;
// initialisation of instance variables
public TheShoppingBasket()
{
items = new ArrayList<Items>();
}
//A method used to add an item to the collection
public void addNewItem(int code, String name, double price)
{
Items newItem = new Items(code, name, price);
items.add(newItem);
}
//count the number of items in the collection
public int getItemsCount()
{
return items.size();
}
//Method used to display items in the basket
public void printItems()
{
int index = 1;
for( Items item : items)
{
System.out.println(index+". The item Code is: "+item.getCode()+"\n"+
" Item Name: "+item.getName()+"\n"+" Item Price: P"+item.getPrice());
index++;
}
}
//Method that gives the sum of items..
public double itemTotal()
{
double total = 0;
for(Items item : items)
{
total = total + item.getPrice();
}
return total;
}
// method that removes all items whose price is greater than some maximum
public void lessItems(int maximum)
{
for(Items item : items)
{
if(maximum < item.getPrice())
{
items.remove(item);
}
}
}
}