gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:39:36 26 4
gpt4 key购买 nike

我想从 GUI 中的函数接收两个值,其中一个是 double,另一个是 int 类型。在我的检查利润接口(interface)中,我希望有两个变量的值,即profitAmount和quantityItemSold,以通过类register.java中计算的值进行更新,因为我实现java对象是通过引用传递的,所以我制作了DoubleprofitAmount和IntegerquantityItemSold的对象和通过各自的对象将它们传递给 gui 的函数。

类(class)列表

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

}

//注册.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);

}

最佳答案

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

Generic pair class

关于java - 从 java 中的函数返回 double 和 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com