TransitPass.java fileThis is a inheritance question, I have to comeplete the mis
ID: 3737143 • Letter: T
Question
TransitPass.java fileThis is a inheritance question, I have to comeplete the missing method, the API link is: https://www.eecs.yorku.ca/course_archive/2017-18/W/2030Z/practice_tests/test3X/doc/test3/PrepaidPass.html, I attached PrepaidPass.java file and TransitPass.java file below:
PrepaidPass.java file:
import java.util.Date;
/**
* A transit pass that holds a prepaid amount of value.
* Every prepaid pass has a balance that is greater than or
* equal to zero.
*
*/
public class PrepaidPass extends TransitPass {
private int balance;
/**
* Initialize this pass so that its expiry date is
* equal to the given expiry date and its balance is equal
* to the given balance.
*
* @param expiryDate the expiry date for this pass
* @param balance the balance for this pass
* @throws IllegalArgumentException if balance is negative or zero
*/
public PrepaidPass(Date expiryDate, int balance) {
}
/**
* Returns the balance of this pass.
*
* @return the balance of this pass
*/
public int getBalance() {
}
/**
* Adds the given amount to the balance of this pass.
*
* @param amount an amount of value to add to the balance of this pass
* @pre. amount > 0
*/
public void add(int amount) {
}
/**
* Returns true if this pass is valid for the given
* date and fare. A pass is valid if its expiry date is
* after the given date and if its balance is greater than
* or equal to the fare amount.
*
* @param date the date of the trip
* @param fare the fare amount for the trip
* @return true if this transit pass is valid for the given
* trip date and fare amount, and false otherwise
* @pre. fare > 0
*/
@Override
public boolean isValid(Date date, int fare) {
}
/**
* Use this pass to pay for a trip on the given
* date and fare amount, and increment the number of times
* that this pass has been used. The fare amount is deducted from
* the balance of this pass.
*
* @param date the date of the trip
* @param fare the fare amount for the trip
* @throws RuntimeException if the pass is not
* valid for the given trip date and fare amount
* @pre. fare > 0
*/
@Override
public void payFare(Date date, int fare) {
}
}
TransitPass.java file:
import java.util.Date;
/**
* The parent class for different kinds of passes used to
* pay fares on a transit system. Every pass has an expiry
* date that can only be modified by using the setExpiryDate
* method. Every pass counts the number of times that it
* has been used to pay a fare.
*
*/
public abstract class TransitPass {
private int timesUsed;
private Date expiry;
/**
* Initialize this transit pass so that its expiry date is
* equal to the given expiry date.
*
* @param expiryDate the expiry date for this transit pass
*/
public TransitPass(Date expiryDate) {
this.timesUsed = 0;
this.setExpiryDate(expiryDate);
}
/**
* Returns a copy of the expiry date for this transit pass.
*
* @return a copy of the expiry date for this transit pass
*/
public final Date getExpiryDate() {
return new Date(this.expiry.getTime());
}
/**
* Sets the expiry date for this transit pass to the given
* date.
*
* @param expiryDate the expiry date for this transit pass
*/
public final void setExpiryDate(Date expiryDate) {
this.expiry = new Date(expiryDate.getTime());
}
/**
* Returns the number of times this pass has been used to
* pay a fare.
*
* @return the number of times this pass has been used to
* pay a fare
*/
public final int getTimesUsed() {
return this.timesUsed;
}
/**
* Returns true if this transit pass is valid for the given
* date and fare. A pass is valid if its expiry date is
* after the given date and if it can be used to pay
* the given fare amount.
*
* @param date the date of the trip
* @param fare the fare amount for the trip
* @return true if this transit pass is valid for the given
* trip date and fare amount, and false otherwise
* @pre. fare > 0
*/
public abstract boolean isValid(Date date, int fare);
/**
* Use this transit pass to pay for a trip on the given
* date and fare amount, and increment the number of times
* that this pass has been used.
*
* @param date the date of the trip
* @param fare the fare amount for the trip
* @throws RuntimeException if the transit pass is not
* valid for the given trip date and fare amount
* @pre. fare > 0
*/
public void payFare(Date date, int fare) {
this.timesUsed++;
}
}
Explanation / Answer
Implemented all the missing functions. Only PrepaidPass is modified. Have a look and leave a comment in case of doubts.
/////////////////////////////////////////////////////PrepaidPass.java/////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Date;
/**
* A transit pass that holds a prepaid amount of value. Every prepaid pass has a
* balance that is greater than or equal to zero.
*
*/
public class PrepaidPass extends TransitPass {
private int balance;
private Date expiryDate;
private static int counter = 0;
/**
* Initialize this pass so that its expiry date is equal to the given expiry
* date and its balance is equal to the given balance.
*
* @param expiryDate
* the expiry date for this pass
* @param balance
* the balance for this pass
* @throws IllegalArgumentException
* if balance is negative or zero
*/
public PrepaidPass(Date expiryDate, int balance) {
super(expiryDate);
if (balance <= 0) {
throw new IllegalArgumentException("There is not sufficient balance");
} else {
this.expiryDate = expiryDate;
this.balance = balance;
}
}
/**
* Returns the balance of this pass.
*
* @return the balance of this pass
*/
public int getBalance() {
return this.balance;
}
/**
* Adds the given amount to the balance of this pass.
*
* @param amount
* an amount of value to add to the balance of this pass @pre. amount
* > 0
*/
public void add(int amount) {
if (amount < 0) {
System.out.println("Amount to be added should be greater than 0");
} else {
balance += amount;
}
}
/**
* Returns true if this pass is valid for the given date and fare. A pass is
* valid if its expiry date is after the given date and if its balance is
* greater than or equal to the fare amount.
*
* @param date
* the date of the trip
* @param fare
* the fare amount for the trip
* @return true if this transit pass is valid for the given trip date and fare
* amount, and false otherwise @pre. fare > 0
*/
@Override
public boolean isValid(Date date, int fare) {
boolean isValid = false;
if (expiryDate.after(date) && balance >= fare) {
System.out.println("The card is still valid and there is enough money in card");
isValid = true;
} else {
System.out.println(" Either the card has expired or there is not enough money in card");
isValid = false;
}
return isValid;
}
/**
*
* Use this pass to pay for a trip on the given date and fare amount, and
* increment the number of times that this pass has been used. The fare amount
* is deducted from the balance of this pass.
*
* @param date
* the date of the trip
* @param fare
* the fare amount for the trip
* @throws RuntimeException
* if the pass is not valid for the given trip date and fare
* amount @pre. fare > 0
*/
@Override
public void payFare(Date date, int fare) {
if (expiryDate.after(date) && balance >= fare) {
balance -= fare;
System.out.println("Remaining Balance is: " + balance);
setCounter(getCounter() + 1);
} else {
String message = "Cant use the card. There is not enough balance in the card";
throw new RuntimeException(message);
}
}
/**
* @return the counter
*/
public static int getCounter() {
return counter;
}
/**
* @param counter
* the counter to set
*/
public static void setCounter(int counter) {
PrepaidPass.counter = counter;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.