In: Computer Science
1).Modify the project so that records are inserted into the random access file in ascending order using an insertion sort methodology with the Social Security Number 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 writes d into the file.
2. With the new organization of the data files. find() and modify() must also be modified. Both methods should now stop their sequential search when they encounter a record greater than the record looked for, or they reach the end of the file.
I am having trouble initializing the compareTo()
Personal.java
import java.io.*;
import java.util.Arrays;
import Student.Student;
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 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);
}
public int compareTo(String p) {
}
}
Student.java
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);
}
public int compareTo() {
}
}
DbObject.java
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();
}
IOmethods,java
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 == '\n') // end of file or end of
line;
break;
else if ((char)ch != '\r') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}
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();
}
}
}
UseDatabase.java
import java.io.IOException;
public class UseDatabase {
static public void main(String a[]) throws IOException {
// (new Database()).run(new Personal());
(new Database()).run(new Student());
}
}
compareTo method used for compare the int value of objects if you want to compare strings then use compare method of Comparator class.
/************************************Personal.java*****************************/
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Comparator;
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 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);
}
// compare by ssn
public static Comparator<Personal> ssnComparator
= new Comparator<Personal>() {
public int
compare(Personal p1, Personal p2) {
String ssnP1 =
p1.SSN.toUpperCase();
String ssnP2 =
p2.SSN.toUpperCase();
//
ascending order
return
ssnP1.compareTo(ssnP2);
}
};
}
Use this method to compare the ssn of personal object in the selection sort
You can use this method as in selection sort :-
Personal.ssnComparator.compare(p1,
p2), it will return an integer value so you can sort it.
And in case of integer SSN then use this
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Comparator;
public class Personal extends IOmethods implements DbObject,
Comparable<Personal> {
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 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);
}
// compare by ssn
public static Comparator<Personal> ssnComparator
= new Comparator<Personal>() {
public int compare(Personal p1,
Personal p2) {
String ssnP1 =
p1.SSN.toUpperCase();
String ssnP2 =
p2.SSN.toUpperCase();
// ascending
order
return
ssnP1.compareTo(ssnP2);
}
};
@Override
public int compareTo(Personal o) {
if (Integer.parseInt(this.SSN) < Integer.parseInt(o.SSN)) {
return -1;
} else if
(Integer.parseInt(this.SSN) > Integer.parseInt(SSN))
{
return 1;
} else {
return 0;
}
}
}