0

I am new here and relatively new to Java coding. I have been studying Java for a while and I started to follow some basic projects available. I got this project where we design a very simple bank application. I will paste the code below and ask my question now. In this code we define the UserName and UserId by typing them onto the code itself. If I wanted to ask the user for his name and ID, how could I do that. I tried using the Scanner function but I didn't know how to get the user answers, store it in a variable and then get the BankAccount method to use those inputs.

Thanks in advance for all the help.

_____________________________________________________________-

import java.util.Scanner;

class BankingApplication {

    public static void main(String[] args) {

        BankAccount obj1 = new BankAccount("Daniel", "DR0001");
        obj1.showMenu();


    }


}

class BankAccount
{
    int balance;
    int previousTransaction;
    String customerName;
    String customerId;

    BankAccount(String cname, String cid)
    {
        customerName = cname;
        customerId = cid;

    }

    void deposit(int amount) 
    {
        if(amount != 0)
        {
        balance = (balance + amount);
        previousTransaction =  amount;

        }
    }

    void withdraw(int amount)
    {
        if(amount != 0)
        {
        balance = balance - amount;
        previousTransaction = - amount;

        }

    }

    void getPreviousTransaction()
    {
        if(previousTransaction > 0 )
        {
            System.out.println("Deposited: " + previousTransaction);
        }
        else if (previousTransaction < 0)
        {
            System.out.println("Withdrawn: " +Math.abs(previousTransaction));
        }
        else 
        {
            System.out.println("No Transaction Occured");           
        }

    }

    void showMenu()
    {
        char option = '\0';
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome " + customerName);
        System.out.println("Your ID is " + customerId);
        System.out.println("\n");
        System.out.println("A. Check Balance");
        System.out.println("B. Deposit");
        System.out.println("C. Withdraw");
        System.out.println("D. Previous Transaction");
        System.out.println("E. Exit");


        do 
        {
            System.out.println("================================================================");
            System.out.println("Enter an option");
            System.out.println("================================================================");
            option = scanner.next().charAt(0);
            System.out.println("\n");

            switch(option) 
            {
            case 'A':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Balance= " + balance);
            System.out.println("----------------------------------------------------------------");
            break;

            case 'B':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Enter an amount to deposit:");
            System.out.println("----------------------------------------------------------------");
            int amount = scanner.nextInt();
            deposit(amount);
            System.out.println("\n");
            break;

            case 'C':
            System.out.println("----------------------------------------------------------------");
            System.out.println("Enter an amount to withdraw:");
            System.out.println("----------------------------------------------------------------");
            int amount2 = scanner.nextInt();
            withdraw(amount2);
            System.out.println("\n");
            break;

            case 'D':
            System.out.println("----------------------------------------------------------------");
            getPreviousTransaction();
            System.out.println("----------------------------------------------------------------");
            System.out.println("\n");
            break;

            case 'E':
            System.out.println("****************************************************************");
            break;

            default:
                System.out.println("Invalid Option! Please enter again");
                break;



            }

        }while (option != 'E');

        System.out.println("Thanks for using our services");
    }
} 
Daniel Rocha
  • 9
  • 1
  • 2
  • 1
    Search before you ask and you'll find things like [this tutorial](https://docs.oracle.com/javase/tutorial/essential/io/scanning.html). This official tutorial, by the way, is an excellent learning resource in general. You'd do well to follow it. – MarsAtomic May 13 '20 at 19:18
  • Does this answer your question? [How can I read input from the console using the Scanner class in Java?](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – Savior May 13 '20 at 19:39

1 Answers1

0

There is a very simple solution for this, and just as you guessed, it's using the Scanner library! So first you import the library by putting this snippet of code at the top of the class

import java.util.Scanner;

(You can also use * instead of Scanner so that you can use any library in java.util)

Then in order to use the Input class, you use the snippet of code below;

Scanner input = new Scanner(System.in);

Then, if you want to ask the user for his/her name, then first you prompt them by using the print commandl

System.out.println("What is your name?);

Lastly, you allow them to input a String value.

String name = input.nextLine();

For an integer it would be .nextInt(), for a double it would be .nextDouble(), for a float it would be .nextFloat() and so on and so on.

Please refer to the documentation in the link below for more help. Hope this solved your question! :D

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html