-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTacoDB.java
122 lines (121 loc) · 2.6 KB
/
TacoDB.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
import java.io.*;
import java.util.*;
public class TacoDB {
private Taco[] tacos;
public static final int DEF_SIZE = 128;
public static final String DELIM = "\t";
public static final int FIELD_AMT = 3;
public TacoDB()
{
init(DEF_SIZE);
}
public TacoDB(int size)
{
init(size);
}
private void init(int size) //Initialization
{
if(size > 0)
tacos = new Taco[size];
}
public void addTaco(Taco aTaco)
{
for(int i = 0;i < tacos.length; i++)
{
if(tacos[i] == null)
{
tacos[i] = aTaco;
break; //Don't forget this
}
}
}
public void removeTaco(Taco aTaco)
{
int rmIndex = -1;
for(int i = 0; i < tacos.length; i++)
{
if(tacos[i] == null)
return;
if(tacos[i].equals(aTaco))
{
rmIndex = i;
break;
}
}
if(rmIndex == -1)
return;
for(int i = rmIndex; i < tacos.length - 1; i++)
tacos[i] = tacos[i+1];
tacos[tacos.length - 1] = null;
}
public void printInfo()
{
for(Taco t : tacos)
if(t != null)
System.out.println(t);
}
public void writeDBFile(String aFileName)
{
try
{
PrintWriter fileWriter = new PrintWriter(new FileOutputStream(aFileName));
//Header
//fileWriter.println("Num Tacos" + DELIM + tacos.length);
//Body
for(Taco t : tacos)
{
if(t == null)
break;
fileWriter.println(t.getName() + DELIM + t.getLocation() + DELIM + t.getPrice());
}
fileWriter.close(); //Don't forget
}
catch(Exception e)
{
System.out.println(e);
}
}
/**
*
* @param aFileName
*/
public void readTacoDBFile(String aFileName)
{
try
{
Scanner fileScanner = new Scanner(new File(aFileName));
int size = 0;
while(fileScanner.hasNextLine())
{
size++;
fileScanner.nextLine();
}
fileScanner = new Scanner(new File(aFileName));
// //Read in the header
// String input = fileScanner.nextLine(); //Read the line
// String [] splitLine = input.split(DELIM);
// if(splitLine.length != 2) //Check the line
// return;
// //Process it
// int size = Integer.parseInt(splitLine[1]);
// this.init(size);
//Read the body
while(fileScanner.hasNextLine())
{
String input = fileScanner.nextLine();
String[] splitLine = input.split(DELIM);
if(splitLine.length != FIELD_AMT)
continue;
String name = splitLine[0];
String loc = splitLine[1];
double price = Double.parseDouble(splitLine[2]);
this.addTaco(new Taco(name, loc, price));
}
fileScanner.close(); //Don't forget
}
catch(Exception e)
{
System.out.println(e);
}
}
}