Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2012 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4032788 to your computer and use it in GitHub Desktop.
Save anonymous/4032788 to your computer and use it in GitHub Desktop.
Create an application that manages cable account information. A form allows the entry of the following data: accountholder id (integer), first name(string), last name(string), debits (decimal), and credits (decimal). The form should have the buttons: Add (which adds a customer to the Cable.txt sequential file), Clear (which clears all text boxes for a new account entry), Balance (credits – debits), and Exit (which closes the form). When the Balance button is clicked, an independent function is called that calculates the balance from the given credits and debits of that user and returns the balance to be displayed in the balance label.
The sequential file must hold the following information:
a) Accountholder id
b) First name
c) Last name
d) Debits
e) Credits
f) balance
The accountholder id must be between 6000 and 22000 (inclusive). If it is not within this range, an error message should be displayed to the user via an error message label on the form and the Add button disabled until an accountholder id within range is entered.
The number of credits and debits must be greater than 0. If it is not within this range, an error message should be displayed to the user via an error message label on the form and the Add button disabled until credits/debits within this range is entered. If the calculated balance is less than 0, an error “Account In Debit” must appear in red in an error message on the appropriate tab.
On another form or in the same form (with additional buttons), allow the user to find an account via the accountholder id. If the account is not found, display an “Account Not Found” in a label beside the Search button. If the account is found, the account info is displayed in the accountholder id, first name, last name, credits, debits, and balance text boxes. The user is allowed to edit any of these fields, except accountholder id or balance. After they finish editing, the modified information for the account is saved, with an updated balance, in their record when the user clicks on the Save button. There is also a Clear and Close button whose functionalities are similar to the account entry form.
Menu options that mirror the actions of buttons, when clicked, should be added.
When the form closes, a dialog box must ask the user if they wish to save the current information to file. If the user chooses yes, the arrays are written to the text file. Otherwise, the form closes.
HERE'S THE CODE:
Imports system.io
Public Class MainForm
' declare arrays for files
Private IDArray() As Integer
Private FirstNameArray() As String
Private LastNamearray() As String
Private CreditsArray() As Decimal
Private DebitsArray() As Decimal
Private BalanceArray() As Decimal
Private ArrPlaceHolder As Integer = 0
Private intArrCnt As Integer = 0
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
WriteArrays()
End Sub
'load all neccessary information to form
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim fRead As New StreamReader(".\Cable.txt")
Dim str As String
' intiialise array counter
intArrCnt = 0
' while array elements need to be read, loop
While fRead.Peek() >= 0
' read line
' since line is string convert and put in appropriate array
str = fRead.ReadLine()
If Not str Is Nothing Then
' add one to size of array and resize all arrays and preserve previous contents
intArrCnt = intArrCnt + 1
ReDim Preserve IDArray(intArrCnt)
ReDim Preserve FirstNameArray(intArrCnt)
ReDim Preserve LastNamearray(intArrCnt)
ReDim Preserve CreditsArray(intArrCnt)
ReDim Preserve DebitsArray(intArrCnt)
ReDim Preserve BalanceArray(intArrCnt)
IDArray(intArrCnt - 1) = Convert.ToInt32(str)
str = fRead.ReadLine
FirstNameArray(intArrCnt - 1) = str
str = fRead.ReadLine
LastNamearray(intArrCnt - 1) = str
str = fRead.ReadLine
CreditsArray(intArrCnt - 1) = Convert.ToDecimal(str)
str = fRead.ReadLine
DebitsArray(intArrCnt - 1) = Convert.ToDecimal(str)
str = fRead.ReadLine
BalanceArray(intArrCnt - 1) = Convert.ToDecimal(str)
Else
Exit While
End If
End While
fRead.Close()
End Sub
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
UpdateAcct()
End Sub
Private Sub WriteArrays()
' go through array and write each element of array as separate line in order
Dim fWrite As New StreamWriter(".\cable.txt", False)
Dim i As Integer
For i = 0 To (intArrCnt - 1)
fWrite.WriteLine(IDArray(i).ToString)
fWrite.WriteLine(FirstNameArray(i))
fWrite.WriteLine(LastNamearray(i))
fWrite.WriteLine(CreditsArray(i).ToString)
fWrite.WriteLine(DebitsArray(i).ToString)
fWrite.WriteLine(BalanceArray(i).ToString)
Next
fWrite.Close()
End Sub
Private Sub UpdateAcct()
' call procedure to calculate new balance
' updates, from textboxes, values in specified element of all arays. Then calls a function to rewrite file
If ArrPlaceHolder >= 0 Then ' employee needs to be found first
Integer.TryParse(Me.txtUpdateID.Text, IDArray(ArrPlaceHolder))
FirstNameArray(ArrPlaceHolder) = Me.txtUpdateFirstName.Text
LastNamearray(ArrPlaceHolder) = Me.txtUpdateLastName.Text
Decimal.TryParse(Me.txtUpdateCredits.Text, CreditsArray(ArrPlaceHolder))
Decimal.TryParse(Me.txtUpdateDebits.Text, DebitsArray(ArrPlaceHolder))
Decimal.TryParse(Me.txtUpdateBal.Text, BalanceArray(ArrPlaceHolder))
WriteArrays()
End If
End Sub
Private Sub FindAcct()
Dim i As Integer
Dim intID As Integer
Dim intArrID As Integer
' set arrplaceholder to 0 indicating account not found
ArrPlaceHolder = -1
' get acct ID to find account
intID = Convert.ToInt32(Me.txtUpdateID.Text)
For i = 0 To (intArrCnt - 1)
intArrID = IDArray(i)
If intID = intArrID Then
ArrPlaceHolder = i ' indicate where in array (element number), acct was found
Me.txtUpdateFirstName.Text = FirstNameArray(i)
Me.txtUpdateLastName.Text = LastNamearray(i)
Me.txtupdatecredits.text = CreditsArray(i)
Me.txtupdatedebits.text = DebitsArray(i)
Me.txtUpdateBal.Text = BalanceArray(i).ToString
Exit For
End If
Next
End Sub
Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Click
FindAcct()
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
UpdateAcct()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment