2014-11-12 2 views
0

현재 BlueJ를 사용하고 있으며 객체가 서로 상호 작용할 수 있도록 노력하고 있습니다. 계정 및 계정 목록 클래스를 만들었습니다. accountlist를 사용하면 계정 객체를 추가하여 arraylist에 넣을 수 있습니다.BlueJ 배열에서 데이터를 가져 와서 사용자 입력과 비교할 수있는 방법

색인을 사용하여 배열에서 이러한 계정을 제거하는 것은 for 루프가 각 계정의 위치를 ​​얻는 것만큼 쉽지만 이제는 계정 번호가 매개 변수로 지정된 계정을 제거해야하며 어떻게 작동 할 수 있습니까? 배열에서 계정 번호를 가져와 사용자 입력과 비교하십시오.

AccountList 클래스

/** 
* Creates an AccountList that will store customers accounts 
* 
* @author Ryan Conway 
* @version 12/11/2014 
*/ 

import java.util.*; 

public class AccountList 
{ 

    private ArrayList <Account> accounts; 

    /** 
    * Create an AccountList that will contain accounts. 
    */ 
    public AccountList() 
    { 

     accounts = new ArrayList <Account>(); 

    } 

    /** 
    * Add a account to this AccountList. 
    */ 
    public void addAccount(Account newAccount) 
    { 
     accounts.add(newAccount); 
    } 

    /** 
    * Return the number of accounts stored in this AccountList. 
    */ 
    public int getNumberOfAccounts() 
    { 
     return accounts.size(); 
    } 

    /** 
    * prints out the number of accounts to the terminal. 
    * for each loop used to get each account. 
    */ 
    public void getAllAccounts() 
    { 
     for(Account account : accounts) 
     { 
      account.printAccountDetails(); 
     } 

     System.out.println("Number of accounts: " + getNumberOfAccounts()); 
    } 


    /** 
    * Print out details of a account 
    * @param accountEntry The entry in the list 
    */ 
    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); 
     } 
    } 

    /** 
    * removes a account from the list 
    * @param accountEntry The entry in the list 
    */ 
    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 (accounts.equals(accountNumber)) 
       System.out.println("Found It!"); 
      index++; 
     } 
     return false; 


    } 


    public void printCollection() 
    { 
     System.out.println(accounts); 
    } 
} 

계정 클래스

/** 
* Write a description of class Account here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class Account 
{ 
    private String firstName; 
    private String lastName; 
    private String accountNumber; 
    private int pointsHeld; 
    private Address address; 

    /** 
    * Constructor for objects of class Account. 
    * The number of pointsHeld should be set to zero. 
    * 
    * @param firstName The Account Holder's first name 
    * @param lastName The Account Holder's last name 
    * @param accNumber The Account Holder's account number 
    * @param street the account holder's street 
    * @param town the account holder's town 
    * @param postcode the account holder's postcode 
    */ 
    public Account(String fName, String lName, String accNumber, 
        String street, String town, String postcode) 
    { 
     firstName = fName; 
     lastName = lName; 
     accountNumber = accNumber; 
     pointsHeld = 0;  
     address = new Address(street, town, postcode); 
    } 

    /** 
    * Constructor for objects of class Account. 
    * The number of pointsHeld should should be set to 
    * the supplied value. 
    * 
    * @param fName The Account Holder's first name 
    * @param lName The Account Holder's last name 
    * @param acctNumber The account number 
    * @param thePoints the pointsHeld awarded when account is initialised 
    * @param street the account holder's street 
    * @param town the account holder's town 
    * @param postcode the account holder's postcode 
    */ 
    public Account(String fName, String lName, String acctNumber, int points, 
        String street, String town, String postcode) 
    { 
     firstName = fName; 
     lastName = lName; 
     accountNumber = acctNumber; 
     pointsHeld = points;  
     address = new Address(street, town, postcode); 
    } 

    // accessors 

    /** 
    * Get the Account Holder's first name 
    * 
    * @return the Account Holder's first name 
    */ 
    public String getFirstName() 
    { 
     return firstName; 
    } 

    /** 
    * Get the Account Holder's last name 
    * 
    * @return the Account Holder's last name 
    */ 
    public String getLastName() 
    { 
     return lastName; 
    } 

    /** 
    * Get the Account Holder's account Number 
    * 
    * @return the Account Holder's account number 
    */ 
    public String getAccountNumber() 
    { 
     return accountNumber; 
    } 

    /** 
    * Get the number of points held 
    * 
    * @return the number of points held 
    */ 
    public int getNoOfPoints() 
    { 
     return pointsHeld; 
    } 

    /** 
    * Print out the Account Holder's details to the console window 
    * 
    */ 
    public void printAccountDetails() 
    { 
     System.out.println(firstName + " " + lastName 
          + "\n" + address.getFullAddress() 
          + "\nAccount Number: " + accountNumber 
          + "\nNumber of points: " + pointsHeld); 
    }  

    /** 
    * Return the account holder's address 
    * 
    * @return the account holder's address 
    */ 
    public String getAddress() 
    { 
     return address.getFullAddress(); 
    } 

    // mutators  

    /** 
    * Change the first name 
    * 
    * @param fName the new first name 
    * 
    */ 
    public void setFirstName(String fName) 
    { 
     firstName = fName; 
    } 

    /** 
    * Change the last name 
    * 
    * @param lName the new last name 
    * 
    */ 
    public void setLastName(String lName) 
    { 
     lastName = lName; 
    } 

    /** 
    * Increase the number of points held by a given number 
    * and output a esage to the console window giving 
    * the revised number of points held. 
    * 
    * @param number of points to add to total 
    */ 
    public void addPoints(int points) 
    { 
     pointsHeld = pointsHeld + points; 
     System.out.println("Points now held: " + pointsHeld);   
    } 

    /** 
    * Remove pointsHeld by a given number and output a 
    * message to the console window giving the revised number 
    * of points held as long as the number of points would 
    * not fall below zero 
    * - otherwise output message to console window instead. 
    * 
    * @param number of pointsHeld to remove total. 
    * 
    */ 
    public void removePoints (int points) 
    { 
     if ((pointsHeld - points) >=0) 
     { 
      pointsHeld = pointsHeld - points; 
      System.out.println("Points now held: " + pointsHeld);   
     } 
     else 
     { 
      System.out.println("Transaction refused: " 
        + "Insufficient points available."); 
     } 
    } 

    /** 
    * Change the account holder's address 
    * 
    * @param street the street 
    * @param town the town 
    * @postcode the postcode 
    */ 
    public void setAddress(String street, String town, String postcode) 
    { 
     address.setFullAddress(street, town, postcode); 
    } 

    /** 
    * Print the account holder's address 
    */ 
    public void printAddress() 
    { 
     address.printAddress(); 
    }  
} // end class 

http://i.imgur.com/dCwt9oK.jpg

감사합니다.

+2

사진 대신 코드를 게시 하시겠습니까? –

+0

나는이 질문을 수정했다 :) –

답변

1

이 시도 :

public boolean removeAccount(String accountNumber) 
{ 
    Iterator<Account> iterator = accounts.iterator(); 

    while (iterator.hasNext()) 
    { 
     if (accountNumber.equals(iterator.next().getAccountNumber())) 
     { 
      System.out.println("Found It!"); 
      iterator.remove(); 
      return true; 
     } 
    } 

    return false; 
} 

가 안전한 작동하지 않지만 Iterator 클래스가이를 가능하게하는 과정이 반복되는 동안 목록에서 항목을 제거. 추가 정보 : Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop