Programming Project OOP Confusion

Any guidance would be greatly appreciated.

Page 1 of 1

2 Replies - 1037 Views - Last Post: 02 November 2009 - 07:30 PM Rate Topic: -----

#1 DesertSky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 09

Programming Project OOP Confusion

Post icon  Posted 02 November 2009 - 02:54 PM

I am having a hard time with my project :crazy: . A description of it is here (Question 2).

I don't know where to begin with classes. I have a functioning program, but it isn't working as designed. Changes and updates are being written directly to the text files, so the listbox doesn't display current information unless it is refreshed in some way. The instructions say that this program should commit files once a "Save accounts to File" button is clicked. Obviously, my program doesn't make use of the button. I was going through another project where changes were written to a list box, and committed to the files upon exit. However the text files were in LSV format, and the program used an array of structures instead of classes. I could really use some help building and incorporating the classes into my program. Here it is in its current state:

 
Public Class Accounts

  'Class-level named constants and variables
  'Data file that stores information
  Const FILE As String = "CSVCHECKING.TXT"
  'Variables used for each entry
  Dim curBal As Double	  'Current balance in account
  Dim transDate, transName As String
  Dim transAmount As Double
  Dim transType As Boolean

  Private Sub InitializeData()
	'Reads or creates the file and sets the class-level variables
	Dim sr As IO.StreamReader

	'If text file exists, then read it.
	If IO.File.Exists(FILE) Then
	  Dim data() As String   'Holds the data read from the file
	  sr = IO.File.OpenText(FILE)
	  'Split the first line of the text file using the separator
	  'Recover name to appear on checks, current balance, number of 
	  'last check written and number of last deposit slip processed
	  Do While (sr.Peek <> -1)
		transDate = CStr(data(0))
		transName = CStr(data(1))
		transAmount = CDbl(data(2))
		transType = CBool(data(3))
	  Loop
	  
	  sr.Close()
	Else
	End If

  End Sub

  Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
	Dim deposit() As String = {txtDate.Text, "Deposit", txtDeposit.Text, "True"}
	Dim strDeposit As String
	Dim sw As IO.StreamWriter = IO.File.AppendText("CSVCHECKING.TXT") 'create streamwriter to append entries to CSV file
	'Make sure there is an entry in each textbox before performing WriteLine
	If (txtDeposit.Text <> "") Then
	  strDeposit = Join(deposit, ",") 'join array elements into string separated by commas
	  sw.WriteLine(strDeposit) 'write the string to the CSV file
	  sw.Close() 'close the CSV file
	  MsgBox("The deposit has been added.")
	Else
	  MsgBox("You must enter a deposit amount.") 'Notify that an entry is missing
	End If
	'Clear all textboxes
	txtDeposit.Clear()
	'Call function to refresh lstTransactions

  End Sub

  Private Sub btnWithdrawal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdrawal.Click
	Dim withdrawal() As String = {txtDate.Text, "Withdrawal", txtWithdrawal.Text, "False"}
	Dim strWithdrawal As String
	Dim sw As IO.StreamWriter = IO.File.AppendText("CSVCHECKING.TXT") 'create streamwriter to append entries to CSV file
	'Make sure there is an entry in each textbox before performing WriteLine
	If (txtWithdrawal.Text <> "") Then
	  strWithdrawal = Join(withdrawal, ",") 'join array elements into string separated by commas
	  sw.WriteLine(strWithdrawal) 'write the string to the CSV file
	  sw.Close() 'close the CSV file
	  MsgBox("The withdrawal has been recorded.")
	Else
	  MsgBox("You must enter an amount to withdrawal.") 'Notify that an entry is missing
	End If
	'Clear all textboxes
	txtWithdrawal.Clear()
	'Call function to refresh lstTransactions
  End Sub

  Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
	'Example format: 1/15/2008,Check cashed by Electric Co.,75,False
	Dim check() As String = {txtDate.Text, ("Check cashed by " & txtCheckTo.Text), txtCheckAmt.Text, "False"}
	Dim strcheck As String
	Dim sw As IO.StreamWriter = IO.File.AppendText("CSVCHECKING.TXT") 'create streamwriter to append entries to CSV file
	'Make sure there is an entry in each textbox before performing WriteLine
	If (txtCheckAmt.Text <> "") And (txtCheckTo.Text <> "") Then
	  strcheck = Join(check, ",") 'join array elements into string separated by commas
	  sw.WriteLine(strcheck) 'write the string to the CSV file
	  sw.Close() 'close the CSV file
	  MsgBox("The check amount has been applied.")
	Else
	  MsgBox("You must complete the payment amount and pay to the order of fields.") 'Notify that an entry is missing
	End If
	'Clear all textboxes
	txtCheckAmt.Clear()
	txtCheckTo.Clear()
	'Call function to refresh lstTransactions
  End Sub

  Private Sub frmAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	'1) ON LOAD, DISPLAY NAMES IN LISTBOX
	txtDate.Text = CStr(Date.Today)
  End Sub

  Private Sub cbAccount_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAccount.SelectedIndexChanged
	Dim addbalance, subbalance As Double
	Dim prevbal As Double
	Dim newbal As Double
	Dim fmtStr As String = "{0,10}{1,8}{2,-50}{3,15}{4,20}{5,15}{6,15}{7,20}"
	Dim TransList() As String

	If cbAccount.Text = "Checking" Then 'If account selected = checking, show Check group box and controls
	  lstTransactions.Items.Clear()
	  gbCheck.Show()
	  Dim sr As IO.StreamReader = IO.File.OpenText("CSVCHECKING.TXT") 'open text file to read
	  Do While (sr.Peek <> -1)
		'Fields are separated by a separator on one line for each entry
		TransList = sr.ReadLine.Split(","c)

		If CBool(TransList(3)) = True Then
		  addbalance += CDbl(TransList(2))
		ElseIf CBool(TransList(3)) = False Then
		  subbalance -= CDbl(TransList(2))
		End If
		newbal = addbalance + subbalance

		If CBool(TransList(3)) = True Then
		  prevbal = newbal - CDbl(TransList(2))
		ElseIf CBool(TransList(3)) = False Then
		  prevbal = newbal + CDbl(TransList(2))
		End If
		lstTransactions.Items.Add(String.Format(fmtStr, TransList(0), " ", TransList(1), FormatCurrency(TransList(2)), "PREV BAL", FormatCurrency(prevbal), "New BAL", FormatCurrency(newbal)))
		txtBalance.Text = FormatCurrency(CStr(newbal))

	  Loop 'loop through entire text file to fill the array
	  sr.Close() 'close streamreader

	Else 'Else (Savings is selected), hide Check group box and controls
	  gbCheck.Hide()
	  lstTransactions.Items.Clear()
	  Dim sr As IO.StreamReader = IO.File.OpenText("CSVSAVINGS.TXT") 'open text file to read
	  Do While (sr.Peek <> -1)
		'Fields are separated by a separator on one line for each entry
		TransList = sr.ReadLine.Split(","c)

		If CBool(TransList(3)) = True Then
		  addbalance += CDbl(TransList(2))
		ElseIf CBool(TransList(3)) = False Then
		  subbalance -= CDbl(TransList(2))
		End If
		newbal = addbalance + subbalance

		If CBool(TransList(3)) = True Then
		  prevbal = newbal - CDbl(TransList(2))
		ElseIf CBool(TransList(3)) = False Then
		  prevbal = newbal + CDbl(TransList(2))
		End If
		lstTransactions.Items.Add(String.Format(fmtStr, TransList(0), " ", TransList(1), FormatCurrency(TransList(2)), "PREV BAL", FormatCurrency(prevbal), "New BAL", FormatCurrency(newbal)))
		txtBalance.Text = FormatCurrency(CStr(newbal))

	  Loop 'loop through entire text file to fill the array
	  sr.Close() 'close streamreader
	End If
  End Sub

  Private Sub cbTransferFrom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTransferFrom.SelectedIndexChanged
	If cbTransferFrom.Text = "Checking" Then 'if Checking is selected as transfer account
	  lblTransferTo.Text = "to Savings" 'change TRANSFER TO label to Savings
	ElseIf cbTransferFrom.Text = "Savings" Then 'if Savings is selected as transfer account
	  lblTransferTo.Text = "to Checking" 'change TRANSFER TO label to Checking
	Else

	End If
  End Sub

 

  Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransfer.Click
	If cbTransferFrom.Text = "Checking" And CDbl(txtBalance.Text) >= CDbl(txtTransferTo.Text) Then
	  Dim withdrawal() As String = {txtDate.Text, "Withdrawal", txtTransferTo.Text, "False"}
	  Dim strWithdrawal As String
	  Dim deposit() As String = {txtDate.Text, "Transfer from Checking", txtTransferTo.Text, "True"}
	  Dim strDeposit As String
	  Dim sw1 As IO.StreamWriter = IO.File.AppendText("CSVSAVINGS.TXT") 'create streamwriter to append entries to CSV file
	  Dim sw2 As IO.StreamWriter = IO.File.AppendText("CSVCHECKING.TXT") 'create streamwriter to append entries to CSV file
	  'Make sure there is an entry in each textbox before performing WriteLine
	  If (txtTransferTo.Text <> "") And (cbTransferFrom.Text <> "Checking") Then
		MsgBox("You must specify an account and amount to transfer.")
	  Else
		strDeposit = Join(deposit, ",") 'join array elements into string separated by commas
		sw1.WriteLine(strDeposit) 'write the string to the CSV file
		strWithdrawal = Join(withdrawal, ",") 'join array elements into string separated by commas
		sw2.WriteLine(strWithdrawal) 'write the string to the CSV file
		sw1.Close() 'close the CSV file
		sw2.Close() 'close the CSV file
		MsgBox("The transfer has been recorded.")
	  End If
	  'Clear all textboxes
	  txtTransferTo.Clear()
	  'Call function to refresh lstTransactions

	ElseIf cbTransferFrom.Text = "Savings" And CDbl(txtBalance.Text) >= CDbl(txtTransferTo.Text) Then
	  Dim withdrawal() As String = {txtDate.Text, "Withdrawal", txtTransferTo.Text, "False"}
	  Dim strWithdrawal As String
	  Dim deposit() As String = {txtDate.Text, "Transfer from Savings", txtTransferTo.Text, "True"}
	  Dim strDeposit As String
	  Dim sw1 As IO.StreamWriter = IO.File.AppendText("CSVCHECKING.TXT") 'create streamwriter to append entries to CSV file
	  Dim sw2 As IO.StreamWriter = IO.File.AppendText("CSVSAVINGS.TXT") 'create streamwriter to append entries to CSV file
	  'Make sure there is an entry in each textbox before performing WriteLine
	  If (txtTransferTo.Text <> "") And (cbTransferFrom.Text <> "Savings") Then
		MsgBox("You must specify an account and amount to transfer.")
	  Else
		strDeposit = Join(deposit, ",") 'join array elements into string separated by commas
		sw1.WriteLine(strDeposit) 'write the string to the CSV file
		strWithdrawal = Join(withdrawal, ",") 'join array elements into string separated by commas
		sw2.WriteLine(strWithdrawal) 'write the string to the CSV file
		sw1.Close() 'close the CSV file
		sw2.Close() 'close the CSV file
		MsgBox("The transfer has been recorded.")
	  End If
	  'Clear all textboxes
	  txtTransferTo.Clear()
	  'Call function to refresh lstTransactions
	ElseIf cbTransferFrom.Text = "Checking" And (txtBalance.Text < txtTransferTo.Text) Then
	  MsgBox("There are insufficient funds to complete the transfer.")
	ElseIf cbTransferFrom.Text = "Savings" And (txtBalance.Text < txtTransferTo.Text) Then
	  MsgBox("There are insufficient funds to complete the transfer.")
	End If
  End Sub
End Class




The entries in the CSV files look like this:
1/2/2008,Deposit,1000,True
1/7/2008,Transfer from Savings,200,True
1/15/2008,Check cashed by Electric Co.,75,False
1/26/2008,Withdrawal,50,False
2/5/2008,Deposit,37.5,True

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Programming Project OOP Confusion

#2 DesertSky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 09

Re: Programming Project OOP Confusion

Posted 02 November 2009 - 03:01 PM

Sorry, URL I provided doesn't work. Here's one that should.
Was This Post Helpful? 0
  • +
  • -

#3 DesertSky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-November 09

Re: Programming Project OOP Confusion

Posted 02 November 2009 - 07:30 PM

Here's what I have started for construction of my classes based on the project instructions:
"The program should use two classes, Transaction and Account. The class Transaction should have properties for transaction name, amount, date, and whether it is a credit (deposit) or debit (withdrawal/check)."

"The class Account, which wil have both a checking account and a savings account as instances, should use an array of Transaction objects. In addition, it should have properties for name (Checking or Savings) and balance. It should have methods to carry out a transaction (if possible), to display the list of transactions, and to lead and retrieve the set of transactions into or from a sequential file. The events InsufficientFunds and TransactionCommitted should be triggered at appropriate times."

"HINT: In order for an Account object to display a list of transactions, a list box should be passed to a method as an argument. The method might be declared with:"
Sub EnumerateTransactions(ByVal lb As ListBox)


  'calls constructor
	Dim WithEvents Accounts As New Transaction()

	Class Account
		Dim Checking As New Transaction()
		Dim Savings As New Transaction()
		Private m_Name As String
		Private m_Balance As Double

		Public ReadOnly Property Name() As String
			Get
				Return m_Name
			End Get
		End Property

		Public ReadOnly Property Balance() As Double
			Get
				Return m_Balance
			End Get
		End Property

	End Class

	Class Transaction
		Private m_tDate As String
		Private m_tName As String
		Private m_tAmount As Double
		Private m_tValue As Boolean

		Public ReadOnly Property tDate() As String
			Get
				Return m_tDate
			End Get
		End Property

		Public ReadOnly Property tName() As String
			Get
				Return m_tName
			End Get
		End Property

		Public ReadOnly Property tAmount() As Double
			Get
				Return m_tAmount
			End Get
		End Property

		Public ReadOnly Property tValue() As Boolean
			Get
				Return m_tValue
			End Get
		End Property


		Public Sub New() 'default constructor

		End Sub


	End Class





I'm struggling with building these classes and adding my methods.
Any help would be appreciated. ~ Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1