Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Last active July 22, 2022 02:26
Show Gist options
  • Save Yuhtin/9d9c6ce661e7a4a9f03e5d2aeb26264f to your computer and use it in GitHub Desktop.
Save Yuhtin/9d9c6ce661e7a4a9f03e5d2aeb26264f to your computer and use it in GitHub Desktop.
Calculate products prices
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of products:");
int products = scanner.nextInt();
List<Product> productsList = new ArrayList<>();
for (int i = 1; i <= products; i++) {
System.out.println("Insert Product #" + i + " code:");
int code = scanner.nextInt();
System.out.println("Insert Product #" + i + " quantity:");
double quantity = scanner.nextDouble();
System.out.println("Insert Product #" + i + " price:");
double price = scanner.nextDouble();
productsList.add(new Product(code, quantity, price));
}
double quantity = productsList.stream().mapToDouble(Product::getQuantity).sum();
double sum = productsList.stream().mapToDouble(Product::getFinalPrice).sum();
System.out.println("Products: " + productsList.size());
System.out.printf("Products quantity: %.0f %n", quantity);
System.out.printf("Total price: U$ %.2f", sum);
}
@AllArgsConstructor
@Data
public static class Product {
private final int code;
private final double quantity;
private final double price;
public double getFinalPrice() {
return quantity * price;
}
}
@Yuhtin
Copy link
Author

Yuhtin commented Jul 22, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment