使用具有不同类的数组

时间:2016-09-24 16:30:11

标签: java arrays methods switch-statement

公共类Business_Software_MainMenu

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");


            }

        }

    }

}

公共类Business_Software_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

    }

}

尚未创建创建新产品和显示产品摘要的方法,因此请忽略这些方法。我的问题在于在一个方法中创建新客户端,然后使用另一种方法打印出客户端列表。现在,当我运行它时,案例4没有输出。我该怎么做才能解决这个问题,以便输出所有输入的客户端?

1 个答案:

答案 0 :(得分:1)

您正在每case创建新对象。在创建新实例时,您为该实例创建的所有非静态变量(clientnameclientbalance等)都是为该实例创建的,它们是全新的(数组不是&#t; t将您的数据ints初始化为0等。您需要做的是在所有这些变量之前添加static关键字,这将使它们与类本身绑定,而不是实例(在创建新实例时它们将保持不变) - 我不建议解。另一个解决方案 - 在Business_Software_Obj类中使您的MainMenu成为实例变量,将其放在main方法之前:

private Business_Software_Obj bso = new Business_Software_Obj();

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

case 1: client.client(); break;

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