-3

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());
     }
 }
Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
csheroe
  • 87
  • 3
  • 11

1 Answers1

0

Your statements

Payment cash1 = user_input.next( );
Payment cash2 = user_input.next( );
Payment credit1 =
user_input.next( );
Payment credit2 =
user_input.next( );

You cannot assign a string value to a different class Correct usage would be something like below

Payment cash1 = new Payment();
Payment cash2 = new Payment();
Payment credit1 = new Payment();
Payment credit2 = new Payment();

cash1.setPayment(new Double(user_input.next( )));
cash2.setPayment(new Double(user_input.next( )));
credit1.setPayment(new Double(user_input.next( )));
credit2.setPayment(new Double(user_input.next( )));

Also your code has a warning "Resource leak: 'user_input' is never closed" You need to work this one out.

My suggestion you need to study Java programming language more before you start putting questions to correct your code. Your are missing basic points which are very important.

Acewin
  • 1,657
  • 4
  • 17
  • 36
  • A payment expects a primitive *double*. – ChiefTwoPencils Oct 22 '15 at 22:04
  • Consider [this](http://stackoverflow.com/questions/9004370/what-is-the-difference-between-new-doublesomestring-and-double-parsedoublesom) as well... – ChiefTwoPencils Oct 22 '15 at 22:09
  • Thanks, unfortunately I'm in an intro to OOP class and we only recently began talking about security, hence the resource leak. Need to close that Scanner! Also, what do you use to compile and execute? I'm compiling fine but it's not running in CMD. I'm using TextPad and Java JDK 1.8.0_60. – csheroe Oct 22 '15 at 22:14
  • in that case go over the suggestions made by me and ChiefTwoPencils. He has suggested to go over this link http://stackoverflow.com/questions/9004370/what-is-the-difference-between-new-doublesomestring-and-double-parsedoublesom which is a nice explanation on Double primitive and Wrapper class usage – Acewin Oct 22 '15 at 22:16