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

public interface AmountInterface
{
public int getPriceCode();
public double getAmount(int a_rentalDays);
}
22 changes: 22 additions & 0 deletions src/main/java/com/digite/kata/refactoring/ChildrenMovie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.digite.kata.refactoring;

public class ChildrenMovie implements AmountInterface {
@Override
public int getPriceCode() {
return Movie.CHILDRENS;
}

@Override
public double getAmount(int a_rentalDays)
{
/*
* thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount +=
* (each.getDaysRented() - 3) * 1.5;
*/
double w_rentAmount = 1.5;
if (a_rentalDays > 3)
w_rentAmount += (a_rentalDays - 3) * 1.5;
return w_rentAmount;
}

}
128 changes: 64 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,65 @@
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 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;
}
}
37 changes: 37 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,37 @@
package com.digite.kata.refactoring;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Test;

public class JTestMovie
{
@Test
public void testRegularMovie()
{
Movie w_movieObj = new Movie("Movie1", 0);
w_movieObj.setPriceCode(4);
AmountInterface w_regMov = new RegularMovie();
double w_amount = w_movieObj.getAmount(w_movieObj, w_regMov);
assertEquals(w_amount, 5);
}

@Test
public void testChildrenMovie()
{
Movie w_movieObj = new Movie("Movie2", 2);
w_movieObj.setPriceCode(4);
AmountInterface w_childMovie = new ChildrenMovie();
double w_amount = w_movieObj.getAmount(w_movieObj, w_childMovie);
assertEquals(w_amount, 3);
}

@Test
public void testNewReleaseMovie()
{
Movie w_movieObj = new Movie("Movie3", 1);
w_movieObj.setPriceCode(4);
AmountInterface w_regMov = new NewReleaseMovie();
double w_amount = w_movieObj.getAmount(w_movieObj, w_regMov);
assertEquals(w_amount, 12);
}
}
57 changes: 31 additions & 26 deletions src/main/java/com/digite/kata/refactoring/Movie.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
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 NEW_RELEASE = 1;
public static final int REGULAR = 0;
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;
}

public double getAmount(Movie a_movie, AmountInterface a_amount) {
double w_amount = a_amount.getAmount(a_movie.getPriceCode());
return w_amount;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/digite/kata/refactoring/NewReleaseMovie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.digite.kata.refactoring;

public class NewReleaseMovie implements AmountInterface{
@Override
public int getPriceCode() {
return Movie.NEW_RELEASE;
}

@Override
public double getAmount(int a_rentalDays)
{
//thisAmount += each.getDaysRented() * 3;
double w_rentAmount = a_rentalDays * 3;
return w_rentAmount;
}

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

public class RegularMovie implements AmountInterface{

public int getPriceCode() {
return Movie.REGULAR;
}

public double getAmount(int a_rentalDays)
{
/* if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;*/
double w_rentAmount = 2;
if (a_rentalDays > 2)
w_rentAmount += (a_rentalDays - 2) * 1.5;
return w_rentAmount;
}
}
40 changes: 20 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,20 @@
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;
public Rental(Movie movie, int daysRented) {
_movie = movie;
_daysRented = daysRented;
}
public Movie getMovie() {
return _movie;
}
public int getDaysRented() {
return _daysRented;
}
}