A music store needs a system to manage its instrument inventory and sales. Write a Java program to handle the process of adding instruments and processing sales.
a) Write a class called Instrument to represent the details of a musical instrument with the following private attributes:
instrumentIdnamebrandpricequantityAvailable
b) Implement constructor overloading in Instrument:
- One constructor should accept all five parameters:
instrumentId,name,brand,price, andquantityAvailable. - Another constructor should accept only
instrumentId,name, andbrand, assigning default values:price = 0.0andquantityAvailable = 0.
c) Implement method overloading for a method called sellInstrument():
- One version should accept the number of units a customer wants to buy and display appropriate messages:
- If
quantityAvailableis less than the requested number, display:"Not enough units available. Please select fewer units or another instrument." - Otherwise:
- Deduct the requested number of units from
quantityAvailable. - Display:
"Sale successful!"
- Deduct the requested number of units from
- If
- Another version should take no parameters and sell 1 unit by default.
d) Implement a method called displayInstrumentDetails() to display the complete details of the instrument, including how many units are still available.
e) Write another Java class called MusicStoreApp to perform the following tasks:
I. Input details of two instruments from the user — one using the full constructor and one using the overloaded constructor.
II. Ask how many units a customer wants to buy for each instrument, and process the sale using both versions of the sellInstrument() method.
III. After all sale operations, display the details of both instruments using the displayInstrumentDetails() method.
🌟 Expected Output
Enter details for first instrument:
Instrument ID: I301
Name: Acoustic Guitar
Brand: Yamaha
Price: 50000.0
Quantity Available: 8
Enter details for second instrument:
Instrument ID: I302
Name: Digital Piano
Brand: Casio
How many units to buy from Instrument 1? 3
Sale successful!
Buying 1 unit from Instrument 2 by default:
Not enough units available. Please select fewer units or another instrument.
Final Instrument Details:
Instrument ID: I301
Name: Acoustic Guitar
Brand: Yamaha
Price: 50000.0
Quantity Available: 5
------------------------------
Instrument ID: I302
Name: Digital Piano
Brand: Casio
Price: 0.0
Quantity Available: 0
------------------------------