Senin, 06 Maret 2017

Ticket Machine

Ticket Machine adalah sebuah mesin seperti halnya mesin ATM, yang berfungsi melayani penjualan tiket kereta api dari satu tujuan ke tujuan yang lain. Di dalam Ticket Machine terdapat sebuah program yang mengatur harga tiket di tiap tujuan, mengatur kembalian uang, dan juga mencetak receipt sebagai bukti pembelian tiket. Penggunaan mesin ini sangat memudahkan pengguna untuk dapat melakukan transaksi yang diinginkan secara langsung.

Dibawah ini merupakan code Java dari program Ticket Machine:

Class Ticket Machine

  1. public class TicketMachine
  2. {
  3.    
  4.  
  5.     private int price;
  6.     // The amount of money entered by a customer so far.
  7.     private int balance;
  8.     // The total amount of money collected by this machine.
  9.     private int total;
  10.  
  11.  
  12.     public TicketMachine(int ticketCost)
  13.     {
  14.         price = ticketCost;
  15.         balance = 0;
  16.         total = 0;
  17.     }
  18.  
  19.     public int getPrice()
  20.     {
  21.         return price;
  22.     }
  23.  
  24.     public int getBalance()
  25.     {
  26.         return balance;
  27.     }
  28.  
  29.     public void insertMoney(int amount)
  30.     {
  31.         balance = balance + amount;
  32.     }
  33.  
  34.     public void printTicket()
  35.     {
  36.         // Simulate the printing of a ticket.
  37.         System.out.println("##################");
  38.         System.out.println("# The BlueJ Line");
  39.         System.out.println("# Ticket");
  40.         System.out.println("# " + price + " cents.");
  41.         System.out.println("##################");
  42.         System.out.println();
  43.  
  44.         // Update the total collected with the balance.
  45.         total = total + balance;
  46.         // Clear the balance.
  47.         balance = 0;
  48.     }
  49. }

Class Ticket Machine

  1. //Main  
  2.  import java.util.Scanner;  
  3.  public class IntMain  
  4.  {  
  5.      public static int main()  
  6.      {  
  7.          Scanner scan= new Scanner(System.in);  
  8.          int cost,menu;
  9.          System.out.println("Masukkan harga tiket \n");  
  10.          cost=scan.nextInt();  
  11.          TicketMachine ticket=new TicketMachine(cost);
  12.  
  13.          while (true)
  14.          {
  15.              System.out.println("1. Get Price");  
  16.              System.out.println("2. Get Balance");  
  17.              System.out.println("3. Insert Money");  
  18.              System.out.println("4. Print Ticket");  
  19.              System.out.println("5. Exit");
  20.              menu=scan.nextInt();  
  21.  
  22.              switch(menu)  
  23.              {  
  24.                  case 1:  
  25.                  cost=ticket.getPrice();  
  26.                  System.out.println(cost);  
  27.                  break;  
  28.    
  29.                  case 2:  
  30.                  System.out.println(ticket.getBalance());  
  31.                  break;  
  32.    
  33.                  case 3:  
  34.                  int money=scan.nextInt();  
  35.                  ticket.insertMoney(money);  
  36.                  break;  
  37.    
  38.                  case 4:  
  39.                  ticket.printTicket();  
  40.                  break;  
  41.          
  42.                  case 5:
  43.                  return 0;
  44.                 }  
  45.             }  
  46.         }
  47. }

Output

Senin, 27 Februari 2017

Classes and Objects


Program Chapter 8 Classes and Objects: A Deeper Look (8.1 - 8.6)



8.1 Time1 class declaration maintains the time in 24-hour format 


  1. /**  
  2.   * Program Kelas Time1  
  3.   * Deklarasi kelas Time1 mengelola waktu dalam format 24 jam  
  4.   * Nama file : Time1.java  
  5.   */
  6. public class Time
  7. {
  8.     private int hour;
  9.     private int minute;
  10.     private int second;
  11.    
  12.     public void setTime (int h, int m, int s)
  13.     {
  14.         if((h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60))
  15.         {
  16.             hour=h;
  17.             minute=s;
  18.             second=s;
  19.         }
  20.         else
  21.             throw new IllegalArgumentException("hour, minute and/or second was out of range");
  22.     }
  23.    
  24.     public String toUniversalString()
  25.     {
  26.         return String.format("%02d:%02d:%02", hour, minute, second);
  27.     }
  28.    
  29.     public String toString()
  30.     {
  31.         return String.format("%d:%02d:%02d %s",
  32.         ((hour==0 || hour==12)?12:hour%12),
  33.         minute, second, (hour<12? "AM" : "PM"));
  34.     }
  35. }

8.2 Time1 object used in an application 


  1. /**  
  2.   * Program Kelas Time1Test  
  3.   * Objek Time1 digunakan dalam aplikasi  
  4.   * nama file : Time1Test.java  
  5.   */  
  6. public class Time1Test  
  7.  {  
  8.    public static void main( String[] args )  
  9.    {  
  10.      Time1 time = new Time1();  
  11.      System.out.print( "The initial universal time is: ");  
  12.      System.out.println(time.toUniversalString() );  
  13.      System.out.print( "The initial standard time is: ");  
  14.      System.out.println(time.toString() );  
  15.      System.out.println();  
  16.      time.setTime( 13, 27, 6);  
  17.      System.out.print( "Universal time after setTime is: ");  
  18.      System.out.println(time.toUniversalString() );  
  19.      System.out.print("Standard time after setTime is: ");  
  20.      System.out.println( time.toString() );  
  21.      System.out.println();  
  22.      try  
  23.      {  
  24.        time.setTime( 99, 99 , 99 );  
  25.      }  
  26.      catch ( IllegalArgumentException e )  
  27.      {  
  28.        System.out.printf( "Exception: %s\n\n", e.getMessage() );  
  29.      }  
  30.      System.out.println( "After attempting invalid settings:");  
  31.      System.out.print( "Universal time: ");  
  32.      System.out.println(time.toUniversalString() );  
  33.      System.out.print("Standard time: ");  
  34.      System.out.println( time.toString() );  
  35.    }  
  36.  }

Output:



8.3 Controlling Access to Members 


  1. /**  
  2.  * Program Kelas MemberAccessTest  
  3.  * Anggota kelas private Time1 tidak dapat diakses  
  4.  * Nama file : MemberAccessTest.java  
  5.  */  
  6.    
  7. public class MemberAccessTest
  8. {
  9.    public static void main(String[] args)
  10.    {
  11.        Time1 time = new Time1();
  12.        
  13.        time.hour = 7; //error: has private access
  14.        time.minute = 15; //error: has private access
  15.        time.second = 30; //error: has private access
  16.    }
  17. }


8.4 Referring to the Current Object's Members with the this Reference 


  1. /**  
  2.  * Program Kelas ThisTest  
  3.  * Program ini digunakan secara implisit dan eksplisit untuk merujuk kepada anggota dari sebuah objek  
  4.  * Nama file : ThisTest.java  
  5.  */  
  6. public class ThisTest
  7. {
  8.     public static void main (String[] args)
  9.     {
  10.         SimpleTime time = new SimpleTime(15, 30, 19);
  11.         System.out.println(time.buildString());
  12.     }
  13. }
  14. class SimpleTime
  15. {
  16.     private int hour;
  17.     private int minute;
  18.     private int second;
  19.    
  20.     public SimpleTime(int hour, int minute, int second)
  21.     {
  22.         this.hour=hour;
  23.         this.minute=minute;
  24.         this.second=second;
  25.     }
  26.    
  27.     public String buildString()
  28.     {
  29.         return String.format("%24s: %s\n%24s: %s",
  30.             "this.toUniversalString()", this.toUniversalString(),
  31.             "toUniversalString()", toUniversalString());
  32.     }
  33.    
  34.     public String toUniversalString()
  35.     {
  36.         return String.format( "%02d:%02d:%02d",
  37.             this.hour, this.minute, this.second);
  38.     }
  39. }

Output:




8.5 Time2 class with overloaded constructors


  1. /**  
  2.   * Program Kelas Time2  
  3.   * Deklarasi kelas Time2 dengan konstuktor overload  
  4.   * Nama file : Time2.java  
  5.   */
  6. public class Time2
  7. {
  8.     private int hour;
  9.     private int minute;
  10.     private int second;
  11.    
  12.     public Time2()
  13.     {
  14.         this( 0, 0, 0 );
  15.     }
  16.    
  17.     public Time2( int h )
  18.     {
  19.         this( h, 0, 0 );
  20.     }
  21.    
  22.     public Time2( int h, int m )
  23.     {
  24.         this( h, m, 0 );
  25.     }
  26.    
  27.     public Time2( int h, int m, int s )
  28.     {
  29.         setTime( h, m, s );
  30.     }
  31.    
  32.     public Time2( Time2 time )
  33.     {
  34.         this( time.getHour(), time.getMinute(), time.getSecond() );
  35.     }
  36.    
  37.     public void setTime( int h, int m, int s )
  38.     {
  39.         setHour( h );
  40.         setMinute( m );
  41.         setSecond( s );
  42.     }
  43.     public void setHour( int h )
  44.     {
  45.         if ( h >= 0 && h < 24 )
  46.             hour = h;
  47.         else
  48.             throw new IllegalArgumentException( "hour must be 0-23" );
  49.     }
  50.     public void setMinute( int m )
  51.     {
  52.         if ( m >= 0 && m < 60 )
  53.             minute = m;
  54.         else
  55.             throw new IllegalArgumentException( "minute must be 0-59" );
  56.     }
  57.     public void setSecond( int s )
  58.     {
  59.         if ( s >= 0 && s < 60 )
  60.             second = ( ( s >= 0 && s < 60 ) ? s : 0 );
  61.         else
  62.             throw new IllegalArgumentException( "second must be 0-59" );
  63.     }
  64.     public int getHour()
  65.     {
  66.         return hour;
  67.     }
  68.     public int getMinute()
  69.     {
  70.         return minute;
  71.     }
  72.    
  73.     public int getSecond()
  74.     {
  75.         return second;
  76.     }
  77.     public String toUniversalString()
  78.     {
  79.         return String.format(
  80.             "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  81.     }
  82.     public String toString()
  83.     {
  84.         return String.format( "%d:%02d:%02d %s",
  85.             ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
  86.                 getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  87.     }
  88. }

8.6 Overloaded constructors used to initialize Time2 objects 


  1. /**  
  2.   * Program Kelas Time2Test  
  3.   * Konstruktor overload digunakan untuk menginisialisasi objek Time2  
  4.   * Nama file : Time2Test.java  
  5.   */  
  6. public class Time2Test
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         Time2 t1 = new Time2();
  11.         Time2 t2 = new Time2( 2 );
  12.         Time2 t3 = new Time2( 21, 34 );
  13.         Time2 t4 = new Time2( 12, 25, 42 );
  14.         Time2 t5 = new Time2( t4 );
  15.        
  16.         System.out.println( "Constructed with:" );
  17.         System.out.println( "t1: all arguments defaulted" );
  18.         System.out.printf( " %s\n", t1.toUniversalString() );
  19.         System.out.printf( " %s\n", t1.toString() );
  20.         System.out.println(
  21.             "t2: hour specified; minute and second defaulted" );
  22.         System.out.printf( " %s\n", t2.toUniversalString() );
  23.         System.out.printf( " %s\n", t2.toString() );
  24.         System.out.println(
  25.             "t3: hour and minute specified; second defaulted" );
  26.         System.out.printf( " %s\n", t3.toUniversalString() );
  27.         System.out.printf( " %s\n", t3.toString() );
  28.         System.out.println( "t4: hour, minute and second specified" );
  29.         System.out.printf( " %s\n", t4.toUniversalString() );
  30.         System.out.printf( " %s\n", t4.toString() );
  31.         System.out.println( "t5: Time2 object t4 specified" );
  32.         System.out.printf( " %s\n", t5.toUniversalString() );
  33.         System.out.printf( " %s\n", t5.toString() );
  34.         try
  35.         {
  36.             Time2 t6 = new Time2( 27, 74, 99 );
  37.         }
  38.         catch ( IllegalArgumentException e )
  39.         {
  40.             System.out.printf( "\nException while initializing t6: %s\n",
  41.             e.getMessage() );
  42.         }
  43.     }
  44.  }

Output:

Rangkuman Buku System Analysis and Design

BAB 3: Requirements Determination -           Fase Analisis Fase analisis menentukan garis besar tujuan bisnis untuk sistem, menentukan...