Java  Array difficulty level Handson - 2

Question

Concept - Array Difficulty Level 2

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 a static method replaceAccountByNumber in the AccountDemo class.

This method will take array of Account objects and one Account object as input. Replace the Account object with same number if found in the array and return true. Else return false

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(33,"atyyfrx",526.0,29);
Account account2= new Account(73,"niyrmyr",838.0,81);
Account account3= new Account(86,"tjhyvix",59.0,3);
Account account4= new Account(53,"xtkrhmr",97.0,63);
Account account5= new Account(97,"bebjwuk",230.0,26);


Account[] objArray= {account1,account2,account3,account4,account5};
Account accountRes1= new Account(31,"mcxdkjh",701.0,64);

boolean result= replaceAccountByNumber(objArray, accountRes1);
System.out.println("Output for performing replace on accountRes1 is: "+ result  );          

System.out.println("Displaying contents of array: ");

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

Account accountRes2= new Account(73,"zgcerte",224.0,29);
result= replaceAccountByNumber(objArray, accountRes2);

System.out.println("Output for performing replace on accountRes2 is: "+ result  );          

System.out.println("Displaying contents of array: ");

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


Output:


Output for performing replace on accountRes1 is: false
Displaying contents of array: 
33 atyyfrx 526.0 29 
73 niyrmyr 838.0 81 
86 tjhyvix 59.0 3 
53 xtkrhmr 97.0 63 
97 bebjwuk 230.0 26 

Output for performing replace on accountRes2 is: true
Displaying contents of array: 
33 atyyfrx 526.0 29 
73 zgcerte 224.0 29 
86 tjhyvix 59.0 3 
53 xtkrhmr 97.0 63 
97 bebjwuk 230.0 26 


Solution:


    class AccountDemo{
        public static void main(String[] args) {

            Account account1= new Account(33,"atyyfrx",526.0,29);
            Account account2= new Account(73,"niyrmyr",838.0,81);
            Account account3= new Account(86,"tjhyvix",59.0,3);
            Account account4= new Account(53,"xtkrhmr",97.0,63);
            Account account5= new Account(97,"bebjwuk",230.0,26);


            Account[] objArray= {account1,account2,account3,account4,account5};
            Account accountRes1= new Account(31,"mcxdkjh",701.0,64);

            boolean result= replaceAccountByNumber(objArray, accountRes1);
            System.out.println("Output for performing replace on accountRes1 is: "+ result  );          

            System.out.println("Displaying contents of array: ");

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

            Account accountRes2= new Account(73,"zgcerte",224.0,29);
            result= replaceAccountByNumber(objArray, accountRes2);

            System.out.println("Output for performing replace on accountRes2 is: "+ result  );          

            System.out.println("Displaying contents of array: ");

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

        private static boolean replaceAccountByNumber(Account[] objArray, Account accountRes1) {
            // for (Account account : objArray) {
            for(int i = 0 ; i < objArray.length ; i++){
                if(objArray[i].number == accountRes1.number){
                    objArray[i] = accountRes1;
                    return true;
                }
            }
            return false;
        }


    }

    class Account{
        int number;
        String acType;
        double balance;
        int noOfAccountHolders;

        /**
         * @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;
        }
    }

 

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