Results 1 to 5 of 5
  1. #1
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default method to print values

    Im working on a homework problem and so far I got it to work but my problem is that I need to have it print through the a paymentDetails method and I cannot get it to work, the goal of the program is to have 3 classes, one payment parent class and a creditcardpayment and cash payment children class. The only part that I'm stuck on is having both children class use the paymentDetails method to display what their supposed to display. I have got this program to work using toString method in each child class but im confused on how to get this working with just one method in the parent class.

    Payment class
    Java Code:
    public class Payment {
        protected double PayAmmt;
    
        public Payment(){
            PayAmmt=0;
        }
    
        public Payment(double PayAmmt){
            this.PayAmmt=PayAmmt;
        }
    
        public double getPayAmmt() {
            return PayAmmt;
        }
    
        /*public String paymentDetails() {
            if(CashPayment==true){
                return "Payment amount " + PayAmmt;
    }
    
        }*/
    
        public String toString(){
        return "Payment Amount: "+PayAmmt;
    }
    
        public static void main(String[] args) {
            CashPayment cashPayment= new CashPayment(230);
            System.out.println(cashPayment);
            CreditCardPayment creditCardPayment= new CreditCardPayment(400, "John Doe", "2345345612345", "10/12/2018");
            System.out.println(creditCardPayment);
    
        }
    CashPayment
    Java Code:
    public class CashPayment extends Payment {
        public CashPayment(double PayAmmt)
        {
            super(PayAmmt);
        }
        public double getPayAmmt(){
            return PayAmmt;
        }
        @Override
        public String toString(){
            return
                    super.toString();
        }
    }


    Credit Card Payment
    Java Code:
    public class CreditCardPayment extends Payment{
        private String name;
        private String CCNum;
        private String ExpDate;
    
        public CreditCardPayment(double PayAmmt, String name, String CCNum, String ExpDate)
        {
            super(PayAmmt);
            this.name=name;
            this.CCNum=CCNum;
            this.ExpDate=ExpDate;
        }
    
        public String getName() {
            return name;
        }
    
        public String getCCNum() {
            return CCNum;
        }
    
        public String getExpDate() {
            return ExpDate;
        }
    
    
    
        @Override
        public String toString(){
            return "Name: "+name+" | Credit Card #: "+CCNum+" | Exp Date: "+ExpDate+" | "+
                    super.toString();
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: method to print values

    I cannot get it to work
    The only part that I'm stuck on is having both children class use the paymentDetails method to display what their supposed to display.
    Please post the current output, add some comments showing what is wrong with it and show what the output should be.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: method to print values

    Java Code:
    Payment Amount: 230.0
    Name: John Doe| Credit Card #: 2345345612345 | Exp Date: 10/12/2018 | Payment Amount: 400.0
    This is the output which is the correct output but this is only because it is outputting from the toString method in both sub classes and I have the paymentDetails method commented out, my goal is to use the paymentDetails method to output this.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: method to print values

    use the paymentDetails method to output this.
    Ok, what happened when you tried it?

    It looks like the paymentDetails method should be abstract and the classes that extend payment need to override it.
    Last edited by Norm; 09-28-2016 at 11:00 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: method to print values

    Finally got it to work, did what you said, thanks Norm once again

Similar Threads

  1. how to print results from void method into main method?
    By BookOfEliX in forum New To Java
    Replies: 5
    Last Post: 09-20-2015, 10:48 PM
  2. Replies: 4
    Last Post: 06-02-2013, 05:54 PM
  3. Replies: 1
    Last Post: 04-30-2013, 12:18 PM
  4. Replies: 0
    Last Post: 04-16-2012, 08:35 AM
  5. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •