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

Modify the project so tat records are inserted into the random acess file in asc

ID: 3787088 • Letter: M

Question

Modify the project so tat records are inserted into the random acess file in ascending order using an insertion sort methodology with the SSN acting as the key value. This requires defining the method compareTo() in the Personal and Student classes to be used in a modified method add() in Database. The method finds a proper position for a record d, moves all the records in the file to make room for d, and writres d into the file. With the new organization of the data files, find() and modify() can also be modified. For example, find() stops its sequential search when it encounters a record greater than the record looked for (or reaches the end of the file).

Database.java

import java.io.*;

public class Database {

private RandomAccessFile database;

private String fName = new String();;

private IOmethods io = new IOmethods();

Database() throws IOException {

System.out.print("File name: ");

fName = io.readLine();

}

private void add(DbObject d) throws IOException {

database = new RandomAccessFile(fName,"rw");

database.seek(database.length());

d.writeToFile(database);

database.close();

}

private void modify(DbObject d) throws IOException {

DbObject[] tmp = new DbObject[1];

d.copy(tmp);

database = new RandomAccessFile(fName,"rw");

while (database.getFilePointer() < database.length()) {

tmp[0].readFromFile(database);

if (tmp[0].equals(d)) {

   tmp[0].readFromConsole();

   database.seek(database.getFilePointer()-d.size());

   tmp[0].writeToFile(database);

   database.close();

   return;

}

}

database.close();

System.out.println("The record to be modified is not in the database");

}

private boolean find(DbObject d) throws IOException {

DbObject[] tmp = new DbObject[1];

d.copy(tmp);

database = new RandomAccessFile(fName,"r");

while (database.getFilePointer() < database.length()) {

tmp[0].readFromFile(database);

if (tmp[0].equals(d)) {

database.close();

return true;

}

}

database.close();

return false;

}

private void printDb(DbObject d) throws IOException {

database = new RandomAccessFile(fName,"r");

while (database.getFilePointer() < database.length()) {

d.readFromFile(database);

d.writeLegibly();

System.out.println();

}

database.close();

}

public void run(DbObject rec) throws IOException {

String option;

System.out.println("1. Add 2. Find 3. Modify a record; 4. Exit");

System.out.print("Enter an option: ");

option = io.readLine();

while (true) {

if (option.charAt(0) == '1') {

   rec.readFromConsole();

   add(rec);

}

else if (option.charAt(0) == '2') {

   rec.readKey();

   System.out.print("The record is ");

   if (find(rec) == false)

   System.out.print("not ");

   System.out.println("in the database");

}

else if (option.charAt(0) == '3') {

   rec.readKey();

   modify(rec);

}

else if (option.charAt(0) != '4')

   System.out.println("Wrong option");

else return;

printDb(rec);

System.out.print("Enter an option: ");

option = io.readLine();

}

}

}

import java.io.*;

public interface DbObject {

public void writeToFile(RandomAccessFile out) throws IOException;

public void readFromFile(RandomAccessFile in) throws IOException;

public void readFromConsole() throws IOException;

public void writeLegibly() throws IOException;

public void readKey() throws IOException;

public void copy(DbObject[] db);

public int size();

}

import java.io.*;

public class IOmethods {

public void writeString(String s, RandomAccessFile out) throws IOException {

for (int i = 0; i < s.length(); i++)

out.writeChar(s.charAt(i));

}

public String readString(int len, RandomAccessFile in) throws IOException {

String s = "";

for (int i = 0; i < len; i++)

s += in.readChar();

return s;

}

public String readLine() throws IOException {

int ch;

String s = "";

while (true) {

ch = System.in.read();

if (ch == -1 || (char)ch == ' ') // end of file or end of line;

   break;

else if ((char)ch != ' ') // ignore carriage return;

   s = s + (char)ch;

}

return s;

}

}

import java.io.*;

public class Personal extends IOmethods implements DbObject {

protected final int nameLen = 10, cityLen = 10;

protected String SSN, name, city;

protected int year;

protected long salary;

protected final int size = 9*2 + nameLen*2 + cityLen*2 + 4 + 8;

Personal() {

}

Personal(String ssn, String n, String c, int y, long s) {

SSN = ssn; name = n; city = c; year = y; salary = s;

}

public int size() {

return size;

}

public boolean equals(Object pr) {

return SSN.equals(((Personal)pr).SSN);

}

public void writeToFile(RandomAccessFile out) throws IOException {

writeString(SSN,out);

writeString(name,out);

writeString(city,out);

out.writeInt(year);

out.writeLong(salary);

}

public void writeLegibly() {

System.out.print("SSN = " + SSN + ", name = " + name.trim()

+ ", city = " + city.trim() + ", year = " + year

+ ", salary = " + salary);

}

public void readFromFile(RandomAccessFile in) throws IOException {

SSN = readString(9,in);

name = readString(nameLen,in);

city = readString(cityLen,in);

year = in.readInt();

salary = in.readLong();

}

public void readKey() throws IOException {

System.out.print("Enter SSN: ");

SSN = readLine();

}

public void readFromConsole() throws IOException {

System.out.print("Enter SSN: ");

SSN = readLine();

System.out.print("Name: ");

name = readLine();

for (int i = name.length(); i < nameLen; i++)

name += ' ';

System.out.print("City: ");

city = readLine();

for (int i = city.length(); i < cityLen; i++)

city += ' ';

System.out.print("Birthyear: ");

year = Integer.valueOf(readLine().trim()).intValue();

System.out.print("Salary: ");

salary = Long.valueOf(readLine().trim()).longValue();

}

public void copy(DbObject[] d) {

d[0] = new Personal(SSN,name,city,year,salary);

}

}

import java.io.*;

public class Student extends Personal {

public int size() {

return super.size() + majorLen*2;

}

protected String major;

protected final int majorLen = 10;

Student() {

super();

}

Student(String ssn, String n, String c, int y, long s, String m) {

super(ssn,n,c,y,s);

major = m;

}

public void writeToFile(RandomAccessFile out) throws IOException {

super.writeToFile(out);

writeString(major,out);

}

public void readFromFile(RandomAccessFile in) throws IOException {

super.readFromFile(in);

major = readString(majorLen,in);

}

public void readFromConsole() throws IOException {

super.readFromConsole();

System.out.print("Enter major: ");

major = readLine();

for (int i = major.length(); i < nameLen; i++)

major += ' ';

}

public void writeLegibly() {

super.writeLegibly();

System.out.print(", major = " + major.trim());

}

public void copy(DbObject[] d) {

d[0] = new Student(SSN,name,city,year,salary,major);

}

}

import java.io.*;

public class UseDatabase {

static public void main(String a[]) throws IOException {

// (new Database()).run(new Personal());

(new Database()).run(new Student());

}

}

Explanation / Answer

public category information non-public RandomAccessFile database;
personal String fName = new String();;
personal IOmethods io = new IOmethods();
Database() throws IOException
}
database.close();
System.out.println("The record to be changed isn't within the database");
}
personal Boolean find(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
information = new RandomAccessFile(fName,"r");
whereas (database.getFilePointer() &lt; information.length()) come back true;
}
}
database.close();
come back false;
}
personal void printDb(DbObject d) throws IOException {
information = new RandomAccessFile(fName,"r");
whereas (database.getFilePointer() &lt; information.length())
database.close();
}
public void run(DbObject rec) throws IOException
else if (option.charAt(0) == '2')
else if (option.charAt(0) != '4')
System.out.println("Wrong option");
else return;
printDb(rec);
System.out.print("Enter AN option: ");
possibility = io.readLine();
}
}
}





import java.io.*;

public interface DbObject




import java.io.*;

public category IOmethods
public String readString(int len, RandomAccessFile in) throws IOException (char)ch == ' ') // {end of file or finish of line;
break;
else if ((char)ch != ' ') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}







import java.io.*;

public category Personal extends IOmethods implements DbObject ten, cityLen = 10;
protected String SSN, name, city;
protected int year;
protected long salary;
protected final int size = 9*2 + nameLen*2 + cityLen*2 + four + 8;
Personal()
Personal(String ssn, String n, String c, int y, long s) town = c; year = y; wage = s;
}
public int size() {
come back size;
}
public Boolean equals(Object pr)
public void writeToFile(RandomAccessFile out) throws IOException
public void writeLegibly()
}






import java.io.*;

public category Student extends Personal {
public int size() {
come back super.size() + majorLen*2;
}
protected String major;
protected final int majorLen = 10;
Student()
Student(String ssn, String n, String c, int y, long s, String m)
public void writeToFile(RandomAccessFile out) throws IOException
public void readFromFile(RandomAccessFile in) throws IOException
public void readFromConsole() throws IOException
public void copy(DbObject[] d)
}









import java.io.*;

public category UseDatabase
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote