Tugas Kelas PBO A

Pada tugas Tiket Kereta saya menambahkan input destinasi.


Penambahan string tujuan:


 public TicketMachine(String tujuan)   
   {   
     dest = tujuan;  
     price = 30000;   
     balance = 0;   
     total = 0;   
   }   
   /**   
    * Return the price of a ticket.   
    */   
   public int getPrice()   
   {   
     return price;   
   }   
   /**   
    * Return the amount of money already inserted for the   
    * next ticket.   
    */   
   public int getBalance()   
   {   
     return balance;   
   }   
   /**   
    * Receive an amount of money in cents from a customer.   
    */   
   public void insertMoney(int amount)   
   {   
     balance = balance + amount;   
   }   
   /**   
    * Print a ticket.   
    * Update the total collected and   
    * reduce the balance to zero.   
    */   
   public void printTicket()   
   {   
     // Simulate the printing of a ticket.   
     System.out.println("##################");   
     System.out.println("# The BlueJ Line");   
     System.out.println("# Ticket");   
     System.out.println("# ” + price + ” cents.");   
     System.out.println("##################");   
     System.out.println();   
     // Update the total collected with the balance.   
     total = total + balance;   
     // Clear the balance.   
     balance = 0;   
   }  
   //Destinasi/tujuan  
   public String getDest(){  
     return dest;  
   }  

Pada class IntMain:

 public static void main(String args[])   
    {   
      Scanner scan= new Scanner(System.in);   
      int cost,menu;  
      String dest;  
      System.out.println("Masukkan Tujuan Anda \n");   
      dest=scan.next();  
      System.out.println(dest);  
      TicketMachine ticket=new TicketMachine(dest);  
      System.out.println("1. Get Price");   
      System.out.println("2. Get Balance");   
      System.out.println("3. Destination");  
      System.out.println("4. Insert Money");   
      System.out.println("5. Print Ticket");   
      menu=scan.nextInt();   
      switch(menu)   
      {   
        case 1:   
         cost=ticket.getPrice();   
         System.out.println(cost);   
         break;   
        case 2:   
         ticket.getBalance();   
         break;   
        case 3:   
         dest=ticket.getDest();  
         System.out.println(ticket.getDest());  
         break;   
        case 4:   
         int money=scan.nextInt();   
         ticket.insertMoney(money);   
         break;   
        case 5:   
         ticket.printTicket();   
         break;  
       }   
    }   

Komentar