Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- // RouteStrategy.java
- public interface RouteStrategy {
- boolean pay(int paymentAmount);
- void collectPaymentDetails();
- }
- // PayByTransfer.java
- import java.io.InputStreamReader;
- import java.io.BufferedReader;
- import java.io.IOException;
- public class PayByTransfer implements RouteStrategy {
- private final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));
- private Transfer transfer;
- @Override
- public boolean pay(int paymentAmount) {
- if(pinIsInsert()) {
- System.out.println("Paying " + paymentAmount + " using Transfer");
- transfer.setAmount(transfer.getAmount() - paymentAmount);
- return true;
- }
- else {
- return false;
- }
- }
- private boolean pinIsInsert() {
- return transfer != null;
- }
- @Override
- public void collectPaymentDetails() {
- try {
- System.out.print("\nInsert PIN : ");
- String pin = READER.readLine();
- System.out.print("Code Booking : ");
- String booking = READER.readLine();
- System.out.print("Enter the number do you want to Transfer : ");
- String number = READER.readLine();
- transfer = new Transfer(pin, booking, number);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
- // Transfer.java
- public class Transfer {
- private int amount;
- private String pin;
- private String number;
- private String booking;
- Transfer(String pin, String number, String booking) {
- this.amount = 1000000;
- this.pin = pin;
- this.number = number;
- this.booking = booking;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
- public int getAmount() {
- return amount;
- }
- }
- // PayByMarket.java
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- public class PayByMarket implements RouteStrategy {
- private final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));
- private Market market;
- @Override
- public boolean pay(int paymentAmount) {
- if(Payed()) {
- System.out.println("Paying " + paymentAmount + " at Alfamart/Indomaret");
- market.setAmount(market.getAmount() - paymentAmount);
- return true;
- }
- else {
- return false;
- }
- }
- private boolean Payed() {
- return market != null;
- }
- @Override
- public void collectPaymentDetails() {
- try {
- System.out.print("\nEnter Code Booking : ");
- String booking = READER.readLine();
- market = new Market(booking);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
- // Market.java
- public class Market {
- private int amount;
- private String booking;
- Market(String booking) {
- this.booking = booking;
- this.amount = 1000000;
- }
- public int getAmount() {
- return amount;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
- }
- // Order.java
- public class Order {
- private int totalCost = 0;
- private boolean isClosed = false;
- public void processOrder(RouteStrategy route) {
- route.collectPaymentDetails();
- }
- public void setTotalCost(int cost) {
- this.totalCost += cost;
- }
- public int getTotalCost() {
- return totalCost;
- }
- public boolean isClosed() {
- return isClosed;
- }
- public void setClosed() {
- isClosed = true;
- }
- }
- // Progress.java
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- public class Progress {
- private static Map<Integer, Integer> priceOnTickets = new HashMap<>();
- private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- private static Order order = new Order();
- private static RouteStrategy route;
- static {
- priceOnTickets.put(1, 96000);
- priceOnTickets.put(2, 74000);
- priceOnTickets.put(3, 350000);
- }
- public static void main(String[] args) throws IOException {
- while(!order.isClosed()) {
- int cost, count;
- String continueChoice;
- do {
- System.out.println("\t\tTRANSPORTATION's TICKET FROM JAKARTA TO YOGYAKARTA");
- System.out.print("\n\nPlease Choose Transportation : \n"
- + "1. Bus (96.000 IDR)\n"
- + "2. Train (74.000 IDR)\n"
- + "3. Plane (350.000 IDR)\n"
- + "Choose one : ");
- int choice = Integer.parseInt(reader.readLine());
- cost = priceOnTickets.get(choice);
- System.out.print("Count : ");
- count = Integer.parseInt(reader.readLine());
- order.setTotalCost(cost * count);
- System.out.print("Do you want to pay, now? Yes/No : ");
- continueChoice = reader.readLine();
- }
- while(continueChoice.equalsIgnoreCase("No"));
- if(route == null) {
- System.out.print("\nPlease select a Payment Method :\n"
- + "1. Transfer\n"
- + "2. Paying at Market\n"
- + "Choose one : ");
- String paymentMethod = reader.readLine();
- if(paymentMethod.equals("1")) {
- route = new PayByTransfer();
- }
- else {
- route = new PayByMarket();
- }
- order.processOrder(route);
- System.out.print("\nPay " + order.getTotalCost() + " tickets or Continue shopping? Pay/No : ");
- String proceed = reader.readLine();
- if(proceed.equalsIgnoreCase("Pay")) {
- if(route.pay(order.getTotalCost())) {
- System.out.println("Payment has been successful");
- }
- else {
- System.out.println("Error! Please check your data");
- }
- order.setClosed();
- }
- System.out.print("\nDo you want to print the Ticket? Yes/No : ");
- String printTicket = reader.readLine();
- if(printTicket.equalsIgnoreCase("Yes")) {
- System.out.print("Choose your transportation? Bus/Train/Plane : ");
- String transport = reader.readLine();
- if(transport.equalsIgnoreCase("Bus")) {
- System.out.println("Bus From Jakarta to Yogyakarta"
- + "\nRoute : Cikampek, Cipali, Palimanan, Kanci, Pejagan, Purwokerto, Kebumen, Wates, Yogyakarta"
- + "\nPrice : " + cost * count);
- }
- else if(transport.equalsIgnoreCase("Train")) {
- System.out.println("Train from Jakarta to Yogyakarta"
- + "\nRoute : Senen, Haurgeulis, Prujakan, Prupuk, Purwokerto, Kroya, Gombong, Kebumen, Kutoarjo, Wates, Yogyakarta"
- + "\nPrice : " + cost * count);
- }
- else if(transport.equalsIgnoreCase("Plane")) {
- System.out.println("Plane from Jakarta to Yogyakarta"
- + "\nRoute : Soekarno-Hatta, Adisutjipto"
- + "\nPrice : " + cost * count);
- }
- else {
- System.out.println("FAIL! Please choose one your transportation!");
- }
- }
- else {
- System.out.println("Thank you for buying a ticket at here");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement