Write a Java program named UseLoanItem that uses an abstract LoanItem class and
Book and DVD subclasses, to display the item type, item name and rental price of
two (2) separate loan items, one (1) of each type. Use constructors, with appropriate
arguments, in each of the classes. Include get and set methods, at least one (1) of
which must be abstract. Also include an abstract Print method in LoanItem that
returns a String for printing. Ensure you include implementation code for the Print
method in each of the subclasses
import java.util.ArrayList;
abstract class LoanItem {
private String name;
private double rentalPrice;
public LoanItem() {
}
public LoanItem(String name, double rentalPrice) {
this.name = name;
this.rentalPrice = rentalPrice;
}
public abstract String Print();
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the rentalPrice
*/
public double getRentalPrice() {
return rentalPrice;
}
/**
* @param rentalPrice the rentalPrice to set
*/
public void setRentalPrice(double rentalPrice) {
this.rentalPrice = rentalPrice;
}
}
class Book extends LoanItem {
public Book() {
}
public Book(String name, double rentalPrice) {
super(name, rentalPrice);
}
@Override
public String Print() {
return "Type: Book\n" + "Name: " + getName() + "\n" + "Rental price: " + getRentalPrice() + "\n";
}
}
class DVD extends LoanItem {
public DVD(String name, double rentalPrice) {
super(name, rentalPrice);
}
@Override
public String Print() {
return "Type: DVD\n" + "Name: " + getName() + "\n" + "Rental price: " + getRentalPrice() + "\n";
}
}
public class UseLoanItem {
public static void main(String[] args) {
ArrayList<LoanItem> loanItems=new ArrayList<LoanItem>();
loanItems.add(new Book("C++",50));
loanItems.add(new DVD("Java",150));
for(int i=0;i<loanItems.size();i++) {
System.out.print(loanItems.get(i).Print());
}
}
}
Comments
Leave a comment