Logout
"Real-world" Full Example of Multiple OOP Class Interaction
MainForTicketing.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Sparta Ticketing - John/src/spartaticketingjohn/MainForTicketing.java |
1 package spartaticketingjohn;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7
8
9 @author
10
11 public class MainForTicketing {
12
13 public static void main(String[] args) {
14 try {
15 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16 System.out.println("What is your name?");
17 String name = br.readLine();
18 System.out.println("How much money would you like to put in your account?");
19 int money = Integer.parseInt(br.readLine());
20 System.out.println("What year were you born? (Example 1996)");
21 int year = Integer.parseInt(br.readLine());
22 System.out.println("What month were you born? (Example 07)");
23 int month = Integer.parseInt(br.readLine());
24 System.out.println("What day were you born? (Example 22)");
25 int day = Integer.parseInt(br.readLine());
26 TicketingAccount user = new TicketingAccount(name, money, year, month, day);
27
28 System.out.println("");
29 System.out.println("How many tickets would you like to buy - wait for 30 seconds if you want a discount...?");
30 int numTickets = Integer.parseInt(br.readLine());
31 int ticketPrice = 200;
32 user.calculateDiscount();
33 ticketPrice *= (1 - user.getDiscountedAmount());
34 int moneyToSpend = numTickets * ticketPrice;
35
36 if (moneyToSpend > user.getAccountBalance()) {
37 System.out.println("Sorry " + user.getName() + " you don't have enough money for this purchase.");
38 System.out.println("You would need " + (moneyToSpend - user.getAccountBalance()) + " more crowns deposited to your account for this.");
39 } else {
40
41 System.out.println("For " + numTickets + " tickets you owe us " + moneyToSpend + ".");
42 user.makePurchase(moneyToSpend);
43 System.out.println("And after payment, " + user.getName()
44 + " your balance will be: " + user.getAccountBalance() + ".");
45 }
46
47 } catch (IOException ex) {
48 System.out.println("Problem in main method.");
49 }
50
51 }
52 }
53
54
TicketingAccount.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Sparta Ticketing - John/src/spartaticketingjohn/TicketingAccount.java |
1
2 package spartaticketingjohn;
3
4 import java.util.Date;
5 import java.util.GregorianCalendar;
6
7
8
9 @author
10
11 public class TicketingAccount {
12
13 private String name = "not set yet";
14 private String userID = "not set yet";
15 private double accountBalance = 0;
16
17 private double discountAmount = 0;
18 private Date birthdayDate = new Date();
19 private Date dateCreatedDate = new Date();
20 private int age = -999;
21
22 public TicketingAccount() {
23 }
24
25 public TicketingAccount(String name, double accountBalance, int year, int month, int day) {
26 this.name = name;
27 this.accountBalance = accountBalance;
28 GregorianCalendar gcForBirthday = new GregorianCalendar(year, month, day);
29 birthdayDate = gcForBirthday.getTime();
30 userID = name + "_" + birthdayDate;
31 dateCreatedDate = new GregorianCalendar().getTime();
32 System.out.println("The current date, which will be recorded as your account creation date is: " + dateCreatedDate);
33
34 }
35
36 public double getDiscountedAmount() {
37 return discountAmount;
38 }
39
40 public double getAccountBalance(){
41 return accountBalance;
42 }
43
44 public void makePurchase(double amount){
45 accountBalance -= amount;
46 }
47
48 public void addCredit(double amount){
49 accountBalance += amount;
50 }
51
52 public String getName(){
53 return name;
54 }
55
56 public void calculateDiscount() {
57
58
59
60
61
62 Date nowDate = new Date();
63 long nowValue = nowDate.getTime();
64
65
66
67
68
69
70 long birthdayValueDays = birthdayDate.getTime() / (24 * 60 * 60 * 1000);
71
72
73 long fiveYearsValueDays = 5 * 365;
74 long seventyYearsValueDays = 70 * 365;
75 long nowValueDays = nowValue / (24 * 60 * 60 * 1000);
76 long numDaysDifference = nowValueDays - birthdayValueDays;
77 if (numDaysDifference < fiveYearsValueDays) {
78 System.out.println("User get's a discount since they are under 5 years old");
79 discountAmount += 0.1;
80 } else if (numDaysDifference > seventyYearsValueDays) {
81 System.out.println("User get's a discount since they are over 70 years old");
82 discountAmount += 0.15;
83 }
84
85
86 long dateCreatedValue = dateCreatedDate.getTime();
87
88
89 if (nowValue - dateCreatedValue > 30000) {
90 System.out.println("User get's a discount since they have been a member for more than 30 seconds.");
91 discountAmount += 0.2;
92 }
93 }
94 }
95
96