-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
116 lines (103 loc) · 4.33 KB
/
Copy pathMain.java
File metadata and controls
116 lines (103 loc) · 4.33 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.ArrayList;
import java.util.Scanner;
class MusicItem {
private String name;
private String artist;
private double price;
private int stock;
public MusicItem(String name, String artist, double price, int stock) {
this.name = name;
this.artist = artist;
this.price = price;
this.stock = stock;
}
public String getName() { return name; }
public String getArtist() { return artist; }
public double getPrice() { return price; }
public int getStock() { return stock; }
public void reduceStock(int qty) { this.stock -= qty; }
public void display() {
System.out.println("-----------------------------------");
System.out.println("Name : " + name);
System.out.println("Artist : " + artist);
System.out.println("Price : $" + price);
System.out.println("Stock : " + stock);
System.out.println("-----------------------------------");
}
}
class MusicStore {
private ArrayList<MusicItem> items = new ArrayList<>();
public void addItem(MusicItem item) {
items.add(item);
System.out.println("Item added successfully!");
}
public void displayItems() {
if (items.isEmpty()) { System.out.println("No items available."); return; }
for (MusicItem item : items) item.display();
}
public MusicItem searchItem(String name) {
for (MusicItem item : items) if (item.getName().equalsIgnoreCase(name)) return item;
return null;
}
public void purchase(String name, int qty) {
MusicItem item = searchItem(name);
if (item == null) { System.out.println("Item not found!"); return; }
if (item.getStock() < qty) { System.out.println("Not enough stock!"); return; }
double total = qty * item.getPrice();
item.reduceStock(qty);
System.out.println("Purchase successful!");
System.out.println("Total Amount: ₹" + total);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MusicStore store = new MusicStore();
while (true) {
System.out.println("\n===== MUSIC STORE MENU =====");
System.out.println("1. Add Music Item");
System.out.println("2. Display All Items");
System.out.println("3. Search Item");
System.out.println("4. Purchase Item");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice;
try {
choice = Integer.parseInt(sc.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid input.");
continue;
}
switch (choice) {
case 1 -> {
System.out.print("Enter music name: ");
String name = sc.nextLine();
System.out.print("Enter artist: ");
String artist = sc.nextLine();
System.out.print("Enter price: ");
double price = Double.parseDouble(sc.nextLine());
System.out.print("Enter stock: ");
int stock = Integer.parseInt(sc.nextLine());
store.addItem(new MusicItem(name, artist, price, stock));
}
case 2 -> store.displayItems();
case 3 -> {
System.out.print("Enter name to search: ");
String search = sc.nextLine();
MusicItem found = store.searchItem(search);
if (found != null) found.display();
else System.out.println("Item not found.");
}
case 4 -> {
System.out.print("Enter item name: ");
String purchaseName = sc.nextLine();
System.out.print("Enter quantity: ");
int qty = Integer.parseInt(sc.nextLine());
store.purchase(purchaseName, qty);
}
case 5 -> { System.out.println("Thank you!"); sc.close(); return; }
default -> System.out.println("Invalid choice!");
}
}
}
}