-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.java
More file actions
75 lines (66 loc) · 2.3 KB
/
Copy pathProduct.java
File metadata and controls
75 lines (66 loc) · 2.3 KB
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
69
70
71
72
73
74
75
/* You are developing an e-commerce application and need to implement a Product class. The Product class should have private instance variables for the
product name, product ID, and price. Implement getter and setter methods for these variables, ensuring that the price cannot be negative. Additionally,
include a private variable to track the stock quantity and provide public methods to update the stock when a product is purchased or restocked.*/
// Manjil Basnet
public class Product {
private String productName;
private String productId;
private double price;
private int stockQuantity;
public Product (String productName, String productId, double price, int stockQuantity){
this.productName = productName;
this.productId = productId;
this.price = price;
this.stockQuantity = stockQuantity;
}
// setter
public void setProductName(String productName){
this.productName = productName;
}
public void setProductId(String productId){
this.productId = productId;
}
public void setPrice(double price) {
if (price >= 0) {
this.price = price;
}
}
// getter
public String getProductName() {
return productName;
}
public String getProductId() {
return productId;
}
public double getPrice() {
return price;
}
public void purchaseProduct(int quantity) {
if (quantity > 0 && stockQuantity >= quantity) {
stockQuantity -= quantity;
}
}
public void restockProduct(int quantity) {
if (quantity > 0) {
stockQuantity += quantity;
}
}
public int getStockQuantity() {
return stockQuantity;
}
public void printinfo(){
System.out.println("Product Name: " + getProductName());
System.out.println("Product Id: " + getProductId());
System.out.println("Price: " + getPrice());
System.out.println("Stock Quantity: " + getStockQuantity());
}
}
class Productimp{
public static void main(String[] args) {
Product product = new Product("Laptop", "P1001", 120000.0, 10);
product.printinfo();
product.purchaseProduct(2);
product.restockProduct(5);
System.out.println("Product Stock: " + product.getStockQuantity());
}
}