0

I need to call the method getAmount() from the Service class only. I do not want to add the values of the Purchaser class. Is there a way I can call the method explicitly from the Service class? I have put a ** where I am referring to.

  package prog24178.assignment;

    import java.util.Scanner;

    public class Assignment3 {

        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            final int MAX = 99999; // Max array size. 
            Customer [] cust = new Customer[MAX];
            int choice = 0;
            int cnt = 0;

            double total = 0;

            //loop to choose customer type and create a new object.
            for(cnt=0; cnt < MAX && (choice == 1 || choice ==2 || choice == 0); cnt++){
                System.out.println("For a Service customer type 1, for a Purchaser type 2, to terminate the program press any number besides 1 or 2");
                choice = s.nextInt();
                switch (choice){
                case 1:
                    cust [cnt] = new Service();
                    break;
                case 2:
                    cust [cnt] = new Purchaser();
                    break;
                default:
                    break;
                }
            }
            //loop to print the entries
            for(int i=0; i < cnt; i++){
                if(cust[i]!= null)
                cust[i].showData();
            }
            //loop to print the total for the service objects.**THIS IS LOOP I AM //REFFERING TO
            for(int i=0; i < cnt; i++ ){
                if(cust[i]!= null)
                total = cust[i].getAmounts() + total;
            }
                System.out.println("Monthly invoice total: " + total);
                s.close();

        }
    }
    interface Functions {
        public void getData();
        public void showData();
        public double getAmounts();
    }
    abstract class Customer implements Functions {
        protected String name;


    }
    class Purchaser extends Customer {
        protected double payment;

        public Purchaser(){
            getData();
        }

        public void getData() {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter the name of the customer");
            name = s.nextLine();
            System.out.println("Enter payment amount: ");
            payment = s.nextDouble();
        }
        public void showData() {    
            System.out.printf("Customer name: %s Payment amount is: %.2f\n",name,payment);
        }

//**I DO NOT WANT TO CALL THIS METHOD   
        public double getAmounts(){
            return this.payment; 
        }
    }
    class Service extends Customer {
        protected String date;
        public double amount;
        public Service () {
            getData();
        }

        public void getData() {     
            Scanner s = new Scanner(System.in);
            System.out.println("Enter the name of the customer");
            name = s.nextLine();
            System.out.println("Enter date of Service: ");
            date = s.nextLine();
            System.out.println("Enter the cost of Service: ");
            amount = s.nextDouble();
        }
        public void showData() {
            System.out.printf("Customer name: %s The date is: %s, the Amount owed is: %.2f\n",name, date, amount);
        }
        //**THIS IS THE METHOD I NEED TO CALL
        public double getAmounts(){
            return this.amount; 
        }
    }
4
  • Did you actually try using it and did you get an error ? Commented Jan 30, 2014 at 14:13
  • Yes I did and I dont get an error but it is adding the values from the customer class. I only want the values from the Service class added. Commented Jan 30, 2014 at 14:16
  • 1
    Well... as your Service class extends Customer class, what you're describing is actually a feature, and precisely a very important feature of OOP. If you don't want this, then you simply shouldn't extend the customer class Commented Jan 30, 2014 at 14:19
  • This is part of an assignment and the customer class must only have the one data member, String name; And must extend as part of the assignment. Commented Jan 30, 2014 at 14:23

1 Answer 1

1

Check your customer for type:

if (cust[i] instanceof Service) {
   total = cust[i].getAmounts() + total;
}

That takes care of the null check automatically as well.

Sign up to request clarification or add additional context in comments.

1 Comment

He means replace the if inside of your for loop with the if he wrote for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.