DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    Suppose our tablename is masterorder with column orderid for which we are goin to find duplicate records.
SELECT orderid, COUNT(*) TotalCount
FROM masterorder
GROUP BY orderid
HAVING COUNT(*) > 1                
                    // Get information about payment_instrument - map from id to name
$instruments = array();
CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, true);
                
                    // Drop-down selector for payment type (credit card, check, etc) - add this to preProcess() function of the controller code
$this->add('select', 'payment_instrument_id', 
                  ts( 'Payment method' ), 
                  array(''=>ts( '- select -' )) + CRM_Contribute_PseudoConstant::paymentInstrument( ),
                  false, array( 'onChange' => "alert('Add an onchange when a selection is made - perhaps using the built-in showHideByValue?');"));

// Show drop-down selector -- add this SMARTY code to the template code
{$form.payment_instrument_selection.html}                
                    package com.dyrio.graphics;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Graphics1 extends Frame {

	private static final long serialVersionUID = 1L;
	private int xPosition = 100;
	private int yPosition = 50;
	private static final int xSize = 300;
	private static final int ySize = 100;
	private static Graphics2D graphics2D;
	private static final String MESSAGE = "Welcome to 2D Graphics";
	
	public Graphics1() {
		addWindowListener(new ExitAdapter());
	}
	
	public void paint(Graphics g) {
		graphics2D = (Graphics2D) g;
		graphics2D.drawString(MESSAGE, xPosition, yPosition);
	}
	
	public static void main(String[] args) {
		Graphics1 graphics1 = new Graphics1();
		graphics1.setTitle("Graphics 1");
		graphics1.setSize(xSize,ySize);
		graphics1.setVisible(true);
	}
	
	public class ExitAdapter extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
	     {
	       System.exit(0);
	     }
	}
}
                
                            public static int[] QuickSort(int[] arr)
        {
            if (arr.Length <= 1)
                return arr;

            int pivot = arr.Length - 1;

            int[] less = GetLessThanEqualToPivot(arr, pivot);
            int[] greater = GetGreaterThanPivot(arr, pivot);

            return Concatenate(QuickSort(less), arr[pivot], QuickSort(greater));
        }

        public static int[] Concatenate(int[] less, int pivotElement, int[] greater)
        {
            List<int> _result = new List<int>();
            _result.AddRange(less);
            _result.Add(pivotElement);
            _result.AddRange(greater);
            return _result.ToArray();
        }

        public static int[] GetLessThanEqualToPivot(int[] arr, int pivot)
        {
            List<int> _result = new List<int>();

            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] <= arr[pivot])
                {
                    _result.Add(arr[i]);
                }
            }

            return _result.ToArray();
        }

        public static int[] GetGreaterThanPivot(int[] arr, int pivot)
        {
            List<int> _result = new List<int>();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] > arr[pivot])
                {
                    _result.Add(arr[i]);
                }
            }
            return _result.ToArray();
        }                
                    // input array is assumed to be sorted
        public int BinarySearch(int[] arr, int x)
        {
            if (arr.Length == 0)
                return -1;

            int mid = arr.Length / 2;

            if (arr[mid] == x)
                return mid;

            if (x < arr[mid])
                return BinarySearch(GetSubArray(arr, 0, mid - 1), x);
            else
            {
                int _indexFound = BinarySearch(GetSubArray(arr, mid + 1, arr.Length - 1), x);
                if (_indexFound == -1)
                    return -1;
                else
                    return mid + 1 + BinarySearch(GetSubArray(arr, mid + 1, arr.Length - 1), x);
            }
        }


        public int[] GetSubArray(int[] arr, int start, int end)
        {
            List<int> _result = new List<int>();
            for (int i = start; i <= end; i++)
            {
                _result.Add(arr[i]);
            }
            return _result.ToArray();
        }                
                        public class BinaryTreeNode
    {
        public BinaryTreeNode Left { get; set; }

        public BinaryTreeNode Right { get; set; }

        public int Data { get; set; }

        public BinaryTreeNode(int data)
        {
            this.Data = data;
        }
    }

     public void InsertIntoBST(BinaryTreeNode root, int data)
        {
            BinaryTreeNode _newNode = new BinaryTreeNode(data);

            BinaryTreeNode _current = root;
            BinaryTreeNode _previous = _current;

            while (_current != null)
            {
                if (data < _current.Data)
                {
                    _previous = _current;
                    _current = _current.Left;
                }
                else if (data > _current.Data)
                {
                    _previous = _current;
                    _current = _current.Right;
                }
            }

            if (data < _previous.Data)
                _previous.Left = _newNode;
            else
                _previous.Right = _newNode;
        }                
                        public class ParkingLot
    {
        List<ParkingSpot> _parkingSpots;

        public ParkingLot()
        {
            _parkingSpots = new List<ParkingSpot>();
            // 10 parking spots; 2 free; 6 Regular Paid; 2 Handicapped Free

            //2 free
            for (int i = 0; i < 2; i++)
            {
                _parkingSpots.Add(new ParkingSpot(true));
            }

            //6 Regular Paid
            for (int i = 0; i < 6; i++)
            {
                _parkingSpots.Add(new ParkingSpot(false));
            }

            //2 Handicapped Free
            for (int i = 0; i < 2; i++)
            {
                _parkingSpots.Add(new HandicappedParkingSpot());
            }
        }


        public ParkingSpot FindFreeSpot()
        {
            return this._parkingSpots.Find(p => p.IsFree == true && p.IsAvailable);
        }

        public ParkingSpot FindPaidSpot()
        {
            return this._parkingSpots.Find(p => p.IsFree == false && p.IsAvailable);
        }

        public int GetAvailableSpotsCount()
        {
            return this._parkingSpots.Count(p => p.IsAvailable);
        }

        public int GetTotalSpots()
        {
            return this._parkingSpots.Count;
        }
    }

    public class ParkingSpot
    {
        public bool IsAvailable { get; set; }
        public bool IsFree { get; set; }
        public IVehicle ParkedVehicle { get; set; }
        public ParkingMeter Meter { get; set; }

        public void Park(IVehicle vehicle)
        {
            if (this.ParkedVehicle == null)
            {
                this.ParkedVehicle = vehicle;
                this.IsAvailable = false;
            }
            else
            {
                throw new Exception("Parking Spot is Taken. Cannot Park here!");
            }
        }

        public ParkingSpot()
        {
            this.IsAvailable = true;
            this.IsFree = true;
        }

        public ParkingSpot(bool isFree)
        {
            this.IsAvailable = true;
            this.IsFree = isFree;
            if (!this.IsFree)
            {
                this.Meter = new ParkingMeter();
            }
        }
    }

    public class HandicappedParkingSpot : ParkingSpot
    {

    }

    public class ParkingMeter
    {
        public DateTime EndTime { get; set; }
        public int MinutesRemaining
        {
            get
            {
                if (DateTime.Now >= EndTime)
                    return 0;
                else
                    return (EndTime - DateTime.Now).Minutes;
            }
        }

        public int ParkingIntervalMins
        {
            get
            {
                return 1;
            }
        }

        public void Pay(int quarters)
        {
            EndTime = DateTime.Now.AddMinutes(quarters * ParkingIntervalMins);
        }

    }


    public interface IVehicle
    {
        string Make { get; set; }
        string Model { get; set; }
        void Drive();
    }

    public class Car : IVehicle
    {
        public string Make
        {
            get;
            set;
        }

        public string Model
        {
            get;
            set;
        }

        public void Drive()
        {
        }

        public void Park()
        {
        }

    }

    public class Truck : IVehicle
    {
        public string Make
        {
            get;
            set;
        }

        public string Model
        {
            get;
            set;
        }

        public void Drive()
        {
        }

        public void Park()
        {
        }
    }                
                        public class BinaryTreeNode
    {
        public BinaryTreeNode Left { get; set; }

        public BinaryTreeNode Right { get; set; }

        public int Data { get; set; }

        public BinaryTreeNode(int data)
        {
            this.Data = data;
        }
    }

        public enum TreeTraversal
        {
            PREORDER,
            INORDER,
            POSTORDER
        }

        public void PrintTree(BinaryTreeNode root, TreeTraversal treeTraversal)
        {
            Action<int> printValue = delegate(int v)
            {
                Console.Write(v + " ");
            };
        
            switch (treeTraversal)
            {
                case TreeTraversal.PREORDER:
                    PreOrderTraversal(printValue, root);
                    break;
                case TreeTraversal.INORDER:
                    InOrderTraversal(printValue, root);
                    break;
                case TreeTraversal.POSTORDER:
                    PostOrderTraversal(printValue, root);
                    break;
                default: break;
            }
        }

        public void PreOrderTraversal(Action<int> action, BinaryTreeNode root)
        {
            if (root == null)
                return;

            action(root.Data);
            PreOrderTraversal(action, root.Left);
            PreOrderTraversal(action, root.Right);
        }

        public void InOrderTraversal(Action<int> action, BinaryTreeNode root)
        {
            if (root == null)
                return;

            InOrderTraversal(action, root.Left);
            action(root.Data);
            InOrderTraversal(action, root.Right);
        }

        public void PostOrderTraversal(Action<int> action, BinaryTreeNode root)
        {
            if (root == null)
                return;

            PostOrderTraversal(action, root.Left);
            PostOrderTraversal(action, root.Right);
            action(root.Data);
        }                
                    public static BinaryTreeNode BuildBinarySearchTree(int[] sortedArray)
        {
            if (sortedArray.Length == 0)
                return null;

            int _mid = sortedArray.Length / 2;
            BinaryTreeNode _root = new BinaryTreeNode(sortedArray[_mid]);
            int[] _left = GetSubArray(sortedArray, 0, _mid - 1);
            int[] _right = GetSubArray(sortedArray, _mid + 1, sortedArray.Length - 1);
            _root.Left = BuildBinarySearchTree(_left);
            _root.Right = BuildBinarySearchTree(_right);

            return _root;
        }


        public int[] GetSubArray(int[] array, int start, int end)
        {
            List<int> _result = new List<int>();
            for (int i = start; i <= end; i++)
            {
                _result.Add(array[i]);
            }
            return _result.ToArray();
        }