Question

please write program in java and comment the code clearly. I am providing previous classes Media...

please write program in java and comment the code clearly. I am providing previous classes Media and payment codes to be used for this class.(its not a big code to write , it seems ,because i added previous classes , pleases help.

public class Media {

String name;
int year;
  
// a constructor which initializes the media with the provided name and publication year.
public Media(String name, int year) {
this.name = name;
this.year = year;
}
  
// retrieves the stored name of the media.
public String getName() {
return this.name;
}
  
// retrieves the stored year of the media.
public int getYear() {
return this.year;
}
  
@Override
public boolean equals(Object other)
{
// checking if both the object references are
// referring to the same object.
if(this == other)
return true;
// it checks if the argument is of the
// type Media by comparing the classes
// of the passed argument and this object.
// if(!(other instanceof Media)) return false;
if(other == null || other.getClass()!= this.getClass())
return false;

// type casting of the argument.
Media media = (Media) other;
// comparing the state of argument with
// the state of 'this' Object.
return (media.name == this.name && media.year == this.year);
}

// It is sufficent for this method to return getName().hashCode().
public int hashCode() {
return getName().hashCode();
}
  
// displays information about the media as a string, in the format "NAME (YEAR)".The Imitation Game(2014)
public String toString() {
return this.name + " (" + this.year + ")";
}
}

public class Payment {
String cardNo;
String name;
int expMonth;
int expYear;

public Payment(String cardNo, String name, int expMonth, int expYear) {
this.cardNo = cardNo;
this.name = name;
this.expMonth = expMonth;
this.expYear = expYear;
  
}
public String getCardNo() {
return cardNo;
}
public String getName() {
return name;
}
public int getExpMonth() {
return expMonth;
}
public int getExpYear() {
return expYear;
}
@Override public String toString() {
return "#" + this.cardNo + " (" + this.name + ")" + ", " + "exp " + this.expMonth +"/"+ this.expYear;
}
}

I need help with the claasess below
public class Rental

The class should contain the following:

  • public Rental(Media media, Payment payment, LocalDate today, double fee) rents the specified media using the provided payment method, with the rental perod beginning on the provided date, using the specified rental fee. When the object is created, the media is assumed to be rented out.
  • public Media getMedia() retrieves the media which has been rented.
  • public Payment getPayment() retrieves the payment method used to rent the media.
  • public LocalDate getRentDate() retrieves the date on which the media was rented.
  • public double getFee() retrieves the rental fee.
  • public double dropoff(LocalDate today) drops off the video on the current date and reports the total rental fee .If the video has not already been dropped off, then this method will set the return date as the current date passed in as a parameter, and then it will return the total fee as given by the getTotalFee method. If the video has already been return previously, then this method would have no additional effect, but would still report back the total fee.
  • public boolean isRented() returns true until the first time that dropoff is called, after which it returns false.
  • public int daysRented(LocalDate today) if the rental has already been returned, then this method will indicate the total number of days that it was rented. Otherwise, this method will report the total number of days from the rental date until the date provided as a parameter.
    Tip: the following call will allow us to find the span of days between two LocalDate objects, firstDate and secondDate:

    Period.between(firstDate, secondDate).getDays()

  • public double getTotalFee(LocalDate today) computes the total fee for the rental. In this case, it simply returns the flat fee which was passed in when the object was created.
  • @Override public String toString() returns information about the rental as a string, in the following format: "MEDIA, rented on DATE using PAYMENT"
  • . For example:

    The Imitation Game (2014) DVD [PG-13, 114 min], rented on 2019-09-15 using #0011223344556677 (George Mason), exp 10/2025

DailyRental Task:
public class DailyRental extends Rental

this class will contain the following methods:

  • public DailyRental(Media media, Payment payment, LocalDate today, double fee, double credit) a constructor which will initialize the rental with the given information about media, payment method, rental date, fee (to be interpreted as a daily fee), and promo credit.
  • public DailyRental(Media media, Payment payment, LocalDate today, double fee) a constructor which will initialize the rental with the given information about media, payment method, rental date, and fee (to be interpreted as a daily fee). The promo credit should default to zero in this version.
  • public double getCredit() retrieves the promo credit value.
  • @Override public double getTotalFee(LocalDate today) replaces the flat-rate total fee calculation with a daily fee calculation. The fee will be given by the product of the number of days the video has been rented (hint: we have a method for that) and the daily rental fee. Assume that the customer must be charged for at least one day, so if the video has been rented for less than one day, treat it as a 1-day rental. After the daily rental rate is calculated, the promo credit is subtracted, with a minimum bound of zero (if subtracting the promo credit results in a negative charge, this method will return zero). Thus, for example, if a video has been rented for 2 days with a fee of $1.50 and a credit of $0.50, then the method will return 2 * $1.50 - $0.50 = $2.50.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution)=>

Program:-

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

class Media {

String name;
int year;
  
// a constructor which initializes the media with the provided name and publication year.
public Media(String name, int year) {
this.name = name;
this.year = year;
}
  
// retrieves the stored name of the media.
public String getName() {
return this.name;
}
  
// retrieves the stored year of the media.
public int getYear() {
return this.year;
}
  
@Override
public boolean equals(Object other)
{
// checking if both the object references are
// referring to the same object.
if(this == other)
return true;
// it checks if the argument is of the
// type Media by comparing the classes
// of the passed argument and this object.
// if(!(other instanceof Media)) return false;
if(other == null || other.getClass()!= this.getClass())
return false;

// type casting of the argument.
Media media = (Media) other;
// comparing the state of argument with
// the state of 'this' Object.
return (media.name == this.name && media.year == this.year);
}

// It is sufficent for this method to return getName().hashCode().
public int hashCode() {
return getName().hashCode();
}
  
// displays information about the media as a string, in the format "NAME (YEAR)".The Imitation Game(2014)
public String toString() {
return this.name + " (" + this.year + ")";
}
}

class Payment {
String cardNo;
String name;
int expMonth;
int expYear;

public Payment(String cardNo, String name, int expMonth, int expYear) {
this.cardNo = cardNo;
this.name = name;
this.expMonth = expMonth;
this.expYear = expYear;
  
}
public String getCardNo() {
return cardNo;
}
public String getName() {
return name;
}
public int getExpMonth() {
return expMonth;
}
public int getExpYear() {
return expYear;
}
@Override public String toString() {
return "#" + this.cardNo + " (" + this.name + ")" + ", " + "exp " + this.expMonth +"/"+ this.expYear;
}
}

class Rental {
   private Media media;
   private Payment payment;
   private Date rentedDate;
   private Date returnDate;
   private double fees;
  
   public Rental(Media media, Payment payment, Date today, double fee) {
       this.media = media;
       this.payment=payment;
       this.rentedDate = today;
       this.fees = fee;
   }
  
  

   public Media getMedia() {
       return media;
   }

   public void setMedia(Media media) {
       this.media = media;
   }

   public Payment getPayment() {
       return payment;
   }

   public void setPayment(Payment payment) {
       this.payment = payment;
   }

   public Date getRenteddate() {
       return rentedDate;
   }

   public void setRenteddate(Date localdate) {
       this.rentedDate = localdate;
   }

   public double getFees() {
       return fees;
   }

   public void setFees(double fees) {
       this.fees = fees;
   }
  
   //This method checked whether the given media is returned or not if it not returned then set today date as returned date and returned the total fees
   //If it is already returned then just return the total fees
   public double dropoff(Date today){
   if(getReturnDate()==null) {
       setReturnDate(today);
       //getFees() gives one day price . To calculate total fees we need to multiply it with number of days.
       double totalfees =   getFees() * (int)daysRented(today);
       return fees;
   }else {
       double totalfees =   getFees()* (int)daysRented(today);
       return fees;
         
   }
     
   }
   public boolean isRented() {
       if(getReturnDate()==null) {
           return true;
       }
       return false;
   }
  
   public long daysRented(Date today) {
       if(getReturnDate()==null) {
           return findPeriod(getRenteddate(),today);
       }else {
           return findPeriod(getRenteddate(),getReturnDate());
       }
      
   }
  
   public Date getReturnDate() {
       return returnDate;
   }

   public void setReturnDate(Date returnDate) {
       this.returnDate = returnDate;
   }
  
   private long findPeriod(Date renteddate2, Date today) {
       // TODO Auto-generated method stub
       long diff = renteddate2.getTime() - today.getTime();
   return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
      
   }

   public String toString() {
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
       return "'"+this.getMedia()+", rented on "+formatter.format(this.getRenteddate())+" using "+ this.getPayment()+"'";
   }
}
public class DailyRental extends Rental {
   private double credit;
  
   //Constructor when the media have credit
   public DailyRental(Media media, Payment payment, Date today, double fee, double credit) {
       super(media,payment,today,fee);
       this.credit = credit;
   }
   //Constructor when the media doesn't have the credits
   public DailyRental(Media media, Payment payment, Date today, double fee) {
       super(media,payment,today,fee);
       this.credit=0;
   }
  
   public double getCredit() {
       return credit;
   }
  
   public void setCredit(double credit) {
       this.credit = credit;
   }
  
   //Returns the total fees
   public double getTotalFee(Date today) {
       double totalfees =   getFees()* (int)daysRented(today) - this.credit;
       return totalfees;
   }
  
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Media m = new Media("The Imitation Game",2014);
       Payment p1 = new Payment("#0011223344556677","George Mason",10,2025);
       DailyRental r1 = new DailyRental(m,p1,new Date(),5.0);
      
      
       Media m2 = new Media("Prison Break",2014);
       Payment p2 = new Payment("#0548448455748545","Himanshu Tomar",10,2028);
       DailyRental r2 = new DailyRental(m2,p2,new Date(),9.0);
      
  
      
      
       System.out.println(r1);
       System.out.println(r2);
   }
  

}
Code Output:-

'The Imitation Game (2014), rented on 29/02/2020 using ##0011223344556677 (George Mason), exp 10/2025'
'Prison Break (2014), rented on 29/02/2020 using ##0548448455748545 (Himanshu Tomar), exp 10/2028'

Hope you will like the answer.

Thanks.

Add a comment
Know the answer?
Add Answer to:
please write program in java and comment the code clearly. I am providing previous classes Media...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • I need help with my code. It keeps getting this error: The assignment is: Create a...

    I need help with my code. It keeps getting this error: The assignment is: Create a class to represent a Food object. Use the description provided below in UML. Food name : String calories : int Food(String, int) // The only constructor. Food name and calories must be // specified setName(String) : void // Sets the name of the Food getName() : String // Returns the name of the Food setCalories(int) : void // Sets the calories of the Food...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT