Guest User

Untitled

a guest
Feb 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1.  
  2. Public Class frmSki
  3. Dim ItemNames(3), ItemCosts(3) As String ' Declaring Arrays
  4. Dim Total, TotalIndex As Integer ' Necessary global variables
  5. Dim Discount As Decimal
  6.  
  7. Private Sub frmSki_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8. Dim Count As Integer
  9. Dim ColsFormat As String = "{0, -12}{1, -15}{2, -15}{3, -15}" ' Spacings for list box
  10. LoadItems(True) ' Runs LoadItems, Creating/writing to file if not found.
  11. For Count = 0 To 3 ' Loop, adding read items to the combo box
  12. cboItems.Items.Add(ItemNames(Count))
  13. Next
  14. cboItems.SelectedIndex = 0 ' Select the first index (prevents blank combo box at startup)
  15. lstItems.Items.Add(String.Format(ColsFormat, "Quantity", "Item Name", "Cost per unit", "Total Cost")) ' Adds headers to list box
  16. TotalIndex = 0 ' Sets global variable TotalIndex.
  17. nudWeeks.Value = 1
  18. nudWeeks.Minimum = 1 ' already set on form, just to prove there's no need for negative validation.
  19. End Sub
  20.  
  21. Private Sub LoadItems(ByVal Create As Boolean) ' Create as in Create/Write file.
  22. Dim Count As Integer
  23.  
  24. Dim FilePathN As String = Application.StartupPath + "\ItemNames.ben" ' file path (app.path + filename)
  25. Dim FilePathC As String = Application.StartupPath + "\ItemCosts.ben"
  26.  
  27. If System.IO.File.Exists(FilePathN) And System.IO.File.Exists(FilePathC) Then ' Validation, if both files exist.
  28. Dim ItemCostR As New System.IO.StreamReader(FilePathC, True) ' Loading StreamReaders
  29. Dim ItemListR As New System.IO.StreamReader(FilePathN, True)
  30. For Count = 0 To 3 ' loops to fill array
  31. ItemNames(Count) = ItemListR.ReadLine ' Reads appropriate line of text file, filling array.
  32. ItemCosts(Count) = ItemCostR.ReadLine
  33. Next Count
  34. ItemCostR.Close() ' Closes StreamReaders, allowing files to be used by other programs.
  35. ItemListR.Close()
  36. ElseIf Create = True Then ' If files aren't found
  37. ItemNames(0) = "Jacket" 'Fills arrays
  38. ItemNames(1) = "Helmet"
  39. ItemNames(2) = "Salopettes"
  40. ItemNames(3) = "Goggles"
  41.  
  42. ItemCosts(0) = "14.0"
  43. ItemCosts(1) = "14.0"
  44. ItemCosts(2) = "12.0"
  45. ItemCosts(3) = "8.0"
  46.  
  47. System.IO.File.WriteAllLines(FilePathN, ItemNames) ' Creates both files, writing arrays to them.
  48. System.IO.File.WriteAllLines(FilePathC, ItemCosts)
  49. End If
  50. End Sub
  51.  
  52. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  53. Dim ColsFormat As String = "{0, -15}{1, -20}{2, -17}{3, -15}" ' Listbox spacing
  54. Dim ItemName As String ' Necessary local variables
  55. Dim UnitPrice, Quantity, TotalItemPrice, Count As Integer
  56. ItemName = cboItems.Text ' Setting variables to appropriate data
  57. Quantity = nudItemNo.Value
  58. If Quantity > 0 Then ' Validation, preventing "0" or negative values (despite the nud minimum being 0.)
  59. For Count = 0 To 3 ' Loops a case, checking the array and setting the unit price.
  60. Select Case ItemName
  61. Case ItemNames(Count)
  62. UnitPrice = ItemCosts(Count) ' Unit price for a length of time.
  63. End Select
  64. Next
  65. TotalItemPrice = UnitPrice * Quantity ' Sets the price for current addition
  66. Total = Total + TotalItemPrice ' Updates Total
  67. lstItems.Items.Add(String.Format(ColsFormat, Quantity, ItemName, FormatCurrency(UnitPrice), FormatCurrency(TotalItemPrice))) ' Adds data to listbox
  68. UpdateTotal() ' Runs UpdateTotal.
  69. TotalIndex = TotalIndex + 1 ' Updates TotalIndex global variable.
  70. Else
  71. Console.Beep()
  72. MsgBox("Please enter a quantity.")
  73. End If
  74. End Sub
  75.  
  76. Private Sub UpdateTotal()
  77. Dim Weeks As Integer
  78. Dim TotalW As Decimal
  79. Weeks = nudWeeks.Value
  80. Discount = 1 ' Sets base discount value.
  81. lblDiscount.Visible = True ' Sets both labels visible on first run (invisible prior for aesthetics).
  82. lblTotal.Visible = True
  83. If chkTrip.Checked Then ' Sets discount if necessary.
  84. Discount = 0.9
  85. End If
  86. TotalW = Total * Weeks
  87. lblTotal.Text = FormatCurrency(TotalW * Discount) ' Works out the total cost (inc. Discount)
  88. lblDiscount.Text = FormatCurrency(TotalW - (TotalW * Discount)) ' Works Out Discount.
  89. End Sub
  90.  
  91. Private Sub chkTrip_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTrip.CheckedChanged
  92. UpdateTotal() ' If the discount checkbox is changed, runs UpdateTotal.
  93. End Sub
  94.  
  95. Private Sub btnOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutput.Click
  96. Dim Count As Integer
  97. Dim List(TotalIndex), DiscountT, TotalT, File As String ' Declares string variables (inc. array)
  98. Dim FilePathO As String = Application.StartupPath + "\Output.ben" ' Sets the file path.
  99. If Not lblDiscount.Text = "" Then
  100. File = "Notepad " + FilePathO ' Sets a string for use in Shell()
  101. TotalT = lblTotal.Text ' Sets necessary variables.
  102. DiscountT = lblDiscount.Text
  103. For Count = 0 To TotalIndex ' Fills out the array with the necessary number of items.
  104. List(Count) = lstItems.Items(Count) ' Uses the listboxes data.
  105. Next
  106. System.IO.File.WriteAllLines(FilePathO, List) ' Writes to text file, overwriting previous data
  107. Dim TotalsO As New System.IO.StreamWriter(FilePathO, True) ' Opens StreamWriter
  108. TotalsO.WriteLine("Discount " + DiscountT, True) ' Writes Discount to file
  109. TotalsO.WriteLine("Total " + TotalT, True) ' Writes Total to file
  110. TotalsO.Close() ' Closes StreamWriter so file can be opened by other programs.
  111. Shell(File) ' Runs File in shell, opening Notepad, along with Output.ben
  112. Else
  113. MsgBox("Please add some items before output.")
  114. End If
  115. End Sub
  116. End Class
Add Comment
Please, Sign In to add comment