DViga
DViga

Reputation: 47

How to create a LinkedList consists of two separate classes?

I've three classes: Account.java, Transaction.java and StudentPrint.java (this last one is the main class). I need to create a linked list with a list of the Account class and Transaction class inside of the main class: StudentPrint. Also I need to have a list of last 6 transactions inside of the Account class.

There might be other ways to do this, but I have requirements is do it as the described way. I have 3 classes one of them being the class where I have the main method. I need to have an account class, a transaction class. Within the class account I need have the attributes of the student and in the transaction class the transaction type (pop-up, print) and transaction amount and transaction data and time and I need to have a linked list so that within the class account I can have a method that hold the last 6 transactions that a student did.

Upvotes: 0

Views: 123

Answers (1)

Milky
Milky

Reputation: 44

From what I understand from the details you provided, I assume what you're looking for is a Data-Structure to manage those last 6 transactions.

One, that's designed for this problem, is called "Fixed-Sized Circular Queue".

Check out these 2 Stack Overflow links for implementation examples or other ideas:

Is there a fixed sized queue which removes excessive elements?

Size-limited queue that holds last N elements in Java

.

It should be implemented like this:

public class Account {
    private int studentId;
    ...
    CircularFifoQueue<Integer> lastTransactions = new CircularFifoQueue<Integer>(6);
}

public class StudentPrint {
    LinkedList<Account> accountList = new LinkedList<Account>();
}

Upvotes: 1

Related Questions