• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Check deposit assignment

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am new to this forum. I am currently in a beginner's class for Java. My assignment was to create code for a checking account.

Here are the instructions

Description

This program will process bank account checks and prevent a negative balance.

Requirements

First, get the initial account balance from the user.
Second, use a while loop to continually allow the user to enters checks until a negative number such as a -1 is entered for the check (note: Other negative numbers such as -2, -999, -0.5, etc. should also terminate the loop.) Do not let this sentinel value influence your balance.
Implement a simple form of error catching within the loop: If a check were to make the balance go negative (bounce), prevent the check from being deducted from the balance. Instead, display the following message: Warning: Check will bounce. No transaction occurred.

Note: You may not subtract the check from the balance and then add it back in if a negative balance occurs; the balance must always be >= 0.

Finally, display the remaining balance after the loop has terminated.
Match the prompts and the output shown in the sample runs below.
Use the Decimal format class as shown in the notes to properly display the balance with a $, commas if necessary, and 2 digits of precision.
Follow all coding guidelines, except the only documentation required is your name above the class.


Checkstyle must be used.

Not using Checkstyle will be an automatic loss of 10%.

Below is my code.. If anyone can help me fix this code to match the requirements, I would REALLY appreciate it.. i am officially stuck Class is tonite, so if you can help, please let me know..

Thanks so much









 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kristina Dummermuth wrote:Requirements

First, get the initial account balance from the user.
Second, use a while loop to continually allow the user to enters checks until a negative number such as a -1 is entered for the check (note: Other negative numbers such as -2, -999, -0.5, etc. should also terminate the loop.) Do not let this sentinel value influence your balance.
Implement a simple form of error catching within the loop: If a check were to make the balance go negative (bounce), prevent the check from being deducted from the balance. Instead, display the following message: Warning: Check will bounce. No transaction occurred.

Note: You may not subtract the check from the balance and then add it back in if a negative balance occurs; the balance must always be >= 0.

Finally, display the remaining balance after the loop has terminated.
Match the prompts and the output shown in the sample runs below.
Use the Decimal format class as shown in the notes to properly display the balance with a $, commas if necessary, and 2 digits of precision.
Follow all coding guidelines, except the only documentation required is your name above the class.

Checkstyle must be used.

What is "Checkstyle"?

In Java a float/Float is almost never used. Typically, the only time you see float is when you are interfacing with legacy code. Use doubles instead.

You are testing exit conditions at the end of the loop which works for exiting the loop but does not meet some of your other requirements. You need to handle exit and error conditions INSIDE the loop before exiting the loop.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What would you like us to do to your code? I am afraid there is a lot that could have been improved.
You are failing one part of the requirements that you are not using a while (...).
Most of us no longer use decimal format. We use System.out.printf() instead. Only write \n and \r if somebody has told you they specifically want those characters. Don't write System.out.print("\n"); Write System.out.println(); instead.
Don't use floats for money (I think you shouldn't use floats for anything, but that is a different story.) Use BigDecimal or denominate all your money in pence/cents/etc. and use integer arithmetic. I seem to be disagreeing with Carey there.
CheckStyle will complain about the variable names (what does ck mean?) and about the name of the class (I hope).
What do the tests in line 34 mean? Do they match your requirements?
 
Kristina Dummermuth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone could fix this for me so that it will work, I would REALLY appreciate it. I am at work now, and won't be able to get anywhere I can fix it myself before class tonite..  You can email the java txt file to me at kdu@frontmatec.com..

I did not use the print F because we have not discussed that in class yet.  I use the System.out.print("\m") so that I have blank lines between code output. I could not get my error to work inside the loop, so if you can fix it for me, I would so much appreciate it
 
Carey Brown
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use floats for money (I think you shouldn't use floats for anything, but that is a different story.) Use BigDecimal or denominate all your money in pence/cents/etc. and use integer arithmetic. I seem to be disagreeing with Carey there.

I agree with you about floating point and currency. Sometimes I try to nudge the OP to the next step without going further. You win.
 
Bartender
Posts: 3518
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this kind of exercises the use of doubles is more than apropriate. I've worked for more than 40 years in the financial business, but I have never used BigDecimals.

More serious though, is that it is explicitely forbidden to withdraw 'ck' from the 'balance' in case the result is negative, yet that is precisely what OP does. And, OP checks for a -1 before printing the final balance, but it is stated that any negative number should do.

I don't know what this StyleChecker will remark for this: a = (b - c). But these things can be strict. I once had to deal with one, and I was punished for using a 'var' instead of a 'val'. So, it is just a matter of running that StyleChecker and improving the code, until it reports no more warnings.


 
Kristina Dummermuth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Style Checker checks the formatting of the code. It's what the instructor prefers us to use with the Eclipse IDE program
 
Greenhorn
Posts: 13
Android Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Send me your code at [deleted]. I'll try to figure out what I can do for you.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't post your email address; that will simply attract spammers.
We recommend you keep all discussions on the forum.
 
Hey! Wanna see my flashlight? It looks like 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!