在Java中将ArrayList作为参数传递

在Java中将ArrayList作为参数传递,java,class,oop,arraylist,parameters,Java,Class,Oop,Arraylist,Parameters,您可以尝试此操作,并在必要时进行修改:) import java.util.ArrayList; 导入java.util.Scanner; 公共类目录{ 静态ArrayList customerlist=新ArrayList(); 静态ArrayList itemlist=新建ArrayList(); 公共静态无效菜单(){ 扫描仪输入=新扫描仪(系统输入); 试一试{ System.out.println(“[1]添加客户”); System.out.println(“[2]显示总成本”);

您可以尝试此操作,并在必要时进行修改:)

import java.util.ArrayList;
导入java.util.Scanner;
公共类目录{
静态ArrayList customerlist=新ArrayList();
静态ArrayList itemlist=新建ArrayList();
公共静态无效菜单(){
扫描仪输入=新扫描仪(系统输入);
试一试{
System.out.println(“[1]添加客户”);
System.out.println(“[2]显示总成本”);
System.out.println(“[3]退出”);
系统输出打印(“选择一个:”;
int num=in.nextInt();
开关(num){
案例1:AddCustomer();//添加客户
打破
案例2:DisplayTotalCost();//显示总成本
打破
案例3:系统退出(0);
默认:中断;
}   
}
捕获(例外e){
System.out.println(“错误:+e”);
}   
}
公共静态void AddCustomer(){
扫描仪输入=新扫描仪(系统输入);
person addcustomer=新的person();
items additem=新项目();
试一试{
System.out.println(“输入客户名称:”);
addcustomer.name=in.nextLine();
System.out.println(“输入客户地址:”);
addcustomer.Address=in.nextLine();
System.out.println(“输入客户联系号码:”);
addcustomer.contactnumber=in.nextLine();
customerlist.add(addcustomer);
}
捕获(例外e){
e、 printStackTrace();
}
addItem();
菜单();
}
公共静态void addItem(){
扫描仪输入=新扫描仪(系统输入);
person addcustomer=新的person();
items additem=新项目();
addcustomer.totalamount=.0;
试一试{
System.out.println(“输入项目名称:”);
additem.itemname=in.nextLine();
System.out.println(“输入项目数量:”);
additem.quantity=in.nextInt();
System.out.println(“输入项目价格:”);
additem.price=in.nextDouble();
itemlist.add(additem);
} 
捕获(例外e){
e、 printStackTrace();
} 
additem.total=(additem.quantity*additem.price);
系统输出打印项次(“总成本:+附加项总计”);
}
公共静态无效显示总成本(){
System.out.println(“名称”+“\t”+“总成本”);

对于(int i=0;icase 2:DisplayTotalCost(customers);有错误,客户是带红色下划线的。您在哪里声明了
客户
?您的代码中有太多的弱点。public void DisplayTotalCost(ArrayList customers){items item=new items();person customer=new person();System.out.println(“Name”+“\t”+“Total Cost”);for(int i=0;ihuh,仅方法
DisplayTotalCost()
认识到
客户
。您应该在所有方法中使用
客户列表
,而不是为每个方法声明一个。谢谢@RafaEl,还有一件事我要把addcustomer.totalamount=addcustomer.totalamount+additem.total放在哪里来合计所有additem.total?
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();
    }
}
public class person {

        String name;
        String Address;

            String contactnumber;
        Double totalamount;
}
public class items {

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

}
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();
}
}