100% found this document useful (1 vote)
120 views8 pages

Class 1

The document provides 10 examples of creating Apex classes with constructors and methods. The examples demonstrate how to: 1. Create classes with data members and methods to display the data 2. Add constructors to initialize data members 3. Pass parameters to constructors to set initial values 4. Create methods to set or update data member values 5. Create objects of the classes and invoke methods on them to demonstrate functionality The examples cover creating classes to represent students, movies, bank accounts, invoices, products and employees. They show different ways of using constructors and methods to work with data members of the classes.

Uploaded by

R P
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
100% found this document useful (1 vote)
120 views8 pages

Class 1

The document provides 10 examples of creating Apex classes with constructors and methods. The examples demonstrate how to: 1. Create classes with data members and methods to display the data 2. Add constructors to initialize data members 3. Pass parameters to constructors to set initial values 4. Create methods to set or update data member values 5. Create objects of the classes and invoke methods on them to demonstrate functionality The examples cover creating classes to represent students, movies, bank accounts, invoices, products and employees. They show different ways of using constructors and methods to work with data members of the classes.

Uploaded by

R P
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/ 8

1.

Create a class student with


1.name
2.city
3.branch
create a method display which display all the data

public class Student{

public String name ;


public String city ;
public String branch;
public void display(){
System.debug('Name :'+name);
System.debug('city :'+city);
System.debug('Branch :'+branch);
}
}

==================================================================================
2. Create a class Moive with following details

1.movieName
2.heroName
3.heroineName
4.cost
5.noTicekts

a.create a method display which display movieName,heroName,heroineName

b.Create a method details which display cost and noTickets


===================================================================================
==
public class Movie{
string movieName;
public string heroName;
public String heroineName;
public Decimal cost;
public Integer noTickets;

public void display(){

System.debug('Movie Name '+movieName);


System.debug('Hero Name'+heroName);
System.debug('HeroineName :'+heroineName);
}
public void show(){
System.debug('Cost :'+cost);
System.debug('noTickets :'+noTickets);
}

===================================================================================
===
3. create a class ICICIBank with following details

1. branchName

2. accountNo // IC0001 ,IC0002


3. balance

4. transactionDate

a. create method display which prints branchName and accountNo

b. create a metho setData which will set balance=1000 and transaction date as
today
===================================================================================
====

public class ICICIBank{

public String branch;


public String accountNo;
public Decimal balance;
public Date transactionDate;

public void display(){

System.debug('Branch :'+branch);
System.debug('accountNo:'+accountNo;
}
public void setData(){

balance=1000;
transactionDate=System.today();
}

}
-----------------------------------------------------------------------------------
------

Contructor :
1. This is a special method defined with in the class with the same name of the
class

Syntax :
public /global class ClassName{

public/global className(parameters){

==================================================================================
Example 1: public class Example{

public Example(){

}
}

Example 2: public class Demo{


public Demo(Integer age){

Example 3: Create a class movie with a constructor

public class Movie{

public Movie(){

}
}

Example 4: Create class Movie with a constructor which has movieName and cost as
parameter

public class Movie{

public Movie(String movieName,Decimal cost){

}
}
===================================================================================
====
Example 5: Create a class Book with

1.bookName
2.authorName

a. create a constructor and assign values


bookName as 3states
author as Chetan

b. Create a method display which prints bookName and authorName


===================================================================================
==
public class Book{

public String bookName;


public String authorName;
public Book(){

bookName='3states';
authorName='ChetanBhagat';
}

public void show(){

System.debug('Author Name '+authorName);


System.debug('Book Name '+bookName);
}

}
===================================================================================
==
Example 6 : Create class InvoiceItems

1. invoiceNo :// CI01

2. amount

3. branch

a. create a constructor and assign the value of


branch as 'SRNagar'
invoice as CI001
amount=0

b. create a method submit with parameter price

->In this method price should be added to amount and display the
amount
===================================================================================
====
public class InvoieItems{

public String invoiceNo;


public Decimal amount;
public String branch;

public InvoieItems(){

invoiceNo='CI001';
amount=0;
branch='SRNagar';
}
public void submit(Decimal price){
amount=amount+price;
System.debug('Amount :'+amount);
}

}
===================================================================================

Example 7: create a class productInfo with

a. productName
b. price
c. quanity
d. productInfo() : assign the value in the constructor
e. show() : Which will display the data memebers of the class

Create two objects and run the program


===================================================================================
==
public class ProductInfo{

public String prodctName;


public Decimal price;
public Integer quanity;

public ProductInfo(){
productName='Dell';
quanity=2;
price=20000;
}

public void show(){

System.debug('Name :'+productName);
System.debug('Price :'+price);
System.debug('Quanity:'+quanity);
}
}

Debug Console :
ProductInfo p1=new ProductInfo();
p1.show();
ProductInfo p2=new ProductInfo();
p2.show();
==========================================================================

Example 8: Create a class studentDetails which has

1. collegeName
2. branch
3. phone
4. name
5. StudentDetails(sname,sphone) :
assign the collegename as 'CBIT'
branch as 'CSE'

6. show() : This method should print all the values of datamemebers

Create two objects for StudentDetails and invoke show() method on them
===================================================================================
=====

public class StudentDetails {


public String name ;
public String branch;
public String collegeName;
public String phone;
public StudentDetails(String sname,String sphone){
name=sname;
phone=sphone;
collegeName='CBIT';
branch='CSE';
}
public void show(){
System.debug('College Name :'+collegeName);
System.debug('Branch :'+branch);
System.debug('Name '+name);
System.debug('Phone :'+phone);
}

Debug :
StudentDetails s1=new StudentDetails('Ravi','123');
s1.show();
StudentDetails s2=new StudentDetails('Kiran','456');
s2.show();

===================================================================================
===
Example 9:

1. Create a class Invoice with datamemebers

1. invoiceDate
2. total
3. tax
4. available
5. cost
6. Invoice(savailable) : invoiceDate as today
total=0
tax=0

7. placeOrder(quanity) :

check if quanity is available


available then total =quantity*cost
reduce the available by quanity

not available : print out of stock

print the total ,available,invoicedate


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

public class Invoice {


public Date invoiceDate;
public Decimal total;
public Decimal cost;
public Integer available;
public Invoice(Integer savailable){
cost=100;
total=0;
available=savailable;
invoiceDate=System.today();
}
public void placeOrder(Integer quantity){
if(quantity <=available){
total=quantity*cost;
available=available-quantity;
System.debug('Available :'+available);
System.debug('Total :'+total);
System.debug('Invoice Date:'+invoiceDate);
}else{
System.debug('Out of stock');
}
}
}

Debug:
Invoice inv=new Invoice(100);
inv.placeOrder(40);
===================================================================================
==
Example 10 :Create a class Employee

1. empName
2. salary
3. exp

4. Employee(empName,salary,exp)

5. calculate() : This method will calculate the bonus and print the
bonu value

if exp > 10 years bonus is 20% of salary

else 10% of salary


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

public class Employee{

public String empName;


public Decimal salary;
public Integer exp;

public Employee(String empName,Decimal salary ,Integer exp){

this.empName=empName;
this.salary=salary;
this.exp=exp;
}
public void calculate(){

Decimal bonus=0;
if(exp > 10){
bonus=salary*0.20;
}else{
bonus=salary*0.10;
}
System.debug('Bonus value :'+bonus);
}

Employee e1=new Employee('Ravi',20000,20);

e1.calculate();

You might also like