Java Main topics: Exception handling Data Files Program Specification: You are t
ID: 3719295 • Letter: J
Question
Java
Main topics:
Exception handling
Data Files
Program Specification:
You are to first design and implement a single class which can be used to manage the salaries for a small organization.
The organization has (text) data files for its various units that are formatted as such :
-----------------------------------------------------
<id>:<slaray>:<years of service><EOL>
.
.
.
<id>:<slaray>:<years of service><EOL>
<EOF>
-----------------------------------------------------
Note:
• the ids are unique and there can not be multiple entries with the same ids
• the entries are arranged in ascending order of id values
Your class must Implement the following interface:
interface Raiseable
{
// Creates a new (empty) file that can later be used to add entries of the above
// format
void create(String fileName)
// Formats and displays the contents of the specified file to the screen
void display(String fileName)
// Creates a new file that is a copy of the given input file - with the new
// entry added such that the new file is sorted by the entries’ ids
// Return true if no match was found and the entry was added; false if a duplicate id boolean addTo(String inFileName, String outFileName, int id, double salary, int yearsOfService)
// Creates a new file that is a copy of the given input file - with the specified
// entry removed
// Returns true if a matching entry was found / removed; false if no such entry exists boolean removeFrom(String inFileName, String outFileName, int id, double salary, int yearsOfService)
// Creates a new file that is a copy of the given input file - where all entries whose
// years of service are greater than or equal to yearsOfService have had their salaries
// increased by salaryIncPercent
// Return the number of entries that the raise was applied to int raise(String inFileName, String outFileName, int yearsOfService, double salaryIncPercent)
// Creates a new file that is a sorted merge of the two given (sorted) input files
// In case of duplicate entries, only the one with the highest salary is kept in
// outFileName
void mergeFiles(String inFileName1, String inFileName2, String outFileName)
}
Rules and Requirements:
• Proper object oriented design practices and conventions are expected
• All checked and foreseeable unchecked exceptions must be handled
• You may not use ArrayLists or Arrays to buffer the input files during a merge
Explanation / Answer
Given below is the needed code. You can use the SalaryManager class with any driver program you have . It iimplements the Riseable interface.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Riseable.java
===============
public interface Riseable {
// Creates a new (empty) file that can later be used to add entries of the above format
void create(String fileName);
// Formats and displays the contents of the specified file to the screen
void display(String fileName);
// Creates a new file that is a copy of the given input file - with the new entry added such that the new file is sorted by the entries’ ids Return true if no match was found and the entry was added; false if a duplicate id
boolean addTo(String inFileName, String outFileName, int id, double salary, int yearsOfService);
// Creates a new file that is a copy of the given input file - with the specified entry removed Returns true if a matching entry was found / removed; false if no such entry exists
boolean removeFrom(String inFileName, String outFileName, int id, double salary, int yearsOfService);
// Creates a new file that is a copy of the given input file - where all entries whose years of service are greater than or equal to yearsOfService have had their salaries increased by salaryIncPercent Return the number of entries that the raise was applied to
int raise(String inFileName, String outFileName, int yearsOfService, double salaryIncPercent);
// Creates a new file that is a sorted merge of the two given (sorted) input files In case of duplicate entries, only the one with the highest salary is kept in outFileName
void mergeFiles(String inFileName1, String inFileName2, String outFileName);
}
SalaryManager.java
=================
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class SalaryManager implements Riseable{
@Override
public void create(String fileName) {
PrintWriter pw;
try {
pw = new PrintWriter(new File(fileName));
pw.close();
} catch (FileNotFoundException e) {
System.out.println("Error occured while trying to create file " + fileName);
}
}
@Override
public void display(String fileName) {
Scanner infile;
try {
infile = new Scanner(new File(fileName));
while(infile.hasNextLine())
System.out.println(infile.nextLine());
infile.close();
} catch (FileNotFoundException e) {
System.out.println("Error occured while trying to read file " + fileName);
}
}
@Override
public boolean addTo(String inFileName, String outFileName, int id, double salary, int yearsOfService) {
String line;
boolean retVal = false;
try {
Scanner infile = new Scanner(new File(inFileName));
PrintWriter outfile = new PrintWriter(new File(outFileName));
while(infile.hasNextLine())
{
line = infile.nextLine().trim();
String[] tokens = line.split(":");
int currId = Integer.parseInt(tokens[0]);
if(currId == id) //duplicate id
break;
else if(id < currId) //we are at correct place to write the new record
{
outfile.println(id + ":" + salary + ":" + yearsOfService );
outfile.println(line);
retVal = true;
break;
}
else
outfile.println(line); //just preserve the line from input as is in output file
}
//copy over any lines left after adding the new line
if(retVal != false)
{
while(infile.hasNextLine())
{
outfile.println(infile.nextLine().trim());
}
}
infile.close();
outfile.close();
} catch (FileNotFoundException e) {
System.out.println("Error occured- " + e.getMessage());
}
return retVal;
}
@Override
public boolean removeFrom(String inFileName, String outFileName, int id, double salary, int yearsOfService) {
String line;
boolean retVal = false;
try {
Scanner infile = new Scanner(new File(inFileName));
PrintWriter outfile = new PrintWriter(new File(outFileName));
while(infile.hasNextLine())
{
line = infile.nextLine().trim();
String[] tokens = line.split(":");
int currId = Integer.parseInt(tokens[0]);
if(currId == id) // id exists
{
retVal = true;
break;
}
else if(id < currId) //we won;t be able to find the id , since all ids are more than this
break;
else
outfile.println(line); //just preserve the line from input as is in output file
}
//copy over any lines left after removing the line with given id
if(retVal != false)
{
while(infile.hasNextLine())
{
outfile.println(infile.nextLine().trim());
}
}
infile.close();
outfile.close();
} catch (FileNotFoundException e) {
System.out.println("Error occured- " + e.getMessage());
}
return retVal;
}
@Override
public int raise(String inFileName, String outFileName, int yearsOfService, double salaryIncPercent) {
String line;
int count = 0;
try {
Scanner infile = new Scanner(new File(inFileName));
PrintWriter outfile = new PrintWriter(new File(outFileName));
while(infile.hasNextLine())
{
line = infile.nextLine().trim();
String[] tokens = line.split(":");
int yos = Integer.parseInt(tokens[2]);
if(yos >= yearsOfService)
{
double salary = Double.parseDouble(tokens[1]);
line = tokens[0] + ":" + (1+salaryIncPercent) * salary + ":" + yos;
count++;
}
outfile.println(line);
}
infile.close();
outfile.close();
} catch (FileNotFoundException e) {
System.out.println("Error occured- " + e.getMessage());
}
return count;
}
@Override
public void mergeFiles(String inFileName1, String inFileName2, String outFileName) {
try {
Scanner file1 = new Scanner(new File(inFileName1));
Scanner file2 = new Scanner(new File(inFileName2));
PrintWriter outfile = new PrintWriter(new File(outFileName));
String line1 = null, line2 = null;
if(file1.hasNextLine())
line1 = file1.nextLine();
if(file2.hasNextLine())
line2 = file2.nextLine();
while(line1 != null || line2 != null)
{
if(line1 == null)
{
outfile.println(line2);
if(file2.hasNextLine())
line2 = file2.nextLine();
else
line2 = null;
}
else if(line2 == null)
{
outfile.println(line1);
if(file1.hasNextLine())
line1 = file1.nextLine();
else
line1 = null;
}
else
{
String[] tokens1 = line1.split(":");
String[] tokens2 = line2.split(":");
int id1 = Integer.parseInt(tokens1[0]);
int id2 = Integer.parseInt(tokens2[0]);
if(id1 < id2)
{
outfile.println(line1);
if(file1.hasNextLine())
line1 = file1.nextLine();
else
line1 = null;
}
else if(id1 > id2)
{
outfile.println(line2);
if(file2.hasNextLine())
line2 = file2.nextLine();
else
line2 = null;
}
else //same ids, compare salaries and keep the once with highest sal
{
Double sal1 = Double.parseDouble(tokens1[1]);
Double sal2 = Double.parseDouble(tokens2[1]);
if(sal1 > sal2)
outfile.println(line1);
else
outfile.println(line2);
if(file1.hasNextLine())
line1 = file1.nextLine();
else
line1 = null;
if(file2.hasNextLine())
line2 = file2.nextLine();
else
line2 = null;
}
}
}
file1.close();
file2.close();
outfile.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.