2014-11-22 153 views
1

我想從GUI中的函數接收兩個值,其中一個是double,另一個是int類型。 在我的檢查利潤接口我想有兩個變量的值是profitAmount和quantityItemSold由在類register.java中計算的值更新,因爲我正在實現java對象是通過引用傳遞,所以我做了double profitAmount和Integer的對象quantityItemSold並通過各自的對象將它們傳遞給gui的函數。類的從java函數返回double和int值

列表

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

} 

回答

2

這聽起來像你想使用一對類來容納一個Double和Integer對象。

Generic pair class

+0

是不是的OOAD概念違反剛剛保持類recieving只有兩個變量。否則,該類保持空閒狀態。 – 2014-11-22 07:41:23

+1

你不能在java中做得更好。只要把它看作是元組的一個類比。 – dmitry 2014-11-22 07:43:03

+0

@AbdullahJahangirAbbasi - 不,它不是。沒有OOAD表示類需要複雜,用於很多事情等。 – 2014-11-22 12:35:30