7 Replies - 920 Views - Last Post: 23 January 2012 - 09:40 PM Rate Topic: -----

#1 The_Programmer-  Icon User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

I can withdrawl more than my account has in this program

Posted 23 January 2012 - 05:09 PM

I'm rushing to finish my VB.NET final today. I set up everything on this program and it seems to work great! My problem: I set up an if statement on the withdrawl command that checks if the number the user entered is more than what they have in their account and if it fails, it is suppose to tell them that they cannot withdrawl it. When I have $0 and I type withdrawl 500, and I type "total," it says that I have -$500 in my account. Can you tell me what I did wrong here?

Module Module1

    Dim accountTotal As Double = 0D
    Dim running As Boolean = True
    Dim firstRun As Boolean = True

    Sub Main()

        While running = True

            Dim command() As String

            If firstRun Then
                Console.WriteLine("Welcome to the Checkbook Register Program. This program will let you keep a running total of what you have in your bank acount.")
                Console.WriteLine("These are the following commands without the stars: *deposit (amount)* *withdrawl (amount)* *total* *clearaccount* *stop* *help*")
                Console.WriteLine("Press any key to continue!")
                Console.ReadKey()
                firstRun = False
            End If

            Console.Clear()

            Console.WriteLine("Command: ")

            command = Console.ReadLine().Split(" ")

            If command.Length > 2 Or command.Length = 0 Then
                Console.WriteLine("Usage: ")
                Console.WriteLine("deposit (amount)")
                Console.WriteLine("withdrawl (amount)")
                Console.WriteLine("total")
                Console.WriteLine("clearaccount")
                Console.WriteLine("stop")
                Console.WriteLine("help")
                Console.WriteLine("Press any key to continue!")
                Console.ReadKey()

            ElseIf command(0).Equals("") Then
                Console.WriteLine("Usage: ")
                Console.WriteLine("deposit (amount)")
                Console.WriteLine("withdrawl (amount)")
                Console.WriteLine("total")
                Console.WriteLine("clearaccount")
                Console.WriteLine("stop")
                Console.WriteLine("help")
                Console.WriteLine("Press any key to continue!")
                Console.ReadKey()

            ElseIf command(0).Equals("help") Then
                Console.WriteLine("Usage: ")
                Console.WriteLine("deposit (amount)")
                Console.WriteLine("withdrawl (amount)")
                Console.WriteLine("total")
                Console.WriteLine("clearaccount")
                Console.WriteLine("stop")
                Console.WriteLine("help")
                Console.WriteLine("Press any key to continue!")
                Console.ReadKey()

            ElseIf command(0).Equals("deposit") Then

                Try
                    Dim addTotal As Double = Double.Parse(command(1))
                    accountTotal += addTotal
                    Console.WriteLine("$" & addTotal & " was added to your account! Press any key to continue.")
                Catch ex As Exception
                    Console.WriteLine("You did not enter a number to add to your account! You might have entered a letter. Press any key to continue!")
                Finally
                    Console.ReadKey()
                End Try

            ElseIf command(0).Equals("withdrawl") Then

                Dim withdrawlAmmount As Double = 0D
                Dim failed As Boolean = False

                Try
                    withdrawlAmmount = Double.Parse(command(1))
                Catch ex As Exception
                    failed = True
                End Try

                If Not (withdrawlAmmount > accountTotal) Or Not (failed) Then
                    accountTotal -= withdrawlAmmount
                    Console.WriteLine("$" & withdrawlAmmount & " was removed from your account! Press any key to continue!")
                    Console.ReadKey()
                Else
                    Console.WriteLine("Either your account balance is too low to perform this withdrawl, or you entered a letter instead of a number! Press any key to continue.")
                    Console.ReadKey()
                End If

            ElseIf command(0).Equals("total") Then
                Console.WriteLine("You have a total of $" & accountTotal & " in your account. Press any key to continue!")
                Console.ReadKey()

            ElseIf command(0).Equals("clearaccount") Then
                Console.WriteLine("Are you sure you want to clear your account of all money? (y / n): ")

                Dim yn As String = Console.ReadLine()

                If yn.Equals("y") Then
                    accountTotal = 0D
                    Console.WriteLine("Your account was cleared! Press any key to continue.")
                    Console.ReadKey()
                Else
                    Console.WriteLine("I take it that you don't want to clear your account? That's okay. Press any key to continue.")
                    Console.ReadKey()
                End If

            ElseIf command(0).Equals("stop") Then

                Console.WriteLine("Are you sure you want to stop this program? Your account total and any other data will be lost. (y / n): ")

                Dim stopProgram As String = Console.ReadLine()

                If stopProgram.Equals("y") Then
                    running = False
                Else
                    Console.WriteLine("I take it you do not want to quit the program? Press any key to continue!")
                    Console.ReadKey()
                End If

            End If

        End While

    End Sub

End Module


Is This A Good Question/Topic? 0
  • +

Replies To: I can withdrawl more than my account has in this program

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 05:21 PM

Your condition is wrong here:
If Not (withdrawlAmmount > accountTotal) Or Not (failed) Then

You need AndAlso instead of OR which will mean "If withdraw amount is not greater that total and also it has not failed, then..."
Was This Post Helpful? 4
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 05:26 PM

When you say "final" do you mean this is a final exam? Like for the end of the term? Because this code is just fugly.

Why do you have your menu of options repeated several times? We never repeat code like that!

OMG! This is one big while construct! There are no methods/functions/subroutines! LMAO

I hope like most 'finals' this one is not weighted to be a large percentage of your overall course grade.




Learning to debug one's own code is an essential skill. Sadly, one that apparently no college course teaches. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.
TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

This post has been edited by tlhIn`toq: 23 January 2012 - 05:27 PM

Was This Post Helpful? -1
  • +
  • -

#4 The_Programmer-  Icon User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 06:12 PM

Wow. Just wow... I wrote this the way I did because it made sense to me. I also took out some of the Help sections because I didn't need them. And also, I used Or instead of AndAlso, because I was going to check ElseIf withdrawlAmount > accountTotal Or failed. So I forgot to remove the Or. This is my style of coding "in vb.net" just because most of VB.NET does not make sense to me. If I was using Java, my primary programming language, I would have used multiple classes and methods.
This is an end of term final. I have wrote programs like this in the past and my teacher gave me an A+ and said great job. This might seem messy to you, but it is not to me. Again, I would not program like this in Java.

This post has been edited by The_Programmer-: 23 January 2012 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 07:21 PM

View PostThe_Programmer-, on 23 January 2012 - 07:12 PM, said:

This is my style of coding "in vb.net" just because most of VB.NET does not make sense to me. If I was using Java, my primary programming language, I would have used multiple classes and methods.


If using a clutch and gear shift knob does not make sense to you, would you just drive along in first gear at 60 mph as "your style in a stick shift car"?

For the love of Pete... you understand the concept of classes and methods and made the decision to write your final exam like this anyway? That makes the entire fiasco above doubly bad!

If you understand using methods then quit being too fraking lazy to learn the VB.NET syntax and use methods and classes in this new language you CHOSE to learn! What's the point of taking on this course just to throw away your time and money and get nothing out of it? All you've done is take a slot in that class that a motivated student could have taken, and maybe might have made an effort.

On the plus side for the other students, you've lowered the median of the bell curve so now the mediocre students can receive A's and B's. They should at least buy you pizza and beer for that!
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 07:33 PM

This would have been a Gimme Teh Codez post if the OP hadn't made an effort! He had code, he made a mistake and he asked for our help!
Was This Post Helpful? 0
  • +
  • -

#7 The_Programmer-  Icon User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 09:34 PM

Someone is pissed. What did you do this time? Did someone steal your drugs? FYI I am in highschool and I HAD to take this class before the Java one. And I have an A+ in the damn class. I don't care what are people think so just stfu!
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I can withdrawl more than my account has in this program

Posted 23 January 2012 - 09:40 PM

Ok this topic is done!
Closed
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1