Question

In: Computer Science

mport java.util.Scanner; public class BankAccount { //available balance store static double availableBalance; // bills constants final...

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.

Solutions

Expert Solution

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:


Related Solutions

import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME =...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; }...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = "xxx"; //sales person String article1, article2; //item name for each purchased int quantity1, quantity2; //number of quantities for each item double unit_cost1, unit_cost2; //unit price for each item double price1, price2; //calculates amounts of money for each item. double sub_total; //total price of two items without tax double tax; //amount of sales tax double total; //total price of...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT