Example using Interfaces In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IDemo
{
using System;
// Property declaration
interface IVechile
{
String Brand {
get;
set;
}
String Color
{
get;
set;
}
int ModalYear
{
get;
set;
}
}
class Car: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for class
public void CarInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Car Brand is :{0}", Brand);
Console.WriteLine("\nSelected Car color :{0}", Color);
Console.WriteLine("\nModeling Year of Car :{0}", ModalYear);
}
}
class Bike: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for The Bike Class
public void BikeInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Bike Brand is :{0}", Brand);
Console.WriteLine("\nSelected Bike color :{0}", Color);
Console.WriteLine("\nModeling Year of Bike :{0}", ModalYear);
}
}
class Truck: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for the Truck Class
public void TruckInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Truck Brand is :{0}", Brand);
Console.WriteLine("\nSelected Truck color :{0}", Color);
Console.WriteLine("\nModeling Year of Truck :{0}", ModalYear);
}
}
class MainInterface
{
public static void Main(string[] args)
{
Console.WriteLine("Vechiles Types :\n \n 1.Car \n 2.Bike \n 3.Truck\n");
Console.WriteLine("\nPlease Select Vechile Type { Ex:1,2,3 }");
string str = Console.ReadLine();
switch (str) //Creating Switch case for option Selections
{
case "1":
Car c1 = new Car();
Console.WriteLine("Enter car Brand");
c1.Brand = Console.ReadLine();
Console.WriteLine("Enter Car Color");
c1.Color = Console.ReadLine();
Console.WriteLine("Enter Car Modal year");
c1.ModalYear = int.Parse(Console.ReadLine());
c1.CarInfo();
break;
case "2":
Bike b2 = new Bike();
Console.WriteLine("Enter Bike Brand");
b2.Brand = Console.ReadLine();
Console.WriteLine("Enter Bike Color");
b2.Color = Console.ReadLine();
Console.WriteLine("Enter Bike Modal year");
b2.ModalYear = int.Parse(Console.ReadLine());
b2.BikeInfo();
break;
case "3":
Truck t1 = new Truck();
Console.WriteLine("Enter Truck Brand");
t1.Brand = Console.ReadLine();
Console.WriteLine("Enter Truck Color");
t1.Color = Console.ReadLine();
Console.WriteLine("Enter Truck Modal year");
t1.ModalYear = int.Parse(Console.ReadLine());
t1.TruckInfo();
break;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IDemo
{
using System;
// Property declaration
interface IVechile
{
String Brand {
get;
set;
}
String Color
{
get;
set;
}
int ModalYear
{
get;
set;
}
}
class Car: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for class
public void CarInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Car Brand is :{0}", Brand);
Console.WriteLine("\nSelected Car color :{0}", Color);
Console.WriteLine("\nModeling Year of Car :{0}", ModalYear);
}
}
class Bike: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for The Bike Class
public void BikeInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Bike Brand is :{0}", Brand);
Console.WriteLine("\nSelected Bike color :{0}", Color);
Console.WriteLine("\nModeling Year of Bike :{0}", ModalYear);
}
}
class Truck: IVechile
{
private string brand;
private string color;
private int modalYear;
// read-write instance property
public string Brand
{
get
{
return brand;
}
set
{
brand = value;
}
}
// read-write instance property
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
// read-write instance property
public int ModalYear
{
get
{
return modalYear;
}
set
{
modalYear = value;
}
}
//constructor for the Truck Class
public void TruckInfo()
{
Console.WriteLine("");
Console.WriteLine("You have selected following prorperties :");
Console.WriteLine("_________________________________________");
Console.WriteLine("\nSelected Truck Brand is :{0}", Brand);
Console.WriteLine("\nSelected Truck color :{0}", Color);
Console.WriteLine("\nModeling Year of Truck :{0}", ModalYear);
}
}
class MainInterface
{
public static void Main(string[] args)
{
Console.WriteLine("Vechiles Types :\n \n 1.Car \n 2.Bike \n 3.Truck\n");
Console.WriteLine("\nPlease Select Vechile Type { Ex:1,2,3 }");
string str = Console.ReadLine();
switch (str) //Creating Switch case for option Selections
{
case "1":
Car c1 = new Car();
Console.WriteLine("Enter car Brand");
c1.Brand = Console.ReadLine();
Console.WriteLine("Enter Car Color");
c1.Color = Console.ReadLine();
Console.WriteLine("Enter Car Modal year");
c1.ModalYear = int.Parse(Console.ReadLine());
c1.CarInfo();
break;
case "2":
Bike b2 = new Bike();
Console.WriteLine("Enter Bike Brand");
b2.Brand = Console.ReadLine();
Console.WriteLine("Enter Bike Color");
b2.Color = Console.ReadLine();
Console.WriteLine("Enter Bike Modal year");
b2.ModalYear = int.Parse(Console.ReadLine());
b2.BikeInfo();
break;
case "3":
Truck t1 = new Truck();
Console.WriteLine("Enter Truck Brand");
t1.Brand = Console.ReadLine();
Console.WriteLine("Enter Truck Color");
t1.Color = Console.ReadLine();
Console.WriteLine("Enter Truck Modal year");
t1.ModalYear = int.Parse(Console.ReadLine());
t1.TruckInfo();
break;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
Console.Read();
}
}
}
0 comments:
Post a Comment