Question

In: Computer Science

You are to create a class called ManageDB. The ManageDB class will have a 2 input...

You are to create a class called ManageDB. The ManageDB class will have a

2 input constructor

that has the following definition:

ManageDB(int number, String fileName)

The constructor will create an array of EmployeeDB objects of length

"number".

The constructor will read the file located at "fileName" and extract the

information from that

file to populate a database of EmployeeDB objects. The file contains

information on the

EmployeeDB objects. This information is contained in records.

The format of each record is as follows:

^ "index of employee" "name of field" = "value of field"

Each record will start with a '^' character.

The "index of employee" is always a 4 character field representing a

positive integer.

The "name of field" is the name of the field to be populated for a

EmployeeDB object.

There is a delimiter between the "name of field" and the "value of field"

which is '='.

The "value of field" is the character string representing the value of the

field specified in "name of field".

The valid "name of field" strings are:

"age"

"name"

"salary"

For each "name of field" the "value of field" will be of the following

type:

age - integer

name - String

salary - double

An example of a valid record is as follow:

^0002age=24

This record would set the 0002 indexed EmployeeDB object age to 24.

You can assume that a record is formatted correctly. That is there will

always be a ^, =, and a valid

"name of field". However, the "index of employee" may not have a valid

number (either out of range or

not a number). The "value of field" may also be invalid if a number is

specified. The name

"value of field" will always have a valid String value (there are no

errors for this field).

If there is an error in the "index of employee" or the "value of field"

portion then ignore the

record and decode the next record. If the "index of employee" portion is

out of range then ignore that

record.

The EmployeeDB class is provided for you. Use the EmployeeDB class to

create the EmployeeDB array and track

the information for each Employee.

You need to create 3 public methods for the ManageDB class.

public String getName(int i) // returns the name of Employee at i index

public int getAge(int i) // returns the age of Employee at i index

public double getSalary(int i) // returns the salary of Employee at i

index

Compile your program with the EmployeeDB class and the DBTester class

which are both supplied to you.

Your program must run to completion and pass all the test cases. Make sure

to handle all exception

that might occur so your program can successfully run. The file to read is

called "EmployeeData.txt"

which is provided to you. Place this file in the working directory of your

project (the base directory

of your package).

Add your name to the DBTester.java file at the top so your name is

displayed.

Capture a screen image of the program ouput with your name and the result

of the test cases.

Files provided:

EmployeeDB.java

DBTester.java

EmployeeData.txt

------------------------

public class DBTester

{

public static void main(String args[ ])

{

System.out.println("Homework 4 test case results by Your Name");

ManageDB m = new ManageDB(5, "EmployeeData.txt");

boolean result;

int testCount = 1; // used to track test numbers

result = isSame(23, m.getAge(0));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

if (m.getName(0).equals("Jane"))

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(1000000.98, m.getSalary(0));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(-1, m.getAge(1));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

if (m.getName(1).equals("Jerry"))

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(100000, m.getSalary(1));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(29, m.getAge(2));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

if (m.getName(2).equals("Joann"))

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(50000.8, m.getSalary(2));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(-1, m.getAge(3));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

if (m.getName(3).equals(""))

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(-1, m.getSalary(3));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(25, m.getAge(4));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

if (m.getName(4).equals("John"))

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

result = isSame(1177.22, m.getSalary(4));

if (result == true)

System.out.println("Test Case " + testCount + " Passed");

else

System.out.println("Test Case " + testCount + " Failed");

testCount++;

}

private static boolean isSame(double x, double y)

{

double error = .00001;

boolean rv = false;

if (((x + error) >= y) && ((x - error) <= y))

{

rv = true;

}

return rv;

}

}

-----------------------------

employeedata.txt

^0000age=23^0000name=Jane^0000salary=1000000.98^0005age=20^0005name=Joe^00

05salary=11888.22^0002age=29^0002name=Joann^0002salary=50000.8

^00a1age=36^0001name=Jerry^0001salary=100000^0004age=25^0003age=3q2^0004sa

lary=1177.22^0w03name=Joan

^0004name=John^0003salary=100a00a

----------------------------

class EmployeeDB

{

// Member variables

private int age;

private String name;

private double salary;

// Constructors

public EmployeeDB()

{

age = -1;

name = "";

salary = -1.0;

}

// Mutators

public void setName(String n)

{

name = n;

}

public void setAge(int a)

{

age = a;

}

public void setSalary(double s)

{

salary = s;

}

// Accessors

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

public double getSalary()

{

return salary;

}

}

Solutions

Expert Solution

Hi, Please find the solution:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class ManageDB {
    EmployeeDB[] employeeDBS;

    public ManageDB(int number, String fileName) {
        employeeDBS = new EmployeeDB[number];
        File f = new File(fileName);
        List<String> strings = null;
        try {
            strings = Files.readAllLines(f.toPath());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        String wholeFile = strings.get(0);//assuming there are no newlines in the file
        //it is ambiguous if the file has new lines
        String arr[] = wholeFile.split("\\^");
        for (String s : arr) {
            if(s.equalsIgnoreCase("")) continue;
            String indexStr = giveIndexFromStringAsString(s);
            if (isStringOnlyNumbers(indexStr)) {
                int index = Integer.parseInt(indexStr);
                if(index>=number)continue;;
                String s1 = s.split("=")[0].trim();
                String valueOfField = s.split("=")[1].trim();
                String fieldForEmployee = s1.substring(4, s1.length());
                if (fieldForEmployee.equalsIgnoreCase("name") && !isStringOnlyNumbers(valueOfField)) {
                    if( employeeDBS[index]==null){
                        employeeDBS[index] = new EmployeeDB();
                    }
                    employeeDBS[index].setName(valueOfField);
                } else if (fieldForEmployee.equalsIgnoreCase("age") && isStringOnlyNumbers(valueOfField)) {
                    if( employeeDBS[index]==null){
                        employeeDBS[index] = new EmployeeDB();
                    }
                    employeeDBS[index].setAge(Integer.parseInt(valueOfField));
                } else if (fieldForEmployee.equalsIgnoreCase("salary") && isStringAFloatNumber(valueOfField)) {
                    if( employeeDBS[index]==null){
                        employeeDBS[index] = new EmployeeDB();
                    }
                    employeeDBS[index].setSalary(Double.parseDouble(valueOfField));
                }
            } else {
                //ignoring records for improper index
            }

        }
    }

    private static boolean isStringOnlyNumbers(String str) {
        boolean birthyearProper = true;
        for (char s : str.toCharArray()) {
            if (!Character.isDigit(s)) {
                birthyearProper = false;
                break;
            }
        }
        return birthyearProper;
    }

    private static boolean isStringAFloatNumber(String str) {
        boolean stringProper = true;
        int countDots = 0;
        for (char s : str.toCharArray()) {
            if (s == '.') {
                countDots++;
                continue;
            }
            if (!Character.isDigit(s)) {
                stringProper = false;
                break;
            }
        }
        return stringProper && ((countDots == 1 || countDots==0) ? true : false);
    }


    static String giveIndexFromStringAsString(String whole) {
        return whole.substring(0, 4);
    }

    // returns the name of Employee at i index
    public String getName(int i) {
        return employeeDBS[i].getName();
    }

    /// returns the age of Employee at i index
    public int getAge(int i) {
        return employeeDBS[i].getAge();
    }

    // returns the salary of Employee at i index
    public double getSalary(int i) {
        return employeeDBS[i].getSalary();
    }

//    index
}

class EmployeeDB {

// Member variables

    private int age;

    private String name;

    private double salary;

// Constructors

    public EmployeeDB() {

        age = -1;

        name = "";

        salary = -1.0;

    }

// Mutators

    public void setName(String n) {

        name = n;

    }

    public void setAge(int a) {

        age = a;

    }

    public void setSalary(double s) {

        salary = s;

    }

// Accessors

    public String getName() {

        return name;

    }

    public int getAge() {

        return age;

    }

    public double getSalary() {

        return salary;

    }

}

Sample output: Exception(below) is because of lack of data. There will be only minor corrections. For the entries in the array where the index is not present, those records can be removed by you. I left them just in case.


Related Solutions

You are to create a class called Person. You should create the definition of the class...
You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp. A Person will have attributes of Height (in inches) Weight (in pounds to 2 decimal places) Name (string) Gender (‘m’ or ‘f’) Ethnicity/Race (could be more than one word) Occupation (also more than a single word) A Person will have...
Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which...
Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which contains the student's name and the student's final grade. Create a function called "GetStudentInfo" for who you will pass the arrays and the user will enter the data for those 9 students. Back in main, loop through both arrays and display the information for each student after its entered in GetStudentInfo I need a C++ code for visual studio. Can anyone help me? I'll...
Create a file called grocery.ts. It should have a definition of a class with the obvious...
Create a file called grocery.ts. It should have a definition of a class with the obvious name Grocery. The class should have some basic attributes such as name, quantity, etc. Feel free to add any other attributes you think will be necessary. Add few grocery items to an array of groceries, such as milk, bread, and eggs, along with some quantities (i.e. 3, 6, 11). Display these grocery items as HTML output. The output of this assignment will be grocery.ts...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
2. Create a class called Invoice that a store might use to represent an invoice for...
2. Create a class called Invoice that a store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—the ID for the item sold (type string), name of item (type string), item description (type string) and the price of the item (type int). Your class should have a constructor that initializes the four data members. A constructor that receives multiple arguments. Example: ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... ) Provide...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT