Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Overview The project is to develop an application which models a shop which hires out expensive tools for one-off usage. The model will contain

JAVA

Overview

The project is to develop an application which models a shop which hires out expensive tools for one-off usage. The model will contain classes which represent the shop, the tools hired out as well as the users of the Tool Hire shop and the staff who work for the shop.

We shall start by introducing Tool and Shop classes, reading in tool data from text files and storing the tools in the hire system.

Step 1

Start a new project Tool Hire Part 1 Step 1 and create a Tool class with, initially, the following fields:

toolName ,a string, e.g. DeWalt Circular Saw

itemCode, a string, e.g. RD6582 used to identify each tool

timesBorrowed, the number of times that an item has been borrowed;

onLoan, whether or not an item is on loan;

cost which is the cost of an item when new (in whole pounds);

weight in whole grammes.

Provide a constructor for the class which has six parameters (in the order given above) for initialising the fields. You should also provide accessor methods and a method printDetails() that outputs details of the tool to the terminal window e.g.

Tool name: DeWalt Circular Saw; code: RD6582; timesBorrowed: 250;

onLoan: no; cost: 350; weight: 345

Now add a new class Shop to the project that, at this early stage of the project, has only one field: toolList used to store Tool objects in an array list. Complete this first step by writing a method storeTool to add a tool to toolList as well as a method printAllTools() that displays the details of all tools in toolList.

Step 2

You are advised to read all of this step carefully before starting to code.

Firstly, save your project from Step 1 as Shop Part 1 Step 2 and continue to adopt this strategy for all subsequent steps so that you can always go back to a "working" version of your project.

The tool hire shop stores data corresponding to each of its tools in a text file called tool_data_1.txt. Download this file from BlackBoard and examine it. Except for blank lines and comment lines, each line of the file contains comma separated data for the fields toolName, itemCode, timesBorrowed, onLoan, cost and weight e.g.

Makita BHP452RFWX,RD2001,12,false,14995,1800

You must now write a method readToolData() in the Shop class that reads this data, creates corresponding Tool objects and adds those objects to toolList. The name of the file that you read from, tool_data_1.txt, should be selected using a file dialog box its name should not be passed to readToolData() as a parameter.

All that you need to know to set up and use a file dialog is either

in Java Supplementary Material 5: More on File Input and Output;

easily available in the documentation for the FileDialog class;

or is given below.

Develop your readToolData()method in the following stages and again, it is recommended that you save a succession of projects, including one for each stage of Step 2.

Firstly, read and make notes about

Slides SM4.14-16 to remind yourself about using a Scanner object;

Slides SM4.4-11 and SM4.16-19 to remind yourself about throwing exceptions;

Slides SM5.12-15 to remind yourself about using a FileDialog.

Then start writing your readToolData()method by creating a FileDialog object which will be used to select the file that will be used as input. Note that the constructor for the FileDialog object has as its first parameter a pointer to its parent or owner. However, for our purposes, you should set the value of this pointer to be null. Dont worry as a FileDialog box will still appear though it may be behind your other windows !

To select the correct file for input, you may find it useful to use the method setDirectory().

Now output the name of the selected file to the terminal window to check that your code works.

Next, set up a Scanner object created by passing the constructor a File object based on the file selected in the file dialog but do not change the default delimiter. Then use a loop that simply reads each line of the file, stores it in a String variable, lineOfText, and prints out the string to the terminal window so that you can "see" all the lines of data that have been read. At this stage:

do not attempt to break down each line into group, toolName, itemCode etc;

let the readToolData() method throw any IOExceptions that may occur.

The lines in the text file that start with // are comments. These, together with blank lines, should be ignored. Amend your code so that only the "real data" is output to the terminal window.

The only exception that can occur is actually a FileNotFoundException in the call to the constructor of the Scanner object so

remove the throws clause from the readToolData()method header;

place the code that may cause that exception in a suitable try-catch block structure.

Before proceeding to Stage iii), make sure that the method behaves sensibly under all circumstances.

Until now, we have adopted an approach that allowed us to easily ignore lines that were not "real data". Now, for each line of real data, we need to extract the tokens from the string lineOfText. But before you proceed any further, make sure that lineOfText does not have any leading or trailing spaces (note you are not allowed to edit the text file tool_data_1.txt !). To do this, look at the documentation of the String class for a helpful method.

Now note the following points:

Although the "real data" in each line of the file tool_data_1.txt is essentially "comma separated" rather than "space separated", it may also contain spaces before or after a comma e.g.

Makita BHP452RFWX,RD2001,12 ,false,14995,1800

Bosch GSR10.8-Li Drill Driver,RD3021,25, true,9995,820

However, apart from the presence of "extra" spaces in the data, you may assume that all data files in the project contain valid data unless, at some later point, you are warned otherwise.

At the moment, your readToolData()method should have a loop that is similar to this

// set up scanner to read from file

while there is more data in the file

{

read a line into the variable lineOfText

if it's not a comment or a blank line

print lineOfText to terminal window

}

Now, if you examine the documentation for the Scanner class, you will find that not only can a scanner take its input from a file, it can also take its input from a String. When such a scanner is created, its constructor is passed the string.

It is this second Scanner object that you use to read the data into the fields of your classes.

You should now declare and create a second Scanner object that is passed lineOfText as an argument. This second scanner should use the Scanner class's useDelimiter() method which you met in Worksheet SM4.

You can now extract each token from lineOfText and so enable you to obtain values for each of the tools fields. A corresponding Tool object can then be created and added to toolList.

After reading the data, check that all the tool data has been added to the list by calling the printAllTools()method.

Step 3

After you have completed Step 2, your pseudocode should be something like

// set up scanner to read from file

while there is more data in the file

{

read a line into the variable lineOfText

if it's not a comment or a blank line

create a second scanner passing it lineOfText

read data for each of tool's fields

create a Tool object using the fields data

and store it in toolList

}

The approach we now have for reading in the tool data works well but does has the disadvantage that the Shop class is closely coupled with the Tool class (see Chapter 8, Designing classes). For example, if an extra field is added to the Tool class then it will be necessary to change the code in the Shop class.

Also, at the moment, we are assuming that all the tools are of the same type whereas in reality they will be of different types and so we will need later ! to introduce subclasses of the Tool class to deal with this. However, if we do not change the present approach, it will mean that the Shop class will then be too closely coupled with both the Tool class and its subclasses.

To deal with this close coupling, we will perform some refactoring of the code. We can uncouple the Shop and Tool classes by letting a Tool object read its own data from within the readToolData() method. This can be accomplished by changing the if block in the above pseudocode to

...

if it's not a comment or a blank line

create a second scanner passing it lineOfText

create a Tool object

pass the scanner to a new readData() method of the Tool

class & read data for each of its fields

store the Tool object in toolList

...

However, before we start coding, we need to make sure that you understand the interplay between the two new lines above. We now want to let each tool read its own data and to do that we need to call tool.readData(scanner2). Clearly, before we execute this line of code, we must have created a Tool object. Ask if you do not understand this. So, we call a "no parameter" constructor before this code you may already have one as your default constructor.

To code the above, you will also need to amend the Tool class by adding

a "no parameter" constructor. Without this, you cannot call the readData() method.

a method readData() that is passed a Scanner object which it then uses to read values for each of the tools fields.

Now test your code by reading the data and then calling printAllTools(). Make sure that all your fields have values. If not, relook at your read methods.

Finally, go over your code again to see if all of it is still necessary.

You should aim to complete this part of the project within one week when Part 2 of the project will be made available.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions

Question

Discuss the major challenges faced by international advertisers.

Answered: 1 week ago