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");
}
}