namespace 票价问题
{
public class Ticket
{
public Ticket(int distance) //构造一个Ticket的方法,方法里有一个int类型的变量distance
{
this.distance = distance;
if (distance >= 300)
{
price = distance * 0.8m;
}
else if (distance >= 200 && distance < 300)
{
price = distance * 0.9m;
}
else if (distance >= 100 && distance < 200)
{
price = distance * 0.95m;
}
else if (distance >= 0 && distance < 100)
{
price = distance;
}
else
{// 当输入的数据小于零的时候,出现了异常
throw new Exception("出现异常的代码了,这里的距离不允许是负数...");
}
}
int distance;
public int Distance
{
get { return this.distance; }
}
decimal price;
public decimal Price
{
get { return this.price; }
}
public void ShowTicket()
{
Console.WriteLine("这张票的距离为{0},折后价格为{1}", distance, this.price);
}
}
}
|