Write a program that asks the user to enter an account number as a string. The account number consists of 8
characters and is divided as follows:
The first 2 characters are digits and represent the branch (00 for Beirut, 01 for Saida, 02 for Tripoli).
The remaining 6 characters represent the customer’s account number
A valid account number consists of exactly 8 characters. If the account is valid, the program prints the details
(branch and account number). If the account is not valid, the program displays a message to the user.
import java.util.Scanner;
public class AccountDemo {
public static boolean isValidAccountNumber(String accountNumber)
{
if(accountNumber.length()!=8)
return false;
if(accountNumber.charAt(0) !='0')
return false;
return accountNumber.charAt(1) == '0' || accountNumber.charAt(1) == '1'
|| accountNumber.charAt(2) == '2';
}
public static void main(String[] args)
{
System.out.print("Enter account number: ");
String accountNumber = new Scanner(System.in).nextLine();
if(!isValidAccountNumber(accountNumber))
System.out.println("Invalid account number");
else
{
if(accountNumber.substring(0,2).equals("00"))
System.out.println("Branch: Beirut");
else if (accountNumber.substring(0,2).equals("01"))
System.out.println("Branch: Saida");
else
System.out.println("Branch: Tripoli");
System.out.println("Account number: "+accountNumber.substring(2));
}
}
}
Comments
Leave a comment