本代码主要用类的基本操作,涉及类的继承,构造,属性等
类的代码
car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 汽车租赁
{
class car //车主类,基类
{
private string name; //车名
private int xuhao; //车序号
private double price; //车一辆,一天的的价格
private int days = 0; //粗车天数,默认0
private int number = 0; //租车数量,默认为0
public string Name
{
get { return name; }
set { name = value; }
}
public int Xuhao
{
get { return xuhao; }
set { xuhao = value; }
}
public double Price
{
get { return price; }
set { price = value; }
}
public int Days
{
get { return days; }
set { days = value; }
}
public int Number
{
get { return number; }
set { number = value; }
}
public double zj()
{
return (this.price * this.number * this.days);
}
}
class GoodsCar : car //载货车,继承Car
{
public int weight;//每辆车每次载重
public int zz() //该种车总载重
{
return (this.Number * this.weight * this.Days);
}
}
class PeopleCar : car
{
public int people; //每辆车载人数
public int zr()
{
return (this.Number * this.people * this.Days);
}
}
class GoodsAndPeopleCar : car //皮卡车(载人拉货)
{
public int weight;
public int people;
public GoodsAndPeopleCar(int days, int number) //构造
{
this.Xuhao = 3;
this.Name = "皮卡雪6";
this.Price = 450;
this.weight = 2;
this.people = 4;
this.Days = days;
this.Number = number;
}
public void toString() //输出
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 {5}坐 {6}吨 一共{7}元 ",
this.Xuhao, this.Name,this.Number , this.Days, this.Price, this.people, this.weight, (this.Days * this.Price*this.Number ));
}
public int zz()
{
return (this.Number * this.weight * this.Days);
}
public int zr()
{
return (this.Number *this.people *this.Days );
}
}
class SongHuaJiangGoodsCar :GoodsCar //松花江货车
{
public SongHuaJiangGoodsCar(int days, int number)
{
this.Xuhao = 5;
this.Name = "松花江";
this.Price = 400;
this.weight = 4;
this.Days = days;
this.Number = number;
}
public void toString()
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 装载货物{5}吨 一共{6}元 ",
this.Xuhao, this.Name,this.Number , this.Days, this.Price, this.weight, (this.Days * this.Price*this.Number ));
}
}
class YiWeiKeGoodsCar : GoodsCar
{
public YiWeiKeGoodsCar(int days, int number)
{
this.Xuhao = 6;
this.Name = "依维柯";
this.Price = 1000;
this.weight = 20;
this.Days = days;
this.Number = number;
}
public void toString()
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 装载货物{5}吨 一共{6}元 ",
this.Xuhao, this.Name, this.Number, this.Days, this.Price, this.weight, (this.Days * this.Price * this.Number));
}
}
class AoDiA4PeopleCar : PeopleCar
{
public AoDiA4PeopleCar(int days, int number)
{
this.Xuhao = 1;
this.Name = "奥迪A4";
this.Price = 500;
this.people = 4;
this.Days = days;
this.Number = number;
}
public void toString()
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 装载货物{5}吨 一共{6}元 ",
this.Xuhao,this.Name, this.Number, this.Days, this.Price, this.people, (this.Days * this.Price * this.Number));
}
}
class MaZiDa6PeopleCar : PeopleCar
{
public MaZiDa6PeopleCar(int days, int number)
{
this.Xuhao = 2;
this.Name = "马自达6";
this.Price = 400;
this.people = 4;
this.Days = days;
this.Number = number;
}
public void toString()
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 装载货物{5}吨 一共{6}元 ",
this.Xuhao, this.Name, this.Number, this.Days, this.Price, this.people, (this.Days * this.Price * this.Number));
}
}
class JinLongPeopleCar : PeopleCar
{
public JinLongPeopleCar(int days, int number)
{
this.Xuhao = 4;
this.Name = "金 龙";
this.Price = 800;
this.people = 20;
this.Days = days;
this.Number = number;
}
public void toString()
{
Console.WriteLine("{0} {1} {2}辆 {3} 租金{4}元 装载货物{5}吨 一共{6}元 ",
this.Xuhao, this.Name, this.Number, this.Days, this.Price, this.people, (this.Days * this.Price * this.Number));
}
}
}
主函数Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 汽车租赁
{
class Program
{
static void Main(string[] args)
{
AoDiA4PeopleCar aodi = new AoDiA4PeopleCar(0,0);
MaZiDa6PeopleCar mazi = new MaZiDa6PeopleCar(0,0);
GoodsAndPeopleCar gope = new GoodsAndPeopleCar(0,0);
JinLongPeopleCar jinlong = new JinLongPeopleCar(0,0);
SongHuaJiangGoodsCar songh = new SongHuaJiangGoodsCar(0,0);
YiWeiKeGoodsCar yiwe = new YiWeiKeGoodsCar(0,0);
Console.WriteLine("\t\t欢迎使用安工大租车系统!\n");
Console.WriteLine("==============================================");
Console.WriteLine("您是否要租车:(1是0否)");
int a = Convert.ToInt32(Console .ReadLine ());
if (a == 1)
{
Console.WriteLine("您可租车的类型及其价目表:");
Console.WriteLine(" 序号\t 汽车名称\t 租金/天 \t 容量");
Console.WriteLine(" 1 \t 奥迪A4 \t 500元 \t 载人:4人");
Console.WriteLine(" 2 \t 马自达6 \t 400元 \t 载人:4人");
Console.WriteLine(" 3 \t 皮卡雪6 \t 450元 \t 载人:4人,载货:2吨");
Console.WriteLine(" 4 \t 金龙 \t 800元 \t 载人:20人");
Console.WriteLine(" 5 \t 松花江 \t 400元 \t 载货:4吨");
Console.WriteLine(" 6 \t 依维柯 \t 1000元 \t 载货:20吨");
Console.WriteLine("请输入您要几种类型的车:");
int m = Convert.ToInt32(Console .ReadLine ());
int[,] xingxi = new int[m,3]; //保存租车信息(序号、车辆数、租用天数)
for (int i = 0; i < m; i++)
{
Console.WriteLine("请输入第{0}俩车的信息:(序号、车辆数、租用天数)",i+1);
Console.Write("序号:");
xingxi[i, 0] = Convert.ToInt32(Console .ReadLine ());
Console.Write("车辆数:");
xingxi[i, 1] = Convert.ToInt32(Console.ReadLine());
Console.Write("租用天数:");
xingxi[i, 2] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < m; i++)
{
switch (xingxi[i, 0])
{
case 1:
aodi = new AoDiA4PeopleCar(xingxi[i,2],xingxi[i,1]);
break;
case 2:
mazi = new MaZiDa6PeopleCar (xingxi[i, 2], xingxi[i, 1]);
break;
case 3:
gope = new GoodsAndPeopleCar (xingxi[i, 2], xingxi[i, 1]);
break;
case 4:
jinlong = new JinLongPeopleCar (xingxi[i, 2], xingxi[i, 1]);
break;
case 5:
songh = new SongHuaJiangGoodsCar (xingxi[i, 2], xingxi[i, 1]);
break;
case 6:
yiwe = new YiWeiKeGoodsCar (xingxi[i, 2], xingxi[i, 1]);
break;
default:
Console.WriteLine("选车序号有误");
break;
}
}
Console.WriteLine("\n选车完毕,正在打印账单\n");
Console.WriteLine("\n\t\t账单为:");
Console.WriteLine("序号 名称 车辆数 天数 租金/天 容量/车 金额/元");
aodi.toString(); //调用输出函数输出类型
mazi.toString();
gope.toString();
jinlong.toString();
songh.toString();
yiwe.toString();
Console.WriteLine("\n一共可以坐{0}人,载重{1}吨",
(aodi.zr() + mazi.zr() + gope.zr() + jinlong.zr()), (gope.zz() + songh.zz() + yiwe.zz()));
Console.WriteLine("一共花费的钱为:{0}元 !", aodi.zj() + mazi.zj() + gope.zj() + jinlong.zj() + songh.zj() + yiwe.zj());
}
else
{
Console.WriteLine("欢迎光顾工大租车系统!欢迎下次再来!告辞!!!");
}
Console.Read();
}
}
}