Generally we are only working with tables to find the row we need. After we find that row, we can make that into an object which can have it's own methods and can be passed to other objects. The object is more complex than a row of data, but much easier to work with and less prone to bugs. It is also a smart object--it can contain methods.
I believe that business objects are the most important advance one can make in programming.
As I have said before, the main concepts used in OOP are granularity, polymorphism, encapsulation, and inheritance. I am using all of these here.
Inheritance: This class is inheriting from my TRecord class which inherits from my TData class.
Granularity: This class is broken down into many small parts.
Polymorphism: Methods New(), End(), Display() are used in many other objects.
Encapsulation: This object looks up it's own data (and saves it too) in the customer table. No PUBLICS or PRIVATES are used.
To instanitate this object all you need one line of code and the CustNo.
oCustomer:= TCustomer():New(cCustNo)
That's it!
Now you can query the object for any info it contains (which can include complex calculations and reports).
Everything is encapsulated so you don't have to worry about workareas, aliases, or variable name conflicts.
You can also do a series of field updates with one call:
oCustomer:applyPayment( nAmount, date() )
This updates three fields and saves the changes. Simple.
Keep in mind that this is an incomplete class. I am still working on it.
So, look at the code and post your questions and/or comments.
James
Code: Select all
/*
Purpose : Customer Class
Author : James Bott, jbott@compuserve.com
Date : 06/27/2017 09:47:36 AM
Company : Intellitech
Copyright: Copyright © 2017 Intellitech. All rights reserved.
Language : Fivewin/xHarbour
Updated :
Notes :
*/
#include "fivewin.ch"
Function Main()
Local oCustomer, oCustomers
Local cCustNo
Field custno, company
REQUEST DBFCDX
rddsetdefault( "DBFCDX" )
set deleted on
ferase("arcust.cdx")
// Just for testing
// Must use TAG clause with CDXs
use arcust exclusive
index on CUSTNO tag "CUSTNO" to arcust
index on upper(COMPANY) tag "COMPANY" to arcust
use
// Instantiate the object
cCustNo:="10007"
oCustomer:= TCustomer():new(cCustNo)
oCustomer:display()
/* Testing
msgInfo( oCustomer:Company,"oCustomer:Company")
msgInfo( oCustomer:getBalance(), "Customer Balance" )
msgInfo( oCustomer:paymentStatus(), "Payment Status" )
msgInfo( oCustomer:PaymentTerms(), "Payment Terms" )
msgInfo( oCustomer:YearToDateSales(),"Year To Date Sales" )
*/
oCustomer:end()
Return nil
//--- Customer class
CLASS TCustomer from TRecord
METHOD New( cCustNo )
METHOD ApplyPayment( nAmount, dDate)
Method Display()
METHOD End()
Method GetBalance()
Method PaymentStatus()
Method PaymentTerms()
Method PastDueAmount()
Method PastDueDays()
Method PTDSales() inline ::ptdsls
Method YearToDateSales() inline ::ytdsls
Method SalesHistory()
ENDCLASS
METHOD New( cCustNo ) CLASS TCustomer
::oTable:= TCustomers():New()
::oTable:setOrder(1)
::oTable:seek( cCustNo )
::Load()
RETURN Self
METHOD End() Class TCustomer
::oTable:End()
Return nil
METHOD ApplyPayment( nAmount, dDate ) CLASS TCustomer
default dDate:= date()
::balance:= ::balance - nAmount
::lastPay:= dDate
::lpymt := nAmount
::save()
RETURN nil
Method GetBalance() Class TCustomer
Return ::balance
Method PaymentStatus() Class TCustomer
Return "Paid Up"
Method PaymentTerms() Class TCustomer
Return "Net "+alltrim(str(::PDays))+" days"
Method PastDueAmount() Class TCustomer
Return 0
Method SalesHistory() Class TCustomer
//Local oSalesHistory:= TSalesHistory():New(::custNo)
//oSalesHistory:Display()
//oSalesHistory:end()
Return nil
Method PastDueDays() Class TCustomer
Return 0
Method Display() Class TCustomer
Local cString:=""
cString := cString + "Customer No: "+ ::custno + CRLF
cString := cString + "Payment Status: "+ ::paymentStatus + CRLF
cString := cString + "Payment Terms: "+ ::paymentTerms() + CRLF
cString := cString + "Balance: " + TRANSFORM(::GetBalance(), "$999,999.99") + CRLF
cString := cString + "Year-To-Date Sales: " + transform(::yeartodateSales(), "$999,999.99") + CRLF
msgInfo(cString, ::Company)
Return nil
//---------------------------------------------------------------------------//
// Customer table class
Class TCustomers From TData
Method New()
Method End() inline ::close()
Endclass
Method New() Class TCustomers
::super:new(,"arcust")
::use()
::setOrder(1)
::gotop()
Return self
// EOF