Advertisement
rafibatam

Design Pattern Strategy JAVA

Mar 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.77 KB | None | 0 0
  1. Strategi Pattern adalah sebuah design pattern yang memungkinkan Anda menentukan sekelompok algoritma, menempatkan masing-masing ke dalam kelas yang terpisah, dan membuat objek yang dapat dipertukarkan.
  2.  
  3. // RouteStrategy.java
  4. public interface RouteStrategy {
  5.     boolean pay(int paymentAmount);
  6.     void collectPaymentDetails();
  7. }
  8.  
  9. // PayByTransfer.java
  10. import java.io.InputStreamReader;
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13.  
  14. public class PayByTransfer implements RouteStrategy {
  15.     private final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));
  16.     private Transfer transfer;
  17.    
  18.     @Override
  19.     public boolean pay(int paymentAmount) {
  20.         if(pinIsInsert()) {
  21.             System.out.println("Paying " + paymentAmount + " using Transfer");
  22.             transfer.setAmount(transfer.getAmount() - paymentAmount);
  23.             return true;
  24.         }
  25.        
  26.         else {
  27.             return false;
  28.         }
  29.     }
  30.    
  31.     private boolean pinIsInsert() {
  32.         return transfer != null;
  33.     }
  34.    
  35.     @Override
  36.     public void collectPaymentDetails() {
  37.         try {
  38.             System.out.print("\nInsert PIN : ");
  39.             String pin = READER.readLine();
  40.             System.out.print("Code Booking : ");
  41.             String booking = READER.readLine();
  42.             System.out.print("Enter the number do you want to Transfer : ");
  43.             String number = READER.readLine();
  44.             transfer = new Transfer(pin, booking, number);
  45.         }
  46.        
  47.         catch(IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51. }
  52.  
  53. // Transfer.java
  54. public class Transfer {
  55.     private int amount;
  56.     private String pin;
  57.     private String number;
  58.     private String booking;
  59.    
  60.     Transfer(String pin, String number, String booking) {
  61.         this.amount = 1000000;
  62.         this.pin = pin;
  63.         this.number = number;
  64.         this.booking = booking;
  65.     }
  66.    
  67.     public void setAmount(int amount) {
  68.         this.amount = amount;
  69.     }
  70.    
  71.     public int getAmount() {
  72.         return amount;
  73.     }
  74. }
  75.  
  76. // PayByMarket.java
  77. import java.io.BufferedReader;
  78. import java.io.InputStreamReader;
  79. import java.io.IOException;
  80.  
  81. public class PayByMarket implements RouteStrategy {
  82.     private final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));
  83.     private Market market;
  84.    
  85.     @Override
  86.     public boolean pay(int paymentAmount) {
  87.         if(Payed()) {
  88.             System.out.println("Paying " + paymentAmount + " at Alfamart/Indomaret");
  89.             market.setAmount(market.getAmount() - paymentAmount);
  90.             return true;
  91.         }
  92.        
  93.         else {
  94.             return false;
  95.         }
  96.     }
  97.  
  98.     private boolean Payed() {
  99.         return market != null;
  100.     }
  101.  
  102.     @Override
  103.     public void collectPaymentDetails() {
  104.         try {
  105.             System.out.print("\nEnter Code Booking : ");
  106.             String booking = READER.readLine();
  107.             market = new Market(booking);
  108.         }
  109.        
  110.         catch(IOException e) {
  111.             e.printStackTrace();
  112.         }
  113.     }
  114. }
  115.  
  116. // Market.java
  117. public class Market {
  118.     private int amount;
  119.     private String booking;
  120.    
  121.     Market(String booking) {
  122.         this.booking = booking;
  123.         this.amount = 1000000;
  124.     }
  125.  
  126.     public int getAmount() {
  127.         return amount;
  128.     }
  129.  
  130.     public void setAmount(int amount) {
  131.         this.amount = amount;
  132.     }
  133. }
  134.  
  135. // Order.java
  136. public class Order {
  137.     private int totalCost = 0;
  138.     private boolean isClosed = false;
  139.    
  140.     public void processOrder(RouteStrategy route) {
  141.         route.collectPaymentDetails();
  142.     }
  143.    
  144.     public void setTotalCost(int cost) {
  145.         this.totalCost += cost;
  146.     }
  147.    
  148.     public int getTotalCost() {
  149.         return totalCost;
  150.     }
  151.    
  152.     public boolean isClosed() {
  153.         return isClosed;
  154.     }
  155.    
  156.     public void setClosed() {
  157.         isClosed = true;
  158.     }
  159. }
  160.  
  161. // Progress.java
  162. import java.io.BufferedReader;
  163. import java.io.InputStreamReader;
  164. import java.io.IOException;
  165. import java.util.HashMap;
  166. import java.util.Map;
  167.  
  168. public class Progress {
  169.     private static Map<Integer, Integer> priceOnTickets = new HashMap<>();
  170.     private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  171.     private static Order order = new Order();
  172.     private static RouteStrategy route;
  173.  
  174.     static {
  175.         priceOnTickets.put(1, 96000);
  176.         priceOnTickets.put(2, 74000);
  177.         priceOnTickets.put(3, 350000);
  178.     }
  179.  
  180.     public static void main(String[] args) throws IOException {
  181.         while(!order.isClosed()) {
  182.             int cost, count;
  183.  
  184.             String continueChoice;
  185.             do {
  186.                 System.out.println("\t\tTRANSPORTATION's TICKET FROM JAKARTA TO YOGYAKARTA");
  187.                 System.out.print("\n\nPlease Choose Transportation : \n"
  188.                         + "1. Bus (96.000 IDR)\n"
  189.                         + "2. Train (74.000 IDR)\n"
  190.                         + "3. Plane (350.000 IDR)\n"
  191.                         + "Choose one : ");
  192.                 int choice = Integer.parseInt(reader.readLine());
  193.                 cost = priceOnTickets.get(choice);
  194.                 System.out.print("Count : ");
  195.                 count = Integer.parseInt(reader.readLine());
  196.                 order.setTotalCost(cost * count);
  197.                 System.out.print("Do you want to pay, now? Yes/No : ");
  198.                 continueChoice = reader.readLine();
  199.             }
  200.  
  201.             while(continueChoice.equalsIgnoreCase("No"));
  202.  
  203.             if(route == null) {
  204.                 System.out.print("\nPlease select a Payment Method :\n"
  205.                         + "1. Transfer\n"
  206.                         + "2. Paying at Market\n"
  207.                         + "Choose one : ");
  208.                 String paymentMethod = reader.readLine();
  209.  
  210.                 if(paymentMethod.equals("1")) {
  211.                     route = new PayByTransfer();
  212.                 }
  213.  
  214.                 else {
  215.                     route = new PayByMarket();
  216.                 }
  217.  
  218.                 order.processOrder(route);
  219.  
  220.                 System.out.print("\nPay " + order.getTotalCost() + " tickets or Continue shopping? Pay/No : ");
  221.                 String proceed = reader.readLine();
  222.                 if(proceed.equalsIgnoreCase("Pay")) {
  223.                     if(route.pay(order.getTotalCost())) {
  224.                         System.out.println("Payment has been successful");
  225.                     }
  226.  
  227.                     else {
  228.                         System.out.println("Error! Please check your data");
  229.                     }
  230.  
  231.                     order.setClosed();
  232.                 }
  233.  
  234.                 System.out.print("\nDo you want to print the Ticket? Yes/No : ");
  235.                 String printTicket = reader.readLine();
  236.                 if(printTicket.equalsIgnoreCase("Yes")) {
  237.                     System.out.print("Choose your transportation? Bus/Train/Plane : ");
  238.                     String transport = reader.readLine();
  239.                     if(transport.equalsIgnoreCase("Bus")) {
  240.                         System.out.println("Bus From Jakarta to Yogyakarta"
  241.                                 + "\nRoute : Cikampek, Cipali, Palimanan, Kanci, Pejagan, Purwokerto, Kebumen, Wates, Yogyakarta"
  242.                                 + "\nPrice : " + cost * count);
  243.                     }
  244.                    
  245.                     else if(transport.equalsIgnoreCase("Train")) {
  246.                         System.out.println("Train from Jakarta to Yogyakarta"
  247.                                 + "\nRoute : Senen, Haurgeulis, Prujakan, Prupuk, Purwokerto, Kroya, Gombong, Kebumen, Kutoarjo, Wates, Yogyakarta"
  248.                                 + "\nPrice : " + cost * count);
  249.                     }
  250.                    
  251.                     else if(transport.equalsIgnoreCase("Plane")) {
  252.                         System.out.println("Plane from Jakarta to Yogyakarta"
  253.                                 + "\nRoute : Soekarno-Hatta, Adisutjipto"
  254.                                 + "\nPrice : " + cost * count);
  255.                     }
  256.                    
  257.                     else {
  258.                         System.out.println("FAIL! Please choose one your transportation!");
  259.                     }
  260.                 }
  261.                
  262.                 else {
  263.                     System.out.println("Thank you for buying a ticket at here");
  264.                 }
  265.             }
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement