
Java Array difficulty level Handson - 5
Question
​
Concept - Array Difficulty Level 5
​
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 searchAccountByAcType in the AccountDemo class.
​
This method will take array of Account objects and acType as input and returns new array of Account objects for all values found with the given acType 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,"ianynap",267.0,41);
Account account2= new Account(80,"gneuwmm",789.0,76);
Account account3= new Account(80,"yyudure",588.0,99);
Account account4= new Account(60,"ahixnty",831.0,39);
Account account5= new Account(72,"iwundhq",804.0,4);
Account[] objArray= {account1,account2,account3,account4,account5};
Account[] objResultArray1= searchAccountByAcType(objArray, "oxxvosw");
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= searchAccountByAcType(objArray, "gneuwmm");
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:
80 gneuwmm 789.0 76
Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
class AccountDemo{
public static void main(String[] args) {
Account account1= new Account(43,"ianynap",267.0,41);
Account account2= new Account(80,"gneuwmm",789.0,76);
Account account3= new Account(80,"yyudure",588.0,99);
Account account4= new Account(60,"ahixnty",831.0,39);
Account account5= new Account(72,"iwundhq",804.0,4);
Account[] objArray= {account1,account2,account3,account4,account5};
Account[] objResultArray1= searchAccountByAcType(objArray, "oxxvosw");
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= searchAccountByAcType(objArray, "gneuwmm");
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[] searchAccountByAcType(Account[] objArray, String string) {
List<Account> acc = new ArrayList<Account>();
for (Account account : objArray) {
if(account.acType.equals(string)){
acc.add(account);
}
}
if(acc.size() > 0){
objArray = new Account[acc.size()];
acc.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);
}
}