|
-
May 5th, 2013, 10:06 PM
#1
Thread Starter
New Member
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!!
-
May 6th, 2013, 05:38 AM
#2
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 ?
-
May 6th, 2013, 05:49 AM
#3
Re: help! I need some help with my array!!
 Originally Posted by Niya
Is this homework ?
definitely. someone posted a question with exactly the same data yesterday
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2013, 11:02 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|