-
Notifications
You must be signed in to change notification settings - Fork 0
/
TacoManager.java
148 lines (147 loc) · 2.89 KB
/
TacoManager.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
142
143
144
145
146
147
148
import java.util.*;
import java.io.*;
public class TacoManager {
private Taco[] tacos;
public static final int DEF_SIZE = 100;
public static final String DELIM = "\t";
public static final int FIELD_AMT = 3;
public TacoManager()
{
init(DEF_SIZE);
}
public TacoManager(int aSize)
{
init(aSize);
}
public void init(int aSize)
{
if(aSize > 0)
{
tacos = new Taco[aSize];
}
}
public void addTaco(Taco aTaco)
{
/*
if(tacos[tacos.length - 1] != null) //Array is full
{
return;
}
*/
for(int i = 0; i < tacos.length; i++) //Found an empty space
{
if(tacos[i] == null)
{
tacos[i] = aTaco;
//break;
return;
}
}
System.out.println("The array is full.");
}
public void removeTaco(Taco aTaco)
{
int rmIndex = -1; //Sentinel value
//Find the index of the remove
for(int i = 0; i < tacos.length; i++)
{
if(tacos[i] != null && tacos[i].equals(aTaco))
{
rmIndex = i;
break;
}
}
//Does the taco exist?
if(rmIndex == -1)
{
return; //Taco was not found
}
for(int i = rmIndex; i < tacos.length - 1; i++)
{
tacos[i] = tacos[i+1];
}
tacos[tacos.length - 1] = null;
}
public void print()
{
for(int i = 0; i < tacos.length; i++)
{
if(tacos[i] == null)
{
break;
}
System.out.println(tacos[i]);
}
}
public void sortTacosByPrice()
{
//Bubble sort
boolean hasSwapped = true;
while(hasSwapped)
{
hasSwapped = false;
for(int i = 0; i < tacos.length - 1; i++)
{
if(tacos[i].getPrice() > tacos[i+1].getPrice())
{
//Swap
Taco temp = tacos[i];
tacos[i] = tacos[i+1];
tacos[i+1] = temp;
hasSwapped = true;
}
}
}
}
//File IO
public void writeToTacoFile(String aFileName)
{
try
{
PrintWriter fileWriter = new PrintWriter(new FileOutputStream(aFileName));
//Print the body
for(Taco t : tacos)
{
fileWriter.println(t.getName()+DELIM+t.getLocation()+DELIM+t.getPrice());
}
fileWriter.close(); //Don't forget
}
catch(Exception e)
{
System.out.println(e);
}
}
public void readATacoFile(String aFileName)
{
try
{
Scanner fileScanner = new Scanner(new File(aFileName));
int count = 0;
while(fileScanner.hasNextLine())
{
count++;
fileScanner.nextLine();
}
init(count);
fileScanner = new Scanner(new File(aFileName));
while(fileScanner.hasNextLine())
{
String fileLine = fileScanner.next();
String[] splitLine = fileLine.split(DELIM);
if(splitLine.length != FIELD_AMT)
{
continue;
}
String aName = splitLine[0];
String aLoc = splitLine[1];
double aPrice = Double.parseDouble(splitLine[2]);
this.addTaco(new Taco(aName, aLoc, aPrice));
}
fileScanner.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}