C#编写快递运费计算小程序

我们寄快递时,针对普通货物快递公司都会根据所寄货物的重量和目的地来计算运费,因此设计一个“快递运费计算程序”,使程序的操作人员(如收费员等)可以通过菜单提示完成对不同种类快递货物的数据录入、运费计算、信息查询及快递信息统计并能自动产生快递单号。

 

 程序中需设置的变量有:

当天能处理的最大快递总数(常量):MAX

当天已经处理的快递个数:number

操作密码(常变量):Password=1234

操作员密码:password

操作员密码输入次数:n

用户选择的菜单项目:choice

目的地区域 area

存放快递信息的数组 cargo[MAX+1]

数组下标变量:i

快递总重量:totalweight

快递总运费:totalcharge

临时变量:num,name

通对操作人员的身分进行验证(密码认证) 提示操作员输入密码,判断密码是否正确,如果不正确退出程序。如果正确,显示“欢迎进入” 

显示系统功能菜单: 在屏幕上显示如下信息:

根据快递收费标准,货物所在的区域不同,首重续重的金额是不同的,但运费都可用以下公式进行计算: 运费=(int)(重量+(double)首重单价/续重单价-0.01)*续重单价 所以我们可以根据区域和重量分段计算货物的运费,可采用if-else if语句或switch语句实现。 

本通过switch循环来进行主界面相关操作的执行

 本程序主要由三部分——主函数、相关数据与计算、主界面组成

主函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExpressFreightCalculation
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (Test.Pass())//调用登录方法
            {
                Console.WriteLine("密码正确,欢迎进入!");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("你是非法用户,程序退出!");
                Console.ReadKey();
                return;
            }
            while (true)
            {
                switch (Test.Menu())
                {
                    case 0:
                        Test.Exit();//返回操作系统(退出程序)
                        break;
                    case 1:
                        Test.Input();//录入数据
                        break;
                    case 2:
                        Test.Search();//查询快递
                        break;
                    case 3:
                        Test.Count();//计算当日总的重量和价格
                        break;
                    case 4:
                        Test.Output();//具体显示每个快递相关内容
                        break;
                }
            }
        }
    }
}

 快递字段与快递重量、价格:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ExpressFreightCalculation
{
    public class Cargo
    {
        public static int num = 0;
        long no;//编号
        string name;//姓名
        long phone;//电话号码
        string address;//地址
        int area;//地区编码
        string getname;//收件人名字
        long getphone;//收件人电话号码
        double weight;//重量
        double charge;//价格
        public long No
        {
            get
            {
                return no;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public long Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;
            }
        }
        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }
        public int Area
        {
            get
            {
                return area;
            }
            set
            {
                if (value >= 1 && value <= 5)
                    area = value;
                else
                    throw new Exception("不在正常区域范围(1-5)!");
            }
        }
        public string Getname
        {
            get
            {
                return getname;
            }
            set
            {
                getname = value;
            }
        }
        public long Getphone
        {
            get
            {
                return getphone;
            }
            set
            {
                getphone = value;
            }
        }
        public double Weight
        {
            get
            {
                return weight;
            }
            set
            {
                if (value > 0)
                    weight = value;
                else
                    throw new Exception("重量不能小于0!");
            }
        }
        public double Charge
        {
            get
            {
                return charge;
            }
        }
        public Cargo(string name, long phone, string address, int area, string getname, long
        getphone, double weight)
        {
            num++;
            this.no = MadeNo();
            this.name = name;
            this.phone = phone;
            this.address = address;
            this.Area = area;
            this.getname = getname;
            this.getphone = getphone;
            this.Weight = weight;
            this.charge = CalCharge();
        }
        private static long MadeNo()
        {
            long number;
            if (num < 10)
                number = long.Parse(DateTime.Now.ToString("yyyyMMdd") + "000" +
                num.ToString());
            else if (num < 100)
                number = long.Parse(DateTime.Now.ToString("yyyyMMdd") + "00" +
                num.ToString());
            else if (num < 1000)
                number = long.Parse(DateTime.Now.ToString("yyyyMMdd") + "0" +
                num.ToString());
            else
                number = long.Parse(DateTime.Now.ToString("yyyyMMdd") +
                num.ToString());
            return number;
        }
        public double CalCharge()
        {
            int first = 0;//首重
            int second = 0;//续重
            double charge;
            switch (area)
            {
                case 1:
                    first = 10;
                    second = 5;
                    break;
                case 2:
                    first = 12;
                    second = 8;
                    break;
                case 3:
                    first = 15;
                    second = 10;
                    break;
                case 4:
                    first = 18;
                    second = 12;
                    break;
                case 5:
                    first = 30;
                    second = 25;
                    break;
            }
            if (weight <= 1)
                charge = first;
            else
                charge = first + (int)(weight - 0.01) * second;
            return charge;
        }
        public void Show()
        {
            Console.WriteLine($"快递编号:{0}", no);
            Console.WriteLine($"寄件人姓名:{0}", name);
            Console.WriteLine($"寄件人电话:{0}", phone);
            Console.WriteLine($"收件人地址:{0}", address);
            Console.WriteLine($"收件人姓名:{0}", getname);
            Console.WriteLine($"收件人电话:{0}", getphone);
            Console.WriteLine($"快递重量:{0}", weight);
            Console.WriteLine($"快递运费:{0}", charge);
        }
    }
}

 程序主界面与相关方法执行:

using ExpressFreightCalculation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ExpressFreightCalculation
{
    class Test
    {
        const int MAX = 10000;
        static Cargo[] cargo = new Cargo[MAX];
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public static bool Pass()
        {
            const string PASSWORD = "aa1234";
            string password;
            for (int n = 0; n < 3; n++)
            {
                Console.WriteLine("请输入密码:");
                password = Console.ReadLine();
                if (password == PASSWORD)
                {
                    return true;
                }
                else
                {
                    if (n < 2)
                        Console.WriteLine("密码错误,请重新输入!");
                }
            }
            return false;
        }
        /// <summary>
        /// 进入主界面
        /// </summary>
        /// <returns></returns>
        public static int Menu()
        {
            int choice;
            do
            {
                Console.Clear();
                Console.WriteLine("\n\t快递运费计算程序");
                Console.WriteLine("1.录入数据并计算运费");
                Console.WriteLine("2.按姓名或编号查找快递信息");
                Console.WriteLine("3.统计并输出总重量和总运费");
                Console.WriteLine("4.显示今日快递清单");
                Console.WriteLine("0.退出程序");
                Console.Write("请输入你的选择(0-4):");
                choice = int.Parse(Console.ReadLine());
                if (choice < 0 || choice > 4)
                {
                    Console.WriteLine("你的选择超出范围,请重新选择!");
                    Console.ReadKey();
                }
            } while (choice < 0 || choice > 4);
            return choice;
        }
        public static void Exit()
        {
            Console.WriteLine("谢谢使用,再见!");
            Console.ReadKey();
            Environment.Exit(0);//返回操作系统
        }
        public static void Input()
        {
            for (int i = Cargo.num + 1; i <= MAX; i++)
            {
                Console.WriteLine("是否输入一个快递的信息(y/n):");
                char a = (char)Console.Read();
                Console.ReadLine();
                if (!(a == 'y' || a == 'Y'))
                    return;
                Console.WriteLine("请输入寄件人的姓名:");
                string name = Console.ReadLine();
                Console.WriteLine("请输入寄件人的电话号码:");
                long phone = long.Parse(Console.ReadLine());
                Console.WriteLine("请输入收件人的地址:");
                string address = Console.ReadLine();
                Console.WriteLine("请输入收件人的姓名:");
                string getname = Console.ReadLine();
                Console.WriteLine("请输入收件人的电话号码:");
                long getphone = long.Parse(Console.ReadLine());
                int area = Menua();
                Console.WriteLine("请输入快递的重量:");
                double weight = double.Parse(Console.ReadLine());
                try
                {
                    cargo[i] = new Cargo(name, phone, address, area, getname, getphone,
                    weight);
                    //Console.WriteLine("已添加一条数据,单号:{0},运费:{1}",cargo[i].No,cargo[i].Charge);
                    cargo[i].Show();
                    Console.WriteLine("现在已有{0}个快递!", Cargo.num);
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    i--;
                    Console.WriteLine(e.Message);
                }
            }
        }
        public static int Menua()
        {
            int area;
            do
            {
                Console.Clear();
                Console.WriteLine("请选择收件人所在的地区:");
                Console.WriteLine("1.广西、广东");
                Console.WriteLine("2.安微、湖南、江苏、浙江、上海、江西、贵州、湖北、山西、福建、河南、云南、重庆、四川");
                Console.WriteLine("3.北京、天津、陕西、河北、山东、海南");
                Console.WriteLine("4.青海、宁夏、甘肃、辽宁、吉林、黑龙江、内蒙古");
                Console.WriteLine("5.西藏、新疆");
                Console.Write("请输入你的选择(1-5):");
                area = int.Parse(Console.ReadLine());
                if (area < 1 || area > 5)
                {
                    Console.WriteLine("你的选择超出范围,请重新选择!");
                    Console.ReadKey();
                }
            } while (area < 1 || area > 5);
            return area;
        }
        public static void Output()
        {
            double totalweight = 0;
            double totalcharge = 0;
            Console.WriteLine("{0,12}{1,6}{2,6}{3,6}", "编号", "寄件人", "快递重量", "快递运费");
            for (int i = 1; i <= Cargo.num; i++)
            {
                totalweight += cargo[i].Weight;
                totalcharge += cargo[i].Charge;
                Console.WriteLine("{0,12}{1,9}{2,10}{3,10}", cargo[i].No,cargo[i].Name, cargo[i].Weight, cargo[i].Charge);
            }
            Console.WriteLine("{0,12}{1,19}{2,10}", "总计", totalweight, totalcharge);
            Console.ReadKey();
        }
        public static void Search()
        {
            bool flag = false;
            int n = 1;
            switch (Menus())
            {
                case 0:
                    break;
                case 1:
                    Console.WriteLine("请输入要查询的姓名:");
                    string name = Console.ReadLine();
                    flag = Search1(name, ref n);
                    break;
                case 2:
                    Console.WriteLine("请输入要查询的编号:");
                    long num = long.Parse(Console.ReadLine());
                    flag = Search2(num, ref n);
                    break;
                case 3:
                    Console.WriteLine("请选择收件人所在的地区:");
                    int area = Menua();
                    flag = Search3(area, ref n);
                    break;
            }
            if (flag == true)
            {
                Console.WriteLine("找到快递信息如下:");
                cargo[n].Show();
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("你查询的快递不存在!");
                Console.ReadKey();
            }
        }
        public static bool Search1(string name, ref int n)
        {
            for (int i = 1; i <= Cargo.num; i++)
            {
                if (cargo[i].Name == name)
                {
                    n = i;
                    return true;
                }
            }
            return false;
        }
        public static bool Search2(long no, ref int n)
        {
            for (int i = 1; i <= Cargo.num; i++)
            {
                if (cargo[i].No == no)
                {
                    n = i;
                    return true;
                }
            }
            return false;
        }
        public static bool Search3(int area, ref int n)
        {
            for (int i = 1; i <= Cargo.num; i++)
            {
                if (cargo[i].Area == area)
                {
                    n = i;
                    return true;
                }
            }
            return false;
        }
        public static int Menus()
        {
            int choice;
            do
            {
                Console.Clear();
                Console.WriteLine("\n\t信息查询模块");
                Console.WriteLine("1.按寄件姓名查询");
                Console.WriteLine("2.按编号查询");
                Console.WriteLine("3.按区域查询");
                Console.WriteLine("0.退出查询");
                Console.Write("请输入你的选择(0-3):");
                choice = int.Parse(Console.ReadLine());
                if (choice < 0 || choice > 3)
                {
                    Console.WriteLine("你的选择超出范围,请重新选择!");
                    Console.ReadKey();
                }
            } while (choice < 0 || choice > 3);
            return choice;
        }
        /// <summary>
        /// 快递数与总价格、重量
        /// </summary>
        public static void Count()
        {
            double totalweight = 0;
            double totalcharge = 0;
            for (int i = 1; i <= Cargo.num; i++)
            {
                totalweight += cargo[i].Weight;
                totalcharge += cargo[i].Charge;
            }
            Console.WriteLine("当前共有{0}个快递!", Cargo.num);
            Console.WriteLine("当前快递的总重量是:{0:0.00}", totalweight);
            Console.WriteLine("当前快递的总运费是:{0:0.00}", totalcharge);
            Console.ReadKey();
        }
    }
}

 

按姓名或编号查找快递 询问用户是按编号查找还是按姓名查找,根据用户的选择进行相应的处理 将用户输入的数据与cargo数组中的每一个元素的姓名或编号进行比较,如果相等表示找到快递,同时显示该快递的相关信息。如果数组中的所有元素都对比过后还找不到相等的数据,表示没有该快递,提示用户。 

统计总重量和总运费 

统计总重量和总运费 先将统计总重量的变量totalweight和统计总运费的变量totalcharge作清零处理。 利用循环操作,将当天所有快递的重量量加入到totalweight中,将所有快递的运费加入到totalcharge中。显示totalweight和totalcharge的值。

显示托运快递清单

按列表的形式显示所有快递的信息,并在最后一行显示重量和运费的合计数。     

 

后续进阶与展望:

由快递基类BaseCargo类派生出普通快递类Cargo类、易碎快递类BrokenCargo类和冷冻快递类

FreshCargo类 定义快递处理接口ICargoHandlr,包括快递运费计算方法CalCharge()和快递信息输出方法ShowMessage(),所有快递类均继承该接口

在Cargo类、BrokenCargo类和FrokenCargo类中定义各自不同的快递运费计算和显示快递信息的方法

定义Test类,在Test类中设置今天能处理的快递最大数量MAX并定义BaseCargo类的数组来保存快递对象的信息,在Test类中定义Pass()方法检验用户密码,定义Menue()方法显示功能菜单,定义Search()方法实现查找功能,定义Count()方法实现统计功能

注:

1.若在程序中添加头文件: #include "stdlib.h" 在程序中可使用system("cls");实现清屏

2.若程序中需要使用getchar()函数,则需添加以下头文件: #include "stdio.h"

3.若程序中需要使用setw()函数,则需添加以下头文件:      #include  "iomanip.h"

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值