Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The organization has (text) data files for its various units that are formatted

ID: 3709351 • Letter: T

Question

The organization has (text) data files for its various units that are formatted as such :

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

<id>:<salary>:<years of service><EOL>

<id>:<salary>:<years of service><EOL>

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

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

import java.io.*;

//interface

public interface Raiseable{

public void create(String filename);

public void display(String filename);

public boolean addTo(String inFile,String outFile,intid,double salary,int yearsOfService);

public boolean removeFrom(String inFile,String outFile,intid,double salary,int yearsOfService);

public void mergeFiles(String inFileName1,String inFileName2,String outFileName);

}

//class impelementing given interface

public Class Salaries implements Raiseable{

//create empty file

public void create(String filename){

  

File f = new File(filename);

}

//displaying contents of the file

public void display(String filename){

BufferedReader br = new BufferedReader(new FileReader(filename));

for (String line; (line = br.readLine()) != null;) {

System.out.print(line);

}

br.close();

}

//add contents to file

public boolean addTo(String inFile,String outFile,int id,double salary,int yearsOfService)

{

//initially making a copy of a file

InputStream is = null;

OutputStream os = null;

try {

is = new FileInputStream(inFile);

os = new FileOutputStream(outFile);

byte[] buffer = new byte[1024];

int length;

while ((length = is.read(buffer)) > 0) {

os.write(buffer, 0, length);

}

} finally {

is.close();

os.close();

}

  

//searching if given id is present in the output file

double count = 0,countBuffer=0,countLine=0;

String lineNumber = "";

String filePath = outFile;

BufferedReader br;

String inputSearch = id;

String line = "";

try {

br = new BufferedReader(new FileReader(filePath));

try {

while((line = br.readLine()) != null)

{

countLine++;

  

String[] words = line.split(" ");

for (String word : words) {

if (word.equals(inputSearch)) {

count++;

countBuffer++;

}

}

if(countBuffer > 0)

{

countBuffer = 0;

lineNumber += countLine + ",";

}

}

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  

  

//if already present then exiting

if(count>0){

return false;

}

  

  

  

  

//if id not present then adding it to file

else{

  

  

BufferedWriter bw = null;

FileWriter fw = null;

try {

String content = <id>:<salary>:<yearsOfService> ";

fw = new FileWriter(outFile);

bw = new BufferedWriter(fw);

bw.write(content);

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null)

bw.close();

if (fw != null)

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

return true;

  

}

  

}

  

  

  

  

//remove contents from file

  

public boolean removeFrom(String inFile,String outFile,intid,double salary,int yearsOfService){

  

//initially making a copy of a file

InputStream is = null;

OutputStream os = null;

try {

is = new FileInputStream(inFile);

os = new FileOutputStream(outFile);

byte[] buffer = new byte[1024];

int length;

while ((length = is.read(buffer)) > 0) {

os.write(buffer, 0, length);

}

} finally {

is.close();

os.close();

}

  

  

  

  

//searching if given id is present in the output file

double count = 0,countBuffer=0,countLine=0;

String lineNumber = "";

String filePath = outFile;

BufferedReader br;

String inputSearch = id;

String line = "";

  

try {

br = new BufferedReader(new FileReader(filePath));

try {

while((line = br.readLine()) != null)

{

countLine++;

//System.out.println(line);

String[] words = line.split(" ");

for (String word : words) {

if (word.equals(inputSearch)) {

count++;

countBuffer++;

}

}

if(countBuffer > 0)

{

countBuffer = 0;

lineNumber += countLine + ",";

}

}

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  

//if id present then removing it from file

if(count>0){

  

File inputFile = new File(outFile);

File tempFile = new File("myTempFile.txt");

BufferedReader reader = new BufferedReader(new FileReader(inputFile));

BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String lineToRemove = <id>:<salary>:<yearsOfService>;

String currentLine;

while((currentLine = reader.readLine()) != null) {

// trim newline when comparing with lineToRemove

String trimmedLine = currentLine.trim();

if(trimmedLine.equals(lineToRemove)) continue;

writer.write(currentLine + System.getProperty("line.separator"));

}

writer.close();

reader.close();

boolean successful = tempFile.renameTo(inputFile);

  

  

return true;

}

  

  

//if not present then exiting

else{

return flase;

  

  

}

}

  

  

//merging files in to another one

public void mergeFiles(String inFileName1,String inFileName2,String outFileName){

  

  

  

PrintWriter pw = new PrintWriter(outFileName);

BufferedReader br1 = new BufferedReader(new FileReader(inFileName1));

BufferedReader br2 = new BufferedReader(new FileReader(inFileName2));

String line1 = br1.readLine();

String line2 = br2.readLine();

// loop to copy lines of

// file1.txt and file2.txt

// to outFile.txt alternatively

while (line1 != null || line2 !=null)

{

if(line1 != null)

{

pw.println(line1);

line1 = br1.readLine();

}

if(line2 != null)

{

pw.println(line2);

line2 = br2.readLine();

}

}

pw.flush();

// closing resources

br1.close();

br2.close();

pw.close();

  

  

  

  

  

  

  

}

  

//main method

public static void main(String[] args){

  

  

// creating object

Salaries sal=new Salaries();

  

//calling methods

sal.create("file1.txt");

sal.addTo("file1.txt","file2.txt",1,10000,1);

sal.display("outFile.txt");

sal.removeFrom("file1.txt","file2.txt",1,10000,1);

sal.display("outFile.txt");

sal.mergeFiles("file1.txt","file2.txt","file3.txt");

sal.display("file3.txt");

  

  

}

  

  

}