Net基础篇_学习笔记_第十一天_面向对象(练习)

本文介绍了一个使用C#编写的票务系统示例,该系统基于乘客行程距离计算票价,并展示了如何通过不同条件设置不同的折扣率。
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace juhnko
 8 {
 9     class Ticket
10     {
11         private double _distance;
12         public double Distance
13         {
14             get { return _distance; }
15         }
16         public Ticket(double distance)
17         {
18             if (distance<0)
19             {
20                 distance = 0;
21             }
22             this._distance=distance;
23         }
24         private double _price;
25         public double Price
26         {
27             get
28             {
29                 if (_distance > 0 && _distance <= 100)
30                 {
31                     return _distance * 1.0*1.00;
32                 }
33                 else if (_distance > 100 && _distance <= 200)
34                 {
35                     return _distance * 0.95 * 1.00;
36                 }
37                 else if (_distance > 200 && _distance <= 300)
38                 {
39                     return _distance * 0.9 * 1.00;
40                 }
41                 else
42                 {
43                     return _distance * 0.8 * 1.00;
44                 }
45             }
46         }
47         public void ShowTicket()
48         {
49             Console.WriteLine("{0}公里需要{1}元",this.Distance,this.Price);
50         }
51     }
52 }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace juhnko
{
    class Program
    {
        static void Main(string[] args)
        {
            Ticket myTicket = new Ticket(260);
            myTicket.ShowTicket();
            Console.ReadKey();
        }
    }
}

 

转载于:https://www.cnblogs.com/NBOWeb/p/7542407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值