I am having an issue with comparing the properties of myProduct.setRefno(product.getRefno()), imported from another class along with a description, price and qty. I need to be able to key in a refno
and if the reference number exists in the basket container, then I add only the qty rather than all the item details:
Actualmente el programa funciona de la siguiente manera:
ref1 description1 price1 1(qty)
ref2 description2 price2 1(qty)
ref1 description1 price1 1(qty)
Sin embargo, me gustaría como:
ref1 description1 price1 2(qty)
ref2 description2 price2 1(qty)
Si es el mismo refno, agrega solo la cantidad.
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");
}
}
Solo tiene que cambiar el add
método-en su Products
clase. Purchase
Supongamos que almacena sus objetos en una Lista en su Product
clase, podría verse así:
private List<Purchase> products;
public void add(Purchase product) {
String refNo = product.getRefno();
for (Purchase p : this.products) { //for every product
if (p.getRefno().equals(refNo)) { //if two refNumbers equals
p.setQty(p.getQty() + product.getQty()); //add the desired quantity
return; //force method to abort
}
}
this.products.add(product); //otherwise, add the new product
}
Aunque, tengo que decir que encuentro algunos de tus nombres de clase un poco inusuales. Recuerde que siempre deben dar una buena pista sobre lo que realmente representan, por ejemplo, su Purchase
clase se parece más a Product
. :)
Entonces, hay un sitio web que tiene un formulario de inicio de sesión. Quiero iniciar sesión y luego descargar un archivo. Al enviar el formulario, no solo se transmiten el nombre de usuario y la contraseña en la POST http, sino también un token ...
Estoy agregando una imagen en el encabezado de un documento de Word. Muestra un marco para la imagen y dice "la imagen no se puede mostrar actualmente". Si agrego texto al encabezado, muestra el texto, y si agrego el ...
Me gustaría hacer esto (reproducción mínima): String key = "foo"; Valor del objeto = nueva barra (); if (target instanceof Map <?,?>) {Map <?,?> map = (Map <?,?>) target; map.put (clave, valor) ...
De acuerdo, no puedo decirlo, pero necesito lo siguiente: C: \ Temp \ Something \ GroupName \ ... \ file.ts -> GroupName \ ... \ file.ts Quiero extraer la ruta de un carpeta hasta el final. Se me ocurrió esto ...