Emperial
Emperial

Reputation: 91

Print from array list

I need to be able to print out all students that have a loan from array list.

Meaning all students with amount above 0.

I already can print all students and how much they own.. but i dont know how to make it print only the students with a loan.

here is the current code

public class LoanCompany
{
    private ArrayList<StudentLoan> loans;
    /**
     * Constructor for objects of class LoanCompany
     */
    public LoanCompany()
    {
        loans = new ArrayList<StudentLoan>();
    }
    /**
     * add a loan amount and ID.
     */
    public void addLoan(String ID,int Amount) 
    { 
        StudentLoan newSLoan= new StudentLoan(ID,Amount);
        loans.add(newSLoan); 
    }
    /**
     * Return amount of loans
     */
    public int getNumberOfLoans()
    {
        return loans.size();
    }
    public void printLoanDitails()
    {
        System.out.println("Loan Summery: ");
        int index=0;
        for(StudentLoan loan : loans) {
            System.out.print(index + " : ");
            loan.printDetails();
            index++;
        }
        System.out.println();
    }
    /**
     * Remove index.
     */
    public void removeLoan(int index)
    {
        if(validIndex(index)) {
            loans.remove(index);
        }
    }
    /**
     * Valid index... validates the index.
     */
   private boolean validIndex(int index)
    {
        boolean valid;        
        if(index < 0) {
            System.out.println("Index must be 0 or a positive number");
            valid = false;
        }
        else if(index >= loans.size()) {
            System.out.println("Index is too large.");
            valid = false;
        }
        else {
            valid = true;
        }
        return valid;
    }
    /**
     * PayOff method, pays off the student loan.
     */
    public void payOff(int index, int Amount)
    {
        if(validIndex(index)) {
            StudentLoan student = loans.get(index);
            student.payOff(Amount);
            }
            else {
                System.out.println("Invalid Index.");
            }
     }
     /**
     * Prints outstanding loans.
     */
    public void printOutstandingLoans()
    {
        System.out.println("Loan Summery: ");
        for(StudentLoan loan : loans ) {
            int index=0;
            if (loan.getAmount() > 0) {
            System.out.print(index + " : ");
            loan.printDetails();
            index++;
            }           
        }
        System.out.println();
    }
    public void removeClearedLoans() {
       for(StudentLoan loan : loans ) {
           int index=0;
        if (loan.getAmount() == 0) {
            loans.remove(loan);
             System.out.print( "Cleared Loans Removed ");
            }
            index++;
        }
    }
}

Upvotes: 0

Views: 183

Answers (3)

Octoshape
Octoshape

Reputation: 1141

I assume that StudentLoan has a field public int loanAmount then you could do this:

for(StudentLoan loan : loans) {
        int index=0;
        if (loans.loanAmount > 0) {
            System.out.print(index + " : ");
            loan.printDetails();
            index++;
        }
    }

This way only loans with loanAmount > 0 are being printed.

If the field loanAmount would be private you could simply implement a getter method for the field and change the condition to:

if (loan.getloanAmount() > 0) {

EDIT

Answering your question from the comments:

To remove all loans that have a loamAmount of 0 we simply add another if to the for each loop:

for(StudentLoan loan : loans) {
        int index=0;

        // print loans with amount > 0

        if (loan.loanAmount > 0) {
            System.out.print(index + " : ");
            loan.printDetails();
            index++;
        }

        // delete loans with amount = 0

        if (loan.loanAmount == 0) {
           loans.remove(loan) // UNSAFE! see edit below
        }
    }

EDIT

Using .remove while iterating over a collection is unsafe. It should be done with an iterator like so:

import java.util.Iterator // add to imports

Iterator<StudentLoan> i = loans.iterator();

while (i.hasNext()) {
    StudentLoan loan = i.next();
    if (loan.getAmount() == 0) {
        i.remove();
    }
}

Upvotes: 1

Alex
Alex

Reputation: 271

not exactly sure what you are doing with your print statement without seeing your studnetLoan class

You have:

for(StudentLoan loans : loan) {
        int index=0;
        System.out.print(index + " : ");
        loans.printDetails();
        index++;
    }

I Would do something more like this:

for(StudentLoan loan : loanList) {         
        if(loan.getAmount() > 0) 
           loan.printDetails();

    }

Note that I have named the list of loans "loanList" or it could be "loans" but it makes no sense to call it "loan"

Upvotes: 0

Qoph
Qoph

Reputation: 43

I'm guessing you have a getter for the variable Amount in the StudentLoan objects, ie, getAmount(). If not, you should create one.

The print method would change to:

public void printLoanDitails()
{
    System.out.println("Loan Summery: ");
    int index=0;  //This should be outside the loop, or it will be set to 0 each time
    for(StudentLoan loans : loan) {

        if(loans.getAmount() > 0)
        {
            System.out.print(index + " : ");
            loans.printDetails();
            index++;
        }
    }
    System.out.println();
}

You should probably add this logic to the PayOff method as well.

Upvotes: 1

Related Questions