Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C#
1. Create an interface Icar that has the following methods:
● Method “calculateTax()”, returns the type float: calculate the tax of the car.
● Method “calculatePrice()”, returns the type float: calculate the total cost of the car.
● Method “getInfor()”, returns the type void: display the information of the car.
2. Create a class Car inherit from interface Icar. [6]
Declare fields has access modifier as private use for properties: name, producer, year, seat and rootprice.
● Create two constructors and all method get/set for its attributes
● Implement method calculateTax() to calculate the Tax as follows:
○ If the car has under 7 seats then tax = RootPrice * 60%.
○ Else tax = RootPrice * 70%
● Implement method calculatePrice() to calculate TotalPrice as follows:
○ TotalPrice=RootPrice + Tax
● Implement method getInfor() to show the information as follows: .... car produced by ... in ... has ... seats with the total price is ....$. Variable includes :name of the car, the name of the manufacturer, year of production, seats and TotalPrice (For example: Ford car produced by Ford in 1997 has 4 seats with the total price is 20000 $).
- Declaring class LuxuryCar inherits from class Car and adding follow attrubtes :
private float specialRate;
● Create two constructors and all method get/set for its attributes
● Override method calculatePrice to calculate TotalPrice as follows :
TotalPrice = RootPrice + Tax + RootPrice * specialRate
● Overload method calculatePrice with 1 parameter named “transportCost” with type float. It calculates TotalCost as follows: TotalPrice = RootPrice + Tax + RootPrice * specialRate + transportCost.
- Create a class named Test.
● Declare and initialize 1 instance named “myLuxuryCar” of class LuxuryCar.
○ Input Name, Producer, Year, Seat, rootPrice from the keyboard (Catch exception when input value). Display the contents of this LuxuryCar.
○ Calculate TotalPrice in case transportCost is $ 20,000, and display it.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nam20021608 [community,AAHN-C2009G]](https://www.gravatar.com/avatar/0b1940debe745e5d5db19e96d6e57811.jpg?s=80&d=mm&r=g)
Nam20021608
2021-10-16 07:09:48
#Program.cs
using System;
using Examl.Interface;
using Examl.Model;
using Examl.Libary;
using System.Collections.Generic;
namespace Examl
{
class Program
{
public static List<iherit> CarList = new List<iherit>();
public static void Main(string[] args)
{
iherit c = new iherit();
c.Input();
CarList.Add(c);
c.getInfor();
LuxuryCar l = new LuxuryCar();
l.calculatePrice(20000);
Console.WriteLine("CalculatePrice :" + l.calculatePrice(20000));
}
}
}
#inherit.cs
using System;
using Examl.Interface;
using Examl.Libary;
namespace Examl.Model
{
class iherit : ICar
{
public String NameCar{get;set;}
public String Producer{get;set;}
public DateTime Year{get;set;}
public int Seat{get;set;}
public float RootPrice{get;set;}
public iherit(){}
public iherit(String name , String producer , String year , int seat , float price){
this.NameCar = name;
this.Producer= producer;
this.Year = Utility.ConvertStrignToDateTime(producer);
this.Seat = seat;
this.RootPrice = price;
}
public void Input(){
Console.WriteLine("Enter Name Car: ");
NameCar = Console.ReadLine();
Console.WriteLine("Enter Producer : ");
Producer = Console.ReadLine();
Console.WriteLine("Enter Year: ");
Year = Utility.ConvertStrignToDateTime();
Console.WriteLine("Enter Seat: ");
Seat = Utility.ReadInt();
Console.WriteLine("Enter Root Price: ");
RootPrice = Utility.ReadFloat();
}
public float calculateTax()
{
float tax;
if(Seat < 7){
tax = (float)(RootPrice * 0.6);
}else{
tax = (float)(RootPrice * 0.7);
}
return tax;
}
public virtual float calculatePrice()
{
float totalPrice = calculateTax() + RootPrice;
return totalPrice;
}
public virtual float calculatePrice(float transportCost)
{
return 0;
}
public void getInfor()
{
Console.WriteLine("{0} car produced by {1} in {2} has {3} seats with the total price is {4} $",NameCar , Producer , Year , Seat , calculatePrice());
}
}
}
#LuxuryCar.cs
using System;
using Examl.Interface;
using Examl.Libary;
namespace Examl.Model
{
class LuxuryCar : iherit
{
private float specialRate{get;set;}
public LuxuryCar(){}
public LuxuryCar(String name , String producer , String year , int seat , float price , float specialrate):base (name,producer,year,seat,price){
this.specialRate = specialrate;
}
public override float calculatePrice()
{
float totalprice = base.RootPrice + base.calculateTax() +(base.RootPrice * specialRate);
return totalprice;
}
public override float calculatePrice(float transportCost)
{
float totalprice2 = calculatePrice() + transportCost;
return totalprice2;
}
}
}
#Utility.cs
using System;
namespace Examl.Libary
{
class Utility{
public static float ReadFloat(){
float a;
while (true)
{
try
{
a = float.Parse(Console.ReadLine());
if(a < 0){
a = float.Parse(Console.ReadLine());
}
return a;
}
catch (System.Exception)
{
Console.WriteLine("Re-Enter !!!");
}
}
}
public static int ReadInt(){
int b ;
while (true)
{
try
{
b = int.Parse(Console.ReadLine());
return b;
}
catch (System.Exception)
{
Console.WriteLine("Re-Enter !!!");
}
}
}
public static DateTime ConvertStrignToDateTime()
{
String c;
while (true)
{
try
{
c = Console.ReadLine();
DateTime myDate = DateTime.ParseExact(c, "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture);
return myDate;
}
catch (System.Exception)
{
Console.WriteLine("nhap lai: ");
}
}
}
public static DateTime ConvertStrignToDateTime(string c)
{
c = Console.ReadLine();
DateTime myDate = DateTime.ParseExact(c, "yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture);
return myDate;
}
}
}
#ICar.cs
using System;
namespace Examl.Interface{
interface ICar
{
public float calculateTax();
public float calculatePrice();
public void getInfor();
}
}
![Hoàng Thiện Thanh [community,AAHN-C2009G]](https://www.gravatar.com/avatar/58e377dde293f6da38c0b5168578557a.jpg?s=80&d=mm&r=g)
Hoàng Thiện Thanh
2021-10-15 14:40:46
#Program.cs
using CarManager.Models;
using System;
using System.Collections.Generic;
namespace CarManager
{
public class Program
{
public static List<Car> CarList = new List<Car>();
public static void Main(string[] args)
{
Car c = new Car();
c.Input();
CarList.Add(c);
c.getInfor();
LuxuryCar l = new LuxuryCar();
l.calculatePrice(20000);
Console.WriteLine("CalculatePrice :" + l.calculatePrice(20000));
}
}
}
#ICar.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace CarManager.Interfaces
{
public interface ICar
{
float calculateTax();
float calculatePrice();
void getInfor();
}
}
#Car.cs
using CarManager.Interfaces;
using CarManager.Utils;
using System;
namespace CarManager.Models
{
public class Car : ICar
{
//name, producer, year, seat and rootprice
public string Name { get; set; }
public string Manufacturer { get; set; }
public int Year { get; set; }
public int Seat { get; set; }
public float RootPrice { get; set; }
public virtual void Input()
{
Console.WriteLine("Input Name: ");
Name = Console.ReadLine();
Console.WriteLine("Input Manufacturer: ");
Manufacturer = Utility.ReadLetters();
Console.WriteLine("Input Year: ");
Year = Utility.ReadInt();
Console.WriteLine("Input Seats: ");
Seat = Utility.ReadInt();
Console.WriteLine("Input Root Price: ");
RootPrice = Utility.ReadFloat();
}
public Car()
{
}
public Car(string Name, int Year, int Seat, float RootPrice)
{
this.Name = Name;
this.Year = Year;
this.Seat = Seat;
this.RootPrice = RootPrice;
}
public virtual float calculatePrice()
{
float Tax = calculateTax();
return RootPrice + Tax;
}
public virtual float calculateTax()
{
float Tax;
if (Seat < 7)
{
Tax = (float)(RootPrice * 0.6);
} else
{
Tax = (float)(RootPrice * 0.7);
}
return Tax;
}
public virtual void getInfor()
{
Console.WriteLine("The {0} car produced by {1} in {2}, it has {3} seats with the total price of {4}$", Name, Manufacturer, Year, Seat, calculatePrice());
}
}
}
#LuxuryCar.cs
using CarManager.Interfaces;
using CarManager.Utils;
using System;
using System.Collections.Generic;
using System.Text;
namespace CarManager.Models
{
public class LuxuryCar : Car, ICar
{
public float SpecialRate { get; set; }
public LuxuryCar() : base(){
}
public LuxuryCar(string Name, int Year, int Seat, float RootPrice, float SpecialRate) : base(Name, Year, Seat, RootPrice)
{
this.SpecialRate = SpecialRate;
}
public override float calculatePrice()
{
float Tax = calculateTax();
return RootPrice + Tax + RootPrice * SpecialRate;
}
public float calculatePrice(float TransportPrice)
{
float Tax = calculateTax();
return RootPrice + Tax + RootPrice * SpecialRate + TransportPrice;
}
public override void Input()
{
base.Input();
Console.WriteLine("Input Special Rate:");
SpecialRate = Utility.ReadFloat();
}
}
}
#Utility.cs
using System;
using System.Text.RegularExpressions;
namespace CarManager.Utils
{
public class Utility
{
public static int ReadInt()
{
string str = Console.ReadLine();
bool check()
{
try { int.Parse(str); } catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Cannot parse input like a floor numeric value");
str = Console.ReadLine();
}
return int.Parse(str);
}
public static float ReadFloat()
{
string str = Console.ReadLine();
bool check()
{
try { float.Parse(str); } catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Cannot parse input like a float numeric value");
str = Console.ReadLine();
}
return float.Parse(str);
}
public static DateTime ReadDate()
{
string str = Console.ReadLine();
DateTime date = new DateTime();
bool check()
{
int ERR = 0;
try { date = DateTime.Parse(str); } catch (Exception) { ERR++; }
try { if (ERR == 1) date = Convert.ToDateTime(str); } catch (Exception) { ERR++; }
if (ERR == 2)
{
return false;
}
return true;
}
while (!check())
{
Console.WriteLine("Cannot read DateTime from input");
str = Console.ReadLine();
}
return date;
}
public static string ReadLetters()
{
string str = Console.ReadLine();
while (!Regex.Match(str, "^([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)*$").Success)
{
Console.WriteLine("Input does not only contain letters");
str = Console.ReadLine();
}
return str;
}
}
}
![Nguyễn Việt Hoàng [community,AAHN-C2009G]](https://www.gravatar.com/avatar/bdbde8074d82148dcda6927ccb016466.jpg?s=80&d=mm&r=g)
Nguyễn Việt Hoàng
2021-10-14 16:42:29
#Car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss11.B2
{
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 Car()
{
}
public Car(string name, string producer, int year, int seat, float rootprice, float tax)
{
this.Name = name;
this.Producer = producer;
this.Year = year;
this.Seat = seat;
this.RootPrice = rootprice;
}
public float calculateTax()
{
float sum = 0;
foreach(Car c in Mains.CarList)
{
if(c.Seat < 7)
{
sum = (float)(c.RootPrice * 0.6);
}else
{
sum = (float)(c.RootPrice * 0.7);
}
}
return sum;
}
public virtual float calculatePrice()
{
float total = 0;
foreach(Car c in Mains.CarList)
{
total = RootPrice * calculateTax();
}
return total;
}
public void Input()
{
Console.WriteLine("Enter Name :");
Name = Console.ReadLine();
Console.WriteLine("Enter Produce :");
Producer = Console.ReadLine();
Console.WriteLine("Enter Year :");
Year = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Seat :");
Seat = int.Parse(Console.ReadLine());
Console.WriteLine("Enter RootPrice :");
RootPrice = float.Parse(Console.ReadLine());
}
public void getInfor()
{
Console.WriteLine("{0} car produced by {1} in {2} has {3} seats with the total price is {4}$", Name, Producer, Year, Seat, calculatePrice());
}
}
}
#Icar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss11.B2
{
interface Icar
{
public float calculateTax();
public float calculatePrice();
public void getInfor();
}
}
#LuxuryCar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss11.B2
{
class LuxuryCar : Car
{
public float SpecialRate { get; set; }
public LuxuryCar()
{
this.SpecialRate = 0.6F;
}
public LuxuryCar(string name, string producer, int year, int seat, float rootprice, float tax, float specialrate) : base(name, producer, year, seat, rootprice, tax)
{
this.SpecialRate = specialrate;
}
public override float calculatePrice()
{
float total = 0;
total = base.RootPrice + base.calculateTax() + base.RootPrice * this.SpecialRate;
return total;
}
public float calculatePrice(float transportCost)
{
float totalCost = 0;
totalCost = base.RootPrice + base.calculateTax() + base.RootPrice * this.SpecialRate + transportCost;
return totalCost;
}
}
}
#Mains.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss11.B2
{
class Mains
{
public static List<Car> CarList = new List<Car>();
public static void Main(string[] args)
{
Car c = new Car();
c.Input();
CarList.Add(c);
c.getInfor();
LuxuryCar l = new LuxuryCar();
l.calculatePrice(20000);
Console.WriteLine("CalculatePrice :" + l.calculatePrice(20000));
}
}
}