1
|
|
package com.instantiations.example.rental;
|
2
|
|
|
3
|
|
import java.io.IOException;
|
4
|
|
import java.io.InputStream;
|
5
|
|
import java.util.GregorianCalendar;
|
6
|
|
|
7
|
|
/**
|
8
|
|
* Car rental sample source.
|
9
|
|
* @author Brad Billings
|
10
|
|
*/
|
11
|
|
|
12
|
|
public class CarRental
|
13
|
|
{
|
14
|
|
public int price = 0;
|
15
|
|
|
16
|
|
public static double weekendAdjustment = 1.2;
|
17
|
|
|
18
|
|
public static GregorianCalendar today = null;
|
19
|
|
|
20
|
|
public static void main(String[] args)
|
21
|
|
{
|
22
|
|
if (args.length == 2) {
|
23
|
|
int age = Integer.parseInt(args[0]);
|
24
|
|
int numPassengers = Integer.parseInt(args[1]);
|
25
|
|
CarRental rental = getRental(age, numPassengers);
|
26
|
|
if (rental != null) {
|
27
|
|
System.out.println("rental price = " + rental.price);
|
28
|
|
} else {
|
29
|
|
System.out.println("no rental");
|
30
|
|
}
|
31
|
|
} else {
|
32
|
|
System.out.println("Arguments: <age> <num passengers>");
|
33
|
|
}
|
34
|
|
}
|
35
|
|
|
36
|
|
public CarRental()
|
37
|
|
{
|
38
|
|
// simulate expensive database operation based on user's rental history
|
39
|
|
price = 10;
|
40
|
|
}
|
41
|
|
|
42
|
|
protected static boolean isWeekend()
|
43
|
|
{
|
44
|
|
if (today == null) {
|
45
|
|
today = new GregorianCalendar();
|
46
|
|
}
|
47
|
|
int dayOfWeek = today.get(GregorianCalendar.DAY_OF_WEEK);
|
48
|
|
return dayOfWeek == GregorianCalendar.SATURDAY
|
49
|
|
|| dayOfWeek == GregorianCalendar.SUNDAY;
|
50
|
|
}
|
51
|
|
|
52
|
|
public static CarRental getRental(int driverAge, int numPassengers)
|
53
|
|
{
|
54
|
|
CarRental rental = null;
|
55
|
|
if (driverAge >= 25) {
|
56
|
|
rental = new CarRental();
|
57
|
|
for (int i = 0; i < numPassengers; i++) {
|
58
|
|
rental.price += (i + 1);
|
59
|
|
}
|
60
|
|
}
|
61
|
|
if (isWeekend()) {
|
62
|
|
rental.price *= weekendAdjustment;
|
63
|
|
}
|
64
|
|
return rental;
|
65
|
|
}
|
66
|
|
|
67
|
|
public static void closeStream(InputStream in)
|
68
|
|
{
|
69
|
|
try
|
70
|
|
{
|
71
|
|
in.close();
|
72
|
|
}
|
73
|
  |
catch (IOException e)
|
74
|
|
{
|
75
|
|
|
76
|
|
}
|
77
|
|
}
|
78
|
|
}
|