In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:
Inheritance
The protected modifier
The super Reference
Abstract class NumberFormat/DecimalFormat Wrapper classes
ArrayList
In Assignment #5, you will need to make use of inheritance by creating a class hierarchy for Lamps. see image.
Lamp is an abstract class, which represents the basic attributes of any lamp to be sold. It is used as the root of the lamp hierarchy. It has the following attributes (should be protected):
Attribute name, Attribute type, Description
quantity, int, The quantity (number) of lamps
unitPrice, double, The price of one lamp unit
totalPrice, double, The total price of lamp (quantity*unitPrice)
lampName, String, the name of lamp
The following constructor method should be provided to initialize the instance variables.
public Lamp(String lName, double someUnitPrice, int q)
The instance variable totalPrice is initialized to 0.0, lampName is initialized to the value of first parameter, unitPrice is initialized to the value of the second parameter, and quantity is initialized to the value of the third parameter.
The following accessor method should be provided for lampName :
public String getLampName()
The Class Lamp also has an abstract method (which should be implemented by its child classes, FluorescentLamp and LEDLamp) to compute the totalPrice of the lamp:
public abstract void computeTotalPrice();
The following public method should be provided:
public String toString()
toString method returns a string of the following format:
\nLamp Name:\t\tarc896\n
Quantity:\t\t2\n
Unit Price:\t\t$99.99\n
Total Price:\t\t$0.00\n
You should make use of the NumberFormat class (in java.text package) to format the total price in the dollar format and the unit price using 2 digits after their decimal point.
FluorescentLamp class
FluorescentLamp is a subclass of Lamp class. It represents a fluorescent lamp. It has the following attribute in addition to the inherited ones:
Attribute name, Attribute type, Description
recycleFee, Double, The recycling fee that will be used to dispose a fluorescent lamp
The following constructor method should be provided:
public FluorescentLamp (String lName, double someUnitPrice, int q, double fee)
The recycleFee is initialized to the value of the third parameter, and the constructor of the parent class Lamp should be called using the first and second parameters. Leave totalPrice as default value (defined in the parents constructor).
The following method should be implemented:
public void computeTotalPrice()
First, it computes the price of each lamp by adding the recycleFee to the unitPrice. Then compute the total price of the lamps (computed by quantity * lampPrice).
Also, the following method should be implemented:
public String toString()
The toString() method inherited from Lamp class should be used to create a new string, and display a fluorescent lamps information using the following format:
\nFluorescent Lamp\n
Recycle Fee:\t\t1.25\n
Lamp Name:\t\tarc896\n
Quantity:\t\t2\n
Unit Price:\t\t$99.99\n
Total Price:\t\t$0.00\n
This toString method should make use of the toString method of the parent class for the last four lines in the output above.
LEDLamp class
LEDLamp is a subclass of Lamp class. It represents an LED lamp. It has the following attributes:
Attribute name, Attribute type, Description
energySavingRebate, double, The energy saving rebate on the purchase of an LED lamp
The following constructor method should be provided:
public LEDLamp(String lName, double someUnitPrice, int q, double rebate)
The rebate is initialized to the value of the third parameter, and the constructor of the parent class Lamp should be called using the first and second parameters. Leave volume and totalPrice as their default value.
The following method should be implemented:
public void computeTotalPrice()
First, it computes the price of each lamp by discounting the energySavingRebate from the unitPrice. Note: energySavingRebate is given in percentage, for example, if there is a 5% rebate on a lamp then it will be given as 0.05. Then compute the total price of the lamps (computed by quantity * lampPrice).
Also, the following method should be implemented:
public String toString()
The toString() method inherited from the Lamp class should be used to create a new string, and display a LED lamps information using the following format:
\nLED Lamp\n
Energy Savings Rebate:\t5%\n
Lamp Name:\t\tcontemporary655\n
Quantity:\t\t4\n
Unit Price:\t\t$69.95\n
Total Price:\t\t$0.00\n
You will need to use NumberFormat (from package java.txt) to display Energy Savings Rebate amount as percentage(%).
This toString method should make use of the toString method of the parent class for the last four lines in the output above.
LampParser class
The LampParser class is a utility class that will be used to create a lamp object (either a fluorescent lamp object or a LED lamp object) from a parsable string. The LampParser class object will never be instantiated. It must have the following method:
public static Lamp parseStringToLamp(String lineToParse)
The parseStringToLamp method's argument will be a string in the following format: For a fluorescent lamp,
type/lampName/unitPrice/quantity/recycleFee
For a LED lamp,
type/lampName/unitPrice/quantity/energySavingRebate
A real example of this string would be:
Fluorescent/arc896/99.99/2/1.25
OR
LED/contemporary655/69.95/4/.05
This method will parse this string, pull out the information, create a new FluorescentLamp or LEDLamp object using their constructor with attributes of the object, and return it to the calling method. The type will always be present and always be either Fluorescent or LED. (It can be lower case or upper case) You may add other methods to the FluorescentLamp and LEDLamp class in order to make your life easier.
Assignment5 class
In this assignment, download Assignment5.java , save and use it for your assignment. You need to add code to this file. The parts you need to add are written in the Assignment5.java file, namely for the four cases "Add Lamp", "Add Compute Total Prices", "Search for Lamp", and "List Lamps".
All input and output should be handled here. The main method should start by displaying this updated menu in this exact format:
Choice\t\tAction\n
------\t\t------\n
A\t\tAdd Lamp\n
C\t\tCompute Total Prices\n
D\t\tSearch for Lamp\n
L\t\tList Lamps\n
Q\t\tQuit\n
?\t\tDisplay Help\n\n
Next, the following prompt should be displayed:
What action would you like to perform?\n
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase.
Add Lamp
Your program should display the following prompt:
Please enter a lamp information to add:\n
Read in the information and parse it using the lamp parser.
Then add the new lamp object (created by lamp parser) to the lamp list.
Compute Total Prices
Your program should compute total price for all lamps created so far by calling computeTotalPrice method for each of them in the lamp list.
After computing total prices, display the following:
total prices computed\n
Search for Lamp
Your program should display the following prompt:
Please enter a Lamp Name to search:\n
Read in the string and look up the lamp list, if there exists a lamp object with the same lamp name, then display the following:
lamp found\n
Otherwise, display this:
lamp not found\n
List Lamps
List all lamps in the lamp list. Make use of toString method defined in LEDLamp and FluorescentLamp classes.
A real example is looked like this:
Fluorescent Lamp
Recycle Fee: $1.25
Lamp Name: arc896
Quantity: 2
Unit Price: $99.99
Total Price: $0.00
LED Lamp
Energy Savings Rebate: 5%
Lamp Name: contemporary655
Quantity: 4
Unit Price: $69.95
Total Price: $0.00
If there is no lamp in the lamp list (the list is empty), then display following:
no lamp\n
Quit
Your program should stop executing and output nothing.
Display Help
Your program should redisplay the "choice action" menu.
Invalid Command
If an invalid command is entered, display the following line:
Unknown action\n