Skip to content

Commit 27982b2

Browse files
Merge pull request #417 from Shalavya8103/shalavya8103
Create stock_management.java
2 parents 1b258e4 + 5668aba commit 27982b2

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

stock_management.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import java.util.ArrayList;
2+
import java.util.Scanner;
3+
4+
public class StockManagementSystem {
5+
6+
public static void main(String[] args) {
7+
Scanner input = new Scanner(System.in);
8+
9+
// create an empty arraylist to hold the stocks
10+
ArrayList<Stock> stocks = new ArrayList<>();
11+
12+
// display the menu and ask for user input
13+
int choice;
14+
do {
15+
System.out.println("1. Add stock");
16+
System.out.println("2. Remove stock");
17+
System.out.println("3. View stock");
18+
System.out.println("4. Exit");
19+
System.out.print("Enter your choice: ");
20+
choice = input.nextInt();
21+
22+
switch (choice) {
23+
case 1:
24+
// ask for stock details
25+
System.out.print("Enter the stock name: ");
26+
String name = input.next();
27+
System.out.print("Enter the stock quantity: ");
28+
int quantity = input.nextInt();
29+
System.out.print("Enter the stock price: ");
30+
double price = input.nextDouble();
31+
32+
// create a new Stock object and add it to the arraylist
33+
Stock newStock = new Stock(name, quantity, price);
34+
stocks.add(newStock);
35+
36+
System.out.println("Stock added successfully");
37+
break;
38+
39+
case 2:
40+
// ask for the stock name and quantity to remove
41+
System.out.print("Enter the stock name: ");
42+
String stockName = input.next();
43+
System.out.print("Enter the quantity to remove: ");
44+
int removeQuantity = input.nextInt();
45+
46+
// loop through the stocks to find the matching stock
47+
boolean stockFound = false;
48+
for (Stock stock : stocks) {
49+
if (stock.getName().equals(stockName)) {
50+
// if the stock is found, remove the specified quantity
51+
stockFound = true;
52+
stock.removeQuantity(removeQuantity);
53+
System.out.println("Stock removed successfully");
54+
break;
55+
}
56+
}
57+
if (!stockFound) {
58+
System.out.println("Stock not found");
59+
}
60+
break;
61+
62+
case 3:
63+
// display the list of stocks
64+
System.out.println("Current stocks:");
65+
for (Stock stock : stocks) {
66+
System.out.println(stock.toString());
67+
}
68+
break;
69+
70+
case 4:
71+
System.out.println("Exiting the program...");
72+
break;
73+
74+
default:
75+
System.out.println("Invalid choice, try again.");
76+
break;
77+
}
78+
79+
} while (choice != 4);
80+
81+
input.close();
82+
}
83+
}
84+
85+
class Stock {
86+
private String name;
87+
private int quantity;
88+
private double price;
89+
90+
public Stock(String name, int quantity, double price) {
91+
this.name = name;
92+
this.quantity = quantity;
93+
this.price = price;
94+
}
95+
96+
public String getName() {
97+
return name;
98+
}
99+
100+
public int getQuantity() {
101+
return quantity;
102+
}
103+
104+
public double getPrice() {
105+
return price;
106+
}
107+
108+
public void removeQuantity(int quantityToRemove) {
109+
if (quantityToRemove <= quantity) {
110+
quantity -= quantityToRemove;
111+
} else {
112+
System.out.println("Not enough quantity to remove");
113+
}
114+
}
115+
116+
public String toString() {
117+
return name + ", quantity: " + quantity + ", price: $" + price;
118+
}
119+
}

0 commit comments

Comments
 (0)