From faf8a84727d8bd02cf787bcea088cb5f662cc877 Mon Sep 17 00:00:00 2001 From: Cynthia Terrazas Date: Fri, 19 May 2017 18:30:27 -0400 Subject: [PATCH 1/7] initial solution for bank ocr --- .../fundacionjala/coding/cynthia/BankOCR.java | 13 +++++++ .../coding/cynthia/BankOCRTest.java | 38 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java create mode 100644 src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java diff --git a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java new file mode 100644 index 0000000..32c1788 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java @@ -0,0 +1,13 @@ +package org.fundacionjala.coding.cynthia; + +/** + * Created by CynthiaTerrazas on 5/16/2017. + */ +public class BankOCR { + + public static int convertEntryToNumber(String entryLine1, String entryLine2, String entryLine3) { + return 123456789; + } + + +} diff --git a/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java b/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java new file mode 100644 index 0000000..8f36ff1 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java @@ -0,0 +1,38 @@ +package org.fundacionjala.coding.cynthia; + +import org.fundacionjala.coding.cynthia.BankOCR; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by CynthiaTerrazas on 5/16/2017. + */ +public class BankOCRTest { + @Test + public void bankOCRtoTry(){ + + String entryLine1 = " _ _ _ _ _ _ _ "; + String entryLine2 = " | _| _||_||_ |_ ||_||_|"; + String entryLine3 = " ||_ _| | _||_| ||_| _|"; + + String expectedResult = "123456789"; + + assertEquals(expectedResult, BankOCR.convertEntryToNumber(entryLine1, entryLine2, entryLine3)); + + } + + @Test + public void theDigitIsOne(){ + String entryLine1 =" "; + String entryLine2 =" |"; + String entryLine3 =" |"; + + } + +} + + + + + From 57beff0864271ed095c60a2bd83063407d473c51 Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Mon, 5 Jun 2017 00:16:41 -0400 Subject: [PATCH 2/7] solution banck OCR --- .../fundacionjala/coding/cynthia/BankOCR.java | 89 ++++++++- .../coding/cynthia/BankOCRTest.java | 172 ++++++++++++++++-- 2 files changed, 245 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java index 32c1788..4a673e7 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java @@ -1,13 +1,94 @@ package org.fundacionjala.coding.cynthia; +import java.util.HashMap; +import java.util.Map; /** - * Created by CynthiaTerrazas on 5/16/2017. + * Created by CynthiaTerrazas on 6/04/2017. */ -public class BankOCR { +public final class BankOCR { + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int FOUR = 4; + private static final int FIVE = 5; + private static final int SIX = 6; + private static final int SEVEN = 7; + private static final int EIGHT = 8; + private static final int NINE = 9; + private static final int NUMBER_DIGITS = 9; + private static final int FIRST_LINE = 27; + private static final int SECOND_LINE = 54; + private static final int LONG_LINE = 3; + private static final int MODULO = 11; + private static final Map STRING_NUMBERS = new HashMap() { + { + put(" _ | ||_|", ZERO); + put(" | |", ONE); + put(" _ _||_ ", TWO); + put(" _ _| _|", THREE); + put(" |_| |", FOUR); + put(" _ |_ _|", FIVE); + put(" _ |_ |_|", SIX); + put(" _ | |", SEVEN); + put(" _ |_||_|", EIGHT); + put(" _ |_| _|", NINE); + } + }; - public static int convertEntryToNumber(String entryLine1, String entryLine2, String entryLine3) { - return 123456789; + /** + * Constructor. + */ + private BankOCR() { } + /** + * @param stringNumber string with the code of the number for example(" _ |_| _|") + * @return String the value the number + */ + public static String convertNumber(String stringNumber) { + String firstLine = stringNumber.substring(0, FIRST_LINE); + String secondLine = stringNumber.substring(FIRST_LINE, SECOND_LINE); + String thirdLine = stringNumber.substring(SECOND_LINE); + int a = 0; + int b = LONG_LINE; + String number = ""; + for (int i = 1; i <= NUMBER_DIGITS; i++) { + String digit = firstLine.substring(a, b) + secondLine.substring(a, b) + thirdLine.substring(a, b); + a = b; + b = b + LONG_LINE; + number = number + getNumberValue(digit); + } + return number; + } + + + /** + * @param digit code of the number for example(" _ |_| _|") + * @return String the value the number + */ + private static String getNumberValue(String digit) { + Integer number = STRING_NUMBERS.get(digit); + return number == null ? "?" : number.toString(); + } + /** + * @param number number convert. + * @return String if is valid number return the number if not return number with "ERR". + */ + private static String validNumber(String number) { + int checksum = 0; + for (int i = 1; i <= number.length(); i++) { + checksum = checksum + (Integer.parseInt("" + number.charAt(number.length() - i))) * i; + } + return checksum % MODULO == 0 ? number : number + " ERR"; + } + + /** + * @param number number convert. + * @return String if contain "?" return number with "ILL" if not number or Number with "ERR" + */ + public static String output(String number) { + return number.contains("?") ? number + " ILL" : validNumber(number); + } } diff --git a/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java b/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java index 8f36ff1..1881f26 100644 --- a/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java +++ b/src/test/java/org/fundacionjala/coding/cynthia/BankOCRTest.java @@ -1,38 +1,186 @@ package org.fundacionjala.coding.cynthia; -import org.fundacionjala.coding.cynthia.BankOCR; import org.junit.Test; -import static org.junit.Assert.*; +import static junit.framework.TestCase.assertEquals; +import static org.fundacionjala.coding.cynthia.BankOCR.convertNumber; +import static org.fundacionjala.coding.cynthia.BankOCR.output; /** * Created by CynthiaTerrazas on 5/16/2017. */ public class BankOCRTest { + /** + * this is the first test with numbers 123456789. + */ @Test - public void bankOCRtoTry(){ + public void bankOCRtoTryWith9DiferentesNumbers() { + String entryLine = " _ _ _ _ _ _ _ " + + " | _| _||_||_ |_ ||_||_|" + + " ||_ _| | _||_| ||_| _|"; - String entryLine1 = " _ _ _ _ _ _ _ "; - String entryLine2 = " | _| _||_||_ |_ ||_||_|"; - String entryLine3 = " ||_ _| | _||_| ||_| _|"; + assertEquals("123456789", convertNumber(entryLine)); + } + + /** + * this test is for just zeros. + */ + @Test + public void testBankOCRForJustZeros() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + "| || || || || || || || || |" + + "|_||_||_||_||_||_||_||_||_|"; + + assertEquals("000000000", convertNumber(entryLine)); + } + + /** + * this test is for just one. + */ + @Test + public void testBankOCRSForJustOnes() { + String entryLine = " " + + " | | | | | | | | |" + + " | | | | | | | | |"; - String expectedResult = "123456789"; + assertEquals("111111111", convertNumber(entryLine)); + } - assertEquals(expectedResult, BankOCR.convertEntryToNumber(entryLine1, entryLine2, entryLine3)); + /** + * this test is for just for two. + */ + @Test + public void testBankOCRSForJustTwos() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + " _| _| _| _| _| _| _| _| _|" + + "|_ |_ |_ |_ |_ |_ |_ |_ |_ "; + assertEquals("222222222", convertNumber(entryLine)); } + /** + * this test is for just three. + */ @Test - public void theDigitIsOne(){ - String entryLine1 =" "; - String entryLine2 =" |"; - String entryLine3 =" |"; + public void testBankOCRForJustThrees() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + " _| _| _| _| _| _| _| _| _|" + + " _| _| _| _| _| _| _| _| _| "; + assertEquals("333333333", convertNumber(entryLine)); } + /** + * this test is for just four. + */ + @Test + public void testBankOCRForJustFours() { + String entryLine = " " + + "|_||_||_||_||_||_||_||_||_|" + + " | | | | | | | | |"; + + assertEquals("444444444", convertNumber(entryLine)); + } + + /** + * this test is for just five. + */ + @Test + public void testBankOCRForJustFives() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + "|_ |_ |_ |_ |_ |_ |_ |_ |_ " + + " _| _| _| _| _| _| _| _| _|"; + + assertEquals("555555555", convertNumber(entryLine)); + } + + /** + * this test is for just six. + */ + @Test + public void testBankOCRForJustSixs() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + "|_ |_ |_ |_ |_ |_ |_ |_ |_ " + + "|_||_||_||_||_||_||_||_||_|"; + + assertEquals("666666666", convertNumber(entryLine)); + } + + /** + * this test is for just seven. + */ + @Test + public void testBankOCRForJustSevens() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + " | | | | | | | | |" + + " | | | | | | | | |"; + + assertEquals("777777777", convertNumber(entryLine)); + } + + /** + * this test is for just eight. + */ + @Test + public void testBankOCRForJustEights() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + "|_||_||_||_||_||_||_||_||_|" + + "|_||_||_||_||_||_||_||_||_|"; + + assertEquals("888888888", convertNumber(entryLine)); + } + + /** + * this test is for just nine. + */ + @Test + public void testBankOCRForJustNines() { + String entryLine = " _ _ _ _ _ _ _ _ _ " + + "|_||_||_||_||_||_||_||_||_|" + + " _| _| _| _| _| _| _| _| _|"; + + assertEquals("999999999", convertNumber(entryLine)); + } + + /** + * this test is for correct number count . + */ + @Test + public void testBankOCRForCorrectNumberCount() { + String entryLine = " _ _ _ _ _ _ _ _ " + + "|_||_ ||_ | ||_|| || || |" + + " | _| | _||_||_||_||_||_|"; + + assertEquals("457508000", output(convertNumber(entryLine))); + } + + /** + * this test is for incorrect number count . + */ + @Test + public void testBankOCRForIncorrectNumberCount() { + String entryLine = " _ _ _ _ _ _ " + + "|_ |_ |_| _| | ||_||_||_ " + + "|_||_| | _| | | | _| _|"; + + assertEquals("664371495 ERR", output(convertNumber(entryLine))); + } + + /** + * this test is for invalid number count . + */ + @Test + public void testBankOCRForInvalidNumberCount() { + String entryLine = " _ _ _ _ _ " + + "|_||_ | || | _| _| _||_ " + + "|_||_| | ||_| | | _||_|"; + + assertEquals("86110??36 ILL", output(convertNumber(entryLine))); + } } + From 507e56b0e8e32f49eb43d9d210c8ee0838ee2d86 Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Mon, 5 Jun 2017 00:52:32 -0400 Subject: [PATCH 3/7] fix warning message using StringBuffer --- src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java index 4a673e7..a18ea10 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java @@ -52,14 +52,14 @@ public static String convertNumber(String stringNumber) { String thirdLine = stringNumber.substring(SECOND_LINE); int a = 0; int b = LONG_LINE; - String number = ""; + StringBuffer number = new StringBuffer(); for (int i = 1; i <= NUMBER_DIGITS; i++) { String digit = firstLine.substring(a, b) + secondLine.substring(a, b) + thirdLine.substring(a, b); a = b; b = b + LONG_LINE; - number = number + getNumberValue(digit); + number = number.append(getNumberValue(digit)); } - return number; + return number.toString(); } From 15936149eaf009047aba6fbd66e03f10934588d4 Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Mon, 5 Jun 2017 19:36:37 -0400 Subject: [PATCH 4/7] fix observations rename variables split function --- .../fundacionjala/coding/cynthia/BankOCR.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java index a18ea10..dd6b863 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/BankOCR.java @@ -2,6 +2,7 @@ import java.util.HashMap; import java.util.Map; + /** * Created by CynthiaTerrazas on 6/04/2017. */ @@ -50,13 +51,14 @@ public static String convertNumber(String stringNumber) { String firstLine = stringNumber.substring(0, FIRST_LINE); String secondLine = stringNumber.substring(FIRST_LINE, SECOND_LINE); String thirdLine = stringNumber.substring(SECOND_LINE); - int a = 0; - int b = LONG_LINE; + int positionFrom = 0; + int positionTo = LONG_LINE; StringBuffer number = new StringBuffer(); for (int i = 1; i <= NUMBER_DIGITS; i++) { - String digit = firstLine.substring(a, b) + secondLine.substring(a, b) + thirdLine.substring(a, b); - a = b; - b = b + LONG_LINE; + String digit = firstLine.substring(positionFrom, positionTo) + + secondLine.substring(positionFrom, positionTo) + thirdLine.substring(positionFrom, positionTo); + positionFrom = positionTo; + positionTo = positionTo + LONG_LINE; number = number.append(getNumberValue(digit)); } return number.toString(); @@ -76,12 +78,12 @@ private static String getNumberValue(String digit) { * @param number number convert. * @return String if is valid number return the number if not return number with "ERR". */ - private static String validNumber(String number) { + private static boolean validNumber(String number) { int checksum = 0; for (int i = 1; i <= number.length(); i++) { - checksum = checksum + (Integer.parseInt("" + number.charAt(number.length() - i))) * i; + checksum += Integer.parseInt(String.valueOf(number.charAt(number.length() - i))) * i; } - return checksum % MODULO == 0 ? number : number + " ERR"; + return checksum % MODULO == 0; } /** @@ -89,6 +91,9 @@ private static String validNumber(String number) { * @return String if contain "?" return number with "ILL" if not number or Number with "ERR" */ public static String output(String number) { - return number.contains("?") ? number + " ILL" : validNumber(number); + if (number.contains("?")) { + return number.concat(" ILL"); + } + return validNumber(number) ? number : number.concat(" ERR"); } } From 5a1d847b75633691ae0d577b103bd5575e963508 Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Fri, 9 Jun 2017 00:29:42 -0400 Subject: [PATCH 5/7] refactor movie code --- .../coding/cynthia/Video/Customer.java | 91 ++++++++++++++++ .../coding/cynthia/Video/Movie.java | 43 ++++++++ .../coding/cynthia/Video/Rental.java | 66 ++++++++++++ .../coding/cynthia/VideoTest.java | 101 ++++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java create mode 100644 src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java new file mode 100644 index 0000000..75ec826 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java @@ -0,0 +1,91 @@ +package org.fundacionjala.coding.cynthia.Video; + +import java.util.Enumeration; +import java.util.Vector; + +/** + * costumer class. + */ +public class Customer { + private String customerName; + private Vector movieRentals = new Vector(); + + /** + * constumer constructor. + * + * @param name of the client. + */ + public Customer(String name) { + customerName = name; + } + + /** + * add a new rental fr the customer. + * + * @param arg rental that will added for the customer. + */ + public void addRental(Rental arg) { + movieRentals.addElement(arg); + } + + /** + * @return name of the customer. + */ + public String getName() { + return customerName; + } + + /** + * @return all movies rented. + */ + public String moviesRented() { + StringBuffer result = new StringBuffer(); + Enumeration rentals = movieRentals.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result.append("\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.costDaysRented()) + "\n"); + } + return result.toString(); + } + + /** + * @return total cost for all movies rented. + */ + public String totalCostRented() { + StringBuffer result = new StringBuffer(); + double totalAmount = 0; + Enumeration rentals = movieRentals.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + double thisAmount = each.costDaysRented(); + totalAmount += thisAmount; + } + result.append("Amount owed is " + String.valueOf(totalAmount) + "\n"); + return result.toString(); + } + + /** + * @return return the bonus for all movies rented. + */ + public String totalBonusFrequencyRented() { + StringBuffer result = new StringBuffer(); + int frequentRenterPoints = 0; + Enumeration rentals = movieRentals.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + frequentRenterPoints += 1 + each.bonusTwoDaysNewReleaseRental(); + } + result.append("You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"); + return result.toString(); + } + + /** + * @return all information about movies rented for the customer. + */ + public String statement() { + StringBuffer result = new StringBuffer(); + result.append("Rental Record for " + getName() + "\n"); + result.append(this.moviesRented()).append(this.totalCostRented()).append(this.totalBonusFrequencyRented()); + return result.toString(); + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java new file mode 100644 index 0000000..03fcc27 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java @@ -0,0 +1,43 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Movie { + public static final int CHILDRENS = 2; + public static final int REGULAR = 0; + public static final int NEW_RELEASE = 1; + private String titleMovie; + private int priceCodeMovie; + + /** + * movie constructor. + * @param title movie. + * @param priceCode movie. + */ + public Movie(String title, int priceCode) { + titleMovie = title; + priceCodeMovie = priceCode; + } + + /** + * @return price code of the movie. + */ + public int getPriceCode() { + return priceCodeMovie; + } + + /** + * @param arg set the price code for the movie. + */ + public void setPriceCode(int arg) { + priceCodeMovie = arg; + } + + /** + * @return the title for the movie. + */ + public String getTitle() { + return titleMovie; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java new file mode 100644 index 0000000..593e5e4 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java @@ -0,0 +1,66 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * Rental Class. + */ +public class Rental { + private Movie movie; + private int daysRented; + + /** + * Rental constructor. + * + * @param movie rental. + * @param daysRented rental. + */ + public Rental(Movie movie, int daysRented) { + this.movie = movie; + this.daysRented = daysRented; + } + + /** + * get days rented. + * + * @return number of days by movie rented. + */ + public int getDaysRented() { + return daysRented; + } + + /** + * @return movie rented. + */ + public Movie getMovie() { + return movie; + } + + /** + * @return Cost of the days rented by all movie. + */ + public double costDaysRented() { + double thisAmount = 0; + switch (this.getMovie().getPriceCode()) { + case Movie.REGULAR: + thisAmount = this.getDaysRented() > 2 ? ((this.getDaysRented() - 2) * 1.5) + 2 : 2; + break; + case Movie.NEW_RELEASE: + thisAmount = this.getDaysRented() * 3; + break; + case Movie.CHILDRENS: + thisAmount = this.getDaysRented() > 2 ? ((this.getDaysRented() - 3) * 1.5) + 1.5 : 1.5; + break; + default: thisAmount = 0; + } + return thisAmount; + } + + /** + * @return bonus for frequency. + */ + public int bonusTwoDaysNewReleaseRental() { + if ((this.getMovie().getPriceCode() == Movie.NEW_RELEASE) && this.getDaysRented() > 1) { + return 1; + } + return 0; + } +} diff --git a/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java new file mode 100644 index 0000000..e5b1207 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java @@ -0,0 +1,101 @@ +package org.fundacionjala.coding.cynthia; + +import org.fundacionjala.coding.cynthia.Video.Customer; +import org.fundacionjala.coding.cynthia.Video.Movie; +import org.fundacionjala.coding.cynthia.Video.Rental; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by USUARIO on 07/06/2017. + */ +public class VideoTest { + + /** + * verify the rental for one customer (RELESEA, CHILDREN). + */ + @Test + public void testVideoOnCustomer() { + Customer customer = new Customer("Test"); + customer.addRental(new Rental(new Movie("The Revenant", 1), 2)); + customer.addRental(new Rental(new Movie("Terminator", 0), 2)); + assertEquals("Rental Record for Test\n" + + "\tThe Revenant\t6.0\n" + + "\tTerminator\t2.0\n" + + "Amount owed is 8.0\n" + + "You earned 3 frequent renter points", customer.statement()); + } + + /** + * verify just to one rented movie (RELEASE movie). + */ + @Test + public void testVideoOneVideoRented() { + Customer customer = new Customer("Juan"); + customer.addRental(new Rental(new Movie("The Revenant", 1), 4)); + assertEquals("Rental Record for Juan\n" + + "\tThe Revenant\t12.0\n" + + "Amount owed is 12.0\n" + + "You earned 2 frequent renter points", customer.statement()); + } + + /** + * verify the rental whithout bonus. + */ + @Test + public void testVideoWithoutBonus() { + Customer customer = new Customer("Pedro"); + customer.addRental(new Rental(new Movie("The Revenant", 1), 1)); + assertEquals("Rental Record for Pedro\n" + + "\tThe Revenant\t3.0\n" + + "Amount owed is 3.0\n" + + "You earned 1 frequent renter points", customer.statement()); + } + + /** + * verify the rental for several movies. + */ + @Test + public void testVideoSeveralMovies() { + Customer customer = new Customer("Oscar"); + customer.addRental(new Rental(new Movie("The Revenant", 0), 1)); + customer.addRental(new Rental(new Movie("Cars", 1), 2)); + customer.addRental(new Rental(new Movie("king kong", 2), 5)); + customer.addRental(new Rental(new Movie("Titanic", 0), 3)); + customer.addRental(new Rental(new Movie("Galaxy", 2), 4)); + assertEquals("Rental Record for Oscar\n" + + "\tThe Revenant\t2.0\n" + + "\tCars\t6.0\n" + + "\tking kong\t4.5\n" + + "\tTitanic\t3.5\n" + + "\tGalaxy\t3.0\n" + + "Amount owed is 19.0\n" + + "You earned 6 frequent renter points", customer.statement()); + } + + /** + * verify rental for two customers. + */ + @Test + public void testVideoTwoCustomers() { + Customer marcan = new Customer("marcan"); + Customer agustin = new Customer("Agustin"); + marcan.addRental(new Rental(new Movie("The Revenant", 0), 1)); + marcan.addRental(new Rental(new Movie("Cars", 1), 2)); + marcan.addRental(new Rental(new Movie("king kong", 2), 5)); + agustin.addRental(new Rental(new Movie("Titanic", 0), 3)); + agustin.addRental(new Rental(new Movie("Galaxy", 2), 4)); + assertEquals("Rental Record for marcan\n" + + "\tThe Revenant\t2.0\n" + + "\tCars\t6.0\n" + + "\tking kong\t4.5\n" + + "Amount owed is 12.5\n" + + "You earned 4 frequent renter points", marcan.statement()); + assertEquals("Rental Record for Agustin\n" + + "\tTitanic\t3.5\n" + + "\tGalaxy\t3.0\n" + + "Amount owed is 6.5\n" + + "You earned 2 frequent renter points", agustin.statement()); + } +} From bd507dcb48ff7f43c5d73e08426fa2a6d611412b Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Sat, 10 Jun 2017 12:34:37 -0400 Subject: [PATCH 6/7] fix observations --- .../coding/cynthia/Video/Children.java | 25 ++++++++++++ .../coding/cynthia/Video/Customer.java | 38 ++++++++----------- .../coding/cynthia/Video/Movie.java | 11 +++++- .../coding/cynthia/Video/Regular.java | 27 +++++++++++++ .../coding/cynthia/Video/Release.java | 25 ++++++++++++ .../coding/cynthia/Video/Rental.java | 15 +------- .../coding/cynthia/VideoTest.java | 32 ++++++++-------- 7 files changed, 120 insertions(+), 53 deletions(-) create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java create mode 100644 src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java new file mode 100644 index 0000000..d98ec9e --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java @@ -0,0 +1,25 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Children extends Movie { + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Children(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() > 2 ? ((rental.getDaysRented() - 3) * 1.5) + 1.5 : 1.5; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java index 75ec826..e3da958 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java @@ -1,14 +1,16 @@ package org.fundacionjala.coding.cynthia.Video; -import java.util.Enumeration; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; /** * costumer class. */ public class Customer { private String customerName; - private Vector movieRentals = new Vector(); + private List movieRentals; + private double totalAmount = 0; + private int frequentRenterPoints; /** * constumer constructor. @@ -17,6 +19,7 @@ public class Customer { */ public Customer(String name) { customerName = name; + movieRentals = new ArrayList<>(); } /** @@ -25,7 +28,7 @@ public Customer(String name) { * @param arg rental that will added for the customer. */ public void addRental(Rental arg) { - movieRentals.addElement(arg); + movieRentals.add(arg); } /** @@ -40,11 +43,9 @@ public String getName() { */ public String moviesRented() { StringBuffer result = new StringBuffer(); - Enumeration rentals = movieRentals.elements(); - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - result.append("\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.costDaysRented()) + "\n"); - } + movieRentals.forEach(rental -> { + result.append("\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(rental.costDaysRented()) + "\n"); + }); return result.toString(); } @@ -53,13 +54,9 @@ public String moviesRented() { */ public String totalCostRented() { StringBuffer result = new StringBuffer(); - double totalAmount = 0; - Enumeration rentals = movieRentals.elements(); - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - double thisAmount = each.costDaysRented(); - totalAmount += thisAmount; - } + movieRentals.forEach(rental -> { + totalAmount += rental.costDaysRented(); + }); result.append("Amount owed is " + String.valueOf(totalAmount) + "\n"); return result.toString(); } @@ -69,12 +66,9 @@ public String totalCostRented() { */ public String totalBonusFrequencyRented() { StringBuffer result = new StringBuffer(); - int frequentRenterPoints = 0; - Enumeration rentals = movieRentals.elements(); - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - frequentRenterPoints += 1 + each.bonusTwoDaysNewReleaseRental(); - } + movieRentals.forEach(rental -> { + frequentRenterPoints += 1 + rental.bonusTwoDaysNewReleaseRental(); + }); result.append("You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"); return result.toString(); } diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java index 03fcc27..0e83af2 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java @@ -3,7 +3,7 @@ /** * movie class. */ -public class Movie { +abstract class Movie { public static final int CHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASE = 1; @@ -15,7 +15,7 @@ public class Movie { * @param title movie. * @param priceCode movie. */ - public Movie(String title, int priceCode) { + Movie(String title, int priceCode) { titleMovie = title; priceCodeMovie = priceCode; } @@ -40,4 +40,11 @@ public void setPriceCode(int arg) { public String getTitle() { return titleMovie; } + + /** + * + * @param rental to get the cost of days rented by movie. + * @return cost days by movies rented. + */ + public abstract double costDaysRented(Rental rental); } diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java new file mode 100644 index 0000000..adc89d9 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java @@ -0,0 +1,27 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Regular extends Movie { + public static final int REGULAR = 0; + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Regular(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() > 2 ? ((rental.getDaysRented() - 2) * 1.5) + 2 : 2; + } + +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java new file mode 100644 index 0000000..94873fb --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java @@ -0,0 +1,25 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Release extends Movie { + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Release(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() * 3; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java index 593e5e4..ebbe71d 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java @@ -38,20 +38,7 @@ public Movie getMovie() { * @return Cost of the days rented by all movie. */ public double costDaysRented() { - double thisAmount = 0; - switch (this.getMovie().getPriceCode()) { - case Movie.REGULAR: - thisAmount = this.getDaysRented() > 2 ? ((this.getDaysRented() - 2) * 1.5) + 2 : 2; - break; - case Movie.NEW_RELEASE: - thisAmount = this.getDaysRented() * 3; - break; - case Movie.CHILDRENS: - thisAmount = this.getDaysRented() > 2 ? ((this.getDaysRented() - 3) * 1.5) + 1.5 : 1.5; - break; - default: thisAmount = 0; - } - return thisAmount; + return movie.costDaysRented(this); } /** diff --git a/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java index e5b1207..109b143 100644 --- a/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java +++ b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java @@ -1,7 +1,9 @@ package org.fundacionjala.coding.cynthia; import org.fundacionjala.coding.cynthia.Video.Customer; -import org.fundacionjala.coding.cynthia.Video.Movie; +import org.fundacionjala.coding.cynthia.Video.Regular; +import org.fundacionjala.coding.cynthia.Video.Release; +import org.fundacionjala.coding.cynthia.Video.Children; import org.fundacionjala.coding.cynthia.Video.Rental; import org.junit.Test; @@ -18,8 +20,8 @@ public class VideoTest { @Test public void testVideoOnCustomer() { Customer customer = new Customer("Test"); - customer.addRental(new Rental(new Movie("The Revenant", 1), 2)); - customer.addRental(new Rental(new Movie("Terminator", 0), 2)); + customer.addRental(new Rental(new Release("The Revenant", 1), 2)); + customer.addRental(new Rental(new Regular("Terminator", 0), 2)); assertEquals("Rental Record for Test\n" + "\tThe Revenant\t6.0\n" + "\tTerminator\t2.0\n" @@ -33,7 +35,7 @@ public void testVideoOnCustomer() { @Test public void testVideoOneVideoRented() { Customer customer = new Customer("Juan"); - customer.addRental(new Rental(new Movie("The Revenant", 1), 4)); + customer.addRental(new Rental(new Release("The Revenant", 1), 4)); assertEquals("Rental Record for Juan\n" + "\tThe Revenant\t12.0\n" + "Amount owed is 12.0\n" @@ -46,7 +48,7 @@ public void testVideoOneVideoRented() { @Test public void testVideoWithoutBonus() { Customer customer = new Customer("Pedro"); - customer.addRental(new Rental(new Movie("The Revenant", 1), 1)); + customer.addRental(new Rental(new Release("The Revenant", 1), 1)); assertEquals("Rental Record for Pedro\n" + "\tThe Revenant\t3.0\n" + "Amount owed is 3.0\n" @@ -59,11 +61,11 @@ public void testVideoWithoutBonus() { @Test public void testVideoSeveralMovies() { Customer customer = new Customer("Oscar"); - customer.addRental(new Rental(new Movie("The Revenant", 0), 1)); - customer.addRental(new Rental(new Movie("Cars", 1), 2)); - customer.addRental(new Rental(new Movie("king kong", 2), 5)); - customer.addRental(new Rental(new Movie("Titanic", 0), 3)); - customer.addRental(new Rental(new Movie("Galaxy", 2), 4)); + customer.addRental(new Rental(new Regular("The Revenant", 0), 1)); + customer.addRental(new Rental(new Release("Cars", 1), 2)); + customer.addRental(new Rental(new Children("king kong", 2), 5)); + customer.addRental(new Rental(new Regular("Titanic", 0), 3)); + customer.addRental(new Rental(new Children("Galaxy", 2), 4)); assertEquals("Rental Record for Oscar\n" + "\tThe Revenant\t2.0\n" + "\tCars\t6.0\n" @@ -81,11 +83,11 @@ public void testVideoSeveralMovies() { public void testVideoTwoCustomers() { Customer marcan = new Customer("marcan"); Customer agustin = new Customer("Agustin"); - marcan.addRental(new Rental(new Movie("The Revenant", 0), 1)); - marcan.addRental(new Rental(new Movie("Cars", 1), 2)); - marcan.addRental(new Rental(new Movie("king kong", 2), 5)); - agustin.addRental(new Rental(new Movie("Titanic", 0), 3)); - agustin.addRental(new Rental(new Movie("Galaxy", 2), 4)); + marcan.addRental(new Rental(new Regular("The Revenant", 0), 1)); + marcan.addRental(new Rental(new Release("Cars", 1), 2)); + marcan.addRental(new Rental(new Children("king kong", 2), 5)); + agustin.addRental(new Rental(new Regular("Titanic", 0), 3)); + agustin.addRental(new Rental(new Children("Galaxy", 2), 4)); assertEquals("Rental Record for marcan\n" + "\tThe Revenant\t2.0\n" + "\tCars\t6.0\n" From bc441eae4461cb0ce3378ccc666f6de6c706fbc8 Mon Sep 17 00:00:00 2001 From: cynthia terrazas gutierrez Date: Wed, 14 Jun 2017 00:52:44 -0400 Subject: [PATCH 7/7] fix observations --- .../coding/cynthia/Video/Customer.java | 16 +++++++--------- .../coding/cynthia/Video/Movie.java | 13 ++++++++++--- .../coding/cynthia/Video/Release.java | 13 +++++++++++++ .../coding/cynthia/Video/Rental.java | 5 +---- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java index e3da958..f4974bb 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java @@ -52,25 +52,21 @@ public String moviesRented() { /** * @return total cost for all movies rented. */ - public String totalCostRented() { - StringBuffer result = new StringBuffer(); + public double totalCostRented() { movieRentals.forEach(rental -> { totalAmount += rental.costDaysRented(); }); - result.append("Amount owed is " + String.valueOf(totalAmount) + "\n"); - return result.toString(); + return totalAmount; } /** * @return return the bonus for all movies rented. */ - public String totalBonusFrequencyRented() { - StringBuffer result = new StringBuffer(); + public int totalBonusFrequencyRented() { movieRentals.forEach(rental -> { frequentRenterPoints += 1 + rental.bonusTwoDaysNewReleaseRental(); }); - result.append("You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"); - return result.toString(); + return frequentRenterPoints; } /** @@ -79,7 +75,9 @@ public String totalBonusFrequencyRented() { public String statement() { StringBuffer result = new StringBuffer(); result.append("Rental Record for " + getName() + "\n"); - result.append(this.moviesRented()).append(this.totalCostRented()).append(this.totalBonusFrequencyRented()); + result.append(this.moviesRented()); + result.append("Amount owed is " + String.valueOf(this.totalCostRented()) + "\n"); + result.append("You earned " + String.valueOf(this.totalBonusFrequencyRented()) + " frequent renter points"); return result.toString(); } } diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java index 0e83af2..2ca4057 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java @@ -4,9 +4,7 @@ * movie class. */ abstract class Movie { - public static final int CHILDRENS = 2; - public static final int REGULAR = 0; - public static final int NEW_RELEASE = 1; + private static final int DEFAULT_BONUS_DAY_RENTED = 0; private String titleMovie; private int priceCodeMovie; @@ -47,4 +45,13 @@ public String getTitle() { * @return cost days by movies rented. */ public abstract double costDaysRented(Rental rental); + + /** + * + * @param rental movie. + * @return bonus by movie rented. + */ + public int bonusTwoDaysNewReleaseRental(Rental rental) { + return DEFAULT_BONUS_DAY_RENTED; + } } diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java index 94873fb..ff278a7 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java @@ -22,4 +22,17 @@ public Release(String title, int priceCode) { public double costDaysRented(Rental rental) { return rental.getDaysRented() * 3; } + + /** + * + * @param rental movie. + * @return bonus by movie rented. + */ + @Override + public int bonusTwoDaysNewReleaseRental(Rental rental) { + if (rental.getDaysRented() > 1) { + return 1; + } + return 0; + } } diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java index ebbe71d..6936b1c 100644 --- a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java @@ -45,9 +45,6 @@ public double costDaysRented() { * @return bonus for frequency. */ public int bonusTwoDaysNewReleaseRental() { - if ((this.getMovie().getPriceCode() == Movie.NEW_RELEASE) && this.getDaysRented() > 1) { - return 1; - } - return 0; + return movie.bonusTwoDaysNewReleaseRental(this); } }