简体   繁体   中英

Calling a method from another class that has its own variables

So I'm trying to call my method printDetails() from the Customer class in CustomerTest class. The values of the variables are not being recorded and I end up getting null for the name and an error for the array when I run it. I'm just not sure on how to get the values for the variables to be recorded and printed out.

public class Customer {   
    public String name;
    public int[] itemCost;    
    public void printDetails(){
        System.out.print("Great, here is your customer's purchase details: \n");
        System.out.print("Name:"+name);
        System.out.println();
        for (int i = 0; i < itemCost.length; i ++){
            System.out.print("Item Cost #" + (i+1) + " : ");
            System.out.print(itemCost[i] + "\n");       
            }
        int sum = IntStream.of(itemCost).sum();
        System.out.println("Total:" + sum);
    }
}

public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main.printDetails();
        }
    }
}

You save the values from you scanner in local variables and don't add them to the variables of the Customer.

Should look like this:

    Customer main = new Customer();
    Scanner scan = new Scanner(System.in);
    System.out.print("Do you want to create a customer? \n");
    String s = scan.next();
    if(s.equals("y") || s.equals("yes")){
        System.out.print("Ok, what's his/her name? \n");
        main.name = scan.next();    //HERE
        System.out.print("How many items is the customer buying? \n");
        int n = scan.nextInt();
        main.itemCost = new int[n];    //HERE
        for (int i = 0; i < main.itemCost.length; i ++){    //HERE
            System.out.print("Enter a value for item #"+(i+1) );
            System.out.printf("%n");
            int j = scan.nextInt();                              
            main.itemCost[i] = j;    //AND HERE
        }
    main.printDetails();
    }

You need to assign value for instance of Customer class.

Sample Code

 public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            main.name = name;             //added to assign value of name to Custommer's name.
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
             main.itemCost = new int[n];   //Here also need to initialize size for array.
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
                main.itemCost[i] = j; //added to assign value of itemCost to Custommer's itemCost.
            }
        main.printDetails();
        }
    }
}

now you can not get null value for name and itemCost .

hope it will help you.

Use Setters method in your Customer Class, so you can set the values from CustomerTest to Customer Class variables. Like this:

Customer Class:

public class Customer {   
public String name;
public int[] itemCost;    
public void printDetails(){
    System.out.print("Great, here is your customer's purchase details: \n");
    System.out.print("Name:"+name);
    System.out.println();
    for (int i = 0; i < itemCost.length; i ++){
        System.out.print("Item Cost #" + (i+1) + " : ");
        System.out.print(itemCost[i] + "\n");       
        }
    int sum = IntStream.of(itemCost).sum();
    System.out.println("Total:" + sum);
}
/**
 * Your Setters method...
 */
public void setName(String name){
    this.name = name;
}
public void setItemCost(int[] itemCost){
    this.itemCost= itemCost;
}
}

after that, you use it in CustomerTest like this:

CustomerTest Class:

// In the End part of your CustomerTestClass
main.setName(name);
main.setItemCost(itemCost);
main.printDetails();

You have created same variables in CustomerTest as in Customer class.

This won't work, as there is no binding between these local - CustomerTest class variables and actual Customer object properties.

You need to do the following to actually make the change reflect to all places :

class CustomerTest{

    public static void main(){
    ...
    ...
    Scanner sc = new Scanner();
    Customer obj = new Customer();
    obj.name = sc.next();
    ...
    ...
    //Test print function
    obj.printDetails();
    }
}

This will work! Point to remember: Making same name variables across class doesn't persist the value between objects!

您可以使用setter和getter来设置客户nameitemCost ,然后调用printDetails()方法,使用此方法时,您必须为每个对象创建客户对象,另一方面,可以通过printDetails()参数传递值像那个public void printDetails(String name, int[] itemCost) { // same logic here }然后使用main.printDetails(name, itemCost);调用它

You can declare a Customer constructor with name and itemCost as parameters, like this

public Customer(String name, int[] itemCost){
    this.name = name;
    this.itemCost = itemCost;
}

and in your test class you can buil a Customer object after the user input, like this

public class CustomerTest {
    public static void main(String[] args){
        Customer main; // = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main = new Customer(name, itemCost);
        main.printDetails();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM