C#复习之停车场小项目

2021-12-04
3 min read
Featured Image

停车场练习

首先勾勒一下所有的类:

  • 汽车父类: 字段: 车牌号, 车辆位置, 品牌, 入场时期
    • 轿车子类: 字段: 排量
    • 客车子类: 字段: 最大载客
    • 卡车子类: 字段: 最大载重
  • 停车场类:
    • 字段: 最大容量
    • 方法: 最大容量驶入方法, 驶出方法, 查询位置方法, 综合信息方法

复习到的点:

public 和 private 的区别

  • 主要区别就是 public 可以在类的外部访问; private 只能在类内访问: 在别的类里创建实例后, 不能调用 private 方法和字段.

  • 枚举, 类, 接口的默认访问修饰符是 internal, 字段和方法的默认访问修饰符是 private.

子类的构造函数

  • 子类的无参构造函数可以不写 : base() .
  • 子类的有参构造函数需要写 :base() 并且括号内也把父类参数写全, 不用写变量类型.

String 的一些内容

  • 比较两个字符串: string1.Equals(string2).
    • 但是用之前, 好像需要判断 string1 的类型, 起码不能为空.
  • 书写含有多个变量的字符串: string = String.Fomat("{0} is looking at {1}", a,b).

项目代码

using System;

namespace ParkingSystem
{
    // 命名空间内, 默认的枚举,类,接口的访问修饰符都是 internal (同一命名空间内)
    enum Brand
    {
        Volkswagen,
        Meredece,
        BMW
    }
    // 父类
    abstract class Vehicle
    {
        // 类内的字段和属性默认的访问修饰符是 private (只有类内可以访问)
        public string numberPlate { get; set; }
        public double location { get; set; }
        public Brand brand { get; set; }
        public DateTime entryTime { get; set; }
        public Vehicle() { }
        public Vehicle(string num, double price, Brand brand, DateTime entry)
        {
            this.numberPlate = num;
            this.location = price;
            this.brand = brand;
            this.entryTime = entry;
        }
    }
    // 子类
    class SedenVehicle : Vehicle
    {
        public double gasCost { get; set; }
        // 无参构造函数可以不写父类 : base
        public SedenVehicle() : base() { }
        public SedenVehicle(double gasCost, string num, double price, Brand brand, DateTime entry)
            : base(num, price, brand, entry)
        {
            this.gasCost = gasCost;

        }
    }
    class BusVehicle : Vehicle
    {
        public int maxVolume { get; set; }
        public BusVehicle() : base() { }
        public BusVehicle(int maxVolume, string num, double price, Brand brand, DateTime entry)
            : base(num, price, brand, entry)
        {
            this.maxVolume = maxVolume;
        }

    }
    class TruckVehicle : Vehicle
    {
        public double maxLoad { get; set; }
        public TruckVehicle() : base() { }
        public TruckVehicle(int maxLoad, string num, double price, Brand brand, DateTime entry)
            : base(num, price, brand, entry)
        {
            this.maxLoad = maxLoad;
        }
    }
    class CarPark
    {
        // 默认 private 字段
        Vehicle[] vehiclesArray = new Vehicle[10];
        // 通过公共方法访问私有字段: 设置数组长度, 确定停车场最大容量
        public void setMax(int num)
        {
            vehiclesArray = new Vehicle[num];
            Console.WriteLine( "The max volume of the carpark is {0}...", vehiclesArray.Length);
        }
        // 默认私有字段: 停车场内的车辆总数
        int count = 0;
        // 车辆进场方法
        public string Inpark(Vehicle v)
        {
            string info = "Sorry, the park is full.";
            // 遍历数组寻找车位.
            for (int i = 0; i < vehiclesArray.Length; i++)
            {
                // 数组默认值是 null, 如果是 null 就可以停入
                if (vehiclesArray[i] == null)
                {
                    count++; // 总数加 1
                    this.vehiclesArray[i] = v; //数组对应停车
                    v.entryTime = DateTime.Now; // 记录驶入时间
                    v.location = i;
                    //设置停车信息
                    info = String.Format("You vehicle rego is {0}, location is {1}, and the entry time is {2}. ", v.numberPlate, i, v.entryTime);
                    break;
                }

            }
            Console.WriteLine(info);
            return info;
        }
        // 驶出停车场
        public string Outpark(Vehicle v)
        {
            string info = "";
            // 遍历数组寻找车位.
            for (int i = 0; i < vehiclesArray.Length; i++)
            {
                // 数组默认值是 null, 如果是 null 就可以停入
                if (vehiclesArray[i] == v)
                {
                    count--; // 总数减 1
                    this.vehiclesArray[i] = null; // 清除数组中的车辆
                    double cost = DateTime.Now.Ticks - v.entryTime.Ticks; // 记录驶出的时间
                    //设置停车信息
                    info = String.Format("You vehicle rego is {0}, the exit time is {1} and the cost is ${2}. ", v.numberPlate, v.entryTime,cost);
                    break;
                }

            }
            Console.WriteLine(info);
            return info;
        }
        // 得到停车场内车辆位置信息
        public Vehicle GetVehicle(string no)
        {
            foreach (Vehicle item in vehiclesArray)
            {
                if (item.numberPlate == no) // ( vehicle.numberPlate.Equals(no) )
                {
                    Console.WriteLine("Your vehicle is at {0}.", item.location);
                    return item;
                }
            }
            Console.WriteLine("No match in the carpark.");
            return null;
        }
        // 得到停车场内综合信息
        void Getinfo(Vehicle v)
        {

            // 各种车辆的 数量
        }


    }
    class Program
    {
        static void Main(string[] args)
        {
            SedenVehicle sedan1 = new SedenVehicle(10.00, "JLN881", 3, Brand.BMW, DateTime.Now);
            BusVehicle bus1 = new BusVehicle(22, "ANZ222", 2, Brand.Meredece, DateTime.Now);
            CarPark uniPark = new CarPark();
            uniPark.setMax(30);
            uniPark.Inpark(sedan1);
            uniPark.Inpark(bus1);
            uniPark.GetVehicle("JLN881");

        }
    }
}

Avatar
Aaron Fan My research interests include machine learning, signal processing, web development and robotics.