Java  Array difficulty level Handson - 6

Question

Concept - Array Difficulty Level 6

Create a class Account with below attributes:

int - number

String - acType

double - balance

int - noOfAccountHolders

Make all the attributes private.


Create corresponding getters and setters.

Create a constructor which takes all parameters in the above sequence.

The constructor should set the value of attributes to parameter values inside the constructor.

Create a class AccountDemo with main method

Create the below static method searchAccountByBalance in the AccountDemo class.

This method will take array of Account objects and balance as input and returns new array of Account objects for all values found with the given balance else return null if not found.

Create an array of 5 Account objects in the main method


Refer below sample main method and test the output:

Call the above static method from the main method


public class AccountDemo {
public static void main(String args[]){
Account account1= new Account(43,"bqhqoho",328.0,1);
Account account2= new Account(95,"pbuyhuk",931.0,73);
Account account3= new Account(74,"jkmxefn",34.0,18);
Account account4= new Account(43,"rnoebjc",799.0,39);
Account account5= new Account(17,"czwgofn",372.0,15);


Account[] objArray= {account1,account2,account3,account4,account5};

Account[] objResultArray1= searchAccountByBalance(objArray, 322.0);
if(objResultArray1==null){
System.out.println("Output after first search is null. ");
}else{
System.out.println("Displaying contents of result array: ");

for(Account account:objResultArray1){
System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
}
}

Account[] objResultArray2= searchAccountByBalance(objArray, 372);
if(objResultArray2==null){
System.out.println("Output after first search is null. ");
}else{
System.out.println("Displaying contents of result array: ");

for(Account account:objResultArray2){
System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
}
}
}}


Output


Output after first search is null. 
Displaying contents of result array: 
17 czwgofn 372.0 15 

Solution:


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Solution2{
    public static void main(String[] args) {
        Account account1= new Account(43,"bqhqoho",328.0,1);
        Account account2= new Account(95,"pbuyhuk",931.0,73);
        Account account3= new Account(74,"jkmxefn",34.0,18);
        Account account4= new Account(43,"rnoebjc",799.0,39);
        Account account5= new Account(17,"czwgofn",372.0,15);
        Account[] objArray= {account1,account2,account3,account4,account5};

        Account[] objResultArray1= searchAccountByBalance(objArray, 322.0);
        
        if(objResultArray1==null){
            System.out.println("Output after first search is null. ");
        }else{
            System.out.println("Displaying contents of result array: ");
            for(Account account:objResultArray1){
                System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
            }
        }

        Account[] objResultArray2= searchAccountByBalance(objArray, 372);
        if(objResultArray2==null){
            System.out.println("Output after first search is null. ");
        }else{
            System.out.println("Displaying contents of result array: ");
            for(Account account:objResultArray2){
                System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
            }
        }
    }

    private static Account[] searchAccountByBalance(Account[] objArray, double d) {
        List<Account> ls = new ArrayList<>();
        for (Account account : objArray) {
            if(account.balance == d){
                ls.add(account);
            }
        }
        if(ls.size() > 0){
            objArray = new Account[ls.size()];
            ls.toArray(objArray);
            return objArray;
        }
        return null;
    }
}


class Account implements Comparator<Account> {
    int number;
    String acType;
    double balance;
    int noOfAccountHolders;

    public Account(){

    }
    /**
     * @return the number
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(int number) {
        this.number = number;
    }

    /**
     * @return the acType
     */
    public String getAcType() {
        return acType;
    }

    /**
     * @param acType the acType to set
     */
    public void setAcType(String acType) {
        this.acType = acType;
    }

    /**
     * @param balance the balance to set
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**
     * @return the balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * @return the noOfAccountHolders
     */
    public int getNoOfAccountHolders() {
        return noOfAccountHolders;
    }

    /**
     * @param noOfAccountHolders the noOfAccountHolders to set
     */
    public void setNoOfAccountHolders(int noOfAccountHolders) {
        this.noOfAccountHolders = noOfAccountHolders;
    }

    public Account(int number,String acType , double balance , int noOfAccountHolders){
        this.number  = number;
        this.acType = acType;
        this.balance = balance;
        this.noOfAccountHolders = noOfAccountHolders;
    }

@Override
public int compare(Account o1, Account o2) {
    // TODO Auto-generated method stub
    return o1.acType.compareTo(o2.acType);
}
}

 

©2018 by The real one. Proudly created with Wix.com