Results 1 to 4 of 4

Thread: help! I need some help with my array!!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    1

    help! I need some help with my array!!

    I'm working on a project and I need it to be able to subtract the quantity ordered from the inventory presented in the array. I also need to be able to not allow the quantity ordered to ever exceed the inventory level in the array. If anyone could offer me some assistance I would greatly appreciate it!

    here's what I have so far... I know I may not be doing this the easy way but I'm kinda new at this..

    Public Class Form1
    Dim catno(intMAX_Subscript) As String
    Dim desc(intMAX_Subscript) As String
    Dim price(intMAX_Subscript) As Single
    Dim quantity(intMAX_Subscript) As Integer
    Dim unitsReq(intMAX_Subscript) As Integer
    Const intMAX_Subscript As Integer = 9
    Dim subtotal(intMAX_Subscript) As Single
    Dim total As Single
    Dim shipping As Single
    Dim grandtotal As Single


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    initArrays()
    End Sub
    Private Sub initArrays()
    catno(0) = "A20gaw"
    desc(0) = "golden apples-WA"
    price(0) = ".83"
    quantity(0) = 388

    catno(1) = "A21ham"
    desc(1) = "HoneyCrisp Aplles-MN"
    price(1) = "3.13"
    quantity(1) = 224

    catno(2) = "B28gbh"
    desc(2) = "Green Banana-HI"
    price(2) = ".25"
    quantity(2) = 2170

    catno(3) = "B67vba"
    desc(3) = "Vaccinium Blueberries-AL"
    price(3) = ".10"
    quantity(3) = 3000

    catno(4) = "G17rgf"
    desc(4) = "Ruby Red Grapefruit-FL"
    price(4) = "1.36"
    quantity(4) = 605

    catno(5) = "M45pmm"
    desc(5) = "Pulp Mango-MX"
    price(5) = "1.11"
    quantity(5) = 315

    catno(6) = "O17nof"
    desc(6) = "Navel Orange-FL"
    price(6) = ".84"
    quantity(6) = 823

    catno(7) = "P27gph"
    desc(7) = "Golden Pinapple-HI"
    price(7) = "2.45"
    quantity(7) = 319

    catno(8) = "S49rsc"
    desc(8) = "Red Strawberry-CA"
    price(8) = "2.99"
    quantity(8) = 800

    catno(9) = "W09swi"
    desc(9) = "Seedless Watermelon-IN"
    price(9) = "1.70"
    quantity(9) = 277
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim intcount As Integer = 0
    Do While intcount <= intMAX_Subscript
    Try
    unitsReq(intcount) = CInt(
    InputBox("enter the amount of " & catno(intcount) & " you would like to purchase"))
    intcount += 1


    Catch
    MessageBox.Show("enter a valid amount")


    Return


    End Try

    Loop


    For intcount = 0 To intMAX_Subscript


    ListBox1.Items.Add("Catalog number: " & catno(intcount))
    ListBox1.Items.Add("Description: " & desc(intcount))
    ListBox1.Items.Add("Price: " & price(intcount).ToString("c"))
    ListBox1.Items.Add("Quantity: " & unitsReq(intcount))
    ListBox1.Items.Add("Subtotal per Item: " & (price(intcount) * unitsReq(intcount)).ToString("c"))

    subtotal(0) = price(0) * unitsReq(0)
    subtotal(1) = price(1) * unitsReq(1)
    subtotal(2) = price(2) * unitsReq(2)
    subtotal(3) = price(3) * unitsReq(3)
    subtotal(4) = price(4) * unitsReq(4)
    subtotal(5) = price(5) * unitsReq(5)
    subtotal(6) = price(6) * unitsReq(6)
    subtotal(7) = price(7) * unitsReq(7)
    subtotal(8) = price(8) * unitsReq(8)
    subtotal(9) = price(9) * unitsReq(9)

    total = subtotal(0) + subtotal(1) + subtotal(2) + subtotal(3) + subtotal(4) + subtotal(5) + subtotal(6) + subtotal(7) + subtotal(8) + subtotal(9)


    If total < "100" Then
    shipping = 4.99
    Else
    If total >= "100" And total < "250" Then
    shipping = 3.49
    Else
    If total >= "250" And total < "500" Then
    shipping = 2.99
    Else
    If total >= "500" Then
    shipping = 0
    End If
    End If
    End If
    End If

    If CheckBox1.Checked = True Then
    grandtotal = total + (total * 0.08) + shipping
    Else
    grandtotal = total + shipping
    End If

    TextBox1.Text = grandtotal.ToString("c")
    Next
    If unitsReq(0) > 1 Then

    End If

    End Sub


    End Class

    ...Thanks for the help in advance!!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: help! I need some help with my array!!

    First of all, why are you using parallel arrays ? Using classes would be much better. Is this homework ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: help! I need some help with my array!!

    Quote Originally Posted by Niya View Post
    Is this homework ?
    definitely. someone posted a question with exactly the same data yesterday

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: help! I need some help with my array!!

    Ok, so it's homework. A couple points:

    1) It looks like Button1 is where you are getting the amount required from the user. You are putting that directly into an array, but I would suggest that you put it into some other variable instead. Suppose you made an integer variable called Temp in that button click event and put the value into Temp. You could then compare temp to the number of items available and refuse the whole order if temp was greater than the number on hand. Alternatively, you could tell the user that they asked for 'temp' items, but there were only 'max' items available, so they could be given a chance to amend their request. Once temp was less than the inventory, you'd be able to put it into the array and also to subtract it from the inventory, which would solve both of your questions.

    2) You use exception handling around the InputBox. That's a bad idea because exception handling is so slow that it should only be used for exceptional circumstances. That situation isn't exceptional at all. The problem is that CInt will throw an exception if it is given a string that can't be converted. If the user presses Cancel then InputBox will return an empty string and CInt will throw an exception. Nothing exceptional about that. What you should do is get the return from InputBox into a string variable, check to see if it is "", in which case you would want to just exit the sub, and if it is not "", then properly convert it using Integer.TryParse. Few classes teach that, but there are thousands of examples of its use on this forum (somebody writes one nearly every day), while MSDN also has good examples.
    My usual boring signature: Nothing

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