I need to some assistance in creating some input validation for a homework assignment. The program is creating an application that shows, based in inventory and order request, how many spools of wire are ready to ship, backed ordered, shipping costs, and total cost. The picture below is the form designed given to use.
Attachment 99773
This chapter is covering creating procuedures and functions. The assignment also said to include input validation; must order at least 1 spool; I've included inventory cannot be less than zero. The assignment also to use an inputbox to get current inventory in stock
I have everything working, almost. The problem is when I give bad data for current inventory or spools ordered, say -100, I get the error messagebox I create, but the program continues, and then gives bad output. I haven't figured how to prevent bad output. Something like
My thinking was to create some kind of Loop to test the input and repeat till I got input that was is correct.
Another option I could do is create another function for output, and have it compared the output values. If they values are less zero, have it change the labels' text property to an empty string. But I don't like this option because I'm not really learning a way to force to user to give good input.
Here's the code the assignment. I realize some is probably not needed, but wasn't fully certain what could be removed without making more difficult to help me.
As for the code, I believe I have some unnecessary redundancy. I'll bother my teacher tomorrow about where I declared some of my variables and, if and when, I should use Static variables, or Class. I'll check with him about some of my Return statements. Some I know I could have just used formula right after it rather than a variable. Needless to say, I'm trying to wrap my mind around variable declarations and the Return statement in regards to functions and procedures.
As always, thanks for any help or guidance.
Rich
Attachment 99773
This chapter is covering creating procuedures and functions. The assignment also said to include input validation; must order at least 1 spool; I've included inventory cannot be less than zero. The assignment also to use an inputbox to get current inventory in stock
I have everything working, almost. The problem is when I give bad data for current inventory or spools ordered, say -100, I get the error messagebox I create, but the program continues, and then gives bad output. I haven't figured how to prevent bad output. Something like
Code:
Function input()
Do
MessageBox.Show("supply must be 0 (zero) or greater", "Current Supply Input Error")
If intCurrentSupply >= 0 Then
Else
MessageBox.Show("supply must be 0 (zero) or greater", "Current Supply Input Error")
End If
Loop Unit Integer.TryParse(InputBox("How many spools are currently in stock?", "Current Spools in Stock"), intCurrentSupply) And intCurrentSupply >= 0
Another option I could do is create another function for output, and have it compared the output values. If they values are less zero, have it change the labels' text property to an empty string. But I don't like this option because I'm not really learning a way to force to user to give good input.
Here's the code the assignment. I realize some is probably not needed, but wasn't fully certain what could be removed without making more difficult to help me.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtNumberOfSpools.Focus()
End Sub
Function GetInStock()
'get current number of spools in stock
Dim intCurrentSupply As Integer
If Integer.TryParse(InputBox("How many spools are currently in stock?", "Current Spools in Stock"), intCurrentSupply) Then
If intCurrentSupply >= 0 Then
Else
MessageBox.Show("supply must be 0 (zero) or greater", "Current Supply Input Error")
End If
Else
MessageBox.Show("must enter a numerical value for current supply of spools", "Current Stock Input Error")
End If
Return intCurrentSupply
End Function
Function SpoolsOrdered()
'get order and validate input
Dim intSpoolsOrdered As Integer
If Integer.TryParse(txtNumberOfSpools.Text, intSpoolsOrdered) Then
If intSpoolsOrdered > 0 Then
Else
MessageBox.Show("must have at least one order", "Order Input Error")
txtNumberOfSpools.Text = String.Empty
txtNumberOfSpools.Focus()
End If
Else
MessageBox.Show("must enter a numerical value for amount ordered", "Order Input Error")
txtNumberOfSpools.Text = String.Empty
txtNumberOfSpools.Focus()
End If
Return intSpoolsOrdered
End Function
Function ReadyToShip(ByVal intCurrentSupply As Integer, ByVal intSpoolsOrdered As Integer) As Integer
'determine how many spools ready to ship now
Dim intReadyToShip As Integer
If intCurrentSupply >= intSpoolsOrdered Then
intReadyToShip = intSpoolsOrdered
Else
intReadyToShip = intCurrentSupply
End If
Return intReadyToShip
End Function
Function BackOrdered(ByVal intCurrentSupply As Integer, ByVal intSpoolsOrdered As Integer) As Integer
'determine how many, if any, spools to place on backorder
Dim intBackOrdered As Integer
If intCurrentSupply <= intSpoolsOrdered Then
intBackOrdered = (intSpoolsOrdered - intCurrentSupply)
Else
intBackOrdered = 0
End If
Return intBackOrdered
End Function
Function ShippingRate()
'determine shipping rate
Const decREGULAR_SHIPPING As Decimal = 10
Const decRUSH_SHIPPING As Decimal = 15
Dim intShippingRate As Integer
If chkRush.Checked = True Then
intShippingRate = decRUSH_SHIPPING
Else
intShippingRate = decREGULAR_SHIPPING
End If
Return intShippingRate
End Function
Function ShippingCharges(ByVal intReadyToShip As Integer, ByVal intShippingRate As Integer) As Integer
'calculate shipping chargers
Dim decShippingCharges As Decimal
decShippingCharges = intReadyToShip * intShippingRate
Return decShippingCharges
End Function
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'needs to call GetInStock, ReadyToShip, BackOrdered, ShippingCharges
Const intSPOOL_COST As Integer = 100
Dim intReadyToShip As Integer
Dim intSpoolsOrdered As Integer
Dim intCurrentSupply As Integer
Dim intBackOrdered As Integer
Dim intShippingRate As Integer
Dim intShippingCharges As Integer
Dim intTotal As Integer
intCurrentSupply = GetInStock()
intSpoolsOrdered = SpoolsOrdered()
intReadyToShip = ReadyToShip(intCurrentSupply, intSpoolsOrdered)
intBackOrdered = BackOrdered(intCurrentSupply, intSpoolsOrdered)
intShippingRate = ShippingRate()
intShippingCharges = ShippingCharges(intReadyToShip, intShippingRate)
intTotal = (intReadyToShip * intSPOOL_COST) + intShippingCharges
lblReadyShip.Text = intReadyToShip.ToString()
lblBackOrder.Text = intBackOrdered.ToString()
lblShippingHandling.Text = intShippingCharges.ToString("c")
lblTotal.Text = intTotal.ToString("c")
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'needs to call ResetSpools and ResetDelivery
Call ResetDelivery()
Call ResetSpools()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Sub ResetSpools()
'clears the text box and check box
txtNumberOfSpools.Clear()
chkRush.Checked = False
End Sub
Sub ResetDelivery()
'clears the display delivery information labels
lblBackOrder.Text = String.Empty
lblReadyShip.Text = String.Empty
lblShippingHandling.Text = String.Empty
lblTotal.Text = String.Empty
End Sub
End Class
As always, thanks for any help or guidance.
Rich