Swinburne University of Technology
Faculty of Science, Engineering and Technology
COS70006
Lab Tutorial 8 - Due by 5.00pm. 30th of April, 2017.
To pass this exercise you must:
- Complete all the tasks below and submit a well-structured OO project
- Discuss your work with your tutor
- After completing the tutorial submit it to ESP (https://esp.ict.swin.edu.au) for assessment (No marks will be awarded if the work is not submitted before the due date)
- Include a printed copy of the completed and submitted tutorial work in your portfolio (due at end of semester)
What to submit to ESP:
- In one zip file
o The fully compiled Blue J Project
Individual Work
- You may talk to other students about the work but must write your own code. No tolerance for plagiarism.
You are required to modify your project 1 submission.
(Start with your solution for Lab 7)
• The design will now include persistence of Asset data.
• Persistence is to be achieved by writing and reading data from a text file in a comma delimited format.
The following changes need to be made to your classes.
IO_Support class
• Provide a method that can write from an ArrayList to file (method signature given below).
o public static void saveData(String fileName, ArrayList data)
• Provide a method that can be used to read an ArrayList from file (method signature given below).
o public static ArrayList readData(String fileName)
AssetPool class
• Use the following format to save asset data in a file.
o Phone data must be recorded starting with a ‘P’
o Laptop data must be recorded starting with a ‘L’
o E.g:
Unassigned Laptop:
L,,, , ,
Unassigned Phone:
P,,, , , ,
Assigned Laptop:
L,,, , , ,, , , ,
Assigned Phone:
P,,, , , ,,, , , ,
• Provide a method writeAllAssetsToFile()
This method must:
o Create a temporary ArrayList to collect the data
o Loop through each asset in its collection
o Call saveAsset on each asset (pass the temporary ArrayList that was created to the saveAsset method)
o Call IO_Support to write the ArrayList to file (AssetData.txt)
• Provide a method readAllAssetsFromFile()
This method must:
o Call IO_Support and read all data from file
o Loop through the list of data received from file
Take each data string in the list and split it to get each asset attribute, assign it to an array of Strings
If the item in the array is a Laptop then call the Laptop constructor and add the created object to the assetList collection
If the item in the array is a Phone then call the Phone constructor and add the created object to the assetList collection
Asset class
• Add an abstract save method:
o public abstract void saveAssetData(ArrayList tmpList)
Laptop Class
• Add a new Constructor public Laptop(String[] dataArrFromFile)
o Extracts the required data
o Calls the super constructor with the common data for the common attributes
o Assigns Laptop specific data to its attributes
o Check the assigned status, if true build employee object with the data and call assignEmployee method to assign it to the employee
• Override method public void saveAssetData(ArrayList tmpList)
o Add the type of asset “L” followed by any common Asset data and specific Laptop data
If the asset is assigned to an employee, need to add the employee details to the same line as well
Phone Class
• Add a new Constructor public Phone(String[] dataArrFromFile)
o Extracts the required data
o Calls the super constructor with the common data for the common attributes
o Assigns Phone specific data to its attributes
o Check the assigned status, if true build employee object with the data and call assignEmployee method to assign it to the employee
• Override the method public void saveAssetData(ArrayList tmpList)
o Add the type of asset “P” followed by any common Asset data and specific Phone data
If the asset is assigned to an employee, need to add the employee details to the same line as well
OTHER Changes
• Data should be saved prior to the system shutting down.
o Do this in the start class by calling writeAllAssetsToFile() after calling the UI run()
• Data needs to be read from file
o Do this in the AssetPool class constructor by calling readAllAssetsFromFile() after initializing the assetList
Finally,
Test it all works
Document your code
Submit to ESP
Apply learnings of this lab to Project 2.
• Think about how to apply the same ideas to project 2
• Discuss with your tutor
• Make your project 2 objects persistent (save all object data to a file on system closure and retrieve all object data when the system starts)