0

我有两个要一起使用的课程。一个数组列表类和一个文本用户界面类,它将调用数组列表类中的方法以完成任务。

每当我尝试从我的 TUI 类中调用带有数组列表类中的参数的方法时,它都会给我一个错误。

我正在尝试从 TUI 类访问我的 arrayList 类中的(添加)方法,这会将用户添加到我的 arrayList。有人可以告诉我如何解决这个问题。返回错误的方法是我的 TUI 类底部的“public void addBorrower()”。

我的两门课都在下面。

这是我的 TUI 课程。

import java.util.ArrayList;
import java.util.Scanner;

public class BorrowerTUI
{

private BorrowerList borrowerList;
private Scanner myScanner;


public BorrowerTUI()
{
    myScanner = new Scanner(System.in);
    BorrowerList borrowerList = new BorrowerList();
}

public void menu()
{
int command = -1;
while (command != 0)
{   
    displayMenu();
    command = getCommand();
    execute (command);
}
}



private void displayMenu()
{
System.out.println( "Options are" );
System.out.println( "Enter 1" );
System.out.println( "Enter 2" );
System.out.println( "Enter 3" );
System.out.println( "Enter 4" );

}

private void execute( int command)
{
if ( command == 1)
    addBorrower();
else
if ( command == 2 )
    getNumberOfBorrowers();
else
if ( command == 3)
    quitCommand();
else
if ( command == 4)
    quitCommand();
else
if ( command == 5)
    quitCommand();
else
    System.out.println("Unknown Command");
}


private int getCommand()
{
    System.out.print ("Enter command: ");
    int command = myScanner.nextInt();
    myScanner.nextLine(); 
    return command;
}

public void getNumberOfBorrowers()
{
    int command = myScanner.nextInt();
    System.out.println("We have" + borrowerList.getNumberOfBorrowers() + "borrowers");
}

public void quitCommand()
{
    int command = myScanner.nextInt();
    System.out.println("Application Closing");
    System.exit(0);
}


public void addBorrower()
{

    borrowerList.addBorrower(Borrower borrower);
}
}

这是我的数组列表类。

import java.util.ArrayList;

public class BorrowerList
{
   private ArrayList<Borrower> borrowers;    


   public BorrowerList()
   {
       borrowers = new ArrayList<Borrower>();
    }

   public void addBorrower(Borrower borrower)
   {
       borrowers.add(borrower);
   }

   public int getNumberOfBorrowers()
   {
       return borrowers.size();
   }

   public boolean getBorrower(String libraryNumber)
   {
       for(Borrower borrower : borrowers)

       borrower.getLibraryNumber();
       return true;
   }

   public void getBorrower(int borrowerEntry)
   {
       if (borrowerEntry < 0)
       {
           System.out.println("Negative entry: " + borrowerEntry);
       }
       else if (borrowerEntry < getNumberOfBorrowers())
       { 
           Borrower borrower = borrowers.get(borrowerEntry);
           borrower.printBorrowerDetails();
       }
       else
       {
           System.out.println("No such entry: " + borrowerEntry);
       }
   }

   public void getAllBorrowers()
   {
        for(Borrower borrower : borrowers)
        {
            borrower.printBorrowerDetails();
            System.out.println();
        }
   }

   public void removeBorrower(int borrowerEntry)
   {
        if(borrowerEntry < 0)
        {
            System.out.println("Negative entry :" + borrowerEntry);
        }
        else if(borrowerEntry < getNumberOfBorrowers())
        {
            borrowers.remove(borrowerEntry);
        }
        else
        {
            System.out.println("No such entry :" + borrowerEntry);
        }
   }

   public boolean removeBorrower(String libraryNumber)
   {
        int index = 0;
        for (Borrower borrower: borrowers)
        {
            if (libraryNumber.equals(borrower.getLibraryNumber()))
            {
                borrowers.remove(index);
                return true;
            }
            index++;
        }
        return false;
   }


   public int search(String libraryNumber)
   {
       int index = 0;
       for (Borrower borrower : borrowers)
       {
           if (libraryNumber.equals(borrower.getLibraryNumber()))
           {
               return index;
           }
           else
           {
               index++;
           }
       }
       return -1;
   }    
}

借款人类别:

public class Borrower
{
    private String firstName;
    private String lastName;
    private String libraryNumber;
    private int noOfBooks;
    private Address address;


    public Borrower(String fName, String lName, String lNumber, 
                    String street, String town, String postcode)
    {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = 1;        
        address = new Address(street, town, postcode);
    }


    public Borrower(String fName, String lName, String lNumber, int     numberOfBooks, 
                    String street, String town, String postcode)
    {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = numberOfBooks;         
        address = new Address(street, town, postcode);
    }


    public String getFirstName()
    {
        return firstName;
    }


    public String getLastName()
    {
        return lastName;
    }


    public String getLibraryNumber()
    {
        return libraryNumber;
    }


    public int getNoOfBooks()
    {
        return noOfBooks;
    }


    public void printBorrowerDetails()
    {
        System.out.println( firstName + " " + lastName 
                           + "\n" + address.getFullAddress()
                           + "\nLibrary Number: " + libraryNumber
                           + "\nNumber of loans: " + noOfBooks);
    }     




    public void borrowBook()
    {
        noOfBooks = noOfBooks + 1;
        System.out.println("Books on loan: " + noOfBooks);        
    }


    public void borrowBooks(int number)
    {
        noOfBooks = noOfBooks + number;
        System.out.println("Books on loan: " + noOfBooks);        
    }


    public void returnBook ()
    {
        noOfBooks = noOfBooks - 1 ;
        System.out.println("Books on loan: " + noOfBooks);        
        }


    public String getAddress()
    {
        return address.getFullAddress();
    }


    public void setAddress(String street, String town, String postcode)
    {
        address.setFullAddress(street, town, postcode);
    }


    public void printAddress()
    {
        address.printAddress();
    }

} // end class
4

0 回答 0