JAVA- This is my code and my output. After the output \"Error: Can not withdraw
ID: 3814420 • Letter: J
Question
JAVA-
This is my code and my output. After the output "Error: Can not withdraw $" + amt);" when you try to withdraw an amount that is not currently in the account, it still prints "$xxx.xx withdrawn from account". How do I edit my code so that when you get the error notice, it doesn't print the withdrawl line and instead just skips down to the next line? I also want to changethe "amt" variable to have 2 decimal places in the output. Here's my output:
import java.util.Random;
//Account class definition
class Account
{
//Private instance variable
private int accountNumber;
private double balance;
//Constructor that initializes variables
public Account()
{
//Random class object
Random rand = new Random();
//Generating a random 4 digit Account Number
accountNumber = rand.nextInt((9999 - 1000) + 1) + 1000;
//Initializing balance
balance = 0.0;
}
//Getter method for Account Number
public int getAccountNumber()
{
//Return Account Number
return accountNumber;
}
//Method that adds amount to balance
public void deposit(double amt)
{
//Updating new balance
balance = balance + amt;
}
//Method that withdraws amount from balance
public void withdraw(double amt)
{
//Validating amount
if(amt > balance)
System.out.println(" Error: Can not withdraw $" + amt);
else
//Updating new balance
balance = balance - amt;
}
//Method that adds interest to balance amount
public double addInterest()
{
double actualBalance = balance;
//Adding interest to balance
balance = balance * 1.005;
//Returning amount of interest added
return (balance - actualBalance);
}
//Method that returns balance amount
public double getBalance()
{
return balance;
}
}
---------------------
import java.util.Scanner;
public class Prog8 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char type;
double amount;
//Creating an Account object
Account myAccount = new Account();
//Reading transactions file
while(reader.hasNext()) {
//Reading type of transaction
type = reader.next().charAt(0);
//Calling appropriate functions
if(type == 'd'){
//Fetching amount
amount = reader.nextDouble();
//Calling deposit function
myAccount.deposit(amount);
//Printing result
System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'w'){
//Fetching amount
amount = reader.nextDouble();
//Calling withdraw function
myAccount.withdraw(amount);
//Printing result
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'i'){
//Calling addInterest function
double intAdded = myAccount.addInterest();
//Printing result
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
}
else if(type == 'p'){
//Printing result
System.out.printf(" Current balance for account %d: $%.2f ",
myAccount.getAccountNumber(), myAccount.getBalance());
}
}
}
}
Explanation / Answer
HI, I have fixed the issue.
It was error in condition checking.
Please let me know in case of any issue.
import java.util.Random;
//Account class definition
public class Account
{
//Private instance variable
private int accountNumber;
private double balance;
//Constructor that initializes variables
public Account()
{
//Random class object
Random rand = new Random();
//Generating a random 4 digit Account Number
accountNumber = rand.nextInt((9999 - 1000) + 1) + 1000;
//Initializing balance
balance = 0.0;
}
//Getter method for Account Number
public int getAccountNumber()
{
//Return Account Number
return accountNumber;
}
//Method that adds amount to balance
public void deposit(double amt)
{
//Updating new balance
balance = balance + amt;
}
//Method that withdraws amount from balance
public void withdraw(double amt)
{
//Validating amount
if((balance-amt) < 0)
System.out.println(" Error: Can not withdraw $" + amt);
else
//Updating new balance
balance = balance - amt;
}
//Method that adds interest to balance amount
public double addInterest()
{
double actualBalance = balance;
//Adding interest to balance
balance = balance * 1.005;
//Returning amount of interest added
return (balance - actualBalance);
}
//Method that returns balance amount
public double getBalance()
{
return balance;
}
}
#####################
import java.util.Scanner;
public class Prog8 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char type;
double amount;
//Creating an Account object
Account myAccount = new Account();
//Reading transactions file
while(reader.hasNext()) {
//Reading type of transaction
type = reader.next().charAt(0);
//Calling appropriate functions
if(type == 'd'){
//Fetching amount
amount = reader.nextDouble();
//Calling deposit function
myAccount.deposit(amount);
//Printing result
System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'w'){
//Fetching amount
amount = reader.nextDouble();
//Calling withdraw function
myAccount.withdraw(amount);
//Printing result
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'i'){
//Calling addInterest function
double intAdded = myAccount.addInterest();
//Printing result
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
}
else if(type == 'p'){
//Printing result
System.out.printf(" Current balance for account %d: $%.2f ",
myAccount.getAccountNumber(), myAccount.getBalance());
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.