-1

I am writing a program that takes names, dates and numbers from a user as many times as they enter it and then adds up the numbers that were entered. If you look at the third for loop you will see where I am trying to add. I tried doing total = cust[i].amount + total; but I cannot access amount for some reason.

This is for an assignment.(I know were not supposed to post homework questions on here but I'm stumped.)The only data member Customer is to have is name

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

        double total = 0;

        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 9");
            choice = s.nextInt();
            switch (choice){
            case 1:
                cust [cnt] = new Service();
                break;
            case 2:
                cust [cnt] = new Purchaser();
                break;
            default:
                break;
            }
        }
        for(int i=0; i < cnt; i++){
            if(cust[i]!= null)
            cust[i].showData();
        }
        for(int i=0; i < cnt; i++ ){
            total = cust[i].amount + total;
        }
            s.close();

    }
}
interface Functions {
    public void getData();
    public void showData();
}
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);

    }   
}
class Service extends Customer {
    protected String date;
    protected 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);
    }
user278153
  • 75
  • 8

3 Answers3

4

There's no field amount in the class Customer.

  • 1
    +1. I would just add that maybe the OP forgot that `amount` is in the `Service` class which extends `Customer`. The OP might want to learn about inheritance. – Nico Jan 30 '14 at 03:00
  • I realize that it is in the service class. Is there anyway to access it with the cust[i] object? – user278153 Jan 30 '14 at 03:03
  • 2
    If you don't want to change the inheritance structure, just add a method getAmount() in your interface. The downside is that you'll be forced to implement this method in Purchase as well. – user3173787 Jan 30 '14 at 03:06
  • Implementing the method in the Purchase would be a good idea since the data coming in is the same representation – BlackHatSamurai Jan 30 '14 at 05:07
2

There are several issues, but you should

// Use one loop...
for(int i=0; i < cnt; i++){
  if(cust[i]!= null) { // <-- check for null.
    cust[i].showData();
    /* or
    if (cust instanceof Service) {
      total += ((Service)cust[i]).amount; // <-- assuming a public amount 
                                   // field in Service. This is not a good
                                   // practice, but your question is difficult to 
                                   // answer.
    }
    */
    total += cust[i].getAmount(); // <-- a method.
  }
}

That is add a getAmount to your interface if you want to get the amount (or, to make this code work - change the access modifier and add a public field named amount to Customer and remove the shadowing fields in your subclasses).

Or, you can learn about Collections (which I strongly recommend you do anyway) - and actually use the correct storage method - perhaps ArrayList.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

This is one solution that I could come up with, where we add a getAmount method to the interface:

import java.util.Scanner;


public class home {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        final int MAX = 2;
        Customer [] cust = new Customer[MAX];
        int choice = 0;
        int cnt;

        double total = 0;

        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 9");
            choice = s.nextInt();
            switch (choice){
            case 1:
                cust [cnt] = new Service();
                break;
            case 2:
                cust [cnt] = new Purchaser();
                break;
            default:
                break;
            }
        }
        for(int i=0; i < cnt; i++){
           if(cust[i]!= null)
           cust[i].showData();
           total = cust[i].getAmount() + total;
        }
            s.close();

    }
}

abstract class Customer implements Functions {
    protected String name;


}

import java.util.Scanner;


class Service extends Customer {
    protected String date;
    protected 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 double getAmount(){
        return this.amount; 
    }
    public void showData() {
        System.out.printf("Customer name: %s The date is: %s, the Amount owed is: %.2f\n",name, date, amount);
    }

}


    import java.util.Scanner;

class Purchaser extends Customer {
    protected double payment;

    public Purchaser(){
        getData();
    }

    public double getAmount(){
        return this.payment; 
    }
    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);

    }   
}



interface Functions {
    public void getData();
    public void showData();
    public double getAmount();
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156