What are the limits on outside help? How much help can I give before you get in trouble?
I'll tell you, I'd do this as a doubly linked list.
Under this, you'd do a class like this:
Code:
class Record{
// all the data you need to collect on each person
// all the methods that operate on a record (account)
Record prev;
Record next;
//initialize both prev and next to null in the constructor
}
class Database{
private Record first;
private Record last;
private Record current;
//initialize all pointers (first, last, current) to null in constructor.
public void Start(){
// make current point to the first object
}
public void Advance(){
// make current point to the object after the one it currently points to. Check if current is null before doing this!
}
public bool IsCurrent(){
//return true if current is not null.
}
public Record GetCurrent(){
// return current
}
public void AddRecord (Record r){
//Add a record. Two general cases: empty list, nonempty list.
}
}
The way Database looks is like a list of records. Imagine one with 4 records, here I'll call them A through D. Each record R has 2 pointers, like this:
R.prev <- R -> R.next
Note that first.prev and last.next will both equal null.
Overall, the whole database of 4 objects conceptually looks like:
null <- A <-> B <-> C <-> D -> null
In this case, first would point to A, and last would point to D. Signify this with (f) and (l), and we get this picture:
null <- A(f) <-> B <-> C <-> D(l) -> null
Now, let's trace how adding a new record, E, will work.
Assume we have a Database object db, and a Record object E, and are doing db.AddRecord(E);
First, we add E after D in the list, by setting D.next to E.
null <- A(f) <-> B <-> C <-> D(l) -> E -> null
However, E is not backlinked to D yet. Set E.prev to D:
null <- A(f) <-> B <-> C <-> D(l) <-> E -> null
But, by definition, last should point to the last object we have linked, so we change it in the third and final step:
null <- A(f) <-> B <-> C <-> D <-> E(l) -> null
Up until this point, we've ignored current. Current is used in 'for' statements and elsewhere to go through the list systematically, for searches, for day advances, for anything else we need to do to each record.
Let (*) refer to current. Now, we execute
db.Start();
null <- A(f)(*) <-> B <-> C <-> D <-> E(l) -> null
after doing a db.Advance(), we would see:
null <- A(f) <-> B(*) <-> C <-> D <-> E(l) -> null
After 3 more, we would have:
null <- A(f) <-> B <-> C <-> D <-> E(l)(*) -> null
And, after one more, current should go to null -- this makes loops easier, you'll see how.
null <- A(f) <-> B <-> C <-> D <-> E(l) -> null (*)
Now, Advance should NOT change current in any way. Current is now a null pointer, and attempting to access its data members can't be done.
Why this is useful:
You can use a for loop like this to go through all records, easily:
for (db.Start();db.IsCurrent();db.Advance()){
Record r = db.GetCurrent();
// do whatever you have to with r here.
}
Because of the "fall off the end of the list" feature of current, it's easier to make loops that work right; otherwise you can easily get cases in which you never access the last record of the list. This is the simplest way to do linked lists, and one I learned and liked for how it can handle any situation.