如何将返回int作为String传递给另一个类?

时间:2014-11-25 20:13:29

标签: object printing private public bluej

我有大学的这个任务,花了好几个小时试图让各个部分在导师指南中工作(如果我可以将数组更改为public :()会很容易。

Anway我有一个AccountList类,其中有一个名为accounts的数组,我需要使用这个方法

AccountList类

  public int getNumberOfAccounts()
   {
       return accounts.size();
   }

在我的基于文本的用户界面中将accounts.size作为字符串返回,这是我到目前为止所得到的但我无法解决如何将该accountList.getNumberOfAccounts转换为此类中的字符串以便我可以打印出来。

AccountTUI类

public void getNumberOfAccounts()
    {

        accountList.getNumberOfAccounts();


    }

这是完整的代码集。

AccountTUI Full

import java.util.*;
/**
 * Write a description of class AccountTUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AccountTUI
{
    AccountList accountList = new AccountList();
    Scanner myScanner = new Scanner(System.in);



    public AccountTUI()
    {

    }


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

    private void displayMenu()
    {
        System.out.println("\n Please Select An Option");
        System.out.println("To quit enter 0");
        System.out.println("To add an Account enter 1");
        System.out.println("To remove an Account enter 2");
        System.out.println("To get the number of Accounts enter 3");
        System.out.println("To show a single Account enter 4");
        System.out.println("To show all Accounts enter 5");


    }



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

    private void execute( int command)
    {
        if ( command == 0 )
            quitCommand();
        else
        if ( command == 1)
           addAccount();
        else
        if ( command == 2 )
             removeAccount();
        else
        if ( command == 3)
             getNumberOfAccounts();
        else
        if ( command == 4)
            showAccount();
        else 
        if ( command == 5)
            showAllAccounts();

        else {

           unknownCommand(command);

        }
    }




    public void addAccount()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print ("\n Enter First Name \n");
        String firstname = keyboard.nextLine();
        System.out.print ("Enter Second Name \n");
        String secondname = keyboard.nextLine();
        System.out.print ("Enter Account Number \n");
        String accountnumber  = keyboard.nextLine();
        System.out.print ("Enter Street \n");
         String street  = keyboard.nextLine();
        System.out.print ("Enter Town \n");
         String town  = keyboard.nextLine();
        System.out.print ("Enter Postcode \n");
         String postcode  = keyboard.nextLine();

        accountList.addAccount(new Account(firstname,secondname,accountnumber,
                   street,town,postcode));

    }


    public void getNumberOfAccounts()
    {

        accountList.getNumberOfAccounts();


    }

    public void quitCommand()
    {
        System.out.println("\n Application is closing");
        System.exit(0);

    }

    public void removeAccount()
    {
    }

    public void showAccount()
    {

    }

    public void showAllAccounts()
    {
       System.out.print('\u000C');
        accountList.getAllAccounts();
        System.out.println("");



}

    public void unknownCommand(int command)
    {
        if (command < 0 || command > 5) {
            System.out.print('\u000C');
        System.out.println("Unknown command.");

    }
}


}

帐户列表已满

import java.util.*;
/**
 * This is the AccountList class provide for students
 * in Blackboard at the start of the homework
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AccountList
{
   private ArrayList<Account> accounts;    

   /**
    * Constructor for objects of class AccountList
    */
   public AccountList()
   {
       accounts = new ArrayList<Account>();
   }

   public void addAccount(Account account)
   {
       accounts.add(account);
   }

   public int getNumberOfAccounts()
   {
       return accounts.size();
   }

      public boolean getAccount(String accountNumber)
   {
       int index = 0;
       for (Account account : accounts)
       {
           if (accountNumber.equals(account.getAccountNumber()))
           {
               account.printAccountDetails();
               return true;
           }
           else
           {
               index++;
           }
       }
       return false;
   }    


   public void getAccount(int accountEntry)
   {
       if (accountEntry < 0)
       {
           System.out.println("Negative entry: " + accountEntry);
       }
       else if (accountEntry < getNumberOfAccounts())
       { 
           Account account = accounts.get(accountEntry);
           account.printAccountDetails();
       }
       else
       {
           System.out.println("No such entry: " + accountEntry);
       }
   }

   public void getAllAccounts()
   {
        for(Account account : accounts)
        {
            account.printAccountDetails();
            System.out.println();
        }
   }

   public void removeAccount(int accountEntry)
   {
        if(accountEntry < 0)
        {
            System.out.println("Negative entry :" + accountEntry);
        }
        else if(accountEntry < getNumberOfAccounts())
        {
            accounts.remove(accountEntry);
        }
        else
        {
            System.out.println("No such entry :" + accountEntry);
        }
   }

   public boolean removeAccount(String accountNumber)
   {
        int index = 0;
        for (Account account: accounts)
        {
            if (accountNumber.equals(account.getAccountNumber()))
            {
                accounts.remove(index);
                return true;
            }
            index++;
        }
        return false;
   }


   public int search(String accountNumber)
   {
       int index = 0;
       for (Account account : accounts)
       {
           if (accountNumber.equals(account.getAccountNumber()))
           {
               return index;
           }
           else
           {
               index++;
           }
       }
       return -1;
   }    



}

2 个答案:

答案 0 :(得分:1)

如果您想将int i转换为String,可以使用

String.valueOf(i);

所以你可以像这样修改你的方法:

public String getNumberOfAccounts()
{
   return String.valueOf(accounts.size());
}

或者您可以将方法保持原样,并在希望将String.valueOf()表示放入用户界面时使用String

但是你说你想在它上面使用println()。请注意,println()也会直接接受int,所以没有什么可以阻止你写的

System.out.println(getNumberOfAccounts());

即使getNumberOfAccounts()返回int

另外,你应该注意到你没有使用数组(或者如果你是,你做错了)。数组上没有.size()方法;但是有一个ArrayList,所以我怀疑你正在使用它。数组的等价物是arr.length(没有(),因为它是一个字段而不是方法。)

答案 1 :(得分:0)

使用以下功能

public String getNumberOfAccounts()
{
return (""+accounts.size());
}