Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.digite.kata.refactoring;

public interface BillingInterface {

public double getRentAmount(int rentDays);
public int getEarnedPoints(int rentDays);

}
20 changes: 20 additions & 0 deletions src/main/java/com/digite/kata/refactoring/Children.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.digite.kata.refactoring;

public class Children implements BillingInterface{

public double getRentAmount(int rentDays)
{
double amount = 1.5;

if (rentDays > 3)
amount += (rentDays - 3) * 1.5;

return amount;

}

public int getEarnedPoints(int rentDays) {
int count = 0;
return count++;
}
}
90 changes: 26 additions & 64 deletions src/main/java/com/digite/kata/refactoring/Customer.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,27 @@
package com.digite.kata.refactoring;

import java.util.Enumeration;
import java.util.Vector;

class Customer {
private String _name;
private Vector<Rental> _rentals = new Vector();

public Customer(String name) {
_name = name;
}

public void addRental(Rental arg) {
_rentals.addElement(arg);
}

public String getName() {
return _name;
}

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;
}
package com.digite.kata.refactoring;

import java.util.Enumeration;
import java.util.Vector;

class Customer {
private String _name;
private Vector<Rental> _rentals = new Vector();

public Customer(String name) {
_name = name;
}

public void addRental(Rental arg) {
_rentals.addElement(arg);
}

public Vector<Rental> getRentals()
{
return _rentals;
}

public String getName() {
return _name;
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/digite/kata/refactoring/Invoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.digite.kata.refactoring;

import java.util.Enumeration;
import java.util.Vector;

public class Invoice {

public static String getInvoice(String w_custName, Vector<Rental> _rentals) {
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentals = _rentals.elements();
String result = "Rental Record for " + w_custName + "\n";
while (rentals.hasMoreElements())
{
double thisAmount = 0;
Rental each = (Rental) rentals.nextElement();
//determine amounts for each line

thisAmount = each.getMovieType().getRentAmount(each.getDaysRented());
frequentRenterPoints += each.getMovieType().getEarnedPoints(each.getDaysRented());

result += each.getMovie().getTitle()+ " " +
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;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/digite/kata/refactoring/JTestMovie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.digite.kata.refactoring;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class JTestMovie {

@Test
public void testForSameType()
{
Movie obj = new Movie("Movie1", Movie.REGULAR);
Movie obj1 = new Movie("Movie2", Movie.REGULAR);
BillingInterface regular = new Regular();

Rental rental = new Rental(obj, 10, regular);
Rental rental1 = new Rental(obj1, 20, regular);

Customer cust = new Customer("Pravin");
cust.addRental(rental);
cust.addRental(rental1);

String finalResult = "Rental Record for Pravin\nMovie1 14.0\nMovie2 29.0\nAmount owed is 43.0\nYou earned 2 frequent renter points";

String w_bill = Invoice.getInvoice(cust.getName(), cust.getRentals());
assertEquals(finalResult, w_bill);
System.out.println(w_bill);

}

@Test
public void testForDifferntType()
{
Movie obj = new Movie("Movie1", Movie.REGULAR);
Movie obj1 = new Movie("Movie2", Movie.NEW_RELEASE);
Movie obj2 = new Movie("Movie3", Movie.CHILDRENS);
BillingInterface regular = new Regular();
BillingInterface newRelease = new NewRelease();
BillingInterface children = new Children();

Rental rental = new Rental(obj, 10, regular);
Rental rental1 = new Rental(obj1, 20, newRelease);
Rental rental3 = new Rental(obj2, 30, children);

Customer cust = new Customer("Pravin");
cust.addRental(rental);
cust.addRental(rental1);
cust.addRental(rental3);

String finalResult = "Rental Record for Pravin\nMovie1 14.0\nMovie2 60.0\nMovie3 42.0\nAmount owed is 116.0\nYou earned 3 frequent renter points";

String w_bill = Invoice.getInvoice(cust.getName(), cust.getRentals());
assertEquals(finalResult, w_bill);
System.out.println(w_bill);

}

}
53 changes: 28 additions & 25 deletions src/main/java/com/digite/kata/refactoring/Movie.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package com.digite.kata.refactoring;

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 String getTitle() {
return _title;
}

public int getPriceCode() {
return _priceCode;
}

public void setPriceCode(int priceCode) {
_priceCode = priceCode;
}
package com.digite.kata.refactoring;

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 String getTitle() {
return _title;
}

public int getPriceCode() {
return _priceCode;
}

public void setPriceCode(int priceCode) {
_priceCode = priceCode;
}


}
24 changes: 24 additions & 0 deletions src/main/java/com/digite/kata/refactoring/NewRelease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.digite.kata.refactoring;

public class NewRelease implements BillingInterface {
public double getRentAmount(int rentDays)
{
double amount = 0;

amount += rentDays * 3;

return amount;

}

public int getEarnedPoints(int rentDays) {
int count = 0;
count++;

if (rentDays > 1)
count++;

return count;
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/digite/kata/refactoring/Regular.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.digite.kata.refactoring;


public class Regular implements BillingInterface{

public double getRentAmount(int rentDays)
{
double amount = 2;

if (rentDays > 2)
amount += (rentDays - 2) * 1.5;

return amount;

}

public int getEarnedPoints(int rentDays) {
int count = 0;
return ++count;
}
}
48 changes: 28 additions & 20 deletions src/main/java/com/digite/kata/refactoring/Rental.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.digite.kata.refactoring;

public class Rental {

private Movie _movie;
private int _daysRented;

public Rental(Movie movie, int daysRented) {
_movie = movie;
_daysRented = daysRented;
}

public Movie getMovie() {
return _movie;
}

public int getDaysRented() {
return _daysRented;
}
}
package com.digite.kata.refactoring;

public class Rental {

private Movie _movie;
private int _daysRented;
private BillingInterface _obj;

public Rental(Movie movie, int daysRented, BillingInterface obj) {
_movie = movie;
_daysRented = daysRented;
_obj = obj;
}

public Movie getMovie() {
return _movie;
}

public int getDaysRented() {
return _daysRented;
}

public BillingInterface getMovieType()
{
return _obj;
}

}