0% found this document useful (0 votes)
79 views11 pages

Apex Codes-Day 1 & 2

The document contains code snippets demonstrating various Apex programming concepts like classes, methods, variables, data types, loops, conditional statements, collections etc. Some key examples include defining a class with methods, declaring and initializing different variable types, using date/time functions, implementing interfaces, nested if/else conditions, foreach loops to iterate over collections.

Uploaded by

Shubham Rajiwade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views11 pages

Apex Codes-Day 1 & 2

The document contains code snippets demonstrating various Apex programming concepts like classes, methods, variables, data types, loops, conditional statements, collections etc. Some key examples include defining a class with methods, declaring and initializing different variable types, using date/time functions, implementing interfaces, nested if/else conditions, foreach loops to iterate over collections.

Uploaded by

Shubham Rajiwade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

public class firstClass //class definition

{//delimiters

public void FirstMethod()//method definition


{
system.debug('Hello World!');
}
}
===================================================================================
=====================
public class VariableExample //class definition
{

public void VarMethod()//method definition


{
string Name='Ankush';//variable declaration and initialization
integer Age=24;
decimal Marks=78.5;
string Address;//variable declaration
Address='New Delhi';//initialization

System.debug('Name is: ' +Name);


System.debug('Age is: ' + Age);
System.debug('Marks are: ' + Marks);
system.debug('Address is: ' + Address);
}
}
===================================================================================
=====================
public class DateTimeClass
{
public void DateTimeExample()
{
Date CD=date.today();//current date
DateTime CDT=dateTime.now();//current date and time
Time CT=DateTime.now().time();//Current Time

Date SD=date.newInstance(1994, 01, 07);//specific date


DateTime SDT=dateTime.newInstance(1994, 01, 07, 07, 30, 40);//specific date and
time
Time ST=time.newInstance(07, 15, 55, 27);//specific time

System.debug('Current Date:'+ CD);


System.debug('Current Date and Time:'+ CDT);
System.debug('Current Time:'+ CT);

System.debug('Specific Date:'+ SD);


System.debug('Specific Date and Time:'+ SDT);
System.debug('Specific Time:'+ ST);
}
}
===================================================================================
====================
public class sObjectExample
{
public void SobjectMethod()
{
Account ac=[select name from account limit 1];//specific Sobject variable
Lead ld=[select firstname from Lead limit 1];//specific Sobject variable

Sobject ob=[select name from account limit 1];//generic sobject variable


}
}
===================================================================================
====================
public class MethodClass {

integer result;//private variable


/*public void Add(integer N1,integer N2)//parameterized method
{

integer res=N1+N2;
System.debug(res);
}*/

public integer Add(integer N1,integer N2)//parameterized method


{
integer res=N1+N2;
return res;
}

}
===================================================================================
====================
public class StaticExample
{

static integer Num1=89;


static integer Num2=75;
public static void Add()
{
integer res=Num1+Num2;
System.debug(Res);
}
}
===================================================================================
====================
public class ConstantExample
{
static final decimal pi=3.14;

public static void CalculateArea(decimal radius)


{

decimal Area=pi*radius*radius;
System.debug('Area of a Circle:' + Area);
}

}
===================================================================================
====================
public class StudentDetails
{
integer Age;
string Name;
string Address;
public StudentDetails()//constructor
{
Age=24;
Name='Ayush';
Address='New Delhi';

DisplayDetails();
}

public void DisplayDetails()//method definition


{
System.debug('Name is:'+ Name);
System.debug('Age is:'+ Age);
System.debug('Address is:'+ Address);
}

}
===================================================================================
====================
public class PropertyClass
{
static integer a;//private variable

public static integer Age//public property


{
get
{
return a;
}
set
{
if(value<0)
{
value=-value;
}
a=value;
}
}

public static string Name


{
get;
set;
}
}
===================================================================================
====================
public interface PurchaseOrder
{
double Discount();//abstract method
}
===================================================================================
====================
public class EmployeeOrders implements PurchaseOrder
{
public double Discount()
{
return .20;
}

public void CalculateBill(double Qty,double Price)


{
double dis=Discount();
double TotalAmount=(Price*Qty)-(Price*Qty)*dis;
system.debug('Total Amount:' + TotalAmount);
}
}
===================================================================================
====================
public class CustomerOrders implements PurchaseOrder
{
public double Discount()
{
return .10;
}
public void CalculateBill(double Qty,double Price)
{
double dis=Discount();
double TotalAmount=(Price*Qty)-(Price*Qty)*dis;
system.debug('Total Amount:' + TotalAmount);
}
}
===================================================================================
====================
public class ifElseClass
{
public static void Check(integer num)
{
if(math.mod(num,2)==0)
{
system.debug('The number is Even:'+ num);
}
else
{
system.debug('The number is Odd:'+ num);
}
}
}
===================================================================================
=====================
public class IfElseLadder
{
static integer BillAmount;
public static void CheckUnits(integer units)
{
if(units<=100)
{
BillAmount=Units*2;
System.debug('Bill Amount: '+ BillAmount);
}

else if(units<=200)
{
BillAmount=Units*3;
System.debug('Bill Amount: '+ BillAmount);
}
else if(Units<=300)
{
BillAmount=Units*4;
System.debug('Bill Amount: '+ BillAmount);
}
else
{
BillAmount=Units*5;
System.debug('Bill Amount: '+ BillAmount);
}
}
}
===================================================================================
====================
public class NestedIfElse
{
public static void CheckEligibility(integer Age,integer Marks,string Qual)
{
if(Age>=18)
{
if(Marks>=75)
{
if(Qual=='B.Tech' || Qual=='BCA')
{
system.debug('Congratulations! Candidate is selected');
}
else
{
system.debug('Sorry! Candidate is rejected as not having the desired
qualification');
}
}
else
{
system.debug('Sorry! Candidate is rejected as marks are less than 75');
}
}
else
{
system.debug('Sorry! Candidate is rejected as Age is less than 18');
}
}

public static void Check(integer Age,integer Marks,string Qual)


{
if(Age>=18 && Marks>=75 && (Qual=='BCA'|| Qual=='B.Tech'))
{
System.debug('Congratulations! You are selected.');
}
else
{
System.debug('Sorry! You are Rejected.');
}
}
}
===================================================================================
====================
public class SwitchExample
{
public static void CheckDay(integer num)
{
switch on num
{
when 1
{
system.debug('Monday');
}
when 2
{
system.debug('Tuesday');
}
when 3
{
system.debug('Wednesday');
}
when 4
{
system.debug('Thursday');
}
when 5
{
system.debug('Friday');
}
when 6
{
system.debug('Saturday');
}
when 7
{
system.debug('Sunday');
}
when else
{
system.debug('Invalid day number');
}
}
}
}
===================================================================================
====================
public class LoopClass
{
public static void doWhileLoop()
{
integer i=0;//initialization

do
{
system.debug(i);//0,1,2,3,4
i++;//unary operator
//i=i+1;//arithmetic operator
//i+=1;//arithmetic assignment operator
}
while(i<5);//condition
}

public static void whileLoop()


{
integer i=0;//initialization
while(i<5)//condition
{
system.debug('Learning Apex!');
i++;//increment
}
}

public static void forLoop()


{
integer sum=0;
for(integer i=0;i<20;i++)
{
if(math.mod(i, 2)==0)
{
sum=sum+i;
}
}

System.debug('Sum is: ' +Sum);


}
}
===================================================================================
====================
public class ArrayClass
{
public static void ArrayMethod()
{
//integer [] Score=new integer[10];//array declaration

/* Score[0]=89;//initialization
Score[1]=67;
Score[2]=78;
Score[3]=75;
Score[4]=81;
Score[5]=58;
Score[6]=89;
Score[7]=59;
Score[8]=57;
Score[9]=85;*/

/* System.debug(Score[0]);
System.debug(Score[1]);
System.debug(Score[2]);*/

string [] names=new
string[]{'Ritik','Heera','Aman','Aryan','Dhruv'};//declaration + initialization

for(integer i=0;i<names.size();i++)
{
system.debug(names[i]);
}

system.debug(names);

integer [] arr=new integer []


{12,34,56,67,32,34,5,6,78,90,87,65,67,89,09,21,32,45,65,21,78,32,45,43,42,65,57,90}
;
for(integer i:arr)//foreach loop
{
if(math.mod(i, 2)!=0)
{
system.debug(i);
}
}
}
}

===================================================================================
===================
public class ForeachExample
{
public static void foreachLoop()
{
contact [] cons= [select firstname from contact];

for(Contact c:cons)
{
system.debug(c);
}
}
}
===================================================================================
====================
public class ListCollection
{
public static void ListExample()
{
// List<string> colors=new List<string>();//declaration
List<string> colors=new List<string>{'Orange','Blue'};//declaration+
initialization
colors.add('Red');//initialization
colors.add('Yellow');//add will add a new value at the end of the list
colors.add('Green');

System.debug(colors);

colors.add(1,'White');//will add a new value at the specified index

System.debug(colors);

colors.set(1,'Cyan');//will replace the value at the specified index

system.debug(colors);

colors.remove(3);//will remove the value from the specified index


system.debug(colors);

List<string> flowers=new list<string>{'Rose','Lotus','Orchid'};

colors.addAll(flowers);//will add values of one list into another


system.debug(colors);

colors.clear();//will clear the values of a list


system.debug(colors);

}
}
===================================================================================
=====================
public class SetCollection
{
public static void SetExample()
{
// Set<string> Addresses=new Set<string>();//declaration
Set<string> Addresses=new Set<string>{'Pune','Chennai'};//declaration +
initialization
Addresses.add('New Delhi');//initialization
Addresses.add('Mumbai');
Addresses.add('Banglore');

System.debug(addresses);
System.debug(addresses.size());

if(Addresses.contains('Delhi'))
{
system.debug('The searched address is available');
}
else
{
system.debug('The searched address is not available');
}

Set<string> Names=new Set<string>{'Ritik','Dhruv','Aashay','Heera'};

Addresses.addAll(Names);
System.debug(Addresses);

}
}
===================================================================================
====================
public class MapCollection
{
public static void MapExample()
{
Map<integer,string> EmpAddress=new Map<integer ,string>();//declaration
EmpAddress.put(1,'New Delhi');//initialization
EmpAddress.put(2,'Mumbai');//will add the values in a map
EmpAddress.put(3,'Chennai');
EmpAddress.put(4,'Hyderabad');
EmpAddress.put(3,'Pune');

Map<string,string> newMap=new
Map<string,string>{'a'=>'apple','b'=>'ball'};//declaration+ initialization
newMap.put('c','cat');

System.debug(EmpAddress);
System.debug(EmpAddress.get(2));//to get the value from a index

Set<integer> keys=EmpAddress.keyset();// to get all the keys of a map


for(integer i:keys)
{
system.debug(i);
}

list<string> Address=EmpAddress.values();//to get all the values of a map

for(string s:Address)
{
system.debug(s);
}

System.debug(NewMap);

}
}
===================================================================================
====================
Anonymous Window Code
===================================================================================
====================
firstClass ob1=new firstClass();//memory allocation
ob1.FirstMethod();//function calling

VariableExample ob=new VariableExample();


ob.VarMethod();

DateTimeClass ob=new DateTimeClass();


ob.DateTimeExample();

MethodClass ob=new MethodClass();


ob.Add(34,78);

MethodClass ob=new MethodClass();


integer result=ob.Add(56,78);
System.debug(result);
MethodClass ob=new MethodClass();
ob.result=90;

StaticExample.Add();
StaticExample ob=new StaticExample();
ob.Add();

ConstantExample.CalculateArea(5);

StudentDetails ob=new StudentDetails();


//ob.DisplayDetails();
PropertyClass.Age=-24;//will call set accessor
System.debug(PropertyClass.Age);//will call get accessor

PropertyClass.Name='US';//will call set


System.debug(PropertyClass.Name);//will call get

EmployeeOrders EO=new EmployeeOrders();


EO.CalculateBill(2,1000);

CustomerOrders CO=new CustomerOrders();


CO.CalculateBill(2,1000);
ifElseClass.Check(10);
IfElseLadder.CheckUnits(350);
NestedIfElse.CheckEligibility(17, 68, 'BCA');
NestedIfElse.check(19,76,'BBA');
SwitchExample.CheckDay(10);
LoopClass.doWhileLoop();
LoopClass.whileLoop();
LoopClass.forLoop();
ArrayClass.ArrayMethod();
ForeachExample.foreachLoop();
ListCollection.ListExample();
SetCollection.SetExample();
MapCollection.MapExample();

===================================================================================
====================

You might also like