/
P7_xPrintFileInOrder.java
142 lines (125 loc) · 5.56 KB
/
P7_xPrintFileInOrder.java
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
// Program P7.x - print file in order
import java.io.*;
import java.util.*;
public class P7_xPrintFileInOrder {
static final int StringFixedLength = 20;
static final int PartNumSize = 6;
static final int PartRecordSize = 64;
static final int MaxRecords = 100;
static final String EndOfData = "END";
public static void main(String[] args) throws IOException {
RandomAccessFile fp = new RandomAccessFile("parts.bin", "rw");
Index[] index = new Index[MaxRecords + 1];
createMasterIndex(index, fp);
saveIndex(index);
printFileInOrder(index, fp);
fp.close();
} //end main
public static void createMasterIndex(Index[] index,
RandomAccessFile f) throws IOException {
Scanner in = new Scanner(new FileReader("parts.txt"));
int numRecords = 0;
Part part = getPartData(in);
while (part != null) {
int searchResult = search(part.partNum, index, numRecords);
if (searchResult > 0)
System.out.printf("Duplicate part: %s ignored\n", part.partNum);
else { //this is a new part number; insert in location -searchResult
if (numRecords == MaxRecords) {
System.out.printf("Too many records: only %d allowed\n", MaxRecords);
System.exit(1);
}
//the index has room; shift entries to accommodate new part
for (int h = numRecords; h >= -searchResult; h--)
index[h + 1] = index[h];
index[-searchResult] = new Index(part.partNum, ++numRecords);
writePartToFile(part, f);
}
part = getPartData(in);
} //end while
index[0] = new Index("NOPART", numRecords);
in.close();
} //end createMasterIndex
public static Part getPartData(Scanner in) {
String pnum = in.next();
if (pnum.equals(EndOfData)) return null;
return new Part(pnum, in.next(), in.nextInt(), in.nextDouble());
} //end getPartData
public static void writePartToFile(Part part, RandomAccessFile f) throws IOException {
for (int h = 0; h < PartNumSize; h++) f.writeChar(part.partNum.charAt(h));
int n = Math.min(part.name.length(), StringFixedLength);
for (int h = 0; h < n; h++) f.writeChar(part.name.charAt(h));
for (int h = n; h < StringFixedLength; h++) f.writeChar(' ');
f.writeInt(part.amtInStock);
f.writeDouble(part.price);
} //end writePartToFile
public static void saveIndex(Index[] index) throws IOException {
RandomAccessFile f = new RandomAccessFile("index.bin", "rw");
int numRecords = index[0].recNum;
//fill the unused index positions with dummy entries
for (int h = numRecords+1; h <= MaxRecords; h++)
index[h] = new Index("NOPART", 0);
f.writeInt(MaxRecords);
for (int h = 0; h <= MaxRecords; h++) {
for (int i = 0; i < PartNumSize; i++)
f.writeChar(index[h].partNum.charAt(i));
f.writeInt(index[h].recNum);
}
f.close();
} //end saveIndex
public static int search(String key, Index[] list, int n) {
//searches list[1..n] for key. If found, it returns the location; otherwise
//it returns the negative of the location in which key should be inserted.
int lo = 1, hi = n;
while (lo <= hi) { // as long as more elements remain to consider
int mid = (lo + hi) / 2;
int cmp = key.compareToIgnoreCase(list[mid].partNum);
if (cmp == 0) return mid; // search succeeds
if (cmp < 0) hi = mid - 1; // key is 'less than' list[mid].partNum
else lo = mid + 1; // key is 'greater than' list[mid].partNum
}
return -lo; // key not found; insert in location lo
} // end search
public static Part readPartFromFile(RandomAccessFile f) throws IOException {
String pname = "";
for (int h = 0; h < PartNumSize; h++) pname += f.readChar();
char[] name = new char[StringFixedLength];
for (int h = 0; h < StringFixedLength; h++) name[h] = f.readChar();
String hold = new String(name, 0, StringFixedLength);
return new Part(pname, hold.trim(), f.readInt(), f.readDouble());
} //end readPartFromFile
public static void printFileInOrder(Index[] index, RandomAccessFile f) throws IOException {
System.out.printf("\nFile sorted by part number: \n\n");
int numRecords = index[0].recNum;
for (int h = 1; h <= numRecords; h++) {
f.seek(PartRecordSize * (index[h].recNum - 1));
readPartFromFile(f).printPart();
System.out.printf("\n");
}
} //end printFileInOrder
} //end class P7_8CreateIndex
class Part {
String partNum, name;
int amtInStock;
double price;
public Part(String pn, String n, int a, double p) {
partNum = pn;
name = n;
amtInStock = a;
price = p;
}
public void printPart() {
System.out.printf("Part number: %s\n", partNum);
System.out.printf("Part name: %s\n", name);
System.out.printf("Amount in stock: %d\n", amtInStock);
System.out.printf("Price: $%3.2f\n", price);
}
} //end class Part
class Index {
String partNum;
int recNum;
public Index(String p, int r) {
partNum = p;
recNum = r;
}
} //end class Index