LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 November 17 2013

OnLiNe
Member

Help with visual basic program

My first topic wasn't quite clear so i'm gonna explain it better this time..

I have a visual basic project to do.
I must program a cash register, that takes in how much a customer must pay and how much he payed. Then display the difference between them. For exemple, if a customer must pay 14250 and he payed 15000 then it must display 750

So far i have no problem but now comes the tricky part for me. The cash register must also display on start up the type of bills that are in the register, like 50 000 L.L, 20 000 L.L etc.. And also display their quantity ex : 30 * 50 000 L.L.
The bills in the register are (from exercise question):50 000 * 30, 20 000 * 5, 10 000*10, 5000 * 10 , 1000 * 100, 500 * 100, 250 * 100, 100* 100.
I return to the exemple above, if the customer payed 15000 then the cashier must pay him back 750 from the bills in the register and the quantity of the bills must decrease to show it. The cashier pays him back with a bill from 500 and a bill from 250 while their quantity decreases minus 1.

This is what i have come up with so far:


Public Class Form1
    Dim money(8), qty(8) As Integer



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        display()

        money(1) = 50000
        qty(1) = 30
        money(2) = 20000
        qty(2) = 5
        money(3) = 10000
        qty(3) = 10
        money(4) = 5000
        qty(4) = 10
        money(5) = 1000
        qty(5) = 100
        money(6) = 500
        qty(6) = 100
        money(7) = 250
        qty(7) = 100
        money(8) = 100
        qty(8) = 100
        Label3.Text = ""


    End Sub
    Private Sub display()
        Dim i As Integer

        money(1) = 50000
        qty(1) = 30
        money(2) = 20000
        qty(2) = 5
        money(3) = 10000
        qty(3) = 10
        money(4) = 5000
        qty(4) = 10
        money(5) = 1000
        qty(5) = 100
        money(6) = 500
        qty(6) = 100
        money(7) = 250
        qty(7) = 100
        money(8) = 100
        qty(8) = 100
        For i = 0 To 7

            Label4.Text = Label4.Text & money(i) & vbTab & "  *  " & qty(i) & vbCrLf


        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim moneyin, moneyout, result, i As Integer
        moneyin = TextBox1.Text
        moneyout = TextBox2.Text
        result = moneyout - moneyin
        Label3.Text = Label3.Text & result
 



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label3.Text = ""

        money(1) = 50000
        qty(1) = 30
        money(2) = 20000
        qty(2) = 5
        money(3) = 10000
        qty(3) = 10
        money(4) = 5000
        qty(4) = 10
        money(5) = 1000
        qty(5) = 100
        money(6) = 500
        qty(6) = 100
        money(7) = 250
        qty(7) = 100
        money(8) = 100
        qty(8) = 100
    End Sub
End class 

I made an array for both the money bills and quantity but i am unsure if that's how it must be done.
I also have no idea how i am gonna write the give money back code..
If anyone can help it will be great.

Last edited by OnLiNe (November 17 2013)

Offline

#2 November 17 2013

nosense
Member

Re: Help with visual basic program

put the code inside a tag for better view

Like This 

Offline

#3 November 18 2013

Joe
Member

Re: Help with visual basic program

Start by solving the problem on a sheet of paper. It's not an easy one, take the time you need to solve it.

Once you solved it, you work on representing the problem in terms of your programming language. It's the part where you build a model for your program to interact with. I don't know VB so I cannot give you a working code, but here are some ideas:

You're maintaining 2 arrays, wouldn't it be simple to just maintain one? Let me explain this using Python code (take a second to read this, even if you don't know Python. It's easier than you'd think):

money = ('50000', '20000', '10000', '5000', '1000', '500', '250', '100')
quantity = (30, 5, 10, 10, 100, 100, 100, 100)

money_and_quantity = (
    ('50000', 30),
    ('20000', 5),
    ('10000', 10),
    ('5000', 10),
    ('1000', 100),
    ('500', 100),
    ('250', 100),
    ('100', 100))

In the first 2 lines I create 2 different arrays to hold the money face value and the quantity. This is pretty annoying to maintain. It works, but it's going to be really annoying to deal with all these arrays if your program wants to grow. In the next line I create a single variable that is an array of elements that hold 2 values each. Each of these element is an array of size 2, representing the face value and the quantity of each bill. That's a cleaner representation.

Also, why do you recreate your arrays constantly? Can't you simply hold the info at a single place that's accessible to all?

Thinking about these 2 issues will help you manage the complexity in your code. Remember the point is to solve the problem on paper first. You cannot write code until you know what you're going to write.

More about the counting change problem

Here's how that exercise is solved in SICP. The solution is used as an introduction to recursive algorithms.

Offline

#4 November 18 2013

OnLiNe
Member

Re: Help with visual basic program

rahmu wrote:

Start by solving the problem on a sheet of paper. It's not an easy one, take the time you need to solve it.

Once you solved it, you work on representing the problem in terms of your programming language. It's the part where you build a model for your program to interact with. I don't know VB so I cannot give you a working code, but here are some ideas:

You're maintaining 2 arrays, wouldn't it be simple to just maintain one? Let me explain this using Python code (take a second to read this, even if you don't know Python. It's easier than you'd think):

money = ('50000', '20000', '10000', '5000', '1000', '500', '250', '100')
quantity = (30, 5, 10, 10, 100, 100, 100, 100)

money_and_quantity = (
    ('50000', 30),
    ('20000', 5),
    ('10000', 10),
    ('5000', 10),
    ('1000', 100),
    ('500', 100),
    ('250', 100),
    ('100', 100))

In the first 2 lines I create 2 different arrays to hold the money face value and the quantity. This is pretty annoying to maintain. It works, but it's going to be really annoying to deal with all these arrays if your program wants to grow. In the next line I create a single variable that is an array of elements that hold 2 values each. Each of these element is an array of size 2, representing the face value and the quantity of each bill. That's a cleaner representation.

Also, why do you recreate your arrays constantly? Can't you simply hold the info at a single place that's accessible to all?

Thinking about these 2 issues will help you manage the complexity in your code. Remember the point is to solve the problem on paper first. You cannot write code until you know what you're going to write.

More about the counting change problem

Here's how that exercise is solved in SICP. The solution is used as an introduction to recursive algorithms.

 
Thank you for the advice, but my instructor didn't teach me how to make 2 arrays into one. In fact he only taught us how to make an array and how to sort the values in it. As you can see it's a hard problem that i have no idea how to continue.

Offline

Board footer