Guest User

Untitled

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