In: Computer Science
mport java.util.Scanner;
public class BankAccount {
//available balance store
static double availableBalance;
// bills constants
final static int HUNDRED_DOLLAR_BILL = 100;
final static int TWENTY_DOLLAR_BILL = 20;
final static int TEN_DOLLAR_BILL = 10;
final static int FIVE_DOLLAR_BILL = 15;
final static int ONE_DOLLAR_BILL = 1;
public static void main(String[] args) {
System.out.print("Welcome to CS Bank\n");
System.out.print("To open a checking account,please provide us with the following information\n:" );
String fullname;
String ssn;
String street;
String city;
int zipCode;
Scanner input = new Scanner(System.in);
System.out.print("Please enter your full name:");
fullname = input.nextLine();
System.out.print("Please enter your street address:");
street = input.nextLine();
System.out.print("Please enter the city:");
city = input.nextLine();
System.out.print("Please nter the zipCode:");
zipCode= input.nextInt();
//zip_code enter must be 5 digit
while(zipCode <10000 || zipCode> 99999) {
System.out.println("You enter an invalid zip code.");
System.out.println("Zip code must be 5 digits.");
System.out.println("Re-enter your zip code.");
zipCode = input.nextInt();
}
input.nextLine();
//System.out.println();
System.out.print("Enter the SSN");
ssn = input.nextLine();
// loop until a valid SSN is not enter
while(!validateSSN(ssn)) {
System.out.println("That was an invalid ssn.");
System.out.println("Example of a valid SSN is: 144-30-1987");
System.out.print("Re-enter the SSN:");
ssn = input.nextLine();
}
System.out.println();
System.out.print("Enter the initial balance in USD:");
availableBalance =Double.parseDouble(input.nextLine());
while(availableBalance < 0) {
System.out.println("That was an invalid amount");
System.out.print("Please re-enter the initial balance in USD:");
availableBalance = Double.parseDouble(input.nextLine());
}
System.out.println();
deposit();
System.out.println();
withdraw();
System.out.println();
displayBills();
}
public static void deposit() {
double amountToDeposit;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount to deposit:");
amountToDeposit=Double.parseDouble(input.nextLine());
while(amountToDeposit<0) {
System.out.println("That was an invalid amount.");
System.out.print("Please re-enter the amount to deposit: $");
amountToDeposit = Double.parseDouble(input.nextLine());
}
availableBalance = availableBalance + amountToDeposit;
System.out.print("Amount deposited successfully\n");
System.out.println("Your final currentbalance is: $" + String.format("%.2f", availableBalance));
}
public static void withdraw() {
double amountToWithdraw;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount to withdraw:");
amountToWithdraw = Double.parseDouble(input.nextLine());
while(amountToWithdraw < 0) {
System.out.print("That was an invalid amount.");
System.out.print("Please re-enter the amount to withdraw:$");
amountToWithdraw = Double.parseDouble(input.nextLine());
}
if((availableBalance-amountToWithdraw)< 0) {
System.out.println("SERIOUS ERROR OCCURED");
System.out.println("You try to withdraw an amount more than the available balance");
System.out.print("Program is exiting....");
System.exit(-1);
}
availableBalance = availableBalance-amountToWithdraw;
System.out.println(" Amount withdraw successfully");
System.out.println("Your final account balance is:$ "+String.format("%.2f",availableBalance));
}
public static void displayBills() {
double balance = availableBalance;
int bill_100 = (int)(balance/HUNDRED_DOLLAR_BILL );
balance = balance-(bill_100*HUNDRED_DOLLAR_BILL);
int bill_20 = (int)(balance/TWENTY_DOLLAR_BILL);
balance = balance-(bill_20*TWENTY_DOLLAR_BILL);
int bill_10 = (int)(balance/TEN_DOLLAR_BILL);
balance = balance-(bill_10*TEN_DOLLAR_BILL);
int bill_5 = (int)(balance/FIVE_DOLLAR_BILL);
balance = balance-(bill_5*FIVE_DOLLAR_BILL);
int bill_1 = (int)balance;
if(bill_100 > 0)
System.out.println("Count of $100 Bill is" + bill_100);
if(bill_20 > 0)
System.out.println("Count of $20 Bill is" + bill_20);
if(bill_10 > 0)
System.out.println("Count of $10 Bill is" + bill_10 );
if(bill_5 > 0)
System.out.println("Count of $5 Bill is" + bill_5);
if(bill_1 > 0)
System.out.println("Count of $1 Bill is" + bill_1);
}
public static boolean validateSSN(String ssn) {
//split the ssn based on '-' as delimiter
String[] tokens =ssn.split("-");
if(tokens.length != 3)
return false;
else {
if(tokens[0].length() != 3)
return false;
else if(tokens[1].length() != 2)
return false;
else if(tokens[2].length() != 4)
return false;
else {
//loop for each token chars
for(int i = 0; i < tokens.length; i++) {
int len = tokens[i].length();
for(int j = 0; j < len; j++) {
if(tokens[i].charAt(j)< 'o' || tokens[i].charAt(j) >'9')
return false;
}
}
return true;
}
}
}
}
Someone please fix my java program I'm having hard properly validating SSN . And fix any problem if found. Thanks.
The only error in your code is while checking if all all the characters are numbers or not, you used o instead of 0.
BankAccount.java
import java.util.Scanner;
public class BankAccount {
//available balance store
static double availableBalance;
// bills constants
final static int HUNDRED_DOLLAR_BILL = 100;
final static int TWENTY_DOLLAR_BILL = 20;
final static int TEN_DOLLAR_BILL = 10;
final static int FIVE_DOLLAR_BILL = 15;
final static int ONE_DOLLAR_BILL = 1;
public static void main(String[] args) {
System.out.print("Welcome to CS
Bank\n");
System.out.print("To open a
checking account,please provide us with the following information:
\n" );
String fullname;
String ssn;
String street;
String city;
int zipCode;
Scanner input = new
Scanner(System.in);
//prompt and read customer
name
System.out.print("Please enter your
full name: ");
fullname = input.nextLine();
//prompt and read address
System.out.print("Please enter your
street address: ");
street = input.nextLine();
//prompt and read city
System.out.print("Please enter the
city: ");
city = input.nextLine();
//prompt and read zip code
System.out.print("Please nter the
zipCode: ");
zipCode= input.nextInt();
//zip_code entered must be 5
digit
while(zipCode <10000 ||
zipCode> 99999) {
//display error
message and reprompt
System.out.println("You enter an invalid zip code.");
System.out.println("Zip code must be 5 digits");
System.out.println("Re-enter your zip code: ");
zipCode =
input.nextInt();
}
input.nextLine();
//System.out.println();
//prompt and read ssn number
System.out.print("Enter the SSN:
");
ssn = input.nextLine();
// loop until a valid SSN is not
enter
while(!validateSSN(ssn)) {
//display error
message and reprompt for ssn
System.out.println("That was an invalid ssn.");
System.out.println("Example of a valid SSN is: 144-30-1987");
System.out.print("Re-enter the SSN: ");
ssn =
input.nextLine();
}
System.out.println();
//prompt and read initial
balance
System.out.print("Enter the initial
balance in USD: ");
availableBalance
=Double.parseDouble(input.nextLine());
//check if amount is valid and
reprompt
while(availableBalance < 0)
{
System.out.println("That was an invalid amount");
System.out.print("Please re-enter the initial balance in USD:
");
availableBalance
= Double.parseDouble(input.nextLine());
}
System.out.println();
//call the deposit method
deposit();
System.out.println();
//call the withdraw method
withdraw();
System.out.println();
//call the displayBills
method
displayBills();
}
//method that deposits amount into the bank
account
public static void deposit() {
double amountToDeposit;
Scanner input = new
Scanner(System.in);
//prompt and read amount to be
deposited
System.out.print("Enter the amount
to deposit: ");
amountToDeposit=Double.parseDouble(input.nextLine());
//check if amount entered is valid
and reprompt if not valid
while(amountToDeposit<0) {
System.out.println("That was an invalid amount.");
System.out.print("Please re-enter the amount to deposit: $");
amountToDeposit
= Double.parseDouble(input.nextLine());
}
//update the balance
availableBalance = availableBalance
+ amountToDeposit;
//display the balance
System.out.print("Amount deposited
successfully\n");
System.out.println("Your final
currentbalance is: $" + String.format("%.2f",
availableBalance));
}
//method that withdraws amount from bank account
public static void withdraw() {
double amountToWithdraw;
Scanner input = new
Scanner(System.in);
//prompt and read amount to
withdraw
System.out.print("Enter the amount
to withdraw: ");
amountToWithdraw =
Double.parseDouble(input.nextLine());
//check if amount entered is valid
and reprompt if not valid
while(amountToWithdraw < 0)
{
System.out.print("That was an invalid amount.");
System.out.print("Please re-enter the amount to withdraw:
$");
amountToWithdraw
= Double.parseDouble(input.nextLine());
}
//display error method and exit if
amount entered is more than balance amount
if((availableBalance-amountToWithdraw)< 0) {
System.out.println("SERIOUS ERROR OCCURED");
System.out.println("You try to withdraw an amount more than the
available balance");
System.out.print("Program is exiting....");
System.exit(-1);
}
//update balance and display
it
availableBalance =
availableBalance-amountToWithdraw;
System.out.println("Amount withdraw
successfully");
System.out.println("Your final
account balance is: $
"+String.format("%.2f",availableBalance));
}
//method that display the bills
public static void displayBills() {
double balance =
availableBalance;
//get the number of hundreds
int bill_100 =
(int)(balance/HUNDRED_DOLLAR_BILL );
balance =
balance-(bill_100*HUNDRED_DOLLAR_BILL);
//get the number of 20's
int bill_20 =
(int)(balance/TWENTY_DOLLAR_BILL);
balance =
balance-(bill_20*TWENTY_DOLLAR_BILL);
//get the number of 10's
int bill_10 =
(int)(balance/TEN_DOLLAR_BILL);
balance =
balance-(bill_10*TEN_DOLLAR_BILL);
//get the number of 5's
int bill_5 =
(int)(balance/FIVE_DOLLAR_BILL);
balance =
balance-(bill_5*FIVE_DOLLAR_BILL);
int bill_1 = (int)balance;
//display the bills available
if(bill_100 > 0)
System.out.println("Count of $100 Bill is " + bill_100);
if(bill_20 > 0)
System.out.println("Count of $20 Bill is " + bill_20);
if(bill_10 > 0)
System.out.println("Count of $10 Bill is " + bill_10 );
if(bill_5 > 0)
System.out.println("Count of $5 Bill is " + bill_5);
if(bill_1 > 0)
System.out.println("Count of $1 Bill is " + bill_1);
}
//method that validates the SSN
public static boolean validateSSN(String ssn) {
//split the ssn based on '-' as
delimiter
String[] tokens
=ssn.split("-");
if(tokens.length != 3)
return
false;
else {
if(tokens[0].length() != 3)
return false;
else
if(tokens[1].length() != 2)
return false;
else
if(tokens[2].length() != 4)
return false;
else {
//loop for each token chars
for(int i = 0; i < tokens.length; i++)
{
int len =
tokens[i].length();
//loop for each char
for(int j = 0; j < len;
j++) {
//check if
all the characters are numbers or not
if(tokens[i].charAt(j)< '0' || tokens[i].charAt(j)
>'9')
return false;
}
}
return true;
}
}
}
}
Output
Program Screenshot: