Ticket Machine

    Ticket machine berikut merupakan sistem mesin tiket, yang mana dalam hal ini ialah mesin tiket handshake JKT48. Dalam kasus ini, tiket handshake JKT48 memiliki besaran harga yang berbeda-beda, tergantung dari timnya, yang terdiri dari Team J, Team K-III, Team T, dan Academy. Sehingga nantinya, tiket yang keluar sebagai output merupakan tiket dari salah satu tim yang dipilih.


    Berikut merupakan diagram classnya.



1.TicketMachine.java

Source Code :

  1. /**
  2. * @author Sabrina Lydia S
  3. * @version 02/11/2020
  4. */
  5.  
  6. public class TicketMachine
  7. {
  8. private int price;
  9. private int balance;
  10. private int total;
  11.  
  12. public TicketMachine(int cost)
  13. {
  14. price = cost;
  15. balance = 0;
  16. total = 0;
  17. }
  18.  
  19. /**
  20. * Return the price of a ticket
  21. */
  22. public int getPrice()
  23. {
  24. return price;
  25. }
  26.  
  27. /**
  28. * Return the amount of money already inserted for the
  29. * next ticket.
  30. */
  31. public int getBalance()
  32. {
  33. return balance;
  34. }
  35.  
  36. /**
  37. * Receive an amount of money from a customer
  38. */
  39. public void insertMoney(int amount)
  40. {
  41. balance = balance + amount;
  42. }
  43.  
  44. /**
  45. * Print a ticket
  46. * Update the total collected
  47. * reduce the balnce to zero.
  48. */
  49. public void printTicket1()
  50. {
  51. //Simulate the printing of a ticket
  52. System.out.println("###########################");
  53. System.out.println("# JKT48 Handshake Ticket #");
  54. System.out.println("# Team J #");
  55. System.out.println("# Rp200.000,- #");
  56. System.out.println("# Enjoy your handshake! #");
  57. System.out.println("###########################");
  58.  
  59. // Update the total collected with the balance.
  60. total = total + balance;
  61.  
  62. // Clear the balance.
  63. balance = 0;
  64. }
  65. public void printTicket2()
  66. {
  67. //Simulate the printing of a ticket
  68. System.out.println("###########################");
  69. System.out.println("# JKT48 Handshake Ticket #");
  70. System.out.println("# Team K-III #");
  71. System.out.println("# Rp200.000,- #");
  72. System.out.println("# Enjoy your handshake! #");
  73. System.out.println("###########################");
  74.  
  75. // Update the total collected with the balance.
  76. total = total + balance;
  77.  
  78. // Clear the balance.
  79. balance = 0;
  80. }
  81. public void printTicket3()
  82. {
  83. //Simulate the printing of a ticket
  84. System.out.println("###########################");
  85. System.out.println("# JKT48 Handshake Ticket #");
  86. System.out.println("# Team T #");
  87. System.out.println("# Rp180.000,- #");
  88. System.out.println("# Enjoy your handshake! #");
  89. System.out.println("###########################");
  90.  
  91. // Update the total collected with the balance.
  92. total = total + balance;
  93.  
  94. // Clear the balance.
  95. balance = 0;
  96. }
  97. public void printTicket4()
  98. {
  99. //Simulate the printing of a ticket
  100. System.out.println("###########################");
  101. System.out.println("# JKT48 Handshake Ticket #");
  102. System.out.println("# Academy #");
  103. System.out.println("# Rp150.000,- #");
  104. System.out.println("# Enjoy your handshake! #");
  105. System.out.println("###########################");
  106.  
  107. // Update the total collected with the balance.
  108. total = total + balance;
  109.  
  110. // Clear the balance.
  111. balance = 0;
  112. }
  113. }

2. TicketMenu.java

Source code:

  1. /**
  2. * @author Sabrina Lydia S
  3. * @version 02/11/20
  4. */
  5.  
  6. import java.util.Scanner;
  7. public class TicketMenu
  8. {
  9. public static void main(String args[])
  10. {
  11. Scanner scan = new Scanner(System.in);
  12. int cost = 0;
  13. int team;
  14. int menu;
  15. System.out.println("JKT48 Handshake Ticket \n");
  16. System.out.println("1.Team J = Rp.200.000");
  17. System.out.println("2.Team K-III = Rp.200.000");
  18. System.out.println("3.Team T = Rp.180.000");
  19. System.out.println("4.Academy = Rp.150.000");
  20. System.out.println("Please enter the number of the team");
  21. team = scan.nextInt();
  22.  
  23. switch(team)
  24. {
  25. case 1:
  26. cost = 200000;
  27. break;
  28. case 2:
  29. cost = 200000;
  30. break;
  31. case 3:
  32. cost = 180000;
  33. break;
  34. case 4:
  35. cost = 150000;
  36. break;
  37.  
  38. }
  39. TicketMachine ticket = new TicketMachine(cost);
  40.  
  41. while(true)
  42. {
  43. System.out.println("1. Get Price");
  44. System.out.println("2. Get Balance");
  45. System.out.println("3. Insert Money");
  46. System.out.println("4. Print Ticket");
  47. menu = scan.nextInt();
  48.  
  49.  
  50. switch(menu)
  51. {
  52. case 1:
  53. cost= ticket.getPrice();
  54. System.out.println(cost);
  55. break;
  56. case 2:
  57. System.out.println(ticket.getBalance());
  58. break;
  59. case 3:
  60. int money = scan.nextInt();
  61. ticket.insertMoney(money);
  62. break;
  63. case 4:
  64. switch(team)
  65. {
  66. case 1:
  67. ticket.printTicket1();
  68. break;
  69. case 2:
  70. ticket.printTicket2();
  71. break;
  72. case 3:
  73. ticket.printTicket3();
  74. break;
  75. case 4:
  76. ticket.printTicket4();
  77. break;
  78.  
  79. }
  80.  
  81. System.out.println("Thank You for Your Purchased!");
  82. break;
  83.  
  84. }
  85.  
  86.  
  87. }
  88. }
  89. }

OUTPUT

Berikut merupakan hasil output dari program.



Komentar

Postingan populer dari blog ini

ETS PBO

Tugas Simulasi Traffic Light

Membuat Aplikasi Desktop - Tugas 2 PBKK