import java.util.ArrayList; | |||||
public class Application { | |||||
public static void main( String[] args ) { | |||||
Application app = new Application(); | |||||
app.init(); | |||||
app.run(); | |||||
app.done(); | |||||
} | |||||
public void init() { | |||||
System.out.println( "initializing application" ); | |||||
movies.add( new Movie("Regular_Movie_1 ", Movie.REGULAR ) ); | |||||
movies.add( new Movie("Regular_Movie_2 ", Movie.REGULAR ) ); | |||||
movies.add( new Movie("Children_Movie_1 ", Movie.CHILDRENS ) ); | |||||
movies.add( new Movie("Children_Movie_2 ", Movie.CHILDRENS ) ); | |||||
movies.add( new Movie("NewRelease_Movie_1", Movie.NEW_RELEASE ) ); | |||||
movies.add( new Movie("NewRelease_Movie_2", Movie.NEW_RELEASE ) ); | |||||
customers.add( new Customer( "Customer_A" ) ); | |||||
customers.add( new Customer( "Customer_B" ) ); | |||||
customers.add( new Customer( "Customer_C" ) ); | |||||
} | |||||
public void run() { | |||||
System.out.println( "application is running" ); | |||||
Customer customer_1 = customers.get( 0 ); | |||||
customer_1.addRental( new Rental( movies.get( 0 ), 2 ) ); | |||||
customer_1.addRental( new Rental( movies.get( 1 ), 3 ) ); | |||||
customer_1.addRental( new Rental( movies.get( 2 ), 3 ) ); | |||||
customer_1.addRental( new Rental( movies.get( 3 ), 4 ) ); | |||||
customer_1.addRental( new Rental( movies.get( 4 ), 3 ) ); | |||||
customer_1.addRental( new Rental( movies.get( 5 ), 4 ) ); | |||||
String statement = customer_1.statement(); | |||||
System.out.println( statement ); | |||||
} | |||||
public void done() { | |||||
System.out.println( "application finished" ); | |||||
} | |||||
private final ArrayList<Movie> movies = new ArrayList<>(); | |||||
private final ArrayList<Customer> customers = new ArrayList<>(); | |||||
} |
import java.util.ArrayList; | |||||
public class Customer { | |||||
private final String name; | |||||
private final ArrayList<Rental> rentals = new ArrayList<>(); | |||||
public Customer( String name ) { | |||||
this.name = name; | |||||
} | |||||
public void addRental( Rental arg ) { | |||||
rentals.add( arg ); | |||||
} | |||||
public String getName() { | |||||
return name; | |||||
} | |||||
public String statement() { | |||||
double totalAmount = 0; | |||||
int frequentRenterPoints = 0; | |||||
String result = "Rental Record for " + getName() + "\n"; | |||||
for( Rental curRental : rentals ) { | |||||
double thisAmount = 0; | |||||
// determine amounts for each line | |||||
switch( curRental.getMovie().getPriceCode() ) { | |||||
case Movie.REGULAR: | |||||
thisAmount += 2; | |||||
if( curRental.getDaysRented() > 2 ) | |||||
thisAmount += ( curRental.getDaysRented() - 2 ) * 1.5; | |||||
break; | |||||
case Movie.NEW_RELEASE: | |||||
thisAmount += curRental.getDaysRented() * 3; | |||||
break; | |||||
case Movie.CHILDRENS: | |||||
thisAmount += 1.5; | |||||
if( curRental.getDaysRented() > 3 ) | |||||
thisAmount += ( curRental.getDaysRented() - 3 ) * 1.5; | |||||
break; | |||||
} | |||||
totalAmount += thisAmount; | |||||
// add frequent renter points | |||||
frequentRenterPoints++; | |||||
// add bonus for a two day new release rental | |||||
if( ( curRental.getMovie().getPriceCode() == Movie.NEW_RELEASE ) && curRental.getDaysRented() > 1 ) | |||||
frequentRenterPoints++; | |||||
// show figures for this rental | |||||
result += "\t" + curRental.getMovie().getTitle() + "\t days rented: " + curRental.getDaysRented() + " = " + String.valueOf( thisAmount ) + "\n"; | |||||
} | |||||
// add footer lines | |||||
result += "Amount owed is " + String.valueOf( totalAmount ) + "\n"; | |||||
result += "You earned " + String.valueOf( frequentRenterPoints ) + " frequent renter points"; | |||||
return result; | |||||
} | |||||
} |
public class Movie { | |||||
public static final int CHILDRENS = 2; | |||||
public static final int REGULAR = 0; | |||||
public static final int NEW_RELEASE = 1; | |||||
private final String title; | |||||
private int priceCode; | |||||
public Movie( String title, int priceCode ) { | |||||
this.title = title; | |||||
this.priceCode = priceCode; | |||||
} | |||||
public String getTitle() { | |||||
return title; | |||||
} | |||||
public int getPriceCode() { | |||||
return priceCode; | |||||
} | |||||
public void setPriceCode( int priceCode ) { | |||||
this.priceCode = priceCode; | |||||
} | |||||
} |
public class Rental { | |||||
private final Movie movie; | |||||
private final int daysRented; | |||||
public Rental( Movie movie, int daysRented ) { | |||||
this.movie = movie; | |||||
this.daysRented = daysRented; | |||||
} | |||||
public Movie getMovie() { | |||||
return movie; | |||||
} | |||||
public int getDaysRented() { | |||||
return daysRented; | |||||
} | |||||
} |