Simple Banking System in java

            A banking system has 4 options. Deposit, withdraw, Balance enquiry and exit. The explanation is given below.

Deposit:

              It asks the user to enter the deposit amount. If the amount is greater than zero, it adds to the balance amount.

Withdraw:

              It asks the withdraw amount from the user. If this amount is less than balance amount and greater than zero, then the withdraw amount is deleted from the balance amount.

Otherwise, it displays there is no sufficient amount.

Balance enquiry:

              It displays the balance amount.

Exit:

      It ends the banking system and displays a thank you message.

Program:

import java.util.Scanner;

public class SimpleBankingEg {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);

        double balance_amt = 0.0;

        while (true) {

            System.out.println("\n--- Simple Banking System ---");

            System.out.println("1. Deposit");

            System.out.println("2. Withdraw");

            System.out.println("3. Balance Enquiry");

            System.out.println("4. Exit");

            System.out.print("Choose an option: ");

            int option = scanner1.nextInt();

            switch (option) {

                case 1:

                    System.out.print("Enter deposit amount :");

                    double deposit_amt = scanner1.nextDouble();

                    if (deposit_amt > 0) {

                        balance_amt += deposit_amt;

                        System.out.println(deposit_amt + " is deposited successfully.");

                    } else {

                        System.out.println("Invalid deposit amount.");

                    }

                    break;                   

                case 2:

                      System.out.print("Enter the amount to withdraw:");

                    double withdrawal_amt = scanner1.nextDouble();

                    if (withdrawal_amt > 0 && withdrawal_amt <= balance_amt) {

                        balance_amt -= withdrawal_amt;

                        System.out.println(withdrawal_amt + "is withdrawn successfully.");

                    } else if (withdrawal_amt > balance_amt) {

                        System.out.println("Insufficient balance.");

                    } else {

                        System.out.println("Invalid withdrawal amount.");

                    }

                    break;

                case 3:

                    System.out.println("Your Current Balance is:" + balance_amt);

                    break;

                case 4:

                    System.out.println("Thank you for using the Banking System. Have a nice day");

                    scanner1.close();

                    System.exit(0);

                default:

                    System.out.println("Invalid option. Please try again.");

            }

        }

    }

}

Output:

C:\raji\blog>javac SimpleBankingEg.java

C:\raji\blog>java SimpleBankingEg

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 1

Enter deposit amount :12000

12000.0 is deposited successfully.

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 1

Enter deposit amount :23450

23450.0 is deposited successfully.

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 3

Your Current Balance is:35450.0

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 2

Enter the amount to withdraw:10000

10000.0is withdrawn successfully.

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 3

Your Current Balance is:25450.0

--- Simple Banking System ---

1. Deposit

2. Withdraw

3. Balance Enquiry

4. Exit

Choose an option: 4

Thank you for using the Banking System. Have a nice day

This is the sample Banking system implemented in java. Hope this will be useful to you. Keep coding!!!

Random Java Programs: Count the number of words in a paragraph.

 A paragraph is a collection of words in a format and has a meaning. To count the number of words in a paragraph, let us follow the steps. let us implement it.

Logic behind the program:

  • First, read the input paragraph from the command line.
  • Check for emptiness, use isEmpty() function.
  • If the value is true, then print “The paragraph does not have any words”.
  • Otherwise, start the process.
  • Let us split the words by space and add the count value.
  • Finally, print the count value.

Program implementation:

//Built in header file inclusion

import java.util.Scanner;

// create a public class with main function

public class countIt {

    public static void main(String[] args) {

        // let us create the scanner object

        Scanner scanner1 = new Scanner(System.in);

       // ask the user to enter the Paragraph

        System.out.println("Enter a Paragraph:");

        String ipara = scanner1.nextLine();

      // let us check whether paragraph is empty or not

        if (ipara.trim().isEmpty()) {

            System.out.println("The paragraph does not have any words");

        } else {

            // Split the paragraph into words using spaces

            String[] iWords = ipara.trim().split("\\s+");

            int wc = iWords.length;

         // print the count value

            System.out.println("The number of words in the Paragraph: " + wc);

        }

        scanner1.close();

    }

}

Output:

Just compile and run the program to get the output.

C:\raji\blog>javac countIt.java

C:\raji\blog>java countIt

Enter a Paragraph:

once there lived a man whose name is David. He is tall and plays music well. He wants to become a music director.But he does not have any degree in music.He worked hard and learnt all types of music.Finally, he became a music director.

The number of words in the Paragraph: 44

This is one of the random java programs to count the number of words in a paragraph given by the user. Hope this program is helpful to you. Keep coding!!!!

Random java programs: Intersection of two arrays.

               Arrays are the great source of storing data elements. Let us find the intersection of two arrays in java.

Logic:

Efficient approach to implement this concept is ‘Hashset’. Let us store the data of first array in Hashset and checks the data in the second array. the common elements are displayed as output.

Java implementation of intersection of two arrays:

The java implementation starts by including built in packages.

  • A public class is created with main() function.
  • Scanner object is created to read the input from the user.
  • It reads two array elements from the user.
  • An intersection function is created.
  • This function gets two arrays as input.
  • It creates two hashsets. Add the first array elements into first hashset.
  • The second array elements are checked with the hashset. If both are matched, it adds the number to the intersection hashset.
  • After the final elements, it displays the intersection elements value.

Program:

import java.util.HashSet;

import java.util.Set;

import java.util.Scanner;

public class ArrayIntersectionEg {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int i;

        System.out.print("Enter the first array elements with spaces: ");

        String input1 = scanner.nextLine();

      // Let us split the input string by spaces

        String[] arr1 = input1.split(" ");

     // Convert the string into an integer array

        int[] intArr1 = new int[arr1.length];

        for (i = 0; i < arr1.length; i++) {

            intArr1[i] = Integer.parseInt(arr1[i]);

        }

       System.out.print("Enter the second array elements with spaces: ");

        String input2 = scanner.nextLine();

        // let us split the input string by spaces

        String[] arr2 = input2.split(" ");

        // Convert the string to an integer array

        int[] intArr2 = new int[arr2.length];

        for (i = 0; i < arr2.length; i++) {

            intArr2[i] = Integer.parseInt(arr2[i]);

        }

     Set<Integer> intersec = findIntersec(intArr1, intArr2);

     System.out.println("The Intersection of two arrays are: " + intersec);

    }

    public static Set<Integer> findIntersec(int[] intArr1, int[] intArr2) {

        Set<Integer> set1 = new HashSet<>();

        Set<Integer> intersec = new HashSet<>();

        for (int no : intArr1) {

            set1.add(no);

        }

        for (int no : intArr2) {

            if (set1.contains(no)) {

                intersec.add(no);

            }

        }

        return intersec;

    }

}

Output:

C:\raji\blog>javac ArrayIntersectionEg.java

C:\raji\blog>java ArrayIntersectionEg

Enter the first array elements with spaces: 23 34 45 56 67 78

Enter the second array elements with spaces: 89 67 23 41 90 10

The Intersection of two arrays are: [67, 23]

This is the way of finding the intersection of two arrays was implemented in java. Hope this code segment is useful to you. Keep coding.