-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyShop.java
110 lines (83 loc) · 3.23 KB
/
MyShop.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
import java.util.ArrayList;
public class MyShop {
private ArrayList<Content> contents;
private ArrayList<User> users;
public MyShop() {
contents = new ArrayList<Content>();
users = new ArrayList<User>();
}
public void addContent(Content content) {
contents.add(content);
// print out adding message according to subclass
if (content instanceof Application)
System.out.println("Application " + content.getId() + " " + content.getName() + " has been added.");
else if (content instanceof Book)
System.out.println("Book " + content.getId() + " " + content.getName() + " has been added.");
else if (content instanceof Magazine)
System.out.println("Magazine " + content.getId() + " " + content.getName() + " has been added.");
}
public void showContent() {
System.out.println("Contents in this shop:");
// print out content id and name according to subclass
for (Content i : contents) {
if (i instanceof Application)
System.out.println("Application ID: " + i.getId() + " | Application Name: " + i.getName());
else if (i instanceof Book)
System.out.println("Book ID: " + i.getId() + " | Book Name: " + i.getName());
else if (i instanceof Magazine)
System.out.println("Magazine ID: " + i.getId() + " | Magazine Name: " + i.getName());
}
}
// show download numbers of all contents in the shop
public void showDownloads() {
System.out.println("Downloads in this shop: ");
for (Content i : contents) {
System.out.println(i.getId() + " " + i.getName() + " downloaded " + i.getDownload() + " times");
}
}
public void addUser(User user) {
users.add(user);
// print out adding message according to subclass
if (user instanceof Customer)
System.out.println("Customer " + user.getId() + " " + user.getName() + " has been added.");
else if (user instanceof Admin)
System.out.println("Admin " + user.getId() + " " + user.getName() + " has been added.");
}
// print all users id and name in the shop
public void showUser() {
System.out.println("Users in this shop:");
// print out user id and name according to subclass
for (User i : users) {
if (i instanceof Customer)
System.out.println("Customer ID: " + i.getId() + " | Customer Name: " + i.getName());
else if (i instanceof Admin)
System.out.println("Admin ID: " + i.getId() + " | Admin Name: " + i.getName());
}
}
// print comments for all contents in the shop
public void showAllReviews() {
System.out.println("Comments for all contents in this shop:");
// print comments if the content has comment
for (Content i : contents) {
if (i.getComments().size() != 0)
i.showReviews();
}
}
// adjust price in bulk
// use return value of admin.login() as boolean parameter login
// double parameter discount should be negative
public boolean setPrice(boolean login, int level, double discount) {
if (login & (level > 5)) {
System.out.println("Level satisfied.");
for (Content i : contents) {
i.setPrice(i.getPrice() * (1 + discount));
}
System.out.println("Bulk price changed.");
return true;
}
else {
System.out.println("Bulk price not changed.");
return false;
}
}
}