Following completion of this task, students should be able to:
- Write Java applications using object and files.
Question 1
In this lab task you are required to write a Java application that reads and writes objects from/to a file.
Step 1
Declare and implement a class named InventoryItem based on the class diagram given below:
InventoryItem |
serialNum : int description : String costPrice : double sellingPrice : double quantityInHand : int |
+ InventoryItem()+ setSerialNum(int) : void+ setDescription(String) : void+ setCostPrice (double) : void+ setSellingPrice (double) : void+ setQuantityInHand(int) : void+ getSerialNum() : int+ getDescription() : String+ getCostPrice () : double+ getSellingPrice () : double+ getQuantityInHand() : int+ getTotalRevenue() : double |
Step 2
Write a Java application that performs the following operations:
- Prompt the user to enter information for several InventoryItem objects that are stored in an ArrayList.
- Save all the InventoryItem objects in a simple csv text file (i.e. comma-separated values format).
- Read all InventoryItem objects from the file and display them in tabular format including the projected total profit for each item and overall inventory as well.
The following shows an example simple csv text file format. Each line represent an objects data.
12534,office chairs,75.0,100.0,25
17654,coffee table,350.0,700.0,5
18834,dining table,1500.0,2750.0,10
14214,sofa set,2000.0,3500.0,7
Hint: For writing, you can write to file using the write() method from the PrintWriter class. For reading, you can use the BufferedReader method to read one line and store as String. Then use the split() method from class String to split each values before storing them in your object.
Reviews
There are no reviews yet.