|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.ObjectSlotDataMgr
This class handles slot data persistence for the drink machine software.
The data is stored in a proprietary binary file format to speed things up.
The class is instantiated with the name of the file that is to store the data.
The name is saved, and the file reference is opened with "rw" as its mode.
The file mode CANNOT BE "rwd"!!
The tini JVM throws exceptions and generally does not work with synchronous
commands.
There are three functions for writing to the file:
saveSlotArray, updateSlot, updateSlotCounts.
saveSlotArray takes an array of slots and saves them to the file. This will overwrite
all data. This should probably only be used when the program is exiting.
updateSlot will update the entire record for that one slot. This should be used
when more than just the can counts have change, such as when the admin changes the name.
updateSlotCounts will update _JUST_ the number of cans left and the number of cans dropped.
This is for when a drop occurs.
Each record is fixed length. Since the name of the record can vary, the padding is
put right after the name.
File format (byte widths below): | number of records | record 1 | record 2 | ... | record n | | 1 | 28 | 28 | | 28 | Record format: | slot index | name length | name + padding | cans left | cans dropped | price | enabled | | 1 | 2 | 18 | 2 | 2 | 2 | 1 | total record length = 28 bytes.
| Field Summary | |
(package private) boolean |
closed
|
(package private) RandomAccessFile |
file
|
(package private) String |
filename
|
static int |
HEADER_LEN
The width, in bytes, of the file header/preamble. |
static int |
RECORD_LEN
The width, in bytes, of each record. |
| Constructor Summary | |
SlotDataMgr(String filename)
Instantiate the class with the name of the file to read/write data to and from. |
|
| Method Summary | |
void |
close()
Tells this class to close the file and invalidate itself. |
void |
printSlots()
A helper method to print all the slot data in the file. |
Slot[] |
readFile()
Reads the slot data out of the file. |
void |
saveSlotArray(Slot[] slotArray)
Write an entire array of slots out to the file this class is handling. |
void |
updateSlot(Slot slot)
Updates the data for just the indicated slot. |
void |
updateSlotCounts(int slotIndex,
int canCount,
int droppedCount)
Updates just the 'cans' and 'dropped' fields for the given slot index. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static int RECORD_LEN
public static int HEADER_LEN
String filename
RandomAccessFile file
boolean closed
| Constructor Detail |
public SlotDataMgr(String filename)
filename - The name of the file to read/write from.| Method Detail |
public Slot[] readFile()
public void saveSlotArray(Slot[] slotArray)
slotArray - The array of slots to write to persistent storage.public void updateSlot(Slot slot)
slot - The slot to update.
public void updateSlotCounts(int slotIndex,
int canCount,
int droppedCount)
slotIndex - The index of the slot to edit.canCount - The new number of cans left in the slot.droppedCount - The new number of cans that have been dropped from the slot.public void close()
public void printSlots()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||