Results 1 to 5 of 5

Thread: Rate calculation for project

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    3

    Rate calculation for project

    As a heads up, I am a complete and utter newbie to both visual basic, as well as coding in general. I have no idea which of my questions have glaringly obvious solutions or make me look like an idiot. Please remember that everybody starts from somewhere?

    I have recently begun to learn VB, and as part of my college's project we have to build a rate calculator. It has to work like this.

    The user enters a number of days, for example, 17. I am to have coded a daily, weekly and fortnightly rate beforehand, and the code should break down the number of days entered (17) into one fortnightly rate, then add on three extra days of the daily rate automatically. The end figure should then be displayed in a read only textbox, letting the user know the end fee charged.

    After reading my VB book and skimming the internet, as well as talking to classmates... I honestly have no idea how to do this.

    Could someone please give me a template or example of code that would let me do this? I realize this forum is probably not meant for newbies pestering you all for help instead of figuring things out on their own, but I genuinelly have tried and everything in my book just seems like jargon. I can't even tell which chapter I need to read to learn to do this...

    I will stop rambling now. Anyway, any help will be appreciated. If not, sorry for bothering you guys.

  2. #2
    New Member
    Join Date
    Sep 2012
    Posts
    5

    Re: Rate calculation for project

    The first thing I would do is figure out the math.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    3

    Re: Rate calculation for project

    It's supposed to do the maths itself.

    The user has like 10 different choices of items to pick from, each item has a different daily/weekly/fortnightly rate for how long they rent it. They pick the item (rdo1 to choose option 1 I guess) and then they enter in the number of days they want to have the item for. The rates have all been coded in, and it's supposed to just calculate itself, using fortnightly and weekly first if they apply and then adding on the daily to make up for any additional days.

    I can't really tell if I am making sense... I am completely self-aware of my own ineptitude here. I just have no idea how to code a system when they can enter in a number of days and then that number goes through a tiered system to calculate a fee. Literally no idea. Which is why I'm asking you guys for help.

    Heres an example

    Car 1 daily = 10 weekly = 60 fortnightly = 110

    If they type in 15 days, I need the code to automatically say it'll cost them 120... somehow? I know it must be possible somehow, since that's what we were assigned

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Rate calculation for project

    If you create a new project and add a button and textbox (both using the given names of Button1 and TextBox1) the following might give you a place to begin:
    vb.net Code:
    1. 'Declare a sub to handle all your calculations etc.
    2.     Private Sub GetPeriodRate(UserTextBox As TextBox)
    3.  
    4.         Dim daycount As Integer 'Declare a variable to hold the number of days.
    5.  
    6.         'Step 1 - converting the content of the TextBox to a number (number of days) that can
    7.         '         be used in our calculations. This is usually accomplished by the TryParse method.
    8.         If Not Integer.TryParse(UserTextBox.Text, daycount) Then
    9.  
    10.             MessageBox.Show("Invalid data entered in the textbox - expecting an integer number.")
    11.             TextBox1.SelectAll() 'Select all text in the textbox and give it focus
    12.             TextBox1.Focus()
    13.             Exit Sub
    14.  
    15.         End If
    16.  
    17.         'NOTE: You might want to add a check here or modify the one above,
    18.         'so the entered number is allways positive.
    19.  
    20.         'Step 2 - using the number of days to calculate the number of fortnights, weeks and days.
    21.         Dim fortnights, weeks, days As Integer
    22.  
    23.         fortnights = daycount \ 14    'The \ operator is integer division.
    24.         weeks = (daycount Mod 14) \ 7 'Mod is tradition modulo - weeks ofc will allways be 1 or 0.
    25.         days = daycount Mod 7
    26.  
    27.         'Used to display the current progress - has no importance for calculations and can be removed.
    28.         'The underscore '_' is used to seperate a command-line that spans more than one actual line.
    29.         MessageBox.Show(String.Format("{0} days is equivalent to {1} fortnights, {2} weeks and {3} days.", _
    30.                                       daycount, fortnights, weeks, days))
    31.  
    32.         'Step 3 - doing the actual calculations:
    33.         Dim total_rate As Decimal
    34.  
    35.         'This will most likely involve multiplying fortnights, weeks and days with the appropriate rates
    36.         'and adding the results together to get a total rate. Something like:
    37.         ' total_rate = fortnight * fortnight_rate + weeks * week_rate + days * day_rate
    38.         'This final result should probably then be displayed to the user in a MessageBox, TextBox or similar.
    39.  
    40.     End Sub
    41.  
    42.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    43.         GetPeriodRate(TextBox1)
    44.     End Sub
    It should just be pasted into the Form1 class.

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    3

    Re: Rate calculation for project

    Thank you so much! I was utterly clueless, that looks perfect as a template for what to do!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width