By GokiSoft.com| 17:26 29/10/2021|
C Sharp

Ôn tập OOP - Table - Trong C# - Lập Trình C# - Lập Trình C Sharp

Tạo một lớp đối tượng Table trong namespace đặt tên là room -> gồm các thuộc tính sau

- Tên, màu sắc, nhà sản xuất, số ngày đã sử dụng

Yêu cầu :

Tạo ra 2 hàm tạo : ham tạo ko đối và hàm tạo có đầy đủ đối số

Tạo ra get/set cho các thuộc tính trên

Tạo ra hàm nhập và hiển thị thông tin cho table.

Viết chương trình test chức năng nhập và hiển thị

Viết chương trình test -> tạo ra 2 đối tượng table với 2 hàm tạo khác nhau -> Hiên thị thông tin của chúng

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

10

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

Nguyễn Hùng Anh [community,C2009G]
Nguyễn Hùng Anh

2021-10-09 03:20:24


#Program.cs


using System;

namespace Lesson03.bt2471
{
    class Program
    {
        static void Main(string[] args)
        {
            Table table1 = new Table();
            table1.Input();
            table1.Display();

            Table table2 = new Table("B", "Red", "BB", 10);
            table2.Display();
        }
    }
}


#Table.cs


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

namespace Lesson03.bt2471
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Producer { get; set; }
        public int UsedDays { get; set; }

        public Table()
        {
        }

        public Table(string name, string color, string producer, int usedDays)
        {
            Name = name;
            Color = color;
            Producer = producer;
            UsedDays = usedDays;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap mau sac: ");
            Color = Console.ReadLine();

            Console.WriteLine("Nhap nha san xuat: ");
            Producer = Console.ReadLine();

            Console.WriteLine("Nhap so ngay da su dung: ");
            UsedDays = int.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine("Ten: " + Name + ", mau sau: " + Color + ", nha sx: " + Producer 
                + ", so ngay da sd: " + UsedDays);
        }
    }
}



Hieu Ngo [community,C2009G]
Hieu Ngo

2021-10-09 03:17:57


#Program.cs


using System;

namespace Table
{
    class Program
    {
        static void Main(string[] args)
        {
            Table a = new Table();

            a.Input();
            a.Display();


            Table b = new Table();

            b.Name = "Abc";
            b.Color = "red";
            b.Nsx = "abc";
            b.Date = 30;

            b.Display();
        }
    }
}


#Table.cs


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

namespace Table
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Nsx { get; set; }
        public int Date { get; set; }

        public Table() { }
        

        public Table(string name, string color, string nsx, int date)
        {
            Name = name;
            Color = color;
            Nsx = nsx;
            Date = date;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Name = Console.ReadLine();
            Console.WriteLine("Nhap mau:");
            Color = Console.ReadLine();
            Console.WriteLine("Nhap nha sx:");
            Nsx = Console.ReadLine();
            Console.WriteLine("Nhap so ngay su dung:");
            Date = int.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine("Ten: " + Name + ", Mau: " + Color + ", Nha san xuat: " + Nsx + ", ngay su dung: " + Date)
        }
    }
}



Hoàng Thiện Thanh [community,AAHN-C2009G]
Hoàng Thiện Thanh

2021-10-01 04:36:57


#Program.cs


using System;

namespace Room
{
    class Program
    {
        static void Main(string[] args)
        {
            Table a = new Table();

            a.input();
            a.display();

            Table b = new Table();

            b.Name = "abc";

            b.Color = "Blue";

            b.Manufacturer = "h";

            b.DayUsed = 50;

            b.display();
        }
    }
}


#Table.cs


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

namespace Room
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Manufacturer { get; set; }
        private int dayUsed;
        public int DayUsed { get { return dayUsed; } set { this.dayUsed = value; } }
        public Table()
        {

        }

        public Table(string Name, string Color, string Manufacturer, int DayUsed)
        {
            this.Name = Name;
            this.Color = Color;
            this.Manufacturer = Manufacturer;
            this.DayUsed = DayUsed;
        }

        public void input()
        {
            Console.WriteLine("Input the name of the Table:");
            Name = Console.ReadLine();
            Console.WriteLine("Input Color of the Table:");
            Color = Console.ReadLine();
            Console.WriteLine("Input the Manufacturer:");
            Manufacturer = Console.ReadLine();
            Console.WriteLine("Input the amount of days the table has been used:");
            DayUsed = validInt(Console.ReadLine());
        }

        public void display()
        {
            Console.WriteLine("\nName: {0}\nColor: {1}\nManufacturer: {2}\nDay of use: {3}", Name, Color, Manufacturer, DayUsed);
        }

        public int validInt(string str) {
            bool check()
            {
                try { int.Parse(str); } catch (Exception){ return false; }
                return true;
            }
            while (!check() || int.Parse(str) < 0)
            {
                if (!check())
                {
                    Console.WriteLine("Enter a valid number input");
                } else if (int.Parse(str) < 0)
                {
                    Console.WriteLine("Number input below 0");
                }
                
                str = Console.ReadLine();
            }
            return int.Parse(str);
        }
    }
}



Kim Văn Thiết [community,AAHN-C2009G]
Kim Văn Thiết

2021-09-25 05:09:39



using System;
using System.Text;

namespace Room
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Manufacturer { get; set; }
        public int DayUsed { get; set; }

        public Table() {
        }

        public Table(string Name, string Color, string Manufacturer, int DayUsed)
        {
            this.Name = Name;
            this.Color = Color;
            this.Manufacturer = Manufacturer;
            this.DayUsed = DayUsed;
        }

        public void input()
        {
            Console.WriteLine("Input the name of the Table:");
            Name = Console.ReadLine();
            Console.WriteLine("Input Color of the Table:");
            Color = Console.ReadLine();
            Console.WriteLine("Input the Manufacturer:");
            Manufacturer = Console.ReadLine();
            Console.WriteLine("Input the amount of days the table has been used:");
            DayUsed = int.Parse(Console.ReadLine());
        }

        public void display()
        {
            Console.WriteLine("\nName: {0}\nColor: {1}\nManufacturer: {2}\nDay of use: {3}", Name, Color, Manufacturer, DayUsed);
        }


    }
    class Test{
        static void Main(string[] args)
        {
            Table a = new Table();

            a.input();
            a.display();

            Table b = new Table();
            
            b.Name = "abc";
            
            b.Color = "Blue";
            
            b.Manufacturer = "h";
            
            b.DayUsed = 50;

            b.display();
        }

    }
    
}



Hoàng Thiện Thanh [community,AAHN-C2009G]
Hoàng Thiện Thanh

2021-09-23 09:15:14


#Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Room
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Manufacturer { get; set; }
        public int DayUsed { get; set; }

        public Table() {
        }

        public Table(string _Name, string _Color, string _Manufacturer, int _DayUsed)
        {
            this.Name = _Name;
            this.Color = _Color;
            this.Manufacturer = _Manufacturer;
            this.DayUsed = _DayUsed;
        }

        public void input()
        {
            Console.WriteLine("Input the name of the Table:");
            Name = Console.ReadLine();
            Console.WriteLine("Input Color of the Table:");
            Color = Console.ReadLine();
            Console.WriteLine("Input the Manufacturer:");
            Manufacturer = Console.ReadLine();
            Console.WriteLine("Input the amount of days the table has been used:");
            DayUsed = int.Parse(Console.ReadLine());
        }

        public void display()
        {
            Console.WriteLine("\nName: {0}\nColor: {1}\nManufacturer: {2}\nDay of use: {3}", Name, Color, Manufacturer, DayUsed);
        }


    }
    class Test{
        static void Main(string[] args)
        {
            Table a = new Table();

            a.input();
            a.display();

            Table b = new Table();
            
            b.Name = "ABN";
            
            b.Color = "Blue";
            
            b.Manufacturer = "Manam";
            
            b.DayUsed = 50;

            b.display();
        }

    }
    
}



Nguyễn Việt Hoàng [community,AAHN-C2009G]
Nguyễn Việt Hoàng

2021-09-23 09:05:59


#room.cs


using System;

namespace Ss2
{
    class room
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Producer { get; set; }
        public int Day { get; set; }
        public room()
        {

        }
        public room(string name, string color, string producer, int day) {
            this.Name = name;
            this.Color = color;
            this.Producer = producer;
            this.Day = day;
        }

        public void input()
        {
            Console.WriteLine("Enter Name :");
              Name = Console.ReadLine();
            Console.WriteLine("Enter Color :");
             Color = Console.ReadLine();
            Console.WriteLine("Enter Producer :");
             Producer = Console.ReadLine();
            Console.WriteLine("Enter Day :");
             Day = int.Parse(Console.ReadLine());
        }
        public void display()
        {
            Console.WriteLine("Name :{0} , Color :{1} , Producer :{2} , Day :{3}", Name, Color, Producer, Day);
        }
        static void Main(string[] args)
        {
            room a = new room();
            a.input();
            a.display();
            room b = new room("H","g","t",3);
            b.display();
         
           
           
        }
    }
}



Hoàng Văn Huy [community,AAHN-C2009G]
Hoàng Văn Huy

2021-09-23 09:03:53


#Table.cs


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

namespace Room
{
    class Table
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Manufactorer { get; set; }
        public int Day { get; set; }

        public Table()
        {
        }

        public Table(string name, string color,
                    string manufactorer, int day)
        {
            this.Name = name;
            this.Color = color;
            this.Manufactorer = manufactorer;
            this.Day = day;
        }

        public virtual void Input()
        {
            Console.WriteLine("Enter the table's name: ");
            this.Name = Console.ReadLine();
            Console.WriteLine("Enter the table's color: ");
            this.Color = Console.ReadLine();
            Console.WriteLine("Enter the table's manufatorer: ");
            this.Manufactorer = Console.ReadLine();
            Console.WriteLine("Enter the table's day(s) used: ");
            this.Day = int.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public override string ToString()
        {
            return "Name: " + this.Name + ", Color: " + this.Color +
                    ", Manufactorer: " + this.Manufactorer + ", Day(s) used: "
                     + this.Day;
        }
    }
}


#Test.cs


using Room;
using System;

namespace Ex2
{
    class Test
    {
        static void Main(string[] args)
        {
            Testing_no1();
        }

        static void Testing_no1()
        {
            Table t1 = new Table("Ban go", "Trang", "Xuong go Ha Noi", 20);
            t1.Display();
            Table t2 = new Table("Ban da", "Xam", "Xuong Che Tac Tram", 349);
            t2.Display();
        }
    }
}



Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2021-05-18 02:02:08



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

namespace room
{
    class Table
    {
        public string name { get; set; }
        public string color { get; set; }
        public string nsx { get; set; }
        public int number_of_day_used { get; set; }

        public Table()
        {

        }

        public Table(string name, string color, string nsx, int number_of_day_used)
        {
            this.name = name;
            this.color = color;
            this.nsx = nsx;
            this.number_of_day_used = number_of_day_used;
        }

        public void input()
        {
            Console.WriteLine("Input name: ");
            name = Console.ReadLine();

            Console.WriteLine("Input color: ");
            color = Console.ReadLine();

            Console.WriteLine("Input nsx: ");
            nsx = Console.ReadLine();

            Console.WriteLine("Input number_of_day_used: ");
            number_of_day_used = Convert.ToInt32(Console.ReadLine());
        }

        public void display()
        {
            Console.WriteLine("Name: " + name);
            Console.WriteLine("color: " + color);
            Console.WriteLine("nsx: " + nsx);
            Console.WriteLine("number_of_day_used: " + number_of_day_used);
        }


    }
}



Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2021-05-18 02:01:45



using System;
using room;
using System.Collections.Generic;
namespace bai1524
{
    class Tset
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Table tb1 = new Table();
            Table tb2 = new Table();

            tb1.input();
            tb2.input();
            Console.WriteLine("\n");
            tb1.display();
            tb2.display();
        }
    }
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-05-17 14:53:31



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

namespace Lesson2.room
{
    public class Table
 {
        public string Name { get; set; }
       public  string Color { get; set; }
        public string Manufacture { get; set; }

        int _UsedDay;
        public int UsedDay
        {
            get
            {
                return _UsedDay;
            }
            set
            {
                if (value <= 0)
                {
                    Console.WriteLine("Price khong duoc phep am!!!");
                }
                else
                {
                    _UsedDay = value;
                }
            }
        }

        public Table() {
        }

        public Table(string Name, string Color, string Manufacture, int UsedDay)
        {
            this.Name = Name;
            this.Color = Color;
            this.Manufacture = Manufacture;
            this.UsedDay = UsedDay;
        }

        public void Display()
        {
            Console.WriteLine("name:" + Name);
            Console.WriteLine("mau:" + Color);
            Console.WriteLine("nha sx:" + Manufacture);
            Console.WriteLine("ngay da su dung:" + UsedDay);
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten:" );
            Name = Console.ReadLine();
            Console.WriteLine("mau:" + Color);
            Color = Console.ReadLine();
            Console.WriteLine("nha sx:" + Manufacture);
            Manufacture = Console.ReadLine();
            Console.WriteLine("ngay da su dung:" + UsedDay);
            UsedDay = Int32.Parse(Console.ReadLine());
        }




    }
}