Repository navigation
Expand file tree
/
Copy pathLab6.java
More file actions
182 lines (172 loc) · 5.99 KB
/
Copy pathLab6.java
File metadata and controls
182 lines (172 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.io.*;
import java.util.Scanner;
public class Lab6 {
public static int count;
public static final int[] Address_list = new int[100];
public static final String[] Name_list = new String[100];
public static Scanner s = new Scanner(System.in);
public static void main(String[] args) throws IOException {
Lab6 obj = new Lab6();
obj.create_index();
int ch;
System.out.println("******Menu******");
System.out.println("1. Add Record");
System.out.println("2. Search Record");
System.out.println("3. Remove Record");
System.out.println("4. Exit");
System.out.println("****************");
while (true) {
System.out.println("\nPlease enter your choice:");
ch = s.nextInt();
s.nextLine();
switch (ch) {
case 1:
obj.insert();
break;
case 2:
obj.search();
break;
case 3:
obj.remove();
break;
case 4:
System.out.println("Do you want to exit? (Y/N)");
if (s.next().equalsIgnoreCase("y")) {
System.out.println("Program Ended");
System.exit(0);
}
break;
default:
System.out.println("Invalid Option");
}
}
}
public void insert() throws IOException {
PrintWriter pw = new PrintWriter(new FileOutputStream("f1.txt", true));
System.out.println("Enter USN,Name,Sem and Branch ");
String usn = s.nextLine();
String name = s.nextLine();
String sem = s.nextLine();
String branch = s.nextLine();
String b = usn + "|" + name + "|" + sem + "|" + branch + "|" + "$";
pw.println(b);
pw.close();
create_index();
}
public void create_index() throws IOException, ArrayIndexOutOfBoundsException {
count = -1;
long pos;
RandomAccessFile file = new RandomAccessFile("f1.txt", "r");
pos = file.getFilePointer();
String s;
while ((s = file.readLine()) != null) {
String[] result = s.split("\\|");
count++;
Name_list[count] = result[1];
Address_list[count] = (int) pos;
pos = file.getFilePointer();
}
file.close();
sort_index();
}
public void sort_index() {
for (int i = 0; i <= count; i++) {
for (int j = i + 1; j <= count; j++) {
if (Name_list[i].compareTo(Name_list[j]) > 0) {
String temp = Name_list[i];
Name_list[i] = Name_list[j];
Name_list[j] = temp;
int temp1 = Address_list[i];
Address_list[i] = Address_list[j];
Address_list[j] = temp1;
}
}
}
}
public void search() throws IOException {
int pos;
System.out.println("Enter the name to be searched");
String key = s.nextLine();
int t = 0;
pos = search_index(key);
if (pos != -1) {
display_record(pos);
t = pos;
while ((t < count) && (Name_list[++t].equals(key)))
display_record(t);
t = pos;
while ((t > 0) && (Name_list[--t].equals(key)))
display_record(t);
} else
System.out.println("Record not found");
}
public int search_index(String key) {
int low = 0, high = count, mid = 0;
while (low <= high) {
mid = (low + high) / 2;
if (Name_list[mid].equals(key))
return mid;
if (Name_list[mid].compareTo(key) > 0)
high = mid - 1;
if (Name_list[mid].compareTo(key) < 0)
low = mid + 1;
}
return -1;
}
public void display_record(int pos) throws IOException {
RandomAccessFile file = new RandomAccessFile("f1.txt", "r");
int address = Address_list[pos];
String usn, sem, branch, name;
file.seek(address);
String s = file.readLine();
while (s != null) {
String[] result = s.split("\\|");
usn = result[0];
name = result[1];
sem = result[2];
branch = result[3];
System.out.println("\nRecord Details");
System.out.println("USN: " + usn);
System.out.println("Name: " + name);
System.out.println("Sem: " + sem);
System.out.println("Branch: " + branch);
break;
}
file.close();
}
public void remove() throws IOException {
int pos, t;
System.out.println("Enter the key to be deleted");
String key = s.nextLine();
pos = search_index(key);
if (pos != -1) {
delete_from_file(pos);
t = pos;
while ((t < count) && (Name_list[++t].equals(key)))
delete_from_file(t);
t = pos;
while ((t > 0) && (Name_list[--t].equals(key)))
delete_from_file(t);
create_index();
} else
System.out.println("Record not found");
}
public void delete_from_file(int pos) throws IOException {
display_record(pos);
RandomAccessFile file = new RandomAccessFile("f1.txt", "rw");
System.out.println("Are you sure you want to delete? (Y/N)");
String ch = s.nextLine();
if (ch.equalsIgnoreCase("y")) {
int address = Address_list[pos];
String del_ch = "*";
file.seek(address);
String str = file.readLine();
int x = str.indexOf('|');
x++;
file.seek(address + x);
file.writeBytes(del_ch);
System.out.println("Record is deleted");
}
file.close();
}
}