forked from foojayio/getting_started_with_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingObject.java
More file actions
49 lines (40 loc) · 1.47 KB
/
Copy pathUsingObject.java
File metadata and controls
49 lines (40 loc) · 1.47 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
import java.util.ArrayList;
import java.util.List;
public class UsingObject {
public static void main (String[] args) {
List<ShoppingCartItem> items = new ArrayList<>();
items.add(new ShoppingCartItem("Raspberry Pi 4, 4Gb", 1, 59.95F));
items.add(new ShoppingCartItem("Micro-HDMI cable", 2, 5.9F));
items.add(new ShoppingCartItem("Raspberry Pi 4 power supply", 1, 9.95F));
double total = 0D;
for (ShoppingCartItem item : items) {
System.out.println(item.getName());
System.out.println(" " + item.getQuantity() + "\tx\t" + item.getPrice() + "\t= " + item.getTotal() + " Euro");
total += item.getTotal();
}
System.out.println("\nTotal for shopping cart:\n " + total + " Euro");
}
public static class ShoppingCartItem {
// These values are final as they should not be changed
private final String name;
private final int quantity;
private final float price;
public ShoppingCartItem(String name, int quantity, float price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public float getPrice() {
return price;
}
public float getTotal() {
return quantity * price;
}
}
}