Java 使用具有不同类的数组

Java 使用具有不同类的数组,java,arrays,methods,switch-statement,Java,Arrays,Methods,Switch Statement,公共类业务\u软件\u主菜单 public static void main(String[] args) { // TODO Auto-generated method stub while (true){ System.out.println("MAIN MENU"); System.out.print("\n1. Create new client\n2. Create new product\n3. Create new invoice\

公共类业务\u软件\u主菜单

public static void main(String[] args) {
    // TODO Auto-generated method stub
    while (true){

        System.out.println("MAIN MENU");
        System.out.print("\n1. Create new client\n2. Create new product\n3. Create new invoice\n4. Summary by client\n5. Summary by product");
        System.out.print("\n\nEnter choice : ");    
        int menuchoice = Keyboard.readInt();
        System.out.print("\n");

        if (menuchoice>0 & menuchoice<6){

            switch (menuchoice){

            case 1:
                //create new client
                Business_Software_Obj client = new Business_Software_Obj();
                client.client();
                break;

            case 2:
                //create new product

            case 3: 
                //create new invoice
                Business_Software_Obj invoice = new Business_Software_Obj();
                invoice.invoice();
                break;

            case 4:
                //summary report by client
                Business_Software_Obj clientsummary = new Business_Software_Obj();
                clientsummary.clientsummary();
                break;

            case 5:
                //summary report by product

            default: 
                System.out.print("Invalid choice, try again");


            }

        }

    }

}
publicstaticvoidmain(字符串[]args){
//TODO自动生成的方法存根
while(true){
System.out.println(“主菜单”);
System.out.print(“\n1.创建新客户端\n2.创建新产品\n3.创建新发票\n4.按客户端汇总\n5.按产品汇总”);
System.out.print(“\n\n输入选项:”);
int menuchoice=Keyboard.readInt();
系统输出打印(“\n”);

如果(menuchoice>0&menuchoice您在每个
案例中创建新对象
。当您创建新实例时,该类中的所有非静态变量(
clientname
clientbalance
等)都是为该实例创建的,它们是全新的(数组没有您的数据,
int
被初始化为
0
等)。您需要做的是在所有这些变量之前添加
static
关键字,这将使它们绑定到类本身,而不是实例(创建新实例时它们将保持不变)-我不建议使用这种解决方案。另一种解决方案-将您的
Business\u Software\u Obj
作为
main菜单
类中的实例变量,将其置于
main
方法之前:

private Business\u Software\u Obj bso=new Business\u Software\u Obj();

然后在您的情况下使用该变量,不要再创建新对象,保持如下状态:

案例1:client.client();break;


在其他情况下也是如此。您已经拥有了该对象,它将在所有情况下共享。

在变量工作之前添加静态的第一个解决方案,但我无法使用首选解决方案。当我键入case1:client.client();,第一个客户端“无法解析”.Ye是我刚才提到的实例变量,您在这里使用它,所以您需要使用它的名称,如果您像在我的帖子中那样命名它(
bso
),那么您需要执行
bso.client()
,在
case2
中,您需要
bso.invoice()
等。现在它告诉我“无法对非静态字段bso进行静态引用”也就是说,因为main方法是静态的,所以将
static
添加到
bso
字段中,它就会工作。或者您可以创建
main菜单的一个实例,但我想这在这里不是必需的。记住,如果您使用该解决方案,那么解决方案1中的字段就不必是静态的(也不应该是静态的)。它使用了
私有静态业务_软件_objbso=新业务_软件_Obj();
。感谢您的帮助
public int clientnamenum = 0;
public int clientbalancenum = 0;

public String[] clientname = new String[1000];
public long[] clientbalance = new long[1000];

public void invoice(){

    // This method creates an invoice

    char answer;
    long sum = 0;
    int descnum = 0;
    int descnumloop;
    int pricenum = 0;

    String[] description = new String[100]; //creates an array for products/services
    int[] price = new int[100]; //creates an array for the prices of the items

    System.out.print("Enter invoice #\t\t: ");
    String invoicenum = Keyboard.readString(); //user inputs the invoice number

    System.out.print("Enter Date\t\t: ");
    String date = Keyboard.readString(); //user inputs the date of the invoice

    System.out.print("Enter client name\t: ");
    String recipient = Keyboard.readString(); //user inputs the recipient of the invoice

    do{

        System.out.print("\nDescription of service or product  : ");
        description[descnum++] =  Keyboard.readString(); //user inputs the description of the service/product       

        System.out.print("Price of service or product   (€)  : ");
        price[pricenum++] = Keyboard.readInt(); //user inputs the price of the item

        System.out.print("\nAdd another service/product? (Y/N) : ");
        answer = Keyboard.readChar(); //User chooses whether he wishes to add another item


        }while(answer=='Y' || answer=='y'); //loops if answer is yes

    System.out.println("\nInvoice # " + invoicenum); 
    System.out.println("Date : " + date);
    System.out.println("Bill to : " + recipient); 

    System.out.println("\nITEM\t\t\t\t€");


    for (descnumloop = 0; descnumloop < descnum; descnumloop++){
        System.out.print ("\n" + description [descnumloop]); //prints all item descriptions
        System.out.print ("\t\t\t\t" + price [descnumloop]); //prints all item prices
    }


    for (int i : price){
        sum += i; //calculates total of all items
    }

    System.out.print("\nTOTAL\t\t\t\t" + sum+"\n\n");

    return;

}

public void client(){

    //this method creates a new client

    System.out.print("Enter client name : ");
    clientname[clientnamenum++] = Keyboard.readString();

    System.out.print("Enter client opening balance : ");
    clientbalance[clientbalancenum++] = Keyboard.readLong();
    System.out.print("\n");

}

public void clientsummary(){

    System.out.println("Test");

    for (int clientnamenumloop = 0; clientnamenumloop < clientnamenum; clientnamenumloop++){
        System.out.print ("\n" + clientname[clientnamenumloop]); //prints all clients

    }

}