Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class frmSki
- Dim ItemNames(3), ItemCosts(3) As String ' Declaring Arrays
- Dim Total, TotalIndex As Integer ' Necessary global variables
- Dim Discount As Decimal
- Private Sub frmSki_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Dim Count As Integer
- Dim ColsFormat As String = "{0, -12}{1, -15}{2, -15}{3, -15}" ' Spacings for list box
- LoadItems(True) ' Runs LoadItems, Creating/writing to file if not found.
- For Count = 0 To 3 ' Loop, adding read items to the combo box
- cboItems.Items.Add(ItemNames(Count))
- Next
- cboItems.SelectedIndex = 0 ' Select the first index (prevents blank combo box at startup)
- lstItems.Items.Add(String.Format(ColsFormat, "Quantity", "Item Name", "Cost per unit", "Total Cost")) ' Adds headers to list box
- TotalIndex = 0 ' Sets global variable TotalIndex.
- End Sub
- Private Sub LoadItems(ByVal Create As Boolean) ' Create as in Create/Write file.
- Dim Count As Integer
- Dim FilePathN As String = Application.StartupPath + "\ItemNames.ben" ' file path (app.path + filename)
- Dim FilePathC As String = Application.StartupPath + "\ItemCosts.ben"
- If System.IO.File.Exists(FilePathN) And System.IO.File.Exists(FilePathC) Then ' Validation, if both files exist.
- Dim ItemCostR As New System.IO.StreamReader(FilePathC, True) ' Loading StreamReaders
- Dim ItemListR As New System.IO.StreamReader(FilePathN, True)
- For Count = 0 To 3 ' loops to fill array
- ItemNames(Count) = ItemListR.ReadLine ' Reads appropriate line of text file, filling array.
- ItemCosts(Count) = ItemCostR.ReadLine
- Next Count
- ItemCostR.Close() ' Closes StreamReaders, allowing files to be used by other programs.
- ItemListR.Close()
- ElseIf Create = True Then ' If files aren't found
- ItemNames(0) = "Jacket" 'Fills arrays
- ItemNames(1) = "Helmet"
- ItemNames(2) = "Salopettes"
- ItemNames(3) = "Goggles"
- ItemCosts(0) = "14.0"
- ItemCosts(1) = "14.0"
- ItemCosts(2) = "12.0"
- ItemCosts(3) = "8.0"
- System.IO.File.WriteAllLines(FilePathN, ItemNames) ' Creates both files, writing arrays to them.
- System.IO.File.WriteAllLines(FilePathC, ItemCosts)
- End If
- End Sub
- Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
- Dim ColsFormat As String = "{0, -15}{1, -20}{2, -17}{3, -15}" ' Listbox spacing
- Dim ItemName As String ' Necessary local variables
- Dim UnitPrice, Quantity, TotalItemPrice, Count, Weeks As Integer
- ItemName = cboItems.Text ' Setting variables to appropriate data
- Quantity = nudItemNo.Value
- Weeks = nudWeeks.Value
- If Quantity > 0 Then ' Validation, preventing "0" or negative values (despite the nud minimum being 0.)
- If Weeks > 0 Then
- For Count = 0 To 3 ' Loops a case, checking the array and setting the unit price.
- Select Case ItemName
- Case ItemNames(Count)
- UnitPrice = ItemCosts(Count) * Weeks ' Unit price for a length of time.
- End Select
- Next
- TotalItemPrice = UnitPrice * Quantity ' Sets the price for current addition
- Total = Total + TotalItemPrice ' Updates Total
- lstItems.Items.Add(String.Format(ColsFormat, Quantity, ItemName, FormatCurrency(UnitPrice), FormatCurrency(TotalItemPrice))) ' Adds data to listbox
- UpdateTotal() ' Runs UpdateTotal.
- TotalIndex = TotalIndex + 1 ' Updates TotalIndex global variable.
- Else
- Console.Beep() ' Makes the machine beep, indicating an error.
- MsgBox("Please enter the length of time you would like to rent the items.")
- End If
- Else
- Console.Beep()
- MsgBox("Please enter a quantity.")
- End If
- End Sub
- Private Sub UpdateTotal()
- Discount = 1 ' Sets base discount value.
- lblDiscount.Visible = True ' Sets both labels visible on first run (invisible prior for aesthetics).
- lblTotal.Visible = True
- If chkTrip.Checked Then ' Sets discount if necessary.
- Discount = 0.9
- End If
- lblTotal.Text = FormatCurrency(Total * Discount) ' Works out the total cost (inc. Discount)
- lblDiscount.Text = FormatCurrency(Total - (Total * Discount)) ' Works Out Discount.
- End Sub
- Private Sub chkTrip_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTrip.CheckedChanged
- UpdateTotal() ' If the discount checkbox is changed, runs UpdateTotal.
- End Sub
- Private Sub btnOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutput.Click
- Dim Count As Integer
- Dim List(TotalIndex), DiscountT, TotalT, File As String ' Declares string variables (inc. array)
- Dim FilePathO As String = Application.StartupPath + "\Output.ben" ' Sets the file path.
- If Not lblDiscount.Text = "" Then
- File = "Notepad " + FilePathO ' Sets a string for use in Shell()
- TotalT = lblTotal.Text ' Sets necessary variables.
- DiscountT = lblDiscount.Text
- For Count = 0 To TotalIndex ' Fills out the array with the necessary number of items.
- List(Count) = lstItems.Items(Count) ' Uses the listboxes data.
- Next
- System.IO.File.WriteAllLines(FilePathO, List) ' Writes to text file, overwriting previous data
- Dim TotalsO As New System.IO.StreamWriter(FilePathO, True) ' Opens StreamWriter
- TotalsO.WriteLine("Discount " + DiscountT, True) ' Writes Discount to file
- TotalsO.WriteLine("Total " + TotalT, True) ' Writes Total to file
- TotalsO.Close() ' Closes StreamWriter so file can be opened by other programs.
- Shell(File) ' Runs File in shell, opening Notepad, along with Output.ben
- Else
- MsgBox("Please add some items before output.")
- End If
- End Sub
- End Class
Add Comment
Please, Sign In to add comment