forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsePartsDB.java
More file actions
60 lines (55 loc) · 1.83 KB
/
Copy pathUsePartsDB.java
File metadata and controls
60 lines (55 loc) · 1.83 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
import java.io.RandomAccessFile;
import java.io.IOException;
public class UsePartsDB{
public static void main(String[] args) throws IOException{
PartsDB pdb = null;
pdb = new PartsDB("parts.db");
if(pdb.numRecs()==0){
// populate the database with records.
pdb.append("1-9009-3323-4x", "Wiper Blade Micro Edge", 30, 2468);
pdb.append("1-3233-44923-7j", "Parking Brake Cable", 5, 1439);
pdb.append("2-3399-6693-2m", "Halogen Bulb H4 55/60W", 22,813);
pdb.append("2-599-2029-6k", "Turbo Oil Line O-Ring", 26, 155);
pdb.append("3-1299-3299-9u", "Air Pump Electric", 9, 20200);
}
dumpRecords(pdb);
pdb.update(1, "1-3233-44923-7j", "Parking Brake Cable", 5, 1995);
dumpRecords(pdb);
pdb.close();
}
static void dumpRecords(PartsDB pdb) throws IOException{
for(int i=0;i<pdb.numRecs();i++){
PartsDB.Part part = pdb.select(i);
System.out.print(format(part.getPartnum(), PartsDB.PART_NUM_LENGTH, true));
System.out.print(" | ");
System.out.print(format(part.getDesc(), PartsDB.DESC_LENGTH, true));
System.out.print(" | ");
System.out.print(format("" + part.getQuantity(), 10, false));
System.out.print(" | ");
String unitCostString = part.getUnitCost()/100 + "." + part.getUnitCost()%100/10 + part.getUnitCost()%10;
System.out.println(format(unitCostString, 10, false));
}
System.out.println("Number of records = " + pdb.numRecs());
System.out.println();
}
static String format(String value, int maxWidth, boolean leftAlign){
StringBuffer sb = new StringBuffer();
int len = value.length();
if(len>maxWidth){
len = maxWidth;
value = value.substring(0, len);
}
if(leftAlign){
sb.append(value);
for(int i=0; i<maxWidth-len;i++){
sb.append(" ");
}
}else{
for(int i=0;i<maxWidth-len;i++){
sb.append(" ");
}
sb.append(value);
}
return sb.toString();
}
}