I'm almost done with a Java program but I'm having a snag compiling. This program is a demonstration on inheritance, with 4 classes (including a driver) but I'm getting 4 errors on the aforementioned driver. I also have a problem with making the program general (as to allow user input) as the data I'm trying to read in are of different types. I think I need to use valueOf somewhere, but we haven't gotten that far in my class. I apologize for the length.
Here are the specifications:
Define a class named Payment that contains an instance variable of type double that stores the amount of the payment and appropriate accessor and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Next, defi ne a class named CashPayment that is derived from Payment.
This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s). Define a class named CreditCardPayment that is derived from Payment . This class should contain instance variables for the name on the card, expiration date, and credit card number.
Include appropriate constructor(s). Finally, redefine the paymentDetails method to include all credit card information in the printout. Create main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each. Be sure to make the program general so that a user can enter data!!!
Here is one of the error messages:
C:\Program Files\Java\jdk1.8.0_60\bin\ghp3driver.java:21: error: incompatible types: String cannot be converted to Payment
and here is the code...
import java.util.*; //new file/class
class ghp3driver {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
Payment cash1 = user_input.next();
Payment cash2 = user_input.next();
Payment credit1 =
user_input.next();
Payment credit2 =
user_input.next();
System.out.println("Cash 1 details:");
cash1.paymentDetails();
System.out.println();
System.out.println("Cash 2 details:");
cash2.paymentDetails();
System.out.println();
System.out.println("Credit 1 details:");
credit1.paymentDetails();
System.out.println();
System.out.println("Credit 2 details:");
credit2.paymentDetails();
System.out.println();
}
}
import java.util.*; //NEW FILE/CLASS
public class Payment {
private double amount;
public Payment() { //constructor
amount = 0.0;
}
public Payment(double paymentAmount) { //initializes payment amount
amount = paymentAmount;
}
public void setPayment(double paymentAmount) {
amount = paymentAmount;
}
public double getPayment() {
return amount;
}
public void paymentDetails() {
System.out.println("The payment amount is " + amount);
}
}
class CreditCardPayment extends Payment {
private String date, name, ccn;
public CreditCardPayment(double amount, String date, String name, String ccn) {
super(amount);
this.date = date;
this.name = name;
this.ccn = ccn;
}
public void paymentDetails() { //printing
System.out.println("Name on card: " + this.getName());
System.out.println("Expiration date: " + this.getDate());
System.out.println("Credit card number: " + this.getCcn());
System.out.println("The payment by credit card amount is " + getPayment());
}
public String getCcn() {
return ccn;
}
public void setCcn(String ccn) {
this.ccn = ccn;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CashPayment extends Payment {
public CashPayment(double paymentAmount) {
super(paymentAmount);
}
public void paymentDetails() {
System.out.println("The payment cash amount is " + this.getPayment());
}
}