By GokiSoft.com|
14:46 16/10/2021|
C Sharp
[Share Code] Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C# - AAHN C2009G2
Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C#
#Program.cs
using System;
namespace BT2490
{
class Program
{
static void Main(string[] args)
{
LuxuryCar myLuxuryCar = new LuxuryCar();
myLuxuryCar.Input();
myLuxuryCar.CalculatePrice(20000);
myLuxuryCar.GetInfo();
}
}
}
#Utility.cs
using System;
namespace BT2490
{
public class Utility
{
public static float ReadFloat()
{
float value;
while(true)
{
try
{
value = float.Parse(Console.ReadLine());
return value;
}
catch
{
Console.WriteLine("Nhap lai!!!");
}
}
}
public static int ReadInt()
{
int value;
while (true)
{
try
{
value = int.Parse(Console.ReadLine());
return value;
}
catch
{
Console.WriteLine("Nhap lai!!!");
}
}
}
}
}
#ICar.cs
using System;
namespace BT2490
{
public interface ICar
{
float CalculateTax();
float CalculatePrice();
void GetInfo();
}
}
#Car.cs
using System;
namespace BT2490
{
public class Car : ICar
{
public string Name { get; set; }
public string Producer { get; set; }
public int Year { get; set; }
public int Seat { get; set; }
public float RootPrice { get; set; }
public float TotalPrice { get; set; }
public Car()
{
}
public Car(string name, string producer, int year, int seat, float rootPrice)
{
Name = name;
Producer = producer;
Year = year;
Seat = seat;
RootPrice = rootPrice;
}
public virtual float CalculatePrice()
{
TotalPrice = RootPrice + CalculateTax();
return TotalPrice;
}
public float CalculateTax()
{
if(Seat < 7)
{
return RootPrice * 0.6F;
}
return RootPrice * 0.7F;
}
public void GetInfo()
{
Console.WriteLine("{0} car produced by {1} in {2} has {3} seats with the total price is {4}$", Name, Producer, Year, Seat, TotalPrice);
}
public virtual void Input()
{
Console.WriteLine("Nhap ten: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap nha sx: ");
Producer = Console.ReadLine();
Console.WriteLine("Nhap nam: ");
Year = Utility.ReadInt();
Console.WriteLine("Nhap cho ngoi: ");
Seat = Utility.ReadInt();
Console.WriteLine("Nhap gia goc: ");
RootPrice = Utility.ReadFloat();
}
}
}
#LuxuryCar.cs
using System;
namespace BT2490
{
public class LuxuryCar : Car
{
public float SpecialRate { get; set; }
public LuxuryCar()
{
}
public LuxuryCar(float specialRate, string name,
string producer, int year,
int seat, float rootPrice):base(name, producer, year, seat, rootPrice)
{
SpecialRate = specialRate;
}
public override float CalculatePrice()
{
TotalPrice = RootPrice + CalculateTax() + RootPrice * SpecialRate;
return TotalPrice;
}
public float CalculatePrice(float transportCost)
{
TotalPrice = CalculatePrice() + transportCost;
return TotalPrice;
}
public override void Input()
{
base.Input();
Console.WriteLine("Nhap gia dac biet: ");
SpecialRate = Utility.ReadFloat();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)