forked from SeungwonVictorOh/RefactoringB1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVRUI.java
211 lines (169 loc) · 5.59 KB
/
VRUI.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class VRUI {
private static Scanner scanner = new Scanner(System.in) ;
private List<Customer> customers = new ArrayList<Customer>() ;
private List<Video> videos = new ArrayList<Video>() ;
public static void main(String[] args) {
VRUI ui = new VRUI() ;
boolean quit = false ;
while ( ! quit ) {
int command = ui.showCommand() ;
switch ( command ) {
case 0: quit = true ; break ;
case 1: ui.listCustomers() ; break ;
case 2: ui.listVideos() ; break ;
case 3: ui.registerCustomer() ; break ;
case 4: ui.registerVideo() ; break ;
case 5: ui.rentVideo() ; break ;
case 6: ui.returnVideo() ; break ;
case 7: ui.getCustomerReport() ; break;
case 8: ui.clearRentals() ; break ;
case -1: ui.init() ; break ;
default: break ;
}
}
System.out.println("Bye");
}
public void printEnterCustomerName() {
System.out.println("Enter customer name: ") ;
}
public void clearRentals() {
printEnterCustomerName();
String customerName = scanner.next() ;
Customer foundCustomer = findCustomer(customerName);
// query
if (foundCustomer == null) {
System.out.println("not found costomer.");
} else {
foundCustomer.printCustomerInfo();
for ( Rental rental: foundCustomer.getRentals() ) {
System.out.print("\tTitle: " + rental.getVideo().getTitle() + " ") ;
System.out.print("\tPrice Code: " + rental.getVideo().getPriceCode()) ;
}
// modifier
foundCustomer.clear();
}
}
public void returnVideo() {
printEnterCustomerName();
String customerName = scanner.next() ;
Customer foundCustomer = findCustomer(customerName);
if ( foundCustomer == null ) return ;
System.out.println("Enter video title to return: ") ;
String videoTitle = scanner.next() ;
List<Rental> customerRentals = foundCustomer.getRentals() ;
for ( Rental rental: customerRentals ) {
if ( rental.getVideo().getTitle().equals(videoTitle) && rental.getVideo().isRented() ) {
rental.returnVideo();
rental.getVideo().setRented(false);
break ;
}
}
}
private Customer findCustomer(String customerName) {
for ( Customer customer: customers ) {
if ( customer.getName().equals(customerName)) {
return customer ;
}
}
return null;
}
private void init() {
Customer james = new Customer("James") ;
Customer brown = new Customer("Brown") ;
customers.add(james) ;
customers.add(brown) ;
Video v1 = new Video("v1", Video.CD, Video.REGULAR, new Date()) ;
Video v2 = new Video("v2", Video.DVD, Video.NEW_RELEASE, new Date()) ;
videos.add(v1) ;
videos.add(v2) ;
Rental r1 = new Rental(v1) ;
Rental r2 = new Rental(v2) ;
james.addRental(r1) ;
james.addRental(r2) ;
}
public void listVideos() {
System.out.println("List of videos");
for ( Video video: videos ) {
System.out.println("Price code: " + video.getPriceCode() +"\tTitle: " + video.getTitle()) ;
}
System.out.println("End of list");
}
public void listCustomers() {
System.out.println("List of customers");
for ( Customer customer: customers ) {
System.out.println("Name: " + customer.getName() +
"\tRentals: " + customer.getRentals().size()) ;
for ( Rental rental: customer.getRentals() ) {
System.out.print("\tTitle: " + rental.getVideo().getTitle() + " ") ;
System.out.print("\tPrice Code: " + rental.getVideo().getPriceCode()) ;
}
}
System.out.println("End of list");
}
public void getCustomerReport() {
printEnterCustomerName();
String customerName = scanner.next() ;
Customer foundCustomer = findCustomer(customerName);
if ( foundCustomer == null ) {
System.out.println("No customer found") ;
} else {
String result = foundCustomer.getReport() ;
System.out.println(result);
}
}
public void rentVideo() {
printEnterCustomerName();
String customerName = scanner.next() ;
Customer foundCustomer = findCustomer(customerName);
if ( foundCustomer == null ) return ;
System.out.println("Enter video title to rent: ") ;
String videoTitle = scanner.next() ;
Video foundVideo = null ;
for ( Video video: videos ) {
if ( video.getTitle().equals(videoTitle) && video.isRented() == false ) {
foundVideo = video ;
break ;
}
}
if ( foundVideo == null ) return ;
Rental rental = new Rental(foundVideo) ;
foundVideo.setRented(true);
// encapulate collection
foundCustomer.addRental(rental);
}
private void registerCustomer() {
printEnterCustomerName();
String name = scanner.next();
Customer customer = new Customer(name) ;
customers.add(customer) ;
}
private void registerVideo() {
System.out.println("Enter video title to register: ") ;
String title = scanner.next() ;
System.out.println("Enter video type( 1 for VHD, 2 for CD, 3 for DVD ):") ;
int videoType = scanner.nextInt();
System.out.println("Enter price code( 1 for Regular, 2 for New Release ):") ;
int priceCode = scanner.nextInt();
Date registeredDate = new Date();
Video video = new Video(title, videoType, priceCode, registeredDate) ;
videos.add(video) ;
}
public int showCommand() {
System.out.println("\nSelect a command !");
System.out.println("\t 0. Quit");
System.out.println("\t 1. List customers");
System.out.println("\t 2. List videos");
System.out.println("\t 3. Register customer");
System.out.println("\t 4. Register video");
System.out.println("\t 5. Rent video");
System.out.println("\t 6. Return video");
System.out.println("\t 7. Show customer report");
System.out.println("\t 8. Show customer and clear rentals");
int command = scanner.nextInt() ;
return command ;
}
}