jeudi 6 août 2020

How to implement strategy design pattern

Can anyone tell how can we implement Strategy design pattern in the following code. I have gone through various links but not fully clear about implementing it. Question is: Calculate points and display the report in PDF or HTML using Strategy Design Pattern. Below is the code already available. Customer, Movie and Rental are classes already created:

  1. Customer.java

     public class Customer {
     private String _name;
     private Vector _rentals = new Vector();
     public Customer(String name) {
     _name = name;
     };
    
     public void addRental(Rental arg) {
     _rentals.addElement(arg);
     }
    
     public String getName() {
     return _name;
     }
    
    public static void main(String[] args) {
    
     Customer c = new Customer("ABC");
     Movie m = new Movie("Title", 1);
     Rental r = new Rental(m, 10);
     c.addRental(r);
     // Rental calculation
     String s = c.statement();
     System.out.println("s: " + s);
    }
         public String statement() {
         double totalAmount = 0;
     int frequentRenterPoints = 0;
     Enumeration rentals = _rentals.elements();
     String result = "Rental Record for " + getName() + "\n";
    
     while (rentals.hasMoreElements()) {
         double thisAmount = 0;
         Rental each = (Rental) rentals.nextElement();
    
         // determine amounts for each line
         switch (each.getMovie().getPriceCode()) {
    
         case Movie.REGULAR:
             thisAmount += 2;
             if (each.getDaysRented() > 2)
                 thisAmount += (each.getDaysRented() - 2) * 1.5;
             break;
    
         case Movie.NEW_RELEASE:
             thisAmount += each.getDaysRented() * 3;
             break;
    
         case Movie.CHILDRENS:
             thisAmount += 1.5;
             if (each.getDaysRented() > 3)
                 thisAmount += (each.getDaysRented() - 3) * 1.5;
             break;
         } // add frequent
             // renter points
         frequentRenterPoints++;
    
         // add bonus for a two day new release rental
         if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1)
             frequentRenterPoints++;
    
         // show figures for this rental
         result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
         totalAmount += thisAmount;
     }
    
     // add footer lines
     result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
     result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
     return result;
     }
     }
    
  2. Movie.java

    public class Movie {
    public static final int CHILDRENS = 2;
    public static final int REGULAR = 0;
    public static final int NEW_RELEASE = 1;
    private String _title;
    private int _priceCode;
    
     public Movie(String title, int priceCode) {
    _title = title;
    _priceCode = priceCode;
    }
    public int getPriceCode() {
        return _priceCode;
    }
    
    public void setPriceCode(int arg) {
    _priceCode = arg;
    }
    public String getTitle() {
    return _title;
    };
    }
    
  3. Rental.java

    public class Rental {
        private Movie _movie;
    private int _daysRented;
    public Rental(Movie movie, int daysRented) {
        _movie = movie;
    _daysRented = daysRented;
    }
    public int getDaysRented() {
        return _daysRented;
    }
        public Movie getMovie() {
        return _movie;
    }
    }
    

Aucun commentaire:

Enregistrer un commentaire