-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyList.java
213 lines (199 loc) · 5.64 KB
/
PropertyList.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* CS-112 FINAL PROJECT
File Name: PropertyList.java
Programmer: James Watkins
Date Last Modified: May 19, 2012
Problem Statement: Define a class which uses ArrayList objects to
manage multiple objects of class Item. This class must be serializable
and should also support text I/O operations. This class will interface
with a gui.
Overall Plan:
1. Provide constructors for creating ArrayLists of type Item.
2. Include a means of copying one propertyList to another (included for
good measure, not required for objectives).
2. Define class specific methods which invoke ArrayList's methods add(),
remove(), isEmpty(), clear(), get(), and size(). Overload the get() method
so that it can be called with parameters int or Item.
3. Create a method to calculate the total value of all assets using an
enhanced for loop and invocations of Item's getPrice() and getQty().
4. Override toString.
5. Make the class serializable to support binary I/O operations.
Classes needed and Purpose (Input, Processing, Output)
NumberFormat - output
String - input, output
*/
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;
import java.io.Serializable;
public class PropertyList implements Serializable
{
Scanner keyboard = new Scanner(System.in);
ArrayList <Item> dataBase;
PrintWriter textOutput;
private ObjectOutputStream output;
private ObjectInputStream inputStream = null;
NumberFormat money = NumberFormat.getCurrencyInstance();
public PropertyList()
{
dataBase = new ArrayList <Item>();
}
public PropertyList(int size)
{
dataBase = new ArrayList <Item>(size);
}
public void copyDataBase()
{
ArrayList<Item> newArrayList = new ArrayList<Item>(getDataBaseSize());
for (Item element : dataBase)
{
newArrayList.add(element);
}
}
//provide access to required ArrayList methods.
public void addToDataBase(Item anItem)
{
dataBase.add(anItem);
}
public void removeFromDataBase(int theLocation)
{
dataBase.remove(theLocation);
}
public boolean listIsEmpty()
{
return dataBase.isEmpty();
}
public void clearDataBase()
{
dataBase.clear();
}
public int getDataBaseSize()
{
return dataBase.size();
}
public Item getItem(int location)
{
return dataBase.get(location);
}
// public Item getItem(Item anItem)
// {
// return anItem;
// }
public double getTotalValue()
{
double totalValue = 0;
//method to calculate the current value of all assets
for (Item element : dataBase)
{
int aQuantity = element.getQty();
if(aQuantity == 1)
totalValue += element.getPrice();
else
totalValue += (element.getPrice() * aQuantity);
}
return totalValue;
}
public int searchDataBase(String query)
{
//search by description, serialnumber, make and model
int itemLocation = -1, i = 0;
for(Item element : dataBase)
{
if (query.equalsIgnoreCase(element.getDescription())||
query.equalsIgnoreCase(element.getSerial())||
query.equalsIgnoreCase(element.getModel())||
query.equalsIgnoreCase(element.getMake()))
{
itemLocation = i;
break;
}
i++;
}
//if query not found, a negative returned value can be filtered out
return itemLocation;
}
public void listAllRecords()
{
for(int i = 0; i < dataBase.size(); i++)
{
System.out.println(dataBase.get(i).toString());
}
}
public void printTextFile(String fileName)
{
int item = 1;
try
{
textOutput = new PrintWriter(new FileOutputStream(fileName));
for(Item element : dataBase)
{
//Item's toString didn't preserve formatting when passed through PrintWriter
textOutput.println("Item Number "+item);
textOutput.println("Description\t"+element.getDescription());
textOutput.println("Manufacturer\t"+element.getMake());
textOutput.println("Model Number\t"+element.getModel());
textOutput.println("Serial Number\t"+element.getSerial());
textOutput.println("Purchase Date\t"+element.getDate());
textOutput.println("Purchase Price\t"+
money.format(element.getPrice()));
textOutput.println("Quantity Avail.\t"+element.getQty());
textOutput.println("Picture File\t" + element.getPic());
textOutput.println("");
item++;
}
textOutput.println("Total Inventory:\t"+
money.format(getTotalValue()));
textOutput.close();
System.out.println("File " + fileName + " created.");
}
catch(IOException e)
{
System.out.println("Couldn't write to file " + fileName);
}
}
public void writeToFile(String fileName)
{
//removes any unused elements prior to writing to file.
dataBase.trimToSize();
try
{
ObjectOutputStream outputStream = new ObjectOutputStream
(new FileOutputStream(fileName));
//write file only if dataBase isn't empty
if (!dataBase.isEmpty())
{
outputStream.writeObject((ArrayList<Item>)dataBase);
outputStream.close();
System.out.println("Attempting to write.");
}
System.out.println("WriteToFile complete.");
}
catch(IOException e)
{
System.err.println("(IO)Error creating binary file " + fileName);
}
}
public void readFromFile(String fileName)
{
try
{
ObjectInputStream inputStream =
new ObjectInputStream(new FileInputStream(fileName));
dataBase = (ArrayList<Item>)inputStream.readObject();
inputStream.close();
System.out.println("Read from file complete");
}
catch(FileNotFoundException e)
{
System.out.println("Can not find binary file " + fileName);
}
catch(ClassNotFoundException e)
{
System.out.println("Can not find class specified.");
}
catch(IOException e)
{
System.out.println("(IO)Can not find binary file " + fileName);
}
}
}