Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve using blue j below is the Tool_data_1.txt Overview The project is to develop an application which models a shop which hires out expensive tools

Solve using blue j image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
below is the Tool_data_1.txt
image text in transcribed
Overview The project is to develop an application which models a shop which hires out expensive tools for eneoff usage. The model will contain classes which represeat the shop, the tools hired out as well as the users of the Tool Hire shop and the staft who work for the shop. We shall start by introducing Tool and shop classes, reading in tool data from text files and stoting 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, c.g. Dewalt Circular Saw i temCode, a string, e.g. RD 6582 used to identify each tool timesDorrowed, the number of times that an iten has been borrowed; ontoan, 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 onder given above) for initialising the fields. You should also provide accessot 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 sage of the project, has only one field: toolList used to store Tool objects in an array list. Complete this first step by wniting a method store Tool to add a fool to coolzist as well as a method printA11Tools () that displays the details of all tools in toolist. Step 2 You are advised to read all of this step cerefully before starting to code. Firstly, save your project from Step I as Shop Part 1 Step 2 and continne fo adope 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. Dowaload 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. itescode, timesBorrowed, onloan, cost and weighteg. 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 cither - in Java Sapplementary Material 5: More on File Input and Output; - casily available in the documentation for the FileDialog class; - or is given below. Develop your readToolDa ta () method in the following stages and again, it is rocommended that you save a suceession of projects, including one for each stage of Step 2 . i) Firstly, read and make notes about a. Slides SM4.14-16 to remind yourself about using a Scanner object, b. Slides SM4.4-11 and SM4. 16-19 to remind yourself about throwing exeeptions: c. Slides SM5.12-15 to remind yourself about using a Filedialog. ii) Then start writing your readToolData () method by creating a Fi.leDia.log 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 vwner. However, for our purposes, you should set the value of this pointer to be nu1.1. Don't worry as a Filedialog box will still appear though it may be behind yoor ether windows! To select the cotrect file for inpat, you may lind it useful to use the method setdirectory () . Now outpat the name of the selected file to the terminal window to check that your code woriks. iii) 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, 1 ineorText, and prints out the string to the terminal window so that you can "soe" all the lines of data that have been read. At this stage: - do nof attempt to break down cach line into group, toolMane, itemCode cte: - let the readToo1Data () method throw any IOExceptions that may occur. iv) The lines in the lext file that start with // are comments. These, together with blank lines, shoold 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 ii), make sure that the method behaves sensibly ander all circumstances. v) 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 ectract the tokens from the string 1 ineofText. But before you proceed any further, make sure that 1 ineOfrext does not have any leading or trailing spaces (note you are mot allowed to edit the tedt file too1_data_1. txt 1). To do this, look at the documentation of the string chss 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, 1600 Bosch GSR10,8-Li Dril1 Driver, RD3021,25, true,9995,820 However, apart from the presence of "exirat spaces in the data, you may assume that all data files in the project contain valid data unless, at some later point, you are wamed otherwise. At the moment, your readroolData () method should have a foop that is similar to this // set up scanner to read from file while there is more data in the file f. read a Iine into the variable lineorText If it's not a comment or a blank 1 ine print lineortext to terminal Mindow Now, if you examine the documentation for the Scanner class, you will find that not caly can a seanner take its input from a file, it can also take its input from a String. When such a scanner is created, its construclor is passed the string It is this second Scanner ebject that you use to read the data into the ficlds of your classes. You should now declare and create a secoud Scanner object that is passed 1 ineottext as an argument. This second scanner should use the scanner classs usedeliniter () method which you met in Workseet 5M4. You can now extract cach token from 1 ineof Text and so cnable you to obtain values for each of the tool's 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 printhl1Tools () method. Step 3 After you have completed Step 2, your pseudocode should be something like /I set up scanner to read from file while there is more data in the file f read a line into the variable lineotText if it's not a comment or a blank 11 ine ereate a second scanner passing it lineotrext read data for each of tool's fields create a Tool object using the field's 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 (se Chapter 8, Designing classes). For example, if an extra field is added to the Tool class then it will be necesoiry to change the code in the Shop class. Also, at the moment, we are aseuming that all the tools are of the same type whereas in reality they will be of different types and wo we will need - later ! - to introduce sabclasses of the 7ool class to deal with this. However, if we do not change the present approach, it will mean that the shop clas will then be too closely coupled with both the Foel 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 Fool object read its own data from within the readToolData () method. This can be accomplished by changing the if block in the above psevdocode 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 too 1. 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 tool's fields. Now test your code by reading the data and then calling printAl1Tools (). 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. // this is a comment, any lines that start with // // (and blank lines) should be ignored // data is toolName, itemCode, timesBorrowed, onLoan, cost, weight Makita BHP452RFWX, RD2001, 12, false, 14995,1800 Flex Impact Screwdriver FIS439, RD2834,14, true, 13499, 1200 DeWalt D23650-GB Circular Saw, RD6582,54,true,14997, 5400 Milwaukee DD2-160XE Diamond Core Drill, RD4734,50, false, 38894, 9000 Bosch GSR10.8-Li Drill Driver, RD3021,25,true,9995, 820 Bosch GSB19-2REA Percussion Drill, RD8654,85, false, 19999, 4567 Flex Impact Screwdriver EIS439, RD2835, 14, false, 13499, 1200 DeWalt DW936 Circular Saw, RD4352,18, false,19999, 3300 Sparky FK652 Wall Chaser,RD7625,15, false, 29994,8400

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_2

Step: 3

blur-text-image_3

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

Identify and briefly describe five common fact-finding methods.

Answered: 1 week ago

Question

differentiate between good and bad ways of working hard;

Answered: 1 week ago

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago