none
MoneeeeY

    Question

  • Hi

    I was asking how to make a money system

    Label2.Text = "0"

    Every Button1 click you earn 300 to 400

    You can't have more then € 3000,-

    You can buy a Can Of Water for €2,-

    Or a M4A16 for €3000,-

    So if you wan't to buy a M4A16 and you have €50 it says you can't buy!, If you have €3000 It automaticly disables the work button, if  You buy a M4a16 and a Can Of Water it buy's the first item ye clickt it goes -3000 so you have €0 and you want to buy a Can Of Water you Money Doe'nt go €-2 but €0 MsgBox("You can't afford that")

    Hope you understand what i sayd hope you can help me.... i tryd many ways to let it work it didnt work so i landed here...

    -Mr.Piggy

    Wednesday, September 11, 2013 12:01 PM

Answers

  • I would suspect you would want this behavior, so what have you tried in regards to altering my code as I did this on purpose so not to give a complete solution as this appears to be homework. This is very simply to change this behavior and rather than coming back and saying "I don't know how to change the current behavior" try to work something out.

    What would happen in real life? How do you replicate what happens say at an ATM when withdrawing with insufficient funds or what happens when you go and make a purchase at a store ? You might think that the purchase is denied but it depends on the bank and how your card is setup. Same with a withdraw, a denial of withdraw may not be that at all.

    My guess is as you said, stop them so perhaps you might setup a Boolean property that works in tangent with the debt i.e. If MyAccount.Debt(xxxxx) Then or setup a boolean in the form code to do this but seems best to be like real life, think the processes thru.

    In the end I want you to think, try and learn. If you fail show your code so I or others can walk you to success.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, September 13, 2013 11:15 AM
    Moderator
  • Hello,

    Here is a revised VS2012 project that still uses the exact same code as my original post but added one more option to purchase which is really no different than the first. You should be able to take this example, learn from it then adapt to your needs.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, September 12, 2013 4:25 PM
    Moderator

All replies

  • Could you please show what you already have?

    Armin

    Wednesday, September 11, 2013 12:58 PM
  • Hello,

    The following concept keeps track of how much money you have, in this case thinking from a bank account where transactions are against a debit card but that can be interpreted any way you see fit.

    When a deposit or debit is made the balance on the account is updated and we trigger a delegate which works with a warning-level/threshold which when hit or below can be captured in an event in your code.

    The following code goes with the screenshot. There are two NumericUpDown controls where min and max values are set to allow no errors (can not be zero etc), two buttons and two labels.

    NOTE I kept things simple so it is easy to understand as many VB.NET developers generally do not work with Delegates as done here.

    Form code

    Public Class Form1
        Private WarningLevel As Decimal = 100
        WithEvents MyAccount As New Account(WarningLevel)
        Private Sub MyAccount_AccountBalanceWarningEvent(
            sender As Object,
            e As AccountBalanceWarningEventArgs) _
        Handles MyAccount.AccountBalanceWarningEvent
            If e.Balance < 0 Then
                MessageBox.Show("Better get a loan")
            Else
                MessageBox.Show(
                    String.Format("This is a warning you set at {0} and now at {1}",
                                  WarningLevel,
                                  e.Balance
                    )
                )
            End If
        End Sub
        Private Sub cmdMakeDeposit_Click(sender As Object, e As EventArgs) Handles cmdMakeDeposit.Click
            MyAccount.Deposit(NumericUpDown1.Value)
            lblbalance.Text = MyAccount.ToString()
        End Sub
        Private Sub cmdPurchase_Click(sender As Object, e As EventArgs) Handles cmdPurchase.Click
            MyAccount.Debit(NumericUpDown2.Value)
            lblbalance.Text = MyAccount.ToString()
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            lblbalance.Text = MyAccount.ToString()
        End Sub
    End Class

    Class code

    Public Class Account
        Private mWarningLevel As Decimal
        Private mBalance As Decimal
        Public Event AccountBalanceWarningEvent As AccountBalanceWarningEventHandler
        ''' <summary>
        ''' Warning level is when to send an alert via our delegate
        ''' </summary>
        ''' <param name="warningLevel"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal warningLevel As Decimal)
            mWarningLevel = warningLevel
        End Sub
        ''' <summary>
        ''' Current balance of account
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Balance As Decimal
            Get
                Return mBalance
            End Get
        End Property
        ''' <summary>
        ''' Deposit money into account
        ''' </summary>
        ''' <param name="amount"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Deposit(ByVal amount As Decimal) As Decimal
            mBalance += amount
            If mBalance < mWarningLevel Then
                RaiseEvent AccountBalanceWarningEvent(Me, New AccountBalanceWarningEventArgs(mWarningLevel, mBalance))
            End If
            Return mBalance
        End Function
        ''' <summary>
        ''' Withdraw from account
        ''' </summary>
        ''' <param name="amount"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Debit(ByVal amount As Decimal) As Decimal
            mBalance -= amount
            If mBalance < mWarningLevel Then
                RaiseEvent AccountBalanceWarningEvent(Me, New AccountBalanceWarningEventArgs(mWarningLevel, mBalance))
            End If
            Return mBalance
        End Function
        Public Overrides Function ToString() As String
            Return Balance.ToString("c2")
        End Function
    End Class
    Public Class AccountBalanceWarningEventArgs
        Inherits EventArgs
        Private Level As Decimal
        Private Current As Decimal
        Public Sub New(ByVal warningLevel As Decimal, ByVal currentBalance As Decimal)
            Me.Level = warningLevel
            Me.Current = currentBalance
        End Sub
        Public ReadOnly Property WarningLevel() As Decimal
            Get
                Return Level
            End Get
        End Property
        Public ReadOnly Property Balance() As Decimal
            Get
                Return Current
            End Get
        End Property
    End Class
    Public Delegate Sub AccountBalanceWarningEventHandler(ByVal sender As Object, ByVal e As AccountBalanceWarningEventArgs)

    Screenshot


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, September 11, 2013 1:29 PM
    Moderator
  • Something Like this.

    Wednesday, September 11, 2013 2:04 PM
  • [IMG]http://i43.tinypic.com/w86ag3.png[/IMG]

    Something Like this.


    Thanks, but I think I can't add anything to what Kevin posted meanwhile.

    Armin

    Wednesday, September 11, 2013 2:14 PM
  • Seems to me a homework assignment.

    Success
    Cor

    Wednesday, September 11, 2013 2:16 PM
  • My screen?
    Wednesday, September 11, 2013 2:32 PM
  • sOMEONE
    Thursday, September 12, 2013 3:50 PM
  • sOMEONE

    Please respond to Kevin's reply below as he seems to have provided a very complete example which you should be able to adapt to your needs.  If you are having trouble doing so, please show us the specific part of the code that is troubling you, and explain the problem in detailed words.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, September 12, 2013 3:56 PM
    Moderator
  • Hello,

    Here is a revised VS2012 project that still uses the exact same code as my original post but added one more option to purchase which is really no different than the first. You should be able to take this example, learn from it then adapt to your needs.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, September 12, 2013 4:25 PM
    Moderator
  • Look when i do Purchase free form 1000 and i have 0 better get a work or loan then it still goes -1000

    I don't want that it says No enough money and your money does'nt go to -1000

    Friday, September 13, 2013 10:27 AM
  • I would suspect you would want this behavior, so what have you tried in regards to altering my code as I did this on purpose so not to give a complete solution as this appears to be homework. This is very simply to change this behavior and rather than coming back and saying "I don't know how to change the current behavior" try to work something out.

    What would happen in real life? How do you replicate what happens say at an ATM when withdrawing with insufficient funds or what happens when you go and make a purchase at a store ? You might think that the purchase is denied but it depends on the bank and how your card is setup. Same with a withdraw, a denial of withdraw may not be that at all.

    My guess is as you said, stop them so perhaps you might setup a Boolean property that works in tangent with the debt i.e. If MyAccount.Debt(xxxxx) Then or setup a boolean in the form code to do this but seems best to be like real life, think the processes thru.

    In the end I want you to think, try and learn. If you fail show your code so I or others can walk you to success.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, September 13, 2013 11:15 AM
    Moderator