Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2005, 8:02 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Unhappy Help

I'm sorta new to java and i haven't learned any of the higher commands yet. I have to complete an assignment in which i want the program to do the following:

Quote:
Cheque Book Program

Please enter Opening Balance? 500.00

(Clear screen here)

Cheque Book Program

Opening Balance: 500.00
Debit or Credit: 4.00
Balance: 504.00

Another Entry? y

Current Balance: 504.00
Debit or Credit: -10.00
Balance: 494.00

Another Entry? n

Thanks for using this program.
My teacher isn't much help since she makes things too complicated so i thought i'd ask here.
This is the code i'm using and its not working properly:

import java.awt.*;
import hsa.Console;
public class cbook
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        double open, dorc, count, count1;
        char entry;
        c.clear ();
        c.print ("Opening Balance: ");
        open = c.readDouble ();
        do
        {
            c.print ("Debit or Credit: ");
            count = c.readInt ();
            if (count >= open - count)
                c.println ("Error!Please eneter a valid amount!");
        }
        while (count >= open - count);
        c.print ("Balance: ");
        c.println (open + count);
        c.println ();
        do
        {
            c.print ("Another Entry? ");
            entry = c.readChar ();
            if (entry == 'Y' || entry == 'y')
                c.println ();

            c.print ("Current Balance: ");
            c.println (count + open, 2, 2);
            c.print ("Debit or Credit: ");
            count1 = c.readInt ();
            if (count1 >= open - count1)
                c.println ("Error!Please eneter a valid amount!");

            c.print ("Balance: ");
            c.println (open + count + count1);
            c.println ();
        }
        while (entry == 'Y' || entry == 'y');
        
    }
}

Last edited by Dark Flare Knight; Mar 22nd, 2005 at 8:09 PM.
Dark Flare Knight is offline   Reply With Quote
Old Mar 22nd, 2005, 10:21 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
What is the code doing? I don't really have time to run it myself, but if you could post a run of the program it'll make it easier for us to help you debug it.
Dizz
PS, this is a great post, has a lot of code, and a detailed description of the problem, props to you DFK!
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 23rd, 2005, 4:13 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
Attemptes it and had some problems with my compiler :/ so i gave up sorry can you post your errors i might be able to suggest routes to go down that way.
Berto is offline   Reply With Quote
Old Mar 23rd, 2005, 7:17 AM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 318
Rep Power: 4 mackenga is on a distinguished road
You've actually got a few problems here; I haven't tried to run the code or anything, so this is just off the top of my head and I don't know if this is everything. Below is your code, with comments added by me to say what I think is up.

import java.awt.*;
import hsa.Console;
public class cbook
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        double open, dorc, count, count1;
        char entry;
        c.clear ();
        c.print ("Opening Balance: ");
        open = c.readDouble ();
        do
        {
            c.print ("Debit or Credit: ");
            count = c.readInt (); // should be c.readDouble() again (1)
            if (count >= open - count)
                c.println ("Error!Please eneter a valid amount!");
        }
        while (count >= open - count);
        c.print ("Balance: ");
        c.println (open + count);
        c.println ();
        do
        {
            c.print ("Another Entry? ");
            entry = c.readChar ();
            if (entry == 'Y' || entry == 'y')
                c.println (); // only this statement is conditional (2)

            c.print ("Current Balance: ");
            c.println (count + open, 2, 2);
            c.print ("Debit or Credit: ");
            count1 = c.readInt (); // again, this should be c.readDouble() (3)
            if (count1 >= open - count1)
                c.println ("Error!Please eneter a valid amount!"); // typo!

            c.print ("Balance: ");
            c.println (open + count + count1); // count1 may not be valid (4)
            c.println ();
        }
        while (entry == 'Y' || entry == 'y');
        
    }
}

If your values are doubles, you can't read them with c.readInt(); anyway, you wouldn't want to because they aren't going to be integers according to your specification in your post (1 and 3). In point 2, I think you wanted more statements to be executed conditionally depending on the test in your if statement; you need to use braces { } around the block to make this happen.

Finally, at point 4, you output balance taking into account the value of count1 even when your program has complained than an invalid value was given; probably not a good idea! You could set the value of count1 to zero where it's invalid to fix this.

Also, because you read the count/count1 values each time, your program is forgetting earlier 'transactions' rather than adding them up; e.g. if I enter -5.00 then -5.00 again, the balance the second time will not have gone down by 10.00, but just by 5.00. You could correct this by reading into a value other than count/count1 and then adding the value (it's as easy as adding because adding a negative is subtracting) so that count/count1 contain running totals. Be careful only to add the newly read value in when it's valid though.

Hope this helps! Like I say, I don't know if it's everything.
mackenga is offline   Reply With Quote
Old Mar 23rd, 2005, 11:24 AM   #5
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Thanks for your help. I think i got it now.

Last edited by Dark Flare Knight; Mar 23rd, 2005 at 4:00 PM.
Dark Flare Knight is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC