Monday, October 8, 2012

oop සදහා අබ්හ්යාස 02


ඔන්‍‍යාලුනේදත්  oop ‍ලින්‍ ‍වත්‍ practise   code ‍කක්‍ ‍ගෙනා. ‍යාමුලින්‍‍පිකිපු ‍‍හැසන්‍‍කල්‍‍යක්‍‍  යෝගීගෙයිමේ programme  දුවේ. ‍

තින්‍ ‍මේකෙන්‍ ‍කරන්‍‍නෙපොඩි salary   sheet ‍කක්‍ ‍සා .
මෙහි employee  ‍‍කි class  කේයිවශ් ක්රියාවන්‍ ‍වෙන්‍‍ methods  තියෙන්‍‍නෙ methods  වශ්වස්‍‍තා main  method  කේදි call  කිරීගින්‍ ‍පිවශ් ක්රියාවන්‍ ‍වශ්වේලාවන්‍‍හිබාවිගෙති. ‍මේ code  හොතේරුම්‍ ‍ගන්‍‍ coding  ඩාමෙවැනිමක්‍ ‍සන්‍‍කීර් coding  ‍‍යාගෙ  දැනුවැඩිගන්‍‍රුකුලක්‍ ‍වෙයි  

Code : -


public class employee {
    private int year;
    private int basic;
    private int otHours;
    private int otsalary;
    private int leaveDays;
    private int workSalary;
   

    public employee(int Year,int othours,int leavedays) {
        year=Year;
        basic=3000;
        otHours=othours;
        otsalary=0;
        leaveDays=leavedays;
        workSalary=0;
    }
   
    public int basicSalary(){
        if(year<=0.5)
            return basic;
        else if(year<=1){
            basic=5000;
            return basic;
        }
           
        else if(year<=2){
            basic=10000;
            return basic;
        }
           
        else{
            int a=year-2;
            for(int i=0;i<a;i++){
                   basic=10000+1000;
            }
            return basic;
        }
    }
    public int otSalary(){
        for(int i=0;i<otHours;i++){
            otsalary=otsalary+85;
        }
        return otsalary;
    }
    public int workHoursalary(){
        //work days for month i expect 26
        int workday=26-leaveDays;
        for(int i=0;i<workday;i++){
            workSalary=workSalary+900;
        }
        return workSalary;
    }

}

main  class ‍එ‍ක ‍ත‍ම‍යි ‍ප‍හ‍ත code  ‍එ‍ක


public class mainEmployee {

   
    public static void main(String[] args) {
       
        java.util.Scanner x=new java.util.Scanner(System.in);
        System.out.print("Enter Employee Name       :- ");
        String name=x.nextLine();
        System.out.print("Enter work duration(Year) :- ");
        int workduration=x.nextInt();
        System.out.print("Enter ot Hours            :- ");
        int otho=x.nextInt();
        System.out.print("Enter Leave days          :- ");
        int leday=x.nextInt();
       
        employee o=new employee(workduration,otho,leday);
       
        System.out.println("............Salary sheet.............");
        System.out.println(" ");
        System.out.println("Employee "+name+" work duration is "+workduration+" years ");
        System.out.println("Basic salary    :- "+o.basicSalary());
        System.out.println("work salary     :- "+o.workHoursalary());
        System.out.println("Ot Hour salary  :- "+o.otSalary());
        System.out.println("...................................");
        System.out.println("Total salary    :- "+(o.basicSalary()+o.otSalary()+o.workHoursalary()));
       
       

    }

}


Out put:- 



හො‍ද‍යි ‍මේ‍කෙ ‍මො‍කක්‍ ‍හ‍රි ‍අ‍වු‍ලක්‍ ‍ති‍ය‍න‍ව‍නම්‍ ‍අ‍පි‍ට ‍කි‍යන්‍‍න ‍‍අ‍ද‍ට ‍අ‍පි ‍න‍ව‍ති‍න‍ව .. ‍දි‍ග‍ට‍ම ‍අ‍පිත්‍ ‍එක්‍‍ක ‍ඉන්‍‍න 

By Manoj Priyankara