By GokiSoft.com| 10:04 09/10/2021|
C Sharp

Bài tập ôn luyện OOP + Interface trong C# - Lập Trình C# - Lập Trình C Sharp



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

vuong huu phu [T2008A]
vuong huu phu

2021-05-25 03:24:51



using System;

namespace baitap_24_5
{
    class Program
    {
        static void Main(string[] args)
        {
            Hiredprogrammers myemployee = new Hiredprogrammers(3);
            Programmer add;
            for (int i = 0; i < 3; i++) {
                Console.WriteLine("Name :");
                String name = Console.ReadLine();
                int id = i + 1;
                add = new Programmer(id, name);
                Console.WriteLine("Skills :");
                add.Skill = Console.ReadLine();
                Console.WriteLine("DOB :");
                add.Dateofbirth = DateTime.Parse(Console.ReadLine());
                myemployee.addnew(add);
            }
            Console.WriteLine();
            Console.WriteLine("underage");
            int underage = Int32.Parse(Console.ReadLine());
            int t = myemployee.Showfilterinfor(underage);
            Console.WriteLine(" underage =" + t);
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace baitap_24_5
{
    interface Iperson
    {
       public String Skill { set; get; }
        public DateTime Dateofbirth { get; set; }
        public int age {get; }

    }
    
}



using System;
using System.Collections.Generic;
using System.Text;

namespace baitap_24_5
{
    public abstract class Employee


    {       
        private string _name; 
        private int _id; 
        public Employee(){ }
        public Employee(int id)
        {
            _id = id;
            Name = " No name ";
        }
        public Employee(int id, string name)
        {
            _id = id;
            Name = name;
        }
        public int Id
        {
            get
            {
                return _id;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value.Length < 3)
                {
                    throw new Exception(" lon hon 3 ki tu");
                }
                _name = value;
            }
        }

        public abstract void ShowInfo();

    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace baitap_24_5
{
    class Programmer : Employee,Iperson
    {
        private String _Skill { set; get; }
        private DateTime _DOB { set; get; }
        private int Age { set; get; }
        public Programmer(int id, String name) : base(id, name) {
            this._Skill = String.Empty;
            this._DOB = DateTime.Now;
        }
        public Programmer(int id, String name,String skill,DateTime dob) : base(id,name) {
            this._Skill = skill;
            this._DOB = dob;
        }


        public string Skill {
            get {
                return _Skill;
            } 
            set {
                if (value.Length < 0) {
                    throw new Exception("Ki nang phai lon hon 0");
                }
                _Skill = value;
            }
        }

        public DateTime Dateofbirth
        {
            get {
                return _DOB;
            }
            set {
                _DOB = value;
            }
        }
        public int age {
            get {
                Age = DateTime.Now.Year - _DOB.Year;
                return Age;
            }
        }

        public override void ShowInfo()
        {
            Console.WriteLine("id : " + Id + " | Skills: " + _Skill + " | DOB " + _DOB + " | Age" + age);
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace baitap_24_5
{
    class Hiredprogrammers
    {
        List<Programmer> HPGM;
        public Hiredprogrammers(int capacity) {
            HPGM = new List<Programmer>(capacity);
        }
        public void addnew(Programmer prog) {
            try
            {
                HPGM.Add(prog);
            }
            catch {
                throw new Exception("Het dung luong");
            }
        }
        public int Showfilterinfor(int underage) {
            int t = 0;
            foreach (Programmer ageu in HPGM) {
                if (ageu.age <= underage) {
                    t++;
                }
            }
            return t;
        }
    }
}



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-05-24 09:28:41


#calculator .cs


using System;

public class IPerson
{
	public IPerson()
	{
	}
    public string Skills { get; set; }
    public DateTime DateOfBirth { get;  }
    public int Age { get;  }


}


#Employee.cs


using System;

{
    abstract class Employee
    {
        private int _id;
        private string _name;
        public int id
        {
            get
            {
                return _id;
            }
        }
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                checkName(value);
            }
        }
        public Employee()
        {


        }
        public Employee(int id)
        {
            this._id = id;
            this._name = "No name";
        }
        public Employee(int id, string name)
        {
            this._id = id;
            this._name = name;
        }
        private void checkName(string name)
        {
            bool isCheck = false;
            while (!isCheck)
            {
                if (name.Length < 3)
                {
                    Console.WriteLine("Name.length > 3 character !!");
                }
                else
                {
                    _name = name;
                    isCheck = true;
                }
            }
        }
        public abstract void showInfor();

    }
}


#Programmer.cs


using System;

{
    class Programmer : Employee, IPerson
    {
        private string _skills;
        private DateTime _DOB;
        private int _age;

        public string skills
        {
            get
            {
                return _skills;
            }
            set
            {
                _skills = value;
            }
        }
        public DateTime birthday
        {
            get
            {
                return _DOB;
            }
            set
            {
                _DOB = value;
            }
        }


        public int age
        {
            get
            {
                return _age;
            }

        }
        public Programmer()
        {

        }
        public Programmer(int id, string name, int age) : base(id, name)
        {
            this.skills = "";
            this.birthday = DateTime.Now;
            this._age = age;
        }
        public override void showInfor()
        {
            while (string.IsNullOrEmpty(skills))
            {


                Console.WriteLine("skills is not empty !");

                Console.WriteLine("Skills : ");
                skills = Console.ReadLine();
            }
            Console.WriteLine("ID : {0} | Name : {1} | Skills : {2} | DOB : {3} | Age : {4}", id, name, skills, birthday, age);
        }


    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-05-24 09:28:28


#Program.cs


using System;
using TestQuestion1.Modals;

namespace TestQuestion1
{
    class Program
    {
        static void Main(string[] args)
        {
            HiredProgrammerscs myemployee = new HiredProgrammerscs(3);
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Input name:");
                string name = Console.ReadLine();
                if (name.Length < 3)
                {
                    Console.WriteLine("Length of name must above 3");
                    throw new Exception();
                }
                Programmer prog = new Programmer(i+1,name);
                Console.WriteLine("Input skills:");
                prog.Skills = Console.ReadLine();
                Console.WriteLine("Input DOB:");
                prog.DateOfBirth = DateTime.Parse(Console.ReadLine());
                myemployee.AddNew(prog);
            }
            Console.WriteLine("Input underage:");
            int underage = int.Parse(Console.ReadLine());
            Console.WriteLine("The number of programmers are underage: {0}", myemployee.ShowFilterInfo(underage));
        }
    }
}


#Employee.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace TestQuestion1.Modals
{
    abstract class Employee
    {
        private int _id;
        private string _name;
        public Employee(int id)
        {
            this._id = id;
            this._name = "No name";
        }
        public Employee(int id,string name)
        {
            this._id = id;
            this._name = name;
        }
        public int ID
        {
            get
            {
                return _id;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if(value.Length < 3)
                {
                    throw new Exception("The length of name must upper 3");
                }
                else
                {
                    _name = value;
                }
            }
        }
        abstract public void ShowInfo();
    }
}


#HiredProgrammerscs.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace TestQuestion1.Modals
{
    class HiredProgrammerscs
    {
        private List<Programmer> HPGM;
        public HiredProgrammerscs(int capacity)
        {
            HPGM = new List<Programmer>(capacity);
        }
        public void AddNew(Programmer prog)
        {
            try
            {
                HPGM.Add(prog);
            }
            catch (Exception)
            {

                throw new Exception("Out of Capacity");
            }
        }
        public int ShowFilterInfo(int underage)
        {
            int count = 0;
            foreach (Programmer prog in HPGM)
            {
                if (prog.Age <= underage) count++;
            }
            return count;
        }
    }
}


#IPerson.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace TestQuestion1.Modals
{
    interface IPerson
    {
        public string Skills { get; set; }
        public DateTime DateOfBirth { get; set; }
        public int Age { get; }
    }
}


#Programmer.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace TestQuestion1.Modals
{
    class Programmer : Employee, IPerson
    {
        private string _skills;
        private DateTime _DOB;
        private int _age;



        public Programmer(int id,string name) : base(id,name)
        {
            this._skills = string.Empty;
            this._DOB = DateTime.Now;
        }
        public Programmer(int id,string name,string skills,DateTime dob) : base(id,name)
        {
            this._skills = skills;
            this._DOB = dob;
        }
        public string Skills
        {
            get
            {
                return _skills;
            }
            set
            {
                if(value.Length < 0)
                {
                    throw new Exception("The value must above 0");
                }
                else
                {
                    _skills = value;
                }
            }
        }

        public DateTime DateOfBirth
        {
            get
            {
                return _DOB;
            }
            set
            {
                _DOB = value;
            }
        }
        public int Age
        {
            get
            {
                _age = DateTime.Now.Year - _DOB.Year;
                if(DateTime.Now.Year < _DOB.Year)
                {
                    _age -= 1;
                }
                return _age;
            }
        }


        public override void ShowInfo()
        {
            Console.WriteLine("Id : {0} | Name: {1} | Skills: {2} | DOB: {3} | Age: {4}", ID, Name, _skills, _DOB, _age);
        }
    }
}



trung [C1907L]
trung

2020-07-02 14:48:55



using System;

namespace Ontap
{
    interface IPerson
    {
        string Skills { get; set; }
        DateTime DateOfBirth { get;}
        int Age { get; }
    }
}



using System;

namespace Ontap
{
    public abstract class Employee
    {
        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value.Length < 3)
                {
                    throw new Exception("Can not be less than 3 char");
                }
                _name = value;
            }
        }

        public Employee()
        { }

        public Employee(int id)
        {
            _id = id;
            Name = "No name";
        }

        public Employee(int id,string name)
        {
            _id = id;
            Name = name;
        }

        public virtual void Input()
        {
            Console.WriteLine("Pleas input name");
            Name = Console.ReadLine();
            Console.WriteLine("Please input id");
            _id = int.Parse(Console.ReadLine());
        }

        public abstract void ShowInfo();

    }
}



using System;

namespace Ontap
{
    public class Programmer : Employee, IPerson
    {
        public Programmer() { }

        public Programmer(int id, string name) : base(id, name)
        {
            _skills = String.Empty;
            _DOB = DateTime.Now;
        }

        public Programmer(int id, string name, string skills, DateTime dob) : base(id, name)
        {
            _skills = skills;
            _DOB = dob;
        }

        private int _age;

        public int Age
        {
            get
            {
                return (DateTime.Now.Year - _DOB.Year);
                // return _age;
            }
        }

        private DateTime _DOB;

        public DateTime DateOfBirth
        {
            get
            {
                return _DOB;
            }
        }

        private string _skills;

        public string Skills
        {
            get
            {
                return _skills;
            }
            set
            {
                if (value.Length < 2)
                {
                    throw new Exception("Invalid input (too short)");
                }
                _skills = value;
            }
        }

        public override void Input()
        {
            base.Input();
            Console.WriteLine("Input skills");
            Skills = Console.ReadLine();
            Console.WriteLine("Input DOB");
            _DOB = DateTime.ParseExact(Console.ReadLine(),"yyyy/MM/dd",System.Globalization.CultureInfo.InvariantCulture);
        }

        public override void ShowInfo()
        {
            Console.WriteLine("ID: {0} | Name: {1} | Skills: {2} | DOB: {3} | Age: {4}",this.Id, this.Name, this.Skills, this.DateOfBirth, this.Age);
        }
    }
}



using System;
using System.Collections.Generic;
namespace Ontap
{
    public class HiredProgrammers
    {
        public List<Programmer> HPGM {get;set;}

        public HiredProgrammers(int capacity)
        {
            HPGM = new List<Programmer>(capacity);
        }

        public void AddNew(Programmer prog)
        {
            if (HPGM.Count == HPGM.Capacity)
            {
                throw new Exception("Capacity reached");
            }
            HPGM.Add(prog);
        }

        public void ShowFilterInfo(int underage)
        {
            int count = 0;
            foreach (Programmer p in HPGM)
            {
                if (p.Age <= underage) 
                {
                    count++;
                    p.ShowInfo();
                }
            }
            Console.WriteLine("Number of Programmer with age < {0} is {1}",underage,count);
        }
    }
}



using System;
using System.Collections.Generic;
namespace Ontap
{
    class Program
    {
        static void Main(string[] args)
        {
            HiredProgrammers myemployees = new HiredProgrammers(3);
            for (int i = 0 ; i < 3; i++)
            {
                Programmer p = new Programmer();
                try
                {
                    p.Input();
                    myemployees.AddNew(p);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
            Console.WriteLine("Please input underage");
            int underage = int.Parse(Console.ReadLine());
            myemployees.ShowFilterInfo(underage);
        }
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-07-02 13:11:54



using System;
using System.Collections.Generic;
using System.Text;

namespace OnTapC_Sharp
{
    class Test
    {
        static void Main(string[] args) {
            HiredProgrammer myemployee = new HiredProgrammer(3);
            Programmer prog;
            for (int i = 0; i < 1; i++) {
                Console.WriteLine("Enter progammer {0} info.",(i+1));
                Console.WriteLine("Enter progammer id : ");
                int id = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter progammer name : ");
                string name = Console.ReadLine();
                Console.WriteLine("Enter progammer date of birth (MM/dd/yyy) : ");
                DateTime date = DateTime.Parse(Console.ReadLine());
               
                Console.WriteLine("Enter progammer skill : ");
                string skills = Console.ReadLine();
                prog = new Programmer(skills, date,id, name);
                myemployee.AddNew(prog);
               

            }
            Console.WriteLine("Enter underage : ");
            int age = int.Parse(Console.ReadLine());
            myemployee.ShowFilterInfo(age);
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OnTapC_Sharp
{
    class HiredProgrammer
    {
        private List<Programmer> HPGM;
        public HiredProgrammer(int n) {
            HPGM = new List<Programmer>(n);
        }
        public void AddNew(Programmer prog) {
            try
            {
                HPGM.Add(prog);
            }
            catch(IndexOutOfRangeException e) {
                Console.WriteLine(e.ToString());
            }       
        }
        public int ShowFilterInfo(int underage) {
            int total = 0;
            for (int i = 0; i < HPGM.Count; i++) {
                if ( HPGM[i].Age <= underage) {
                    HPGM[i].ShowInfo();
                    total++;
                }
            }
            return total;
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OnTapC_Sharp
{
    class InputValueSmallerThan0 : Exception {
        public InputValueSmallerThan0() { }
        public InputValueSmallerThan0(string message) : base(message) { }
    }
    class Programmer : Employee, IPerson
    {

        private string _skill;
        public string Skills
        {
            get
            {
                return _skill;
            } set {
                if (value.Length < 0)
                {
                    throw new InputValueSmallerThan0("Input value less than 0!");
                }              
                    _skill = value;            
            }
        }
        private DateTime _DOB;
        public DateTime DateOfBirth
        {
            get
            {
                return _DOB;
            }
            set 
            {
                _DOB = value;
            }
        }
        private int _age;
        public int Age
        {
            get
            {
                _age = DateTime.Now.Year - DateOfBirth.Year ;
                return _age;
            }
         
        }
        public Programmer(int id,string name) :base(id,name)
        {
            Skills = string.Empty;
            DateOfBirth = DateTime.Now;
        }

        public Programmer(string skills, DateTime dateOfBirth,int id, string name) : base (id ,name)
        {
            Skills = skills;
            DateOfBirth = dateOfBirth;
        }

        public override void ShowInfo() {
            Console.WriteLine("Id {0} | Name: {1} | Skills: {2} | DOB: {3} | Age: {4}",Id,Name,Skills,DateOfBirth,Age);
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OnTapC_Sharp
{
    public class MustLongerThan3 : Exception
    {
        public MustLongerThan3()
        {
        }

        public MustLongerThan3(string message)
            : base(message)
        {
        }

        public MustLongerThan3(string message, Exception inner)
            : base(message, inner)
        {
        }
    }
    abstract class Employee
    {
        private int _id;
        public int Id {
            get {
                return _id;
            }
          
        }
        private string _name;
        public string Name {
            get {
                return _name;
            }
            set {
                if (value.Length < 3)
                {
                    throw new MustLongerThan3("Name must be logger than 3!");
                }
                else {
                    _name = value;
                }
               
            }
        }

        public Employee(int id)
        {
            _id = id;
            Name = "No name";
        }

        public Employee(int id, string name)
        {
            Name = name;
            _id = id;
        }

        public Employee()
        {
        }

        public abstract void ShowInfo();
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OnTapC_Sharp
{
    public interface IPerson
    {
        public string Skills { get; set; }
        public DateTime DateOfBirth { get; }
        public int Age { get; }


    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-05-28 08:59:57



using System;

namespace OOPInterface
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Programmer programmer = new Programmer(1,"vinh",15);
            programmer.showInfor();
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OOPInterface
{
    interface IPerson
    {
        public string skills { get; set; }
        public DateTime birthday { get; set; }
        public int age { get; }

       
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace OOPInterface
{
    abstract class  Employee
    {
        private int _id;
        private string _name;
        public int id {
            get
            {
                return _id;
            }
        }
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                checkName(value);
            }
        }
        public Employee()
        {
            

        }
        public Employee(int id)
        {
            this._id = id;
            this._name = "No name";
        }
        public Employee(int id ,string name)
        {
            this._id = id;
            this._name = name;
        }
        private void checkName(string name)
        {
            bool isCheck = false;
            while (!isCheck)
            {
                if (name.Length<3)
                {
                    Console.WriteLine("Name.length > 3 character !!");
                }
                else
                {
                    _name = name;
                    isCheck = true;
                }
            }
        }
        public abstract void showInfor();
       
    }
}



using System;

namespace OOPInterface
{
    class Programmer : Employee, IPerson
    {
        private string _skills;
        private DateTime _DOB;
        private int _age;

        public string skills
        {
            get
            {
                return _skills;
            }
            set
            {
                _skills = value ;
            }
        }
        public DateTime birthday
        {
            get
            {
                return _DOB;
            }
            set
            {
                _DOB = value;
            }
        }


        public int age
        {
            get
            {
                return _age;
            }

        }
        public Programmer()
        {

        }
        public Programmer(int id, string name,int age) : base(id, name)
        {
            this.skills = "";
            this.birthday = DateTime.Now;
            this._age = age;
        }
        public override void showInfor()
        {
            while (string.IsNullOrEmpty(skills))
            {


                Console.WriteLine("skills is not empty !");

                Console.WriteLine("Skills : ");
                skills = Console.ReadLine();
            }
            Console.WriteLine("ID : {0} | Name : {1} | Skills : {2} | DOB : {3} | Age : {4}", id, name, skills,birthday, age);
        }
       
        
    }
}