'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++;
}
}
}
Solution 1:[1]
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.
Solution 2:[2]
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"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qoph |
| Solution 2 | Alex |
