-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicket.java
More file actions
33 lines (25 loc) · 939 Bytes
/
Ticket.java
File metadata and controls
33 lines (25 loc) · 939 Bytes
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
public abstract class Ticket {
// because this field is static, it will be initialized
// with the Ticket.class when that class is loaded by JVM
private static int counter = 0; //
// the following field is private; all derived classes
// will carry the data but will not be able to access it directly
private int serialNumber;
// because this class is abstract, the constructor below will
// not be called directly. It will, however, be called by
// each derived class, as the first line of their constructor!
public Ticket() {
serialNumber = getNextSerialNumber();
}
// returns the price for this ticket
public abstract double getPrice();
// returns a string with info about the ticket
public String toString() {
return "Number: "+serialNumber+"\nPrice: "+getPrice();
}
// increment and then return our ticket counter
private static int getNextSerialNumber() {
counter++;
return counter;
}
}