我在比较从另一个类导入的 myProduct.setRefno(product.getRefno()) 的属性以及描述、价格和数量时遇到问题。我需要能够输入 arefno
并且如果篮子容器中存在参考编号,那么我只添加数量而不是所有项目详细信息:
目前程序的工作方式如下:
ref1 description1 price1 1(qty)
ref2 description2 price2 1(qty)
ref1 description1 price1 1(qty)
但是我希望它是:
ref1 description1 price1 2(qty)
ref2 description2 price2 1(qty)
如果它是相同的 refno,那么它只添加数量。
public class Zapper {
public static void main(String[] args) throws ItemException {
System.out.println("\n\nThis is Purchases\n\n");
Products stock=new Products();// Define Variable
Products basket=new Products();// Define Variable
Purchase product;
String refno;
int offer;
int count;
int grandtotal;
char option;//char variable option
boolean finished=false;//variable "boolean" set
while (!finished) {
try {
option=Console.askOption("\n A)dd P)rint R)emove Q)uit");
stock.open("stock.lin");
switch (option) {
case 'A':
product= new Purchase();
refno= Console.askString("Enter Ref No:..");
product=(Purchase)stock.find(refno);//cast operator
if ( product == null) {
System.out.println("Cannot find Ref No");
} else {
product.print("");
Purchase myProduct = new Purchase();
myProduct.setRefno(product.getRefno());
myProduct.setDescription(product.getDescription());
myProduct.setPrice(product.getPrice());
myProduct.setQty(1);
myProduct.setOffer(product.getOffer());
basket.add(myProduct);//add the value of item into Container stock
}
break;//end of case statement Add
case 'R':
refno= Console.askString("Enter Ref No:..");
Product myProduct = new Product();
myProduct=(Purchase)basket.find(refno);//cast operator
myProduct.setQty(1);
if ( myProduct == null)
System.out.println("Cannot find Ref No");
else {
basket.remove(myProduct);
basket.print("");
}
break;//end of case statement Find
case 'P':
basket.print("\nYou have purchased...");
break;//end of case statement Print
case 'Q':
finished=true;
break;//end of case statement "Q"
case '\0':/*Do Nothing*/
break;//end of case statement "Do Nothing"
default:
System.out.println("Error: Invalid Option ");
break;//end of case statement default
}
} catch (ItemException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
System.out.println("\n\nPurchases Finished \n\n");
}
}