Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Information About This Project In the realm of database processing, a flat file is a text file that holds a table of records. Here is

Information About This Project

In the realm of database processing, a flat file is a text file that holds a table of records.

Here is the data file that is used in this project. The data is converted to comma separated values ( CSV ) to allow easy reading into an array.

Table: Consultants

ID

LName

Fee

Specialty

101

Roberts

3500

Media

102

Peters

2700

Accounting

103

Paul

1600

Media

104

Michael

2300

Web Design

105

Manfred

3500

Broadcasting

Steps to Complete This Project

STEP 1 Open a Java Editor

Open NetBeans or Eclipse and create a Java project with the following details.

For Project Name include: DataApplication

For the Main Class include: DataApplication

In your Code window for this class, shown below, copy the program code shown in Figure 1 below, in the appropriate places, except substitute your own name in place of Sammy Student.

PROJECT Java File Processing Application: Database Application

Figure 1 Source Code for the Database File Processing Program

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

// Programmer: Sammy Student

public class DataApplication

{

public static void main(String[] args)

{

try

{

File fin = new File("data.txt");

Scanner scan = new Scanner(fin);

ArrayList theData = new ArrayList();

// read the column headings from the flat text file

String line = scan.nextLine();

while(scan.hasNextLine())

{

line = scan.nextLine();

String[] list = line.split(",");

int key = Integer.parseInt(list[0]);

String name = list[1];

int fee = Integer.parseInt(list[2]);

String specialty = list[3];

theData.add(String.valueOf(key));

theData.add(name);

theData.add(String.valueOf(fee));

theData.add(specialty);

}

int count = 1;

for (int i = 0; i < theData.size(); i++)

{

System.out.print(theData.get(i) + "\t\t");

if(count % 4 == 0 )

System.out.println(" ");

count++;

}

scan.close();

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

}

Create and Save a Text Data File

Open a text file within your project folder. Name the text file as: data.txt

Copy the CSV formatted data below into the text file that you just created.

Save the data in the text file.

[ Data File ]

ID,LName,Fee,Specialty

101,Roberts,3500,Media

102,Peters,2700,Accounting

103,Paul,1600,Media

104,Michael,2300,Web Design

105,Manfred,3500,Broadcasting

STEP 3 Build, Compile and Run the Program

From the menu select [ Run ] and click [ Run Project ] to run your app.

Observe the program's output. Notice that the data from the text file is read into an ArrayList and the array list elements are displayed in a console output statement. The data values are displayed in a tabular format.

STEP 4 Modify Your Program

You will now modify your database application by including a method named searchData() that receives the ArrayList elements and allows the program user to search for a name located within the flat file.

If the name of the consultant is located, a message as such is displayed by the search method otherwise a message indicating that the name is not found in the file.

The method for searching names in the flat file is shown below. Place the method in an appropriate location in your program code.

Remember to also write a statement that calls the method.

searchData(theData);

Save your program to update it and perform a trial run of your modified code. Test your program by entering the name of a consultant that appears in the text file and run the program again using a name that is not in the file. Take screen snapshots showing both a data found and data not found example.

PROJECT Java File Processing Application: Database Application

Figure 2 Additional Source Code for the Database File Processing Program

public static void searchData(ArrayList vals)

{

System.out.print("enter a name: ");

Scanner sc = new Scanner(System.in);

String strName = sc.nextLine().trim();

boolean found = false;

for (int i = 0; i < vals.size(); i++)

{

if(vals.get(i).equals(strName.trim()))

{

found = true;

break;

}

}

if(found == true)

System.out.println(" data found ");

else

System.out.println(" data not found ");

sc.close();

}

STEP 5 Test the Program and Write the Data

Now modify your program again by including a new method that will determine if any consultant charges a fee that exceeds $ 2,000 .

Save and test your program.

Finally include another method that will allow the program user to query the flat file and show a count of the consultants that specialize in providing media services.

Save and test your program.

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

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions