Browse Source

test

master
Sarah Pastors 2 years ago
parent
commit
467cb0a037
2 changed files with 196 additions and 33 deletions
  1. 143
    0
      VideoStore/Test/CustomerTest.java
  2. 53
    33
      VideoStore/src/Customer.java

+ 143
- 0
VideoStore/Test/CustomerTest.java View File

@@ -0,0 +1,143 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CustomerTest {

Customer customer;

@Before
public void setUp() throws Exception {
customer = new Customer("customer A");
}

@Test
public void test_statement_no_rentals() {
String expected_text = "Rental Record for customer A\n" +
"Amount owed is 0.0\n" +
"You earned 0 frequent renter points";
String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}

@Test
public void test_statement_one_regular_two_days() {
String expected_text = "Rental Record for customer A\n" +
"\tregular_movie\t days rented: 2 = 2.0\n" +
"Amount owed is 2.0\n" +
"You earned 1 frequent renter points";
Movie regular_movie = new Movie ("regular_movie", Movie.REGULAR );
Rental rental = new Rental(regular_movie, 2);
customer.addRental(rental);

String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}

@Test
public void test_statement_one_regular_three_days() {
String expected_text = "Rental Record for customer A\n" +
"\tregular_movie\t days rented: 3 = 3.5\n" +
"Amount owed is 3.5\n" +
"You earned 1 frequent renter points";
Movie regular_movie = new Movie ("regular_movie", Movie.REGULAR );
Rental rental = new Rental(regular_movie, 3);
customer.addRental(rental);

String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}

@Test
public void test_statement_one_childrens_two_days() {
String expected_text = "Rental Record for customer A\n" +
"\tchildrens_movie\t days rented: 2 = 1.5\n" +
"Amount owed is 1.5\n" +
"You earned 1 frequent renter points";
Movie childrens_movie = new Movie ("childrens_movie", Movie.CHILDRENS );
Rental rental = new Rental(childrens_movie, 2);
customer.addRental(rental);

String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}

@Test
public void test_statement_one_new_release_two_days() {
String expected_text = "Rental Record for customer A\n" +
"\tnew_release_movie\t days rented: 2 = 6.0\n" +
"Amount owed is 6.0\n" +
"You earned 2 frequent renter points";
Movie new_release_movie = new Movie ("new_release_movie", Movie.NEW_RELEASE );
Rental rental = new Rental(new_release_movie, 2);
customer.addRental(rental);

String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}

@Test
public void test_statement_with_all_kinds_of_movies_one_and_four_days() {
String expected_text = "Rental Record for customer A\n" +
"\tregular_movie_1\t days rented: 1 = 2.0\n" +
"\tregular_movie_2\t days rented: 4 = 5.0\n" +
"\tchildrens_movie_1\t days rented: 1 = 1.5\n" +
"\tchildrens_movie_2\t days rented: 4 = 3.0\n" +
"\tnew_release_movie_1\t days rented: 1 = 3.0\n" +
"\tnew_release_movie_2\t days rented: 4 = 12.0\n" +
"Amount owed is 26.5\n" +
"You earned 7 frequent renter points";

Movie regular_movie_1 = new Movie ("regular_movie_1", Movie.REGULAR );
Rental rental_1 = new Rental(regular_movie_1, 1);
customer.addRental(rental_1);

Movie regular_movie_2 = new Movie ("regular_movie_2", Movie.REGULAR );
Rental rental_2 = new Rental(regular_movie_2, 4);
customer.addRental(rental_2);

Movie childrens_movie_1 = new Movie ("childrens_movie_1", Movie.CHILDRENS );
Rental rental_3 = new Rental(childrens_movie_1, 1);
customer.addRental(rental_3);

Movie childrens_movie_2 = new Movie ("childrens_movie_2", Movie.CHILDRENS );
Rental rental_4 = new Rental(childrens_movie_2, 4);
customer.addRental(rental_4);

Movie new_release_movie_1 = new Movie ("new_release_movie_1", Movie.NEW_RELEASE );
Rental rental_5 = new Rental(new_release_movie_1, 1);
customer.addRental(rental_5);

Movie new_release_movie_2 = new Movie ("new_release_movie_2", Movie.NEW_RELEASE );
Rental rental_6 = new Rental(new_release_movie_2, 4);
customer.addRental(rental_6);

String invoice_text = customer.statement();
assertEquals(expected_text, invoice_text);

System.out.println("test_statement_no_rentals");
System.out.println(invoice_text);

}
}

+ 53
- 33
VideoStore/src/Customer.java View File

@@ -18,45 +18,65 @@ public class Customer {
}

public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
// add header
StringBuilder result = new StringBuilder();
result.append("Rental Record for ").append(getName()).append("\n");

String result = "Rental Record for " + getName() + "\n";
// add line for each rental
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;
// show figures for this rental
result.append("\t").append(curRental.getMovie().getTitle()).append("\t days rented: ").append(curRental.getDaysRented()).append(" = ").append(String.valueOf( calculateAmountFor(curRental) )).append("\n");
}
// add footer lines
result.append("Amount owed is ").append(String.valueOf( getTotalAmount() )).append("\n");
result.append("You earned ").append(String.valueOf( getFrequentRenterPoints() )).append(" frequent renter points");
return result.toString();
}
public double calculateAmountFor (Rental curRental) {
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;

// add frequent renter points
}
return thisAmount;
}
public int calculateFrequentRenterPointsFor (Rental curRental){
int frequentRenterPoints = 1;
// add bonus for a two day new release rental
if( ( curRental.getMovie().getPriceCode() == Movie.NEW_RELEASE ) && curRental.getDaysRented() > 1 )
frequentRenterPoints++;
// add bonus for a two day new release rental
if( ( curRental.getMovie().getPriceCode() == Movie.NEW_RELEASE ) && curRental.getDaysRented() > 1 )
frequentRenterPoints++;
return frequentRenterPoints;
}

// show figures for this rental
result += "\t" + curRental.getMovie().getTitle() + "\t days rented: " + curRental.getDaysRented() + " = " + String.valueOf( thisAmount ) + "\n";
private int getFrequentRenterPoints() {
int frequentRenterPoints = 0;
for( Rental curRental : rentals ) {
// add frequent renter points
frequentRenterPoints += calculateFrequentRenterPointsFor(curRental);
}
return frequentRenterPoints;
}

private double getTotalAmount() {
double totalAmount = 0;
for( Rental curRental : rentals ) {
double thisAmount = calculateAmountFor(curRental);
totalAmount += thisAmount;
}
// add footer lines
result += "Amount owed is " + String.valueOf( totalAmount ) + "\n";
result += "You earned " + String.valueOf( frequentRenterPoints ) + " frequent renter points";
return result;
return totalAmount;
}

}

Loading…
Cancel
Save