Advertisement
rafibatam

Design Pattern Facade JAVA

Apr 1st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. Facade Pattern digunakan ketika segmen dari client membutuhkan interface yang sederhana untuk menjalankan semua fungsi dari subsistem yang kompleks.
  2.  
  3. // SportCarShop.java
  4. import java.io.IOException;
  5.  
  6. public interface SportCarShop {
  7.     void model() throws NumberFormatException, IOException;
  8.     void productionYear() throws NumberFormatException, IOException;
  9.     void price() throws NumberFormatException, IOException;
  10. }
  11.  
  12. // Lamborghini.java
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16.  
  17. public class Lamborghini implements SportCarShop {
  18.     BufferedReader select = new BufferedReader(new InputStreamReader(System.in));
  19.     private static int choose;
  20.    
  21.     @Override
  22.     public void model() throws NumberFormatException, IOException {
  23.         System.out.println("\nChoose model do you want :");
  24.         System.out.print("1. Lamborghini Urus"
  25.                 + "\n2. Lamborghini Centenario"
  26.                 + "\n3. Lamborghini Aventador S Roadster"
  27.                 + "\nChoose one : ");
  28.         choose = Integer.parseInt(select.readLine());
  29.        
  30.         switch(choose) {
  31.         case 1 :
  32.             {
  33.                 System.out.print("You choose Lamborghini Urus");
  34.             }
  35.             break;
  36.            
  37.         case 2 :
  38.             {
  39.                 System.out.print("You choose Lamborghini Centenario");
  40.             }
  41.             break;
  42.            
  43.         case 3 :
  44.             {
  45.                 System.out.print("You choose Lamborghini Aventador S Roadster");
  46.             }
  47.             break;
  48.            
  49.             default :
  50.             {
  51.                 System.out.print("\nSorry, this item is not found.");
  52.             }
  53.            
  54.             return;
  55.         }
  56.     }
  57.  
  58.     @Override
  59.     public void productionYear() throws NumberFormatException, IOException {
  60.         System.out.println("\n\nChoose production year do you want : ");
  61.         System.out.print("1. 2015"
  62.                 + "\n2. 2016"
  63.                 + "\n3. 2017"
  64.                 + "\n4. 2018"
  65.                 + "\n5. 2019"
  66.                 + "\nChoose one : ");
  67.         choose = Integer.parseInt(select.readLine());
  68.        
  69.         switch(choose) {
  70.         case 1 :
  71.             {
  72.                 System.out.print("You choose 2015");
  73.             }
  74.             break;
  75.            
  76.         case 2 :
  77.             {
  78.                 System.out.print("You choose 2016");
  79.             }
  80.             break;
  81.            
  82.         case 3 :
  83.             {
  84.                 System.out.print("You choose 2017");
  85.             }
  86.             break;
  87.            
  88.         case 4 :
  89.             {
  90.                 System.out.print("You choose 2018");
  91.             }
  92.             break;
  93.            
  94.         case 5 :
  95.             {
  96.                 System.out.print("You choose 2019");
  97.             }
  98.             break;
  99.            
  100.         default :
  101.             {
  102.                 System.out.print("\nSorry, production year is not found.");
  103.             }
  104.            
  105.             return;
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public void price() throws NumberFormatException, IOException {
  111.         System.out.print("\n\nWhat number the type of car you want to buy : ");
  112.         choose = Integer.parseInt(select.readLine());
  113.        
  114.         switch(choose) {
  115.         case 1 :
  116.             {
  117.                 System.out.print("Rp3.000.000.000\n\n");
  118.             }
  119.             break;
  120.            
  121.         case 2 :
  122.             {
  123.                 System.out.print("Rp25.000.000.000\n\n");  
  124.             }
  125.             break;
  126.            
  127.         case 3 :
  128.             {
  129.                 System.out.print("Rp5.000.000.000\n\n");
  130.             }
  131.             break;
  132.            
  133.         default :
  134.             {
  135.                 System.out.print("\nSorry, this item is not found.\n\n");
  136.             }
  137.            
  138.             return;
  139.         }
  140.     }
  141. }
  142.  
  143. // MercedesBenz.java
  144. import java.io.BufferedReader;
  145. import java.io.IOException;
  146. import java.io.InputStreamReader;
  147.  
  148. public class MercedesBenz implements SportCarShop {
  149.     BufferedReader select = new BufferedReader(new InputStreamReader(System.in));
  150.     private static int choose;
  151.    
  152.     @Override
  153.     public void model() throws NumberFormatException, IOException {
  154.         System.out.println("\nChoose model do you want :");
  155.         System.out.print("1. Mercedes Benz SL 63 AMG"
  156.                 + "\n2. Mercedes Benz AMG GT R"
  157.                 + "\n3. Mercedes Benz Maybech S 560"
  158.                 + "\nChoose one : ");
  159.         choose = Integer.parseInt(select.readLine());
  160.        
  161.         switch(choose) {
  162.         case 1 :
  163.             {
  164.                 System.out.print("You choose Mercedes Benz SL 63 AMG");
  165.             }
  166.             break;
  167.            
  168.         case 2 :
  169.             {
  170.                 System.out.print("You choose Mercedes Benz AMG GT R");
  171.             }
  172.             break;
  173.            
  174.         case 3 :
  175.             {
  176.                 System.out.print("You choose Mercedes Benz Maybech S 560");
  177.             }
  178.             break;
  179.            
  180.             default :
  181.             {
  182.                 System.out.print("\nSorry, this item is not found.");
  183.             }
  184.            
  185.             return;
  186.         }
  187.     }
  188.  
  189.     @Override
  190.     public void productionYear() throws NumberFormatException, IOException {
  191.         System.out.println("\n\nChoose production year do you want : ");
  192.         System.out.print("1. 2015"
  193.                 + "\n2. 2016"
  194.                 + "\n3. 2017"
  195.                 + "\n4. 2018"
  196.                 + "\n5. 2019"
  197.                 + "\nChoose one : ");
  198.         choose = Integer.parseInt(select.readLine());
  199.        
  200.         switch(choose) {
  201.         case 1 :
  202.             {
  203.                 System.out.print("You choose 2015");
  204.             }
  205.             break;
  206.            
  207.         case 2 :
  208.             {
  209.                 System.out.print("You choose 2016");
  210.             }
  211.             break;
  212.            
  213.         case 3 :
  214.             {
  215.                 System.out.print("You choose 2017");
  216.             }
  217.             break;
  218.            
  219.         case 4 :
  220.             {
  221.                 System.out.print("You choose 2018");
  222.             }
  223.             break;
  224.            
  225.         case 5 :
  226.             {
  227.                 System.out.print("You choose 2019");
  228.             }
  229.             break;
  230.            
  231.         default :
  232.             {
  233.                 System.out.print("\nSorry, production year is not found.");
  234.             }
  235.            
  236.             return;
  237.         }
  238.     }
  239.  
  240.     @Override
  241.     public void price() throws NumberFormatException, IOException {
  242.         System.out.print("\n\nWhat the type of car you want to buy : ");
  243.         choose = Integer.parseInt(select.readLine());
  244.        
  245.         switch(choose) {
  246.         case 1 :
  247.             {
  248.                 System.out.print("Rp3.400.000.000\n\n");
  249.             }
  250.             break;
  251.            
  252.         case 2 :
  253.             {
  254.                 System.out.print("Rp5.560.000.000\n\n");   
  255.             }
  256.             break;
  257.            
  258.         case 3 :
  259.             {
  260.                 System.out.print("Rp7.380.000.000\n\n");
  261.             }
  262.             break;
  263.            
  264.         default :
  265.             {
  266.                 System.out.print("\nSorry, this item is not found.\n\n");
  267.             }
  268.            
  269.             return;
  270.         }
  271.     }
  272. }
  273.  
  274. // Pay.java
  275. import java.io.IOException;
  276.  
  277. public class Pay {
  278.     private SportCarShop Lamborghini;
  279.     private SportCarShop MercedesBenz;
  280.    
  281.     public Pay() {
  282.         Lamborghini = new Lamborghini();
  283.         MercedesBenz = new MercedesBenz();
  284.     }
  285.    
  286.     public void LamborghiniSale() throws NumberFormatException, IOException {
  287.         Lamborghini.model();
  288.         Lamborghini.productionYear();
  289.         Lamborghini.price();
  290.     }
  291.    
  292.     public void MercedesBenzSale() throws NumberFormatException, IOException {
  293.         MercedesBenz.model();
  294.         MercedesBenz.productionYear();
  295.         MercedesBenz.price();
  296.     }
  297. }
  298.  
  299. // Progress.java
  300. import java.io.BufferedReader;
  301. import java.io.IOException;
  302. import java.io.InputStreamReader;
  303.  
  304. public class Progress {
  305.     private static int choice;
  306.     public static void main(String[] args) throws NumberFormatException, IOException {
  307.         do {
  308.             System.out.println("\t\tWELCOME TO SPORT CAR SHOP");
  309.             System.out.print("\n1. Lamborghini"
  310.                     + "\n2. Mercedes Benz"
  311.                     + "\nEnter Your Choice : ");
  312.  
  313.             BufferedReader selection = new BufferedReader(new InputStreamReader(System.in));
  314.             choice = Integer.parseInt(selection.readLine());
  315.             Pay car = new Pay();
  316.  
  317.             switch(choice) {
  318.             case 1 :
  319.                 {
  320.                     car.LamborghiniSale();
  321.                 }
  322.                 break;
  323.  
  324.             case 2 :
  325.                 {
  326.                     car.MercedesBenzSale();
  327.                 }
  328.  
  329.                 break;
  330.                
  331.             default :
  332.                 {
  333.                     System.out.println("Nothing you purchased");
  334.                 }
  335.            
  336.                 return;
  337.             }
  338.         }
  339.        
  340.         while(choice != 4);
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement