In: Computer Science
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;
}
}
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.