Class SlotDataMgr

java.lang.Object
  extended bySlotDataMgr

public class SlotDataMgr
extends Object

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.
 

Author:
Kevin Thompson

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

RECORD_LEN

public static int RECORD_LEN
The width, in bytes, of each record.


HEADER_LEN

public static int HEADER_LEN
The width, in bytes, of the file header/preamble. Currently the header is one byte wide, and stores the number of records.


filename

String filename

file

RandomAccessFile file

closed

boolean closed
Constructor Detail

SlotDataMgr

public SlotDataMgr(String filename)
Instantiate the class with the name of the file to read/write data to and from. The filename argument is saved, and then the file variable is instantiated.

Parameters:
filename - The name of the file to read/write from.
Method Detail

readFile

public Slot[] readFile()
Reads the slot data out of the file.

Returns:
An array of Slot objects, created from the data in the file.

saveSlotArray

public void saveSlotArray(Slot[] slotArray)
Write an entire array of slots out to the file this class is handling. This method should not be used when you just updateing the 'can' and 'dropped' fields of a single slot, nor when you are just updating a whole single slot. This is usually used to force a complete write of all the slot information, for example when the system is shutting down.

Parameters:
slotArray - The array of slots to write to persistent storage.

updateSlot

public void updateSlot(Slot slot)
Updates the data for just the indicated slot. This should not be used when only the 'cans' or 'dropped' fields of the slot is being updated, but instead when any other combination of fields are altered. An example would be when the name of the indicated slot changes, or when the enabled status changes.

Parameters:
slot - The slot to update.

updateSlotCounts

public void updateSlotCounts(int slotIndex,
                             int canCount,
                             int droppedCount)
Updates just the 'cans' and 'dropped' fields for the given slot index. This is usually used when a drop has occured, and the only two data elements to change are the number of cans left ( which goes down one) and the number of cans dropped (which will go up one).

Parameters:
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.

close

public void close()
Tells this class to close the file and invalidate itself.


printSlots

public void printSlots()
A helper method to print all the slot data in the file.