9 Replies - 1013 Views - Last Post: 28 September 2012 - 01:21 PM Rate Topic: -----

#1 Kurdistan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Visual Basic Soda Machine

Posted 24 September 2012 - 07:34 PM

Write a Visual Basic Application that is user friendly and easy to use. You are going to be creating a soda machine. Your soda machine will have 3 different kinds of soda, one has to be diet coke, and the others are of your choice. The cost of the soda, 1.25 must be displayed. There has to be a place for a person to “Enter money”. The user can enter dollar bills, quarters, dimes and nickels. The user must have enough money to purchase soda, if not a message will show. Change will be given in the smallest number of coins. There must be a button to get all the money back and not buy a soda. If that is the case, the user will get his money back in all nickels.
The design of the form must be easy to understand and use.
Note: Each time the application begins, we only 5 sodas of each kind. In one run of the program, if we sell out of one kind, that slot is empty and should be shown as empty. You must keep track of what you sell. User can only buy one soda at a time.

Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic Soda Machine

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Visual Basic Soda Machine

Posted 24 September 2012 - 08:45 PM

View PostKurdistan1, on 25 September 2012 - 08:04 AM, said:

Write a Visual Basic Application that is user friendly and easy to use. You are going to be creating a soda machine. Your soda machine will have 3 different kinds of soda, one has to be diet coke, and the others are of your choice. The cost of the soda, 1.25 must be displayed. There has to be a place for a person to “Enter money”. The user can enter dollar bills, quarters, dimes and nickels. The user must have enough money to purchase soda, if not a message will show. Change will be given in the smallest number of coins. There must be a button to get all the money back and not buy a soda. If that is the case, the user will get his money back in all nickels.
The design of the form must be easy to understand and use.
Note: Each time the application begins, we only 5 sodas of each kind. In one run of the program, if we sell out of one kind, that slot is empty and should be shown as empty. You must keep track of what you sell. User can only buy one soda at a time.


Well, posting the question here just won't help you. There must be some kind of effort from your part as well. What I suggest is write the code for the program and ask specific questions regarding errors or roadblocks that you face. Please do not think that people will write programs for you blindly. They expect that you put in at least 80 % effort and they will certainly help you with the remaining 20 %.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 Kurdistan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Re: Visual Basic Soda Machine

Posted 25 September 2012 - 12:56 PM

thanks you for let me know that. is just i am have hard time with this one.
Was This Post Helpful? 0
  • +
  • -

#4 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Visual Basic Soda Machine

Posted 25 September 2012 - 02:58 PM

I always sketch out a design of how the form is going to look like first. From there on you'll know what is available to store/display your values. Like 6 textboxes or labels to display your soda name and their price. Just think about it a bit more.
Was This Post Helpful? 0
  • +
  • -

#5 Kurdistan1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 12

Re: Visual Basic Soda Machine

Posted 26 September 2012 - 09:08 AM

this is what i have know












Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalc.Click


        Dim PennyValue As Single
        Dim NickelValue As Single
        Dim DimeValue As Single
        Dim QuarterValue As Single
        Dim DollarsValue As Single
        Dim TotalAmount As Single

        PennyValue = Val(txtPennies.Text) * 0.01
        NickelValue = Val(txtNickels.Text) * 0.05
        DimeValue = Val(txtDimes.Text) * 0.1
        QuarterValue = Val(txtQuarters.Text) * 0.25
        DollarsValue = Val(txtDollars.Text) * 100

        TotalAmount = PennyValue + NickelValue + DimeValue + QuarterValue

        lblTotal.Text = Format(TotalAmount, "Currency")

    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click

        txtPennies.Text = ""
        txtNickels.Text = ""
        txtDimes.Text = ""
        txtQuarters.Text = ""
        txtDollars.Text = ""
        lblTotal.Text = ""
        txtPennies.Focus()

    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

        Me.Close()

    End Sub

    Private Sub txtPennies_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPennies.GotFocus

        txtPennies.SelectAll()

    End Sub

    Private Sub txtPennies_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPennies.KeyPress

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) < 32)

    End Sub

    Private Sub txtPennies_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPennies.TextChanged

        With txtPennies
            If Len(.Text) = .MaxLength Then
                txtNickels.Focus()
            End If
        End With

    End Sub


    Private Sub txtNickels_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNickels.GotFocus

        txtNickels.SelectAll()

    End Sub

    Private Sub txtNickels_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNickels.KeyPress

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) < 32)

    End Sub

    Private Sub txtNickels_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNickels.TextChanged

        With txtNickels
            If Len(.Text) = .MaxLength Then
                txtDimes.Focus()
            End If
        End With

    End Sub

    Private Sub txtDimes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDimes.GotFocus

        txtDimes.SelectAll()

    End Sub

    Private Sub txtDimes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDimes.KeyPress

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) < 32)


    End Sub

    Private Sub txtDimes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDimes.TextChanged

        With txtDimes
            If Len(.Text) = .MaxLength Then
                txtQuarters.Focus()
            End If
        End With

    End Sub

    Private Sub txtQuarters_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuarters.GotFocus

        txtQuarters.SelectAll()

    End Sub

    Private Sub txtQuarters_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuarters.KeyPress

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) < 32)

    End Sub

    Private Sub txtQuarters_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQuarters.TextChanged

        With txtQuarters
            If Len(.Text) = .MaxLength Then
                cmdCalc.Focus()
            End If
        End With
    End Sub

    Private Sub txtDollars_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDollars.TextChanged

        txtDollars.SelectAll()

    End Sub
End Class

This post has been edited by modi123_1: 26 September 2012 - 09:12 AM
Reason for edit:: highlight the lines THEN click the code tags

Was This Post Helpful? 0
  • +
  • -

#6 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 271
  • Joined: 22-August 11

Re: Visual Basic Soda Machine

Posted 26 September 2012 - 11:52 AM

You need to plan out how your logic needs to work on paper first, then write the code.
If you design your algorithm before you code, the code almost can write itself.

Couple things I'd suggest you change.
First, give your controls meaningful names, Button2_Click means nothing to the the next guy that has to look at your code, even if you are the next guy.
try, btnCalculate_Total or something.
Second, you declare a single variable, why not assign its value in the declaration.
then you can do the calculations with variables

Const PennyValue as Single = .01 'Value doesn't change
Const NickelValue as Single = .05 'so make it constant
and so on.


You'll probably need variables for the total number of pennies inserted

dim totalPennies as single
dim totalPennies as double
etc..



don't use the Val() method, use the tryparse method instead, val can return bad values if the users doesn't put in a number.

totalPennies = Single.TryParse(txtPennies.Text, totalPennies)
totalNickels = Single.TryParse(txtNickels.Text, totalNickels)
etc.



Then you probably will need a loop of some sort with a counter to keep track of how many sodas you have left.

What are you doing in your textChanged subs?
Your KeyPress events can be done in a single sub,

Private Sub CancelKeys(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNickels.KeyPress, txtPennies.KeyPress, txtDimes.KeyPress,  'etc etc 

        e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) < 32)

End Sub


This post has been edited by torind_2000: 26 September 2012 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Visual Basic Soda Machine

Posted 26 September 2012 - 12:01 PM

View Posttorind_2000, on 26 September 2012 - 02:52 PM, said:

totalPennies = Single.TryParse(txtPennies.Text, totalPennies)
totalNickels = Single.TryParse(txtNickels.Text, totalNickels)
etc.


Actually you're probably better off with
If Not Single.TryParse(txtPennies.Text, totalPennies) Then
   'Display an error as the value could not be parsed
End if

Because upon success, totalPennies will hold the parsed value.

The method above could still return an unwanted value
With option strict off, it will return 0 if unsuccessful
with Option Strict On, you receive implicit conversions from Boolean to Single are not allowed.
Was This Post Helpful? 1
  • +
  • -

#8 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 271
  • Joined: 22-August 11

Re: Visual Basic Soda Machine

Posted 26 September 2012 - 12:12 PM

thanks Charlie, I'm adding that to my "Better way to do stuff" file :)
Was This Post Helpful? 0
  • +
  • -

#9 seobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-September 12

Re: Visual Basic Soda Machine

Posted 28 September 2012 - 12:18 PM

View PostKurdistan1, on 24 September 2012 - 07:34 PM, said:

Write a Visual Basic Application that is user friendly and easy to use. You are going to be creating a soda machine. Your soda machine will have 3 different kinds of soda, one has to be diet coke, and the others are of your choice. The cost of the soda, 1.25 must be displayed. There has to be a place for a person to “Enter money”. The user can enter dollar bills, quarters, dimes and nickels. The user must have enough money to purchase soda, if not a message will show. Change will be given in the smallest number of coins. There must be a button to get all the money back and not buy a soda. If that is the case, the user will get his money back in all nickels.
The design of the form must be easy to understand and use.
Note: Each time the application begins, we only 5 sodas of each kind. In one run of the program, if we sell out of one kind, that slot is empty and should be shown as empty. You must keep track of what you sell. User can only buy one soda at a time.


Do you currently take CST 216? I have an lab (If Statements) with an identical question due this week.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Visual Basic Soda Machine

Posted 28 September 2012 - 01:21 PM

A useful approach is to separate the Logic from the GUI.

Guessing Game Tutorial

You build an core that just handles to logic and state transitions, via methods.
Convey changes externally via Events.

sodaMachine.InsertCoin(coin As Coin)
sodaMachine.PushButton(button As SodaMachineButton)
sodaMachine.CurrentState() As sodaMachineState




So do you can build differ GUIs on top of it, by listening to the events.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1