-
Notifications
You must be signed in to change notification settings - Fork 0
/
slip17B.java
68 lines (55 loc) · 2.05 KB
/
slip17B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Scanner;
class Product {
private int pid;
private String pname;
private double price;
private int qty;
public Product(int pid, String pname, double price, int qty) {
this.pid = pid;
this.pname = pname;
this.price = price;
this.qty = qty;
}
public double getTotalAmount() {
return price * qty;
}
public void displayProductDetails() {
System.out.println("Product ID: " + pid);
System.out.println("Product Name: " + pname);
System.out.println("Price: $" + price);
System.out.println("Quantity: " + qty);
System.out.println("Total Amount: $" + getTotalAmount());
}
}
public class ProductManagement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of products: ");
int n = scanner.nextInt();
Product[] products = new Product[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter details for product " + (i + 1) + ":");
System.out.print("Product ID: ");
int pid = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Product Name: ");
String pname = scanner.nextLine();
System.out.print("Price: $");
double price = scanner.nextDouble();
System.out.print("Quantity: ");
int qty = scanner.nextInt();
products[i] = new Product(pid, pname, price, qty);
}
System.out.println("Product Details:");
for (Product product : products) {
System.out.println("-------------------");
product.displayProductDetails();
}
double totalAmount = 0;
for (Product product : products) {
totalAmount += product.getTotalAmount();
}
System.out.println("Total Amount for all products: $" + totalAmount);
scanner.close();
}
}