Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,097 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,868 people online right now. Registration is fast and FREE... Join Now!




Seperating Business Logic From Design Form

 
Reply to this topicStart new topic

Seperating Business Logic From Design Form

Tastybrownies
6 Jun, 2008 - 01:38 PM
Post #1

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 27



Thanked: 1 times
My Contributions
Well, for the whole quarter of my first vb.net class I was on top of the world and breezing through it with not very much trouble. But I guess we fell behind and somehow didn't teach the concept of OOP until 4 days left in the quarter!?!?

So anyway, I am currently working on a project in my book labeled exercise 10B. It says that we must modify a previous form so that it now has a class. In addition with adding the class, the problem wants us to switch the business logic code of the form over to the class. The outcome of the problem doesn't change, just that the calculations for the problem are now in the class instead of the form.

I've been trying this for awhile now and still can't get it and I'm confused becuase I probably missedsomekind of concept because it wasn't explained that well. But now I have finals and I need to get this done because I'm all stressed out. It would be greatly appreciated if some of you guys could point me in the right direction.

The exercise consists of of the following input boxes for the user to fill in.
THE INPUT BOXES
1.salesPersonNameTextBox -it doesn't really play a part in the math.
2.weeklySalesTextBox - this is the textbox in which the user enters their weekly sales.

THE OUTPUT BOXES
1.richTotalPayTextBox - this text box takes the users weekly sales entered and does the equation I'm going to talk about below.
2.commTextBox - This displays the users commission after calculation.

These are the constants.
Const QUOTA_Decimal As Decimal = 1000D
Const COMMISSION_RATE_DecimalAsDecimal=0.15D
Const BASE_PAY_Decimal As Decimal = 250D

If the user makes puts anything >= 1000 in the weeklySalesTextBox its supposed to do the following,
richTotalTextBox = weeklySalesDecimal(the one the user entered) * COMMISSION_RATE_Decimal) + BASE_PAY_Decimal.

If the user enters anything in the weeklySalesTextBox < 1000 then all they get is the base pay and no added commission.

So an example would look like this.
salesPersonNameTextBox: joe joe
weeklySalesTextBox:1000

richTotalTextBox:400
commTextBox:150

Then there's a summary, but that's not important right now.

If I could do it in the form it would be no problem but I'm just really confused. I will first post my code to the form and then the class.



CODE
Public Class salesForm
    Private aSalesPerson As SalesPerson
    Public weeklySalesOneDecimal As Decimal
    Public weeklySalesTwoDecimal As Decimal
    Public specialCommDecimal As Decimal
    'Declare module level variables for summary infor.
    Dim sumTotalPayDecimal, sumTotalDecimal, sumTotalCommDecimal As Decimal
    Dim salesCountInteger As Integer

    'Declare constants.
    Const QUOTA_Decimal As Decimal = 1000D
    Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    Const BASE_PAY_Decimal As Decimal = 250D


    Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click

        'Calculate the total sales, total commission, and total pay.
        Dim messageString As String
        'confirming the clear by the user'


        With Me
            .commTextBox.Clear()
            .weeklySalesTextBox.Clear()
            .richTotalPayTextBox.Clear()
            With .salesPersonNameTextBox
                .Clear()
                .Focus()



                'Calculate summary.
                If weeklySalesTextBox.Text > 0 Then
                    messageString = "Weekly Sales: " _
                        & sumTotalDecimal.ToString("C2") _
                        & ControlChars.NewLine & ControlChars.NewLine _
                        & "Total Commissions" & sumTotalCommDecimal.ToString("C2") _
                        & ControlChars.NewLine & ControlChars.NewLine _
                        & "Total Pay" & sumTotalPayDecimal.ToString("C2")
                    MessageBox.Show(messageString, "Total Sales Summary", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    messageString = "No data to summarize."
                    MessageBox.Show(messageString, "Error", _
                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End With
        End With




    End Sub


    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Closing the program.

        Me.Close()
    End Sub

    Private Sub CalculatePayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculatePayToolStripMenuItem.Click
        'Calculate the current amounts and add to the totals.

        With Me
            Try


                If weeklySalesTextBox.Text >= 1000 Then


                    'Istantiate the SalesPerson object and set the properties.
                    aSalesPerson = New SalesPerson(.salesPersonNameTextBox.Text, _
                    Decimal.Parse(.weeklySalesTextBox.Text))
                    'Calculate and format the results.
                    .richTotalPayTextBox.Text = aSalesPerson.WeeklySalesOne.ToString("N2")
                    .commTextBox.Text = aSalesPerson.SpecialComm.ToString("N2")
                End If
                If weeklySalesTextBox.Text < 1000 Then

                    aSalesPerson = New SalesPerson(.salesPersonNameTextBox.Text, _
                    Decimal.Parse(.weeklySalesTextBox.Text))
                    'Calculate and format the results.
                    .richTotalPayTextBox.Text = aSalesPerson.WeeklySalesTwo.ToString("N2")
                    With .commTextBox.Text = " "
                    End With
                End If
            Catch quantityExcpetion As FormatException
                MessageBox.Show("You must enter a quantity.", "Data entry error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
                With Me
                    .salesPersonNameTextBox.Clear()
                    .richTotalPayTextBox.Clear()
                    With .weeklySalesTextBox
                        .Clear()
                        .Focus()

                    End With
                End With
            End Try
        End With
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        'Clear the appropriate controls and display the Yes.NO dialog box.

        With Me
            .commTextBox.Clear()
            .weeklySalesTextBox.Clear()
            .richTotalPayTextBox.Clear()
            With .salesPersonNameTextBox
                .Clear()
                .Focus()
            End With
        End With
    End Sub


    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        'Allow the user to change the fonts.

        With Me.FontDialog1
            .Font = Me.richTotalPayTextBox.Font
            .ShowDialog()
            Me.richTotalPayTextBox.Font = .Font

        End With


    End Sub

    Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
        'Allow the user to change the colors.

        With Me.ColorDialog1
            .Color = Me.richTotalPayTextBox.ForeColor
            .ShowDialog()
            Me.richTotalPayTextBox.ForeColor = .Color
        End With
    End Sub


    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        'Display the about messageBox.
        Dim messageString As String

        messageString = "Sales Form" & ControlChars.NewLine _
        & ControlChars.NewLine & "Programmed by joe joe"

        MessageBox.Show(messageString, "About Sales Form", MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
    End Sub
End Class


Now here's my class names SalesPerson.
CODE
Public Class SalesPerson
    Const QUOTA_Decimal As Decimal = 1000D
    Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    Const BASE_PAY_Decimal As Decimal = 250D
    Private salesString As String
    Private priceTotalDecimal, priceTotalLesserDecimal, specialCommDecimal As Decimal
    Private Shared sumTotalDecimal, sumTotalPayDecimal, sumTotalCommDecimal, _
    totalSalesDecimal, commDecimal As Decimal
    Public weeklySalesOneDecimal, weeklySalesTwoDecimal As Decimal


    'Parameterized constructor.
    Sub New(ByVal Name As String, ByVal WeeklySalesInput As Decimal)
        'Assign the property values.

        With Me
            .Sales = Sales
            .WeeklySalesOne = WeeklySalesOne
            .WeeklySalesTwo = WeeklySalesTwo
            .SpecialComm = SpecialComm
        End With
    End Sub

    Property Sales() As String
        Get
            Return salesString
        End Get
        Set(ByVal value As String)
            salesString = value
        End Set
    End Property

    Property WeeklySalesOne() As Decimal
        Get
            Return weeklySalesOneDecimal
        End Get
        Set(ByVal value As Decimal)
            If value >= 1000 Then
                weeklySalesOneDecimal = specialCommDecimal + 250
            End If

        End Set
    End Property

    Property SpecialComm() As Decimal
        Get
            Return specialCommDecimal
        End Get
        Set(ByVal value As Decimal)
            If value >= 1000 Then
                specialCommDecimal = weeklySalesOneDecimal * 0.15D
            End If


        End Set
    End Property
    Property WeeklySalesTwo() As Decimal
        Get
            Return weeklySalesTwoDecimal
        End Get
        Set(ByVal value As Decimal)
            If value < 1000 Then
                weeklySalesTwoDecimal = BASE_PAY_Decimal
            End If

        End Set
    End Property
    
End Class


It's probably really easy and once I see it I'll think what took so long, but right now I'm in a world of hurt. wink2.gif


User is offlineProfile CardPM
+Quote Post

Tastybrownies
RE: Seperating Business Logic From Design Form
6 Jun, 2008 - 06:21 PM
Post #2

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 27



Thanked: 1 times
My Contributions
Well, I finally figured out what I was doing wrong and yup it was something simple.

The first part of the constructor of the class was not exactly the same as the textbox inputs. So because of that when the solution ran it didn't even get past the first salesperson textbox.

I love that feeling where you finally get it.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seperating Business Logic From Design Form
7 Jun, 2008 - 07:42 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Congratulations on finding the solution to your problem and thanks for sharing.
User is offlineProfile CardPM
+Quote Post

Tastybrownies
RE: Seperating Business Logic From Design Form
7 Jun, 2008 - 09:46 AM
Post #4

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 27



Thanked: 1 times
My Contributions
Thank you, now I'm having trouble with the summary for some reason. The only thing that doesn't make sense is when I declare something like CalculateCount() then give it a property and a procedure step it still says its undeclared!

I've done other Calculate steps for other variables and they turned out great. Maybe someone can find something because if there is something I can't see it with my own eyes.

This is the class where I was starting to make the variables and procedures for the summary and concatenate it with the regular form.

CODE
Public Class SalesPerson
    Const QUOTA_Decimal As Decimal = 1000D
    Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    Const BASE_PAY_Decimal As Decimal = 250D
    Private salesString As String
    Private weeklySalesLessDecimal As Decimal
    Private Shared weeklySalesDecimal, sumTotalCommDecimal, _
    totalSalesDecimal, commDecimal, sumTotalPayDecimal, totalPayDecimal As Decimal
    Private weeklyDecimal As Decimal
    Private countInteger As Integer





    'Parameterized constructor.
    Sub New(ByVal Sales As String, ByVal Weekly As Decimal)
        'Assign the property values.

        With Me
            .Sales = Sales
            .Weekly = Weekly
            CalculateComm()
            CalculateWeeklySalesLess()
            CalculateWeekly()
            CalculateSumTotalComm()
            CalculateCount()
        End With
    End Sub

    Property Sales() As String
        Get
            Return salesString
        End Get
        Set(ByVal value As String)
            salesString = value
        End Set
    End Property

    Property Count() As Integer
        Get
            Return countInteger

        End Get
        Set(ByVal value As Integer)
            If value >= 0 Then
                countInteger = value

            End If


        End Set
    End Property
    Property SumTotalComm() As Decimal
        Get
            Return sumTotalCommDecimal
        End Get
        Set(ByVal value As Decimal)
            sumTotalCommDecimal = value

        End Set
    End Property

    Property Weekly() As Decimal
        Get
            Return weeklyDecimal
        End Get
        Set(ByVal value As Decimal)
            If value >= 0 Then
                weeklyDecimal = value
            End If

        End Set
    End Property

    Property WeeklySalesLess() As Decimal
        Get
            Return weeklySalesLessDecimal
        End Get
        Set(ByVal value As Decimal)
            If weeklySalesDecimal < 1000 Then
                CalculateWeeklySalesLess()
            End If

        End Set
    End Property
    Property Comm() As Decimal
        Get
            Return commDecimal
        End Get
        Set(ByVal value As Decimal)
            commDecimal = value
        End Set
    End Property

    Protected Overridable Sub CalculateComm()
        'please calculate commission.
        commDecimal = weeklyDecimal * COMMISSION_RATE_Decimal
    End Sub
    Protected Overridable Sub CalculateWeeklySalesLess()
        weeklySalesLessDecimal = BASE_PAY_Decimal
    End Sub

    Protected Overridable Sub CalculateWeekly()
        weeklyDecimal = (weeklyDecimal * COMMISSION_RATE_Decimal) + 250

    End Sub

    Protected Overridable Sub CalculateSumCounterInteger()
        sumCounterInteger += 1
    End Sub

    Protected Overridable Sub CalculateSumTotalComm()
        sumTotalCommDecimal += commDecimal
    End Sub
    Protected Overridable Sub CalculateSumTotalPayDecimal()
        sumTotalPayDecimal += totalSalesDecimal


    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seperating Business Logic From Design Form
7 Jun, 2008 - 12:38 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Well currently I see where you are trying to call the method in the With statement. But I do not see the method definition declared anywhere in your class.

Can you post the code for the CalculateCount() method?
User is offlineProfile CardPM
+Quote Post

Tastybrownies
RE: Seperating Business Logic From Design Form
8 Jun, 2008 - 10:54 AM
Post #6

New D.I.C Head
*

Joined: 13 May, 2008
Posts: 27



Thanked: 1 times
My Contributions
Finally got everything done, whewwwwww and it's just done in time for tomorrow's deadline.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:19AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month