Question: Introducing subclasses of the Tool class and using inheritance to enhance the usability of your code.The data file tools_data_2.txt contains more tool data and, if
Introducing subclasses of the Tool class and using inheritance to enhance the usability of your code.The data file tools_data_2.txt contains more tool data and, if you examine the data, you will see that the data has been split into two types: electric tool data and handtool data.
Step 1
Examine the new data file and you will see that in addition to data for fields toolName, toolCode, timesBorrowed, onLoan, cost and weight, all vehicles have data for the following additional fields the electric tool data now includes data for whether that tool is rechargeable and its power. The handtool data also has some additional different data to the electric tool data.
Note that the new data file contains lines such as
[Electric tool data]
that will not be relevant until later though it should be obvious that these are labels or flags that signpost the type of data immediately below this label.
Now amend the Tool class so that it has these new fields shown above and modify the readData() method so that as well as ignoring comment lines and blank lines, it also (temporarily) ignores lines that start with [.
With these changes you should now be able to read in data from the file tool_data_2.txt . Modify the printDetails() method so that the details of each tool are displayed in the terminal window in a format similar to that in the data file.
Step 2:
ElectricTool and Handtool are direct subclasses of Tool and as well as the fields inherited from the Tool class:
the ElectricTool class has two additional fields: rechargeable and power;
the HandTool class has one additional field: sharpenable i.e. whether that tool is able to be sharpened.
What we will next do is to write code that
firstly, creates the ElectricTool class and links it with the Tool class as shown in the diagram above;
then fills the contents of the fields in both ElectricTool and Tool with data from tool_data_2.txt;
then creates the Handtool class, links it with the Tool class as shown in the diagram above and then fills the contents of its fields with data from tool_data_2.txt.
Step 3;
As detailed above, lets consider firstly reading the data for electric tools. It is obvious that we could write a method readElectricToolData() (that would be similar to a method readHandToolData()) and have a data file electricTool_data.txt. But, as our model grows, this approach means that we will end up with many "read" methods in the class Shop e.g. readElectricToolData(), readHandToolData(), readAnyOtherSimilarData() etc. We will also end up with many different data files. A more sensible approach is to have one "read" method readToolData() in the Shop class and all the data in one file. This is the approach that we will adopt here.
Remember that in the last step of Part 1, we let a Tool object read its own data. Now, as an ElectricTool object holds the data specific to electric tools and a Handtool object holds the data specific to hand tools, it makes sense for the same idea to be used with these classes i.e. the readToolData() method in Shop lets the data for an ElectricTool object be read by the ElectricTool class and similarly for the data for HandTool objects to be read by its own class.
If you understood the notes above, then you will realise that because you are going to use read methods in each class to populate your fields then your constructor with parameters in Tool is no longer needed and should be deleted.
Step 4 Starting coding
We shall now add our first subclass: ElectricTool. Start coding by
adding the subclass ElectricTool which should have the fields mentioned above;
then introduce a printDetails() method in the ElectricTool class that overrides the Tool class's printDetails() method, in a similar manner to that employed by the print() method of the Network project;
similarly, introduce a readData() method in the ElectricTool class. This should be the method that is used at run-time to read the data into this object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
