• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

need help with code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Scanner;

public class Account
{
private double balance;
private double withdrawal;

public Account(double initialBalance)
{
if(initialBalance > 0.0)
balance = initialBalance;

if(withdrawal < 25000.00)
withdrawal = balance;
else
{
System.out.print("Debit amount exceeded account balance");
}
}
public void debit(double withdrawal)
{
balance = balance - withdrawal;
}

public void credit(double amount)
{
balance = balance + amount;
}

public double getBalance()
{
return balance;
}

public static void main(String args[])
{
Account account = new Account(25000.00);

Scanner input = new Scanner(System.in);
double depositAmount;
double withdrawalAmount;


System.out.printf("You've got $%.2f in your account\n",account.getBalance());

System.out.print("Enter deposit amount");
depositAmount = input.nextDouble();
System.out.printf("nAdding %.2f to the account concerned\n\n",depositAmount);
account.credit(depositAmount);
System.out.printf("You've got $%.2f in your account\n",account.getBalance());



System.out.print("Enter withdrawal amount");
withdrawalAmount = input.nextDouble();
System.out.printf("nDeducting $%.2f from the account concerned\n\n",withdrawalAmount);
account.debit(withdrawalAmount);
System.out.printf("You've got $%.2f in your account\n",account.getBalance());
}
}


the above code is supposed to give me ma account balance and prompt me to enter a deposit amount which it'll add up to the initial balance and afterwards prompt me to enter withdrawal amount,now this is where ma problem lies.im tryin to ensure that the debit amount does not exceed the account's balance,if it does the balance is to be left unchanged and printing a message indicating "Debit amount exceeds account balance".instead it keeps leaving the account with a negative balance.what do i do?
 
lowercase baba
Posts: 12989
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code tags around your source code. there are those little "instand ubb code" buttons - one is labeld "code". by using this, and pasting your code between the tags, the formatting is preserved, making what you have MUCH easier to read.

Now, take a look at this code:


what is it doing?
[ March 19, 2007: Message edited by: Fred Rosenberger ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic