[JAVA][作業][被拋棄的 Lab5]
課本 Page 541. 第 2, 3 題, 為什麼說被拋棄了呢 ?
教授上課出了作業, 結果到最後不用繳交, 導致我們這群提早寫作業的人成了笨蛋!
Create a class named Movie that can be used with your video rental business. The Movie class should track the Motion Picture Association of America (MPAA) rating (e.g., Rated G, PG-13, R), ID Number, and movie title with appropriate accessor and mutator methods. Also create an equals() method that overrides Object’s equals() method, where two movies are equal if their ID number is identical. Next, create three additional classes named Action, Comedy, and Drama that are derived from Movie.
Finally, create an overridden method named calcLateFees() that takes as input the number of days a movie is late and returns the late fee for that movie. The default late fee is $10/day. Action movies have a late fee of $8/day, comedies are $6/day, and dramas are $4/day. Test your classes from a main() method.
Extend the previous problem with a Rental class. This class should store a Movie that is rented, an integer representing the ID of the customer that rented the movie, and an integer indicating how many days late the movie is. Add a method that calculates the late fees for the rental. In your main() method create an array of type Rental filled with sample data of all types of movies. Then, create a method named lateFeesOwed() that iterates through the array and returns the total amount of late fees that are outstanding.
Movie Class
package Movie;
public class Movie {
protected int ID;
protected String title;
protected String rating;
protected int tax = 10;
public Movie() {
this(0, "No title", "No rating");
}
public Movie(int ID, String title, String rating) {
if (ID < 0 || title == null || rating == null) {
System.out.println("Error: Constructor error");
System.exit(0);
}
this.ID = ID;
this.title = title;
this.rating = rating;
}
public Movie(Movie other) {
this(other.ID, other.title, other.rating);
}
public Movie clone() {
return new Movie(this);
}
public void setID(int ID) {
if (ID < 0) {
System.out.println("Error: setID error");
System.exit(0);
}
this.ID = ID;
}
public void setTitle(String title) {
if (title == null) {
System.out.println("Error: setTitle error");
System.exit(0);
}
this.title = title;
}
public void setRating(String rating) {
if (rating == null) {
System.out.println("Error: setRating error");
System.exit(0);
}
this.rating = rating;
}
public int getID() {
return ID;
}
public String getTitle() {
return title;
}
public String getRating() {
return rating;
}
public int getTax() {
return tax;
}
public int calcLateFees(int days) {
return tax * days;
}
@Override
public String toString() {
return String.format(
"Movie ID: %d, Title: %s, Rating: %s, $%d/day", ID, title,
rating, this.getTax());
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
return this.ID == ((Movie) obj).ID;
}
}
Action Class
package Movie;
public class Action extends Movie {
public Action() {
super();
tax = 8;
}
public Action(int ID, String title, String rating) {
super(ID, title, rating);
tax = 8;
}
public Action(Action other) {
this(other.ID, other.title, other.rating);
}
public Action clone() {
return new Action(this);
}
@Override
public String toString() {
return "Action " + super.toString();
}
@Override
public int calcLateFees(int days) {
return tax * days;
}
}
Comedy Class
package Movie;
public class Comedy extends Movie {
public Comedy() {
super();
tax = 6;
}
public Comedy(int ID, String title, String rating) {
super(ID, title, rating);
tax = 6;
}
public Comedy(Comedy other) {
this(other.ID, other.title, other.rating);
}
public Comedy clone() {
return new Comedy(this);
}
@Override
public String toString() {
return "Comedy " + super.toString();
}
@Override
public int calcLateFees(int days) {
return tax * days;
}
}
Drama Class
package Movie;
public class Drama extends Movie {
public Drama() {
super();
tax = 4;
}
public Drama(int ID, String title, String rating) {
super(ID, title, rating);
tax = 4;
}
public Drama(Drama other) {
this(other.ID, other.title, other.rating);
}
public Drama clone() {
return new Drama(this);
}
@Override
public String toString() {
return "Drama " + super.toString();
}
@Override
public int calcLateFees(int days) {
return tax * days;
}
}
Rental Class
package Rental;
import Movie.*;
public class Rental {
private int customerID;
private int rentalDay;
private Movie movie;
public Rental(int rentalDay, Movie movie) {
this(0, rentalDay, movie);
}
public Rental(int customerID, int rentalDay, Movie movie) {
if (customerID < 0 || rentalDay < 0 || movie == null) {
System.out.println("Error: Constructor error");
System.exit(0);
}
this.customerID = customerID;
this.rentalDay = rentalDay;
this.movie = movie.clone();
}
public void setCustomerID(int customerID) {
if(customerID < 0) {
System.out.println("Error: setCustomerID error");
System.exit(0);
}
this.customerID = customerID;
}
public void setRentalDay(int rentalDay) {
if(rentalDay < 0) {
System.out.println("Error: setRentalDay error");
System.exit(0);
}
this.rentalDay = rentalDay;
}
public void setMovie(Movie movie) {
if(movie == null) {
System.out.println("Error: setMovie error");
System.exit(0);
}
this.movie = movie.clone();
}
public int getCustomerID() {
return customerID;
}
public int getRentalDay() {
return rentalDay;
}
public Movie getMovie() {
return movie.clone();
}
public int calcLateFees() {
return movie.calcLateFees(rentalDay);
}
@Override
public String toString() {
return String.format("Customer ID: %d, Rental Day: %d, ", customerID,
rentalDay) + movie.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
return this.customerID == ((Rental) obj).customerID
&& this.rentalDay == ((Rental) obj).rentalDay
&& this.movie.equals(((Rental) obj).movie);
}
public static int lateFeesOwed(Rental[] arr) {
if (arr.length == 0) {
System.out.println("Error: Null Array");
System.exit(0);
}
int totalFees = 0;
for (Rental element : arr) {
System.out.println(element);
totalFees += element.calcLateFees();
}
return totalFees;
}
}
Demo Class
import Movie.*;
import Rental.*;
public class Demo {
public static void main(String[] args) {
System.out
.printf("Test for Movie, Action, Comedy, and Drama Class:\n\n");
Movie A = new Movie(1562, "Movie_A", "PG");
Movie B = new Action();
Movie C = new Comedy(1025, "Comedy_C", "R");
Movie D = new Drama(7695, "Drama_D", "NC-17");
System.out.printf("Movie ID: %d, Title: %s, MPAA Rating: %s\n", A.getID(), A.getTitle(), A.getRating());
System.out.printf("Late Fee for 10 days = $%d\n", A.calcLateFees(10));
B.setID(6953);
B.setTitle("Action_B");
B.setRating("PG-13");
System.out.println(B);
System.out.printf("Late Fee for 10 days = $%d\n", B.calcLateFees(10));
System.out.println(C);
System.out.printf("Late Fee for 10 days = $%d\n", C.calcLateFees(10));
System.out.println(D);
System.out.printf("Late Fee for 10 days = $%d\n", D.calcLateFees(10));
System.out.println("D.equals(C) = " + D.equals(C));
System.out.println("D.equals(D) = " + D.equals(D));
System.out.printf("\nTest for Rental Class:\n\n");
Rental[] rentalList = new Rental[5];
rentalList[0] = new Rental(41056008, 3, A);
rentalList[1] = new Rental(41056009, 5, B);
rentalList[2] = new Rental(41056008, 7, C);
rentalList[3] = new Rental(41056007, 6, D);
rentalList[4] = new Rental(8, B);
System.out.printf("Total amount of late fees = %d\n",
Rental.lateFeesOwed(rentalList));
}
}
MPAA rating 只有 G,PG,PG-13,R,NC-17
以及尚未分級 NR(Not Rated) or UR(Unrated)
所以rating可以修改為
if(Rating != "G" || Rating != "PG" ||
Rating != "PG-13" || Rating != "R" ||
Rating != "NC-17"){ System.out.println("Rating error");
System.exit(0);
}