-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase3.java
More file actions
89 lines (69 loc) · 1.65 KB
/
case3.java
File metadata and controls
89 lines (69 loc) · 1.65 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
class Product
{
String name;
double price;
Product(String n, double p)
{
name = n;
price = p;
}
}
class cart
{
int items = 0;
Product[] shop = new Product[100];
void addProduct(Product product)
{
if(items < 100)
{
shop[items] = product;
items++;
}
else
{
System.out.println("Cart is full");
}
}
void remProduct(String productName)
{
}
void totalCost()
{
double cost = 0;
for(int i = 0; i < items; i++)
{
cost = cost + shop[i].price;
}
System.out.println("Total cost: " + cost);
}
void display()
{
if(items == 0)
{
System.out.println("Cart is empty");
return;
}
for(int i = 0; i < items; i++)
{
System.out.println("Product: " + shop[i].name + "\tPrice: " + shop[i].price);
}
}
}
public class case3 {
public static void main(String[] args) {
cart shop = new cart();
Product p1 = new Product("Laptop", 50000);
shop.addProduct(p1);
Product p2 = new Product("Mouse", 800);
shop.addProduct(p2);
Product p3 = new Product("Keyboard", 1500);
shop.addProduct(p3);
System.out.println("Items in Cart:");
shop.display();
shop.totalCost();
shop.remProduct("Mouse");
System.out.println("\nAfter removing Mouse:");
shop.display();
shop.totalCost();
}
}