Home > Domino Tips > Developer > Other > Tony Higham's Object-oriented LotusScript
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

OTHER

Tony Higham's Object-oriented LotusScript


Tony Higham
11.18.2001
Rating: -3.52- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Domino applications can be object-oriented in every sense of the word. This is an example of some truly object-oriented LotusScript code that will help you take your development skills to the next level.

In case you missed Tony Higham in our Live Expert Event on Tuesday, November 20th when he discussed how to use objects and LotusScript and how to use Domino and the UML (Unified Modeling Language) you can view the archived transcript here. Tony used the object-oriented LotusScript example featured in this tip.

Code

Public Class Cart
	
	'Define the itemList attribute as a List of Item objects.  The List enables us to
	'store Item objects using the ProductId as a key value.  Similar to a HashTable
	'in Java
	Private itemList List As Item
	
	'Here is the constructor method for this class.  This method is called when
	'a new instance of the Cart class is created.  Though it's not being used
	'here, I am including it for completeness
	Public Sub New()
		
	End Sub
	
	'This method adds an item to the shopping cart
	Public Sub add( pid As String )
		
		Dim cartItem As Item
		
		'See if the item is already on the shopping cart
		If Iselement( itemList( pid )) Then
			
			'It's already in the cart so increase it's quantity
			Set cartItem =  itemList( pid )
			
			' Increment the quantity of this item
			Call cartItem.increment()		
			
		Else
			
			'This particular product does not exist in the shopping cart
			'so create a new Item object (which in turn will create a 
			'Product object for the Product ID passed to the add method
			'and place it on the ItemList
			Set cartItem = New Item( pid )
			
			'Now add the new Item object to the itemList using the product ID
			'as a key value so that we can look it up later to increment or
			'decrement the quantity of this product that is in the cart
			Set itemList( pid ) = cartItem	
			
		End If		
		
	End Sub
	
	'This method removes an item from the shopping cart
	Public Sub remove( pid As String )
		
		Dim cartItem As Item
		
		'Check that the product is already in the shopping cart
		If Iselement( itemList( pid )) Then
			
			'It's already in the cart so increase it's quantity
			Set cartItem =  itemList( pid )
			
			'Increment the quantity of this item
			Call cartItem.decrement()		
			
			'If the quantity has reached zero remove the item from the cart
			If cartItem.getQty <= 0 Then
				Erase Me.itemList( pid )
			End If
			
			'This particular product does not exist in the shopping cart
			'so create a new Item object (which in turn will create a 
			'Product object for the Product ID passed to the add method
			'and place it on the ItemList
			Set cartItem = New Item( pid )
			
			'Now add the new Item object to the itemList using the product ID
			'as a key value so that we can look it up later to increment or
			'decrement the quantity of this product that is in the cart
			Set itemList( pid ) = cartItem	
			
		End If		
		
	End Sub
	
End Class

'The Item class serves as a container for a Product object with a count
'of how many of that product has been ordered.  From a memory usage
'perspective this object containment hierarchy makes a lot of sense
'as the user may order the same Product several times
Public Class Item
	
	'Define the properties provided by this class as private so that we can
	'control whether they are read-only or read-write using get/set methods 
	'for reading from then and writing to the respectively.  An Item object
	'is used to store Product objects along with the quantity of the Product
	'that the user desires	
	Private prod As Product
	Private qty As Integer
	
	'The constructor sub is used to initialize the contents of the Item object
	Public Sub New( pid As String )
		
		'Create a new Product object and store it in the internal property 
		Set Me.prod = New Product( pid )
		
		'Set the quantity to one
		Me.qty = 1
		
	End Sub
	
	'Methods for increasing and decreasing the quantity as more of this product
	'is added to the shopping cart (or removed from the cart)
	Public Sub increment()
		Me.qty = Me.qty + 1
	End Sub
	
	Public Sub decrement
		Me.qty = Me.qty - 1
	End Sub
	
	'The following two methods are used to access the value of the Product object
	'and quantity stored by the Item object
	Public Function getProduct() As Product
		Set getProduct = Me.prod
	End Function
	
	Public Function getQty() As Integer
		getQty = Me.qty
	End Function
	
End Class

'The Product object contains all the basic data about the Products in a
'product catalog.  It has private attributes to store the details about a 
'given product, and makes that data available through get methods so
'they it's read-only data.
Public Class Product
	
	'Define the properties provided by the class as private so that we can control
	'whether they are read-only or read-write using get/set methods for reading
	'and writing to them.  This makes the class work just like a JavaBean or COM
	'object
	Private productId As String
	Private description As String
	Private price As Double
	
	Sub New( pid As String )
		
		'Here's where we'd look up the product information using the product ID as a key
		'value into a database.  As product information is relational in nature, and Domino
		'is NOT a relational data store, it is likely that the lookup would be into a relational
		'database such as DB2, SQL Server, Oracle etc.  For the purposes of this example
		'we'll just dummy up some results
		
		productId = pid				
' Store the product ID provided in the constructor description = "Widget"	
' Total dummy response 	price = 24.99
		
	End Sub
	
	'The following functions are provide read-only access to the properties (attributes)
	'offered by this class.  The Me keyword refers to the variables defined above at
	'the instance scope of this class
	Public Function getProductId()  As String		
		getProductId = Me.productId		
	End Function
	
	Public Function getDescription()  As String		
		getDescription = Me.description
	End Function
	
	Public Function getPrice()  As Double		
		getPrice = Me.price
	End Function
	
End Class

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Other
Create a dynamic user-driven navigator for a Notes/Domino application
How to apply XSL style sheets to XML views
Comparing replicas on clustered Lotus Domino servers
Creating a Lotus Notes view column categorized by month
Using the XMLHTTP object for integration with Domino or any RDBMS back end
Hiding field properties/data from DocProperties box
Export a view to Excel without coding
Prevent document deletion if there are response documents
Switching between test IDs quickly
AddParameter to a NotesXSLTransFormer

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts