Java - Bank Account
Questions/Tutorials for any programming language not covered in other sections.
4 posts
Page 1 of 1
Code: Select all
package edu.pitt.ChenHomework4;
import java.util.Scanner;
public class BankAccount {
public static void main(String[] args) {
String accountStatus = "active"; // is the account active or not?
double accountBalance = 1000.00; // initial account balance
Scanner in = new Scanner(System.in); // the Scanner class to get user input
System.out.print("Please enter the amount that you would like to withdraw. " +
"Enter -1 to exit the program: "); // Asks user to enter amount, with an input to enter
// if user wishes to exit
double withdrawalAmount = in.nextDouble(); // withdrawal amount is the amount user entered
/*if the account is active, and the account balance is greater or equal to the
* withdrawal amount, print out the updated account balance by subtracting the
* withdrawal amount from the total account balance.
*/
do {
} while (withdrawalAmount == -1);
if (accountStatus.equals("active") && accountBalance >= withdrawalAmount) {
System.out.println(accountBalance - withdrawalAmount);
}
else {
System.out.println("Account balance is insufficient.");
/*If withdrawal amount is more than the account balance, it will say so and nothing will be
* withdrawn
*/
}
}
}
Directions/Requirements:
Add a do/while loop to your program (you’ll have to place most of your logic inside the while loop) that would keep asking the user to enter a withdrawal amount and perform the withdrawal until the user enters -1
Keep in mind that every time a withdrawal amount is entered, you still have to check if the user
has a sufficient balance and if the account is active. All calculations must be correct
Your program must print the account balance after each withdrawal.
My code is wrong, need a bit of help. Due tomorrow, so please reply before tomorrow midnight EST
this should work
Code: Select all
import java.util.Scanner;
public class BankAccount {
public static void main(String[] args) {
boolean accountStatusActive = true;
double accountBalance = 1000.00;
Scanner in = new Scanner(System.in);
System.out.print("Please enter the amount that you would like to withdraw. " + "Enter -1 to exit the program: ");
double withdrawalAmount = in.nextDouble();
while (withdrawalAmount != -1) {
if (accountStatusActive) {
accountStatusActive = false;
if (accountBalance >= withdrawalAmount) {
accountBalance -= withdrawalAmount;
System.out.println(accountBalance);
} else {
System.out.println("Account balance is insufficient.");
}
}
withdrawalAmount = in.nextDouble();
accountStatusActive = true;
}
System.exit(-1);
}
}
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023