简体   繁体   中英

Pass ArrayList as parameter in Java

import java.util.ArrayList;
import java.util.Scanner;

public class inventory {

        ArrayList<person> customerlist = new ArrayList<person>();
        ArrayList<items> itemlist = new ArrayList<items>(); 

    public void menu (){
        Scanner in = new Scanner(System.in);
            try
                {
                    System.out.println("[1] Add Customer");
                    System.out.println("[2] Show Total Cost ");
                    System.out.println("[3] exit");
                    System.out.print("Choose one : ");
                    int num = in.nextInt();

                    switch(num){
                        case 1 : AddCustomer();//add customer
                            break;
                        case 2 : DisplayTotalCost(customers);//display total cost
                            break;
                        case 3 : System.exit(0);
                        default : break;
                        }   
                }
            catch(Exception e)
                {
                    System.out.println("error: " + e);
                }   
        }

    public void AddCustomer(){
        inventory function = new inventory();
        Scanner in = new Scanner(System.in);
        person addcustomer = new person();
        items additem = new items();

        System.out.println("Enter Customer Name:");
        try {
                addcustomer.name = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Customer Address:");
        try {
                addcustomer.Address = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Customer Contact Number:");
        try {
                addcustomer.contactnumber = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            }

        System.out.println("Do you want to add an item? [1]YES [2]NO");
        int x = in.nextInt();

        while (x == 1){
                function.addItem();

                System.out.println("Do you want to add an item? [1]YES [2]NO");
                x = in.nextInt();

        }

                System.out.println(addcustomer.totalamount);
                function.menu();
        }

    public void addItem(){
        Scanner in = new Scanner(System.in);
        person addcustomer = new person();
        items additem = new items();
        addcustomer.totalamount = .0;

        System.out.println("Enter Item Name:");
        try {
                additem.itemname = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Item Quantity:");
        try {
                additem.quantity = in.nextInt();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Item Price:");
        try {
                additem.price = in.nextDouble();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 

        additem.total = (additem.quantity*additem.price);
        System.out.println("Total Cost:" + additem.total);

    }

    public void DisplayTotalCost(ArrayList<person> customers){
        items item = new items();
        person customer = new person();

        System.out.println("Name" + "\t" + "Total Cost");
        for (int i=0; i<customerlist.size(); i++){
            System.out.println(customer.name + "\t");
        }

    }        

    public static void main(String[] args){
        inventory function = new inventory();
        function.menu();
    }
}

I want to pass the ArrayList<person> customers when calling the function DisplayTotalCost on the function menu. How do I do that? And how where will I put addcustomer.totalamount = addcustomer.totalamount + additem.total to sum all the additem.total ?

classes

public class person {

        String name;
        String Address;

            String contactnumber;
        Double totalamount;
}
public class items {

    String itemname;
    int quantity;
    Double price;
    Double total;

}

You may try this and modify where necessary :)

import java.util.ArrayList;
import java.util.Scanner;

public class inventory {
static ArrayList<person> customerlist = new ArrayList<person>();
static ArrayList<items> itemlist = new ArrayList<items>(); 

public static void menu (){      
    Scanner in = new Scanner(System.in);
    try{
        System.out.println("[1] Add Customer");
        System.out.println("[2] Show Total Cost ");
        System.out.println("[3] exit");
        System.out.print("Choose one : ");
        int num = in.nextInt();
        switch(num){
        case 1 : AddCustomer();//add customer
            break;
        case 2 : DisplayTotalCost();//display total cost
            break;
        case 3 : System.exit(0);
            default : break;
        }   
    }
    catch(Exception e){
        System.out.println("error: " + e);
    }   
}

public static void AddCustomer(){
    Scanner in = new Scanner(System.in);
    person addcustomer = new person();
    items additem = new items();
    try {
        System.out.println("Enter Customer Name:");
        addcustomer.name = in.nextLine();
        System.out.println("Enter Customer Address:");
        addcustomer.Address = in.nextLine();
        System.out.println("Enter Customer Contact Number:");
        addcustomer.contactnumber = in.nextLine();
        customerlist.add(addcustomer);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    addItem();
    menu();
}

public static void addItem(){
    Scanner in = new Scanner(System.in);
    person addcustomer = new person();
    items additem = new items();
    addcustomer.totalamount = .0;      
    try {
        System.out.println("Enter Item Name:");
        additem.itemname = in.nextLine();
        System.out.println("Enter Item Quantity:");
        additem.quantity = in.nextInt();
        System.out.println("Enter Item Price:");
        additem.price = in.nextDouble();
        itemlist.add(additem);
    } 
    catch (Exception e) {
        e.printStackTrace();
    } 
    additem.total = (additem.quantity*additem.price);
    System.out.println("Total Cost:" + additem.total);
}

public static void DisplayTotalCost(){
    System.out.println("Name" + "\t" + "Total Cost");
    for (int i=0; i<customerlist.size(); i++){
        System.out.println(customerlist.get(i).name + "\t"+itemlist.get(i).total);
    }
}        

public static void main(String[] args){
    menu();
}
}

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