从java中的函数返回double和int值

时间:2014-11-22 07:29:57

标签: java

我希望从GUI中的函数接收两个值,其中一个是double,另一个是int类型。 在我的支票利润界面中,我希望有两个变量的值,即profitAmount和quantityItemSold,由类register.java中计算的值更新,因为我实现了java对象是通过引用传递所以我创建了Double profitAmount和Integer quantityItemSold的对象通过各自的对象将它们传递给gui的功能。

课程列表

// checkProfitOfItemInterface.java

if(checkButton==btnNewButtonCheck){

    shop.checkProfit(jTextFieldItemCode.getText(),jTextFieldDate.getText(),profitAmount,quantityOfItemSold);
    System.out.println("Quantity:  "+ quantityOfItemSold + "  Profit: "+ profitAmount); 
    //model.addRow(arg0);
    model.addRow(new Object[]{"s","s",quantityOfItemSold,"s",profitAmount});
}

// shop.java

public void checkProfit(String itemCode, String date, Double profitAmount, Integer quantityOfItemSold) {

    int itemCodeInt = Integer.parseInt(itemCode);
    ItemDescription objItemDescription =itemDescriptions.get(itemCodeInt); 
    register.checkProfit(itemCode,objItemDescription,date,profitAmount,quantityOfItemSold);

}

// register.java

public void checkProfit(String itemCode, ItemDescription itemDescription, String enteredDate,Double profitAmount, Integer quantityOfItemSold1) {

    int itemCodeInt = Integer.parseInt(itemCode);

    //GET no of sales being saved in register
    int noOfSales = sales.size();
    double salePrice=0;
    double purchasePrice =0;

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    Date saleDate=null;
    Date enteredDateToCheckProfitFrom = null;


    int quantityOfItemSold =0;


    for(int i=0;i<noOfSales;i++){

        Sale objSale= sales.get(i);

        //get date of required sale 
        saleDate =  objSale.getDateOfSale();
        try {
            enteredDateToCheckProfitFrom = sdf.parse(enteredDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //compared entered date to required sale date
        int value = enteredDateToCheckProfitFrom.compareTo(saleDate);

        if(value >= 0){

        quantityOfItemSold += objSale.getCountOfItemsInSale(itemCodeInt);
        }

    }

    salePrice =itemDescription.getSalePrice();
    purchasePrice = itemDescription.getPurchasePrice();

    double profitOnSaleOfOneItem = salePrice - purchasePrice;

    double totalProfit = quantityOfItemSold * profitOnSaleOfOneItem;

    profitAmount = totalProfit;
    quantityOfItemSold1 = quantityOfItemSold;

    System.out.println("Quantity:  "+ quantityOfItemSold1 + "  Profit: "+ profitAmount);

}

1 个答案:

答案 0 :(得分:2)

听起来你想使用Pair类来保存Double和Integer对象。

Generic pair class