forked from unclebob/videostore
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jramnani/working-java-project
Working java project
- Loading branch information
Showing
10 changed files
with
171 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ videostore.iml | |
.settings/ | ||
.classpath | ||
.project | ||
**/target/ | ||
|
||
*.suo | ||
*.user | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example.videostore</groupId> | ||
<artifactId>videostore</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.2</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
java/videostore/src/main/java/com/example/videostore/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.example.videostore; | ||
|
||
import java.util.Vector; | ||
import java.util.Enumeration; | ||
|
||
public class Customer { | ||
public Customer(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void addRental(Rental rental) { | ||
rentals.addElement(rental); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String statement() { | ||
double totalAmount = 0; | ||
int frequentRenterPoints = 0; | ||
Enumeration rentals = this.rentals.elements(); | ||
String result = "Rental Record for " + getName() + "\n"; | ||
|
||
while (rentals.hasMoreElements()) { | ||
double thisAmount = 0; | ||
Rental each = (Rental) rentals.nextElement(); | ||
|
||
// determines the amount 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; | ||
} | ||
|
||
frequentRenterPoints++; | ||
|
||
if (each.getMovie().getPriceCode() == Movie.NEW_RELEASE | ||
&& each.getDaysRented() > 1) | ||
frequentRenterPoints++; | ||
|
||
result += "\t" + each.getMovie().getTitle() + "\t" | ||
+ String.valueOf(thisAmount) + "\n"; | ||
totalAmount += thisAmount; | ||
} | ||
|
||
result += "You owed " + String.valueOf(totalAmount) + "\n"; | ||
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points\n"; | ||
|
||
return result; | ||
} | ||
|
||
|
||
private String name; | ||
private Vector rentals = new Vector(); | ||
} |
28 changes: 28 additions & 0 deletions
28
java/videostore/src/main/java/com/example/videostore/Movie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.videostore; | ||
|
||
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) { | ||
this.title = title; | ||
this.priceCode = priceCode; | ||
} | ||
|
||
public int getPriceCode() { | ||
return priceCode; | ||
} | ||
|
||
public void setPriceCode(int code) { | ||
priceCode = code; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
java/videostore/src/main/java/com/example/videostore/Rental.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.videostore; | ||
|
||
public class Rental { | ||
public Rental(Movie movie, int daysRented) { | ||
this.movie = movie; | ||
this.daysRented = daysRented; | ||
} | ||
|
||
public int getDaysRented() { | ||
return daysRented; | ||
} | ||
|
||
public Movie getMovie() { | ||
return movie; | ||
} | ||
|
||
private Movie movie; | ||
private int daysRented; | ||
} |
39 changes: 39 additions & 0 deletions
39
java/videostore/src/test/java/com/example/videostore/VideoStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.videostore; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class VideoStoreTest extends TestCase { | ||
public VideoStoreTest(String name) { | ||
super(name); | ||
} | ||
|
||
protected void setUp() { | ||
customer = new Customer("Fred"); | ||
} | ||
|
||
public void testSingleNewReleaseStatement() { | ||
customer.addRental(new Rental(new Movie("The Cell", Movie.NEW_RELEASE), 3)); | ||
assertEquals("Rental Record for Fred\n\tThe Cell\t9.0\nYou owed 9.0\nYou earned 2 frequent renter points\n", customer.statement()); | ||
} | ||
|
||
public void testDualNewReleaseStatement() { | ||
customer.addRental(new Rental(new Movie("The Cell", Movie.NEW_RELEASE), 3)); | ||
customer.addRental(new Rental(new Movie("The Tigger com.example.videostore.Movie", Movie.NEW_RELEASE), 3)); | ||
assertEquals("Rental Record for Fred\n\tThe Cell\t9.0\n\tThe Tigger com.example.videostore.Movie\t9.0\nYou owed 18.0\nYou earned 4 frequent renter points\n", customer.statement()); | ||
} | ||
|
||
public void testSingleChildrensStatement() { | ||
customer.addRental(new Rental(new Movie("The Tigger com.example.videostore.Movie", Movie.CHILDRENS), 3)); | ||
assertEquals("Rental Record for Fred\n\tThe Tigger com.example.videostore.Movie\t1.5\nYou owed 1.5\nYou earned 1 frequent renter points\n", customer.statement()); | ||
} | ||
|
||
public void testMultipleRegularStatement() { | ||
customer.addRental(new Rental(new Movie("Plan 9 from Outer Space", Movie.REGULAR), 1)); | ||
customer.addRental(new Rental(new Movie("8 1/2", Movie.REGULAR), 2)); | ||
customer.addRental(new Rental(new Movie("Eraserhead", Movie.REGULAR), 3)); | ||
|
||
assertEquals("Rental Record for Fred\n\tPlan 9 from Outer Space\t2.0\n\t8 1/2\t2.0\n\tEraserhead\t3.5\nYou owed 7.5\nYou earned 3 frequent renter points\n", customer.statement()); | ||
} | ||
|
||
private Customer customer; | ||
} |