Advertisement
zenados

TicketMachine TicketMachine.java

Sep 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. public class TicketMachine  
  2. {  
  3.     public int balance;
  4.     private int price;
  5.     /**
  6.      * Constructor for ticket machine that
  7.      * takes in the price of the ticket as parameter
  8.      */
  9.     public TicketMachine(int ticketCost)  
  10.     {  
  11.         balance=0;  
  12.         price=ticketCost;  
  13.     }  
  14.  
  15.     /**  
  16.      * Method for Ticket Machine  
  17.      * Balance, Price, Insert Money  
  18.      */  
  19.     public int getBalance()  
  20.     {  
  21.         return balance;  
  22.     }  
  23.  
  24.     /**
  25.      * get method for price of ticket
  26.      */
  27.     public int getPrice()  
  28.     {  
  29.         return price;  
  30.     }  
  31.  
  32.     /**
  33.      * method when user inserts money to ticket machine
  34.      */
  35.     public void insertMoney(int money)  
  36.     {  
  37.         balance = balance + money;  
  38.     }  
  39.  
  40.     /**
  41.      * Prints the ticket if the user has inserted sufficient
  42.      * money into the machine.
  43.      */
  44.     public void printTicket()  
  45.     {  
  46.         if(price<=balance){  
  47.             System.out.println(".----------------------");  
  48.             System.out.println("| Ticket CommuterTrain ");  
  49.             System.out.println("| Price : " + price);  
  50.             System.out.println(";______________________");  
  51.             balance = balance - getPrice();
  52.         } else {  
  53.             System.out.println("Insufficent Money");  
  54.         }  
  55.     }  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement