0

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.

DViga
  • 47
  • 6
  • 1) It is not clear to me what is the question. 2) Many of the comments add nothing to the code and should be deleted. – Jonathan Rosenne Jan 06 '19 at 20:16
  • @JonathanRosenne 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. – DViga Jan 06 '19 at 20:27
  • What is oTransaction? Why is it needed? You are not limiting the transaction list to 6. – Jonathan Rosenne Jan 06 '19 at 20:38
  • Maybe @Asaf can help. – DViga Jan 08 '19 at 01:46

1 Answers1

1

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>();
}
Milky
  • 44
  • 10
  • You should hold an instance of this fixed(6)-size queue in every Account. And have a list of Accounts inside the main class. (You've already implemented the last part I've noticed) – Milky Jan 07 '19 at 20:50
  • Hello @Milky.Thank you for reply me. I need to have a LinkedList. Inside the class Account I have to have an ArrayList of the Transaction class. ArrayList ... and in the main class I have to implement the LinkedList of the Account class. Ok I know, but how do I insert the ArrayList data into the LinkedList because I can insert only the class Account object, but I can not enter the transaction. If I try to print the LinkedList only returns me the attributes of the account class. And if I do LinkedAccountList.add (objAcc, arrayListTransaction) returns error. – DViga Jan 08 '19 at 01:31
  • You're welcome, @DViga. You need to add a field of ArrayList inside Account class, just like you need a field of LinkedList inside your main class. – Milky Jan 09 '19 at 02:46
  • I've add code example to the answer. Please check it out. – Milky Jan 09 '19 at 04:04
  • Hi @Milky I tried do that, but when I try import import org.apache.commons.collections4.BoundedCollection, it return error "package org.apache.... do not exist. – DViga Jan 09 '19 at 04:49