using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 构造方法
{
class Ticket
{
public Ticket(int distance)
{
try
{
if (distance < 0)
{
throw new Exception("距离不能为负数");
}
else
{
this.distance = distance;
}
}
catch
{
}
}
int distance;
public int Distance
{
get { return distance; }
}
//decimal price;
public double Price
{
get
{
if (distance >= 300)
{
return 1.0 * distance * 0.8;
}
else if (distance > 200)
{
return 1.0 * distance * 0.9;
}
else if (distance >= 100)
{
return 1.0 * distance * 0.95;
}
else
{
return 1.0 * distance;
}
}
}
public void ShowPrice()
{
Console.WriteLine("距离为{0}的票,票价为{1}", distance, Price);
}
}
}
主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 构造方法
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入行驶的距离90");
Ticket jl = new Ticket(Convert.ToInt32(Console.ReadLine()));
jl.ShowPrice();
Console.ReadKey();
}
}
}
|