IMG-LOGO
×

Giáo Trình Học

Khoá học lập trình C# (.NET Core)

Kiến Thức Căn Bản - Khai Báo Biến, Toán Tử, Mệnh Đề Điều Kiện, Lặp

[Video] Giới thiệu về ngôn ngữ lập trình C# & .NET Framework + Khai báo biến trong C# [Video] Hướng dẫn tìm hiểu biến + toán tử trong lập trình C# [Video] Hướng dẫn tìm hiểu mệnh đề điều kiện if - else - switch trong C# [Video] Tìm dãy số nguyên tố - Lập trình C# - Loop trong C# [Video] In dãy số chẵn & lẻ từ mảng số nguyên - Lập trình C# - Loop trong C#




Trang Chủ C Sharp Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C#

Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C#

by GokiSoft.com - 17:27 29/10/2021 1,365 Lượt Xem

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 $).                                                                               

 
  1. 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.

   
  1. 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.

Bình luận



Chia sẻ từ lớp học

Nam20021608 [community,AAHN-C2009G]

Ngày viết: 14:09 16/10/2021


#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();
    }
}


Phân Loại Bài Viết

Tài Liệu Tham Khảo

Đã sao chép!!!