这个小项目是我用C#写的第一个项目,写的不足请大家谅解。
大佬勿喷!!!
也可以多多提建议!
整个小项目都是我一个人写的,由于是新手,写的很多地方可能不太标准,不过基本功能都可以实现的,由于是第一版以后有机会会继续优化的。
下面我也会把我写代码的逻辑简单说一下的,也会把我每个封装的模块功能说说的。
项目功能流程图
项目需求说明:
1)登录
用户输入账号(邮箱)、密码和验证码实现在线缴费平台登录功能。
2)缴水费
根据用户住宅类型,自动匹配水费价格,完成水费计算,实现在线水费缴费功能。
3)缴电费
根据用户已用电量,匹配阶梯电价,用户输入需要购买的电量后,使用阶梯电价计算应缴电费金额。
4)缴物业费
(1)物业费缴费方式分为季度缴、半年缴和年度缴。缴物业费时,根据缴费方式与房屋面积计算应缴物业费总金额。
(2)物业费缴费成功后,输出缴费凭证。
(3)统计该小区住户物业费缴费金额。
5)缴手机话费
(1)根据用户输入的手机号码和缴费金额进行话费充值,并对传入金额进行正确性校验,充值成功后给出正确提示。
(2)充值话费后,系统会将增加的积分充值到个人账户。系统规则:10元对应1积分。
效果展示:
登录成功展示
账号错误展示
缴水费功能展示
缴电费功能展示
缴物业费功能展示
缴手机话费功能展示
效果就先展示这么多吧,这些只是其中的一部分效果哦,感兴趣的话可以自己去跑一边代码看看效果哦!!!也可以去添加一些有意思功能,或者去优化代码,本人很乐意交流,虽然是新手。
接下来就开始写代码吧!!
首先创建两个类
Owner(业主类)
Owner | |
字段名 | 字段类型 |
Account(账号) | string |
Password(密码) | string |
PhoneNumber(手机号码) | string |
UserName(用户姓名) | string |
RoomNo(房号) | string |
Integral(积分) | int |
PhoneBalances(话费余额) | double |
代码展示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiaoXiangMu1
{
/// <summary>
/// 业主类
/// </summary>
class Owner
{
// 账号
private string _account;
public string Account
{
get
{
return this._account;
}
set
{
this._account = value;
}
}
// 密码
private string _password;
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
// 手机号码
private string _phoneNumber;
public string PhoneNumber
{
get
{
return this._phoneNumber;
}
set
{
this._phoneNumber = value;
}
}
// 业主姓名
private string _userName;
public string UserName
{
get
{
return this._userName;
}
set
{
this._userName = value;
}
}
// 房号
private string _roomNo;
public string RoomNo
{
get
{
return this._roomNo;
}
set
{
this._roomNo = value;
}
}
// 积分
private int _integral;
public int Integral
{
get
{
return this._integral;
}
set
{
this._integral = value;
}
}
// 话费余额
private double _phoneBalances;
public double PhoneBalances
{
get
{
return this._phoneBalances;
}
set
{
this._phoneBalances = value;
}
}
//已用电量
private int _usedElectricity;
public int UsedElectricity
{
get
{
return this._usedElectricity;
}
set
{
this._usedElectricity = value;
}
}
//剩余电量
private int _surplusElectrictiy;
public int SurplusElectrictiy
{
get
{
return this._surplusElectrictiy;
}
set
{
this._surplusElectrictiy = value;
}
}
}
}
Payment(公共缴费类)
Payment | |
字段名 | 字段类型 |
Account(账号) | string |
PaymentCategory(缴费类别) | int |
Price(金额) | double |
代码展示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiaoXiangMu1
{
/// <summary>
/// 公共缴费类
/// </summary>
class Payment
{
// 账号
private string _account;
public string Account
{
get
{
return this._account;
}
set
{
this._account = value;
}
}
// 缴费类别
private string _paymentCategory;
public string PaymentCategory
{
get
{
return this._paymentCategory;
}
set
{
this._paymentCategory = value;
}
}
//金额
private double _price;
public double Price
{
get
{
return this._price;
}
set
{
this._price = value;
}
}
}
}
接下来就是主程序喽!
我的设计思想分为各个模块,然后由大化小,再分成子模块。
我写代码是由大到小的思想写的,不过这里为了更好的理解我就由小到大展示吧
1.缴水费模块
缴水费方法我主要用了switch跟if进行简单的判断
/// <summary>
/// 缴水费方法
/// </summary>
public static void WaterRate(Owner yzdx, Payment ggjfdx)
{
ggjfdx.PaymentCategory = "水费"; //定义缴费类别
Console.WriteLine("=============================================");
Console.WriteLine("|| 欢迎进入水费管理界面! ||");
Console.WriteLine("=============================================");
Console.WriteLine();
Console.WriteLine("=======================水费类型=======================");
Console.WriteLine("|| 1.居民用水,2.行政用水,3.工商用水,4.餐饮用水 ||");
Console.WriteLine("======================================================");
Console.WriteLine("请输入水费类型:");
int waterType = int.Parse(Console.ReadLine()); //水费类型
Console.WriteLine("请输入用水量(立方米):");
int waterConsumption = int.Parse(Console.ReadLine()); //用水量
const double RESIDENT = 2.8; //居民用水
const double ADMINISTRATIVE = 3.9; //行政用水
const double INDUSTRY_AND_COMMERCE = 4.1; //工商用水
const double CATERING = 4.6; //餐饮用水
double sum = 0;
string jiaoFeiLeiXing = ""; //缴水费类型
switch (waterType)
{
case 1:
double residentSum = waterConsumption * RESIDENT; //居民水费
sum = residentSum;
jiaoFeiLeiXing = "居民用水";
break;
case 2:
double administrativeSum = waterConsumption * ADMINISTRATIVE; //行政水费
sum = administrativeSum;
jiaoFeiLeiXing = "行政用水";
break;
case 3:
double industry_and_commerceSum = waterConsumption * INDUSTRY_AND_COMMERCE; //工商水费
sum = industry_and_commerceSum;
jiaoFeiLeiXing = "工商用水";
break;
case 4:
double cateringSum = waterConsumption * CATERING; //餐饮水费
sum = cateringSum;
jiaoFeiLeiXing = "餐饮用水";
break;
default:
Console.WriteLine("没有该选项!");
break;
}
Console.WriteLine("应缴水费金额:{0,8:C2}元。", sum);
Console.WriteLine("是否进行缴费:Y:是,N:否?");
string isJiaoFei = Console.ReadLine(); //是否继续缴费
if (isJiaoFei == "Y") //判断是否继续缴费
{
Console.WriteLine($"水费缴费成功,业主姓名:{yzdx.UserName},缴费类型:{jiaoFeiLeiXing},缴费金额:{sum,8:C2}元。");
}
else
{
Console.WriteLine("放弃缴费!");
}
}
2.缴电费模块
缴电费方法我主要用了多重if进行逻辑判断
/// <summary>
/// 缴电费方法
/// </summary>
public static void ElectricCharge(Owner yzdx, Payment ggjfdx)
{
ggjfdx.PaymentCategory = "电费"; //定义缴费类别
Console.WriteLine("==============================================");
Console.WriteLine("|| 欢迎进入电费管理界面! ||");
Console.WriteLine("==============================================");
Console.WriteLine();
Console.WriteLine("=======================阶梯电价=======================");
Console.WriteLine("|| 第一阶梯0.56元,2160度及以下。 ||");
Console.WriteLine("|| 第二阶梯0.61元,2160-3120度。 ||");
Console.WriteLine("|| 第三阶梯0.86元,超过3120度。 ||");
Console.WriteLine("======================================================");
const double ONE = 0.56; //第一阶梯价钱
const double TWO = 0.61; //第二阶梯价钱
const double THREE = 0.86; //第三阶梯价钱
Console.WriteLine($"业主姓名:{yzdx.UserName},已用电量:{yzdx.UsedElectricity}度,剩余电量:{yzdx.SurplusElectrictiy}度");
Console.Write("请输入要购买的电量(单位:度):");
int purchase = int.Parse(Console.ReadLine()); //获取购买电量
double sum = 0; //应缴电费
//判断各个阶段应缴的电费
if (yzdx.UsedElectricity < 2160)
{
sum = ONE * purchase; //计算第一阶段应缴电费价钱
}
else if (yzdx.UsedElectricity >= 2160 && yzdx.UsedElectricity <= 3120)
{
sum = TWO * purchase; //计算第二阶段应缴电费价钱
}
else if (yzdx.UsedElectricity > 3120)
{
sum = THREE * purchase; //计算第三阶段应缴电费价钱
}
Console.WriteLine($"应缴电费:{sum,10:C2}元。");
Console.WriteLine("是否进行购买:Y:是,N:否?");
string isGouMai = Console.ReadLine(); //是否继续购买
if (isGouMai == "Y") //判断是否继续购买
{
Console.WriteLine($"购买成功,业主姓名:{yzdx.UserName},购买电量:{purchase}度,金额:{sum,10:C2}元,剩余电量:{yzdx.SurplusElectrictiy + purchase}度");
}
else
{
Console.WriteLine("放弃购买!");
}
}
3.缴物业费模块
缴物业费模块就比前两个模块稍微复杂点,我在里面又建了三个子模块
(1)物业缴费模块
物业缴费模块我主要用的是switch语句进行逻辑判断,匹配对应值
/// <summary>
/// 物业缴费方法
/// </summary>
public static void PropertyPayment(Owner yzdx, Payment ggjfdx)
{
Console.WriteLine("==========物业缴费方式==========");
Console.WriteLine("|| 1.季节缴:折扣100% ||");
Console.WriteLine("|| 2.半年缴:折扣90% ||");
Console.WriteLine("|| 3.年度缴:折扣80% ||");
Console.WriteLine("================================");
Console.WriteLine("请输入缴费方式:");
int paymentMethod = int.Parse(Console.ReadLine()); //缴费方式
Console.WriteLine("请输入房屋面积(平方米):");
int area = int.Parse(Console.ReadLine()); //房屋面积
const int PROPERTY_FEE = 2; //物业费单价
double propertyFeeSum = 0; //物业费
string type = ""; //缴费类型
switch (paymentMethod)
{
case 1:
type = "季度";
propertyFeeSum = area * PROPERTY_FEE * 1 * 3; //一季度3个月
break;
case 2:
type = "半年";
propertyFeeSum = area * PROPERTY_FEE * 0.9 * 6; //半年6个月
break;
case 3:
type = "整年";
propertyFeeSum = area * PROPERTY_FEE * 0.8 * 12; //一年12个月
break;
default:
Console.WriteLine("没有该选项!");
break;
}
Console.WriteLine($"你需要缴纳的物业费用:{propertyFeeSum}元。");
Console.WriteLine("是否确认缴费:Y:是,N:否?");
string isPayment = Console.ReadLine(); //是否确认缴费
if (isPayment == "Y") //判断是否确认缴费
{
Console.WriteLine($"缴费成功,姓名:{yzdx.UserName},房号:{yzdx.RoomNo},缴费方式:{type},缴费金额:{propertyFeeSum}元。");
}
else
{
Console.WriteLine("放弃缴费!");
}
}
(2)打印凭证模块
这里就只是单纯的输出跟调用对象属性来展示
/// <summary>
/// 打印凭证方法
/// </summary>
public static void PrintCertificate(Owner yzdx, Payment ggjfdx)
{
double discount = 8.6; //定义折扣
Console.WriteLine("========物业费缴费凭证========");
Console.WriteLine();
Console.WriteLine("==============================");
Console.WriteLine($"|| 账号:{yzdx.Account,18}||");
Console.WriteLine($"|| 姓名:{yzdx.UserName,16}||");
Console.WriteLine($"|| 房号:{yzdx.RoomNo,18}||");
Console.WriteLine($"|| 类别:{ggjfdx.PaymentCategory,15}||");
Console.WriteLine($"|| 折扣:{discount,18:F1}||");
Console.WriteLine($"|| 金额:{ggjfdx.Price,15:C2}元||");
Console.WriteLine("==============================");
}
(3)物业费统计模块
这里主要用for循环进行一次次的输出对象赋的初始值内容
/// <summary>
/// 物业费统计方法
/// </summary>
public static void Statistics(Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("******物业费统计平台******");
Console.WriteLine("物业缴费明细:");
for (int i = 0; i < ownerArr.Length; i++)
{
Console.WriteLine($"房号:{ownerArr[i].RoomNo},业主姓名:{ownerArr[i].UserName},缴费金额:{paymentArr[i].Price}元。");
}
}
(4)缴物业费方法
这里主要用到方法调用跟switch进行简单逻辑判断,寻找匹配的内容
调用的方法正是之前上面写到的物业缴费模块、打印凭证模块、物业费统计模块
/// <summary>
/// 缴物业费方法
/// </summary>
public static void PropertyFee(Owner yzdx, Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
ggjfdx.PaymentCategory = "物业费"; //定义缴费类别
Console.WriteLine("==============================================");
Console.WriteLine("|| 欢迎进入物业费管理界面! ||");
Console.WriteLine("==============================================");
Console.WriteLine();
Console.WriteLine("===================物业缴费===================");
Console.WriteLine("|| 1.物业缴费,2.打印凭证,3.缴费统计 ||");
Console.WriteLine("==============================================");
Console.WriteLine("请输入操作类型:");
int operationType = int.Parse(Console.ReadLine()); //操作类型
switch (operationType)
{
case 1:
//调用物业缴费方法
PropertyPayment(yzdx, ggjfdx);
break;
case 2:
//调用打印凭证方法
PrintCertificate(yzdx, ggjfdx);
break;
case 3:
//调用物业费统计方法
Statistics(ownerArr, paymentArr);
break;
default:
Console.WriteLine("没有该选项!");
break;
}
}
4.缴手机话费模块
缴手机话费模块我也给他分了两个子模块
(1)话费充值模块
这里主要用的是for循环跟if判断,因为里面需要查看输入的手机号是否为真,并要可以不停输入
/// <summary>
/// 话费充值方法
/// </summary>
public static void PrepaidRefill(Owner yzdx, Payment ggjfdx,Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("============话费充值============");
Console.WriteLine("|| ||");
Console.WriteLine("|| Y.继续充值 ||");
Console.WriteLine("|| N.退出充值 ||");
Console.WriteLine("|| ||");
Console.WriteLine("================================");
bool isNo = true; //循环变量为真
bool isN = true; //用来记录判断手机号是否存在的容器
Owner dx = yzdx; //用来存该用户对象
while (isNo)
{
Console.WriteLine("请输入手机号码:");
string number = Console.ReadLine(); //手机号码
Console.WriteLine("请输入充值金额:");
int money = int.Parse(Console.ReadLine()); //充值金额
for (int i = 0; i < ownerArr.Length; i++)
{
if (ownerArr[i].PhoneNumber.Equals(number))
{
dx = ownerArr[i];
isN = true;
break;
}
isN = false;
}
if (isN == false)
{
Console.WriteLine("未找到该手机号码,无法进行充值!");
continue;
}
Console.WriteLine("充值成功!");
Console.WriteLine("充值前:");
Console.WriteLine($"姓名:{dx.UserName},手机号码:{number},余额:{dx.PhoneBalances + money}元,积分:{dx.Integral + (money / 10)}");
Console.WriteLine("充值后:");
Console.WriteLine($"姓名:{dx.UserName},手机号码:{number},余额:{dx.PhoneBalances + money}元,充值金额:{money}元,积分:{dx.Integral + (money / 10)},积分增加:{money / 10}");
Console.WriteLine("是否继续充值:Y:是,N:否?");
string isRecharge = Console.ReadLine(); //是否继续充值
if (isRecharge == "Y")
{
isNo = true;
}
else
{
isNo = false;
Console.WriteLine("退出充值!");
}
}
}
(2)积分发放模块
这里主要就是输出跟简单运算符运用
/// <summary>
/// 积分发放方法
/// </summary>
public static void PointRelease(Owner yzdx)
{
Console.WriteLine($"姓名:{yzdx.UserName},话费余额:{yzdx.PhoneBalances}元,积分:{yzdx.Integral}");
Console.WriteLine("请输入要充值的话费:");
int recharge = int.Parse(Console.ReadLine()); //充值
Console.WriteLine($"充值成功,姓名:{yzdx.UserName},话费余额:{yzdx.PhoneBalances + recharge}元,积分:{yzdx.Integral + recharge / 10},积分增加:{recharge / 10}");
}
(3)缴手机话费模块
这里主要用的switch判断跟方法调用,调用的是上面话费充值模块、积分发放模块
/// <summary>
/// 缴手机话费方法
/// </summary>
public static void PhoneBill(Owner yzdx, Payment ggjfdx,Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("================================");
Console.WriteLine("||欢迎进入缴手机话费管理界面!||");
Console.WriteLine("================================");
Console.WriteLine();
Console.WriteLine("================================");
Console.WriteLine("|| 1.话费充值 ||");
Console.WriteLine("|| 2.积分发放 ||");
Console.WriteLine("================================");
Console.Write("请选择服务:");
string selectService = Console.ReadLine(); //选择服务
switch (selectService)
{
case "1":
//调用话费充值方法
PrepaidRefill(yzdx, ggjfdx,ownerArr, paymentArr);
break;
case "2":
//调用积分发放方法
PointRelease(yzdx);
break;
default:
Console.WriteLine("没有该选项服务!");
break;
}
}
5.登录模块
这里我也分成两个子模块
(1)显示选项模块
这里就是纯输出
/// <summary>
/// 显示选项方法
/// </summary>
public static void ShowOption()
{
Console.WriteLine("================在线缴费平台==================");
Console.WriteLine("|| ||");
Console.WriteLine("|| 1.缴水费 ||");
Console.WriteLine("|| 2.缴电费 ||");
Console.WriteLine("|| 3.缴物业费 ||");
Console.WriteLine("|| 4.缴手机话费 ||");
Console.WriteLine("|| Q.退出 ||");
Console.WriteLine("|| ||");
Console.WriteLine("==============================================");
}
(2)选择选项模块
这里主要用的就是switch进行判断
/// <summary>
/// 选择选项方法
/// </summary>
public static void SelectOption(Owner yzdx, Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
Console.Write("请输入缴费类型:");
string PaymentType = Console.ReadLine(); //获取缴费类型
switch (PaymentType)
{
case "1":
//缴水费
WaterRate(yzdx, ggjfdx);
break;
case "2":
//缴电费
ElectricCharge(yzdx, ggjfdx);
break;
case "3":
//缴物业费
PropertyFee(yzdx, ggjfdx, ownerArr, paymentArr);
break;
case "4":
//缴手机话费
PhoneBill(yzdx, ggjfdx,ownerArr, paymentArr);
break;
case "Q":
//退出
Console.WriteLine("==================退出成功====================");
break;
default:
Console.WriteLine("没有该选项");
break;
}
}
(3)登录模块
这里主要用的if嵌套进行判断跟调用方法,调用的是上面的显示选项模块、选择选项模块
(这部分的代码很冗余,我之前看过有更简洁的写法,不过我目前技术不太行,等我以后研究研究再来优化,目前是先以代码能运行起来为主)
/// <summary>
/// 登录方法
/// </summary>
public static void Register(string account, string password, string verificationCode, string VERIFICATION_CODE, Owner yzdx,Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
if (account != "" && password != "" && verificationCode != "")
{
if (account.Equals(yzdx.Account))
{
if (password.Equals(yzdx.Password))
{
if ((verificationCode.ToUpper()).Equals(VERIFICATION_CODE.ToUpper()))
{
Console.WriteLine("==================登录成功====================");
Console.WriteLine();
//调用显示选项方法
ShowOption();
//调用选择选项方法
SelectOption(yzdx, ggjfdx, ownerArr, paymentArr);
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:验证码错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:密码错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:账号错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:账号、密码、验证码不能为空");
}
}
接下来就是main方法了
这里的代码对象初始化其实可以封装成方法的,不过目前不知道封装后的返回值是个,等以后想到更好的方法再来把这个对象初始化的部分进行封装吧!!!
static void Main(string[] args)
{
Owner owner1 = new Owner(); //业主对象1
owner1.Account = "lilei@163.com"; //账号
owner1.Password = "123456"; //密码
owner1.PhoneNumber = "13866755466"; //手机号码
owner1.UserName = "李雷"; //用户姓名
owner1.RoomNo = "1-1-108"; //房号
owner1.Integral = 10; //积分
owner1.PhoneBalances = 50.0; //话费余额
owner1.UsedElectricity = 1080; //已用电量
owner1.SurplusElectrictiy = 10; //剩余电量
Owner owner2 = new Owner(); //业主对象2
owner2.Account = "liming@163.com"; //账号
owner2.Password = "123456"; //密码
owner2.PhoneNumber = "13920123597"; //手机号码
owner2.UserName = "李明"; //用户姓名
owner2.RoomNo = "1-1-102"; //房号
owner2.Integral = 20; //积分
owner2.PhoneBalances = 60.0; //话费余额
owner2.UsedElectricity = 1080; //已用电量
owner2.SurplusElectrictiy = 10; //剩余电量
Owner owner3 = new Owner(); //业主对象3
owner3.Account = "hanmeimei@163.com";//账号
owner3.Password = "123456"; //密码
owner3.PhoneNumber = "13711516894"; //手机号码
owner3.UserName = "韩梅梅"; //用户姓名
owner3.RoomNo = "1-1-203"; //房号
owner3.Integral = 50; //积分
owner3.PhoneBalances = 20.0; //话费余额
owner3.UsedElectricity = 1080; //已用电量
owner3.SurplusElectrictiy = 10; //剩余电量
Owner owner4 = new Owner(); //业主对象4
owner4.Account = "xiaoai@163.com"; //账号
owner4.Password = "123456"; //密码
owner4.PhoneNumber = "15201238912"; //手机号码
owner4.UserName = "小爱"; //用户姓名
owner4.RoomNo = "1-1-303"; //房号
owner4.Integral = 35; //积分
owner4.PhoneBalances = 45.0; //话费余额
owner4.UsedElectricity = 1080; //已用电量
owner4.SurplusElectrictiy = 10; //剩余电量
Owner owner5 = new Owner(); //业主对象5
owner5.Account = "lili@163.com"; //账号
owner5.Password = "123456"; //密码
owner5.PhoneNumber = "13605987421"; //手机号码
owner5.UserName = "李丽"; //用户姓名
owner5.RoomNo = "1-1-705"; //房号
owner5.Integral = 40; //积分
owner5.PhoneBalances = 40.0; //话费余额
owner5.UsedElectricity = 1080; //已用电量
owner5.SurplusElectrictiy = 10; //剩余电量
Owner[] ownerArr = new Owner[5] { owner1, owner2, owner3, owner4, owner5 }; //业主对象数组
Owner yzdx = owner1; //存业主对象名
Payment payment1 = new Payment(); //公共缴费对象1
payment1.Account = owner1.Account; //账号
payment1.PaymentCategory = ""; //缴费类别
payment1.Price = 1280; //金额
Payment payment2 = new Payment(); //公共缴费对象2
payment2.Account = owner2.Account; //账号
payment2.PaymentCategory = ""; //缴费类别
payment2.Price = 1560; //金额
Payment payment3 = new Payment(); //公共缴费对象3
payment3.Account = owner3.Account; //账号
payment3.PaymentCategory = ""; //缴费类别
payment3.Price = 6940; //金额
Payment payment4 = new Payment(); //公共缴费对象4
payment4.Account = owner4.Account; //账号
payment4.PaymentCategory = ""; //缴费类别
payment4.Price = 9990; //金额
Payment payment5 = new Payment(); //公共缴费对象5
payment5.Account = owner5.Account; //账号
payment5.PaymentCategory = ""; //缴费类别
payment5.Price = 1356; //金额
Payment[] paymentArr = new Payment[5] { payment1, payment2, payment3, payment4, payment5 }; //公共缴费对象数组
Payment ggjfdx = payment1; //存公共缴费对象名
const string VERIFICATION_CODE = "Bd4S"; //验证码
Console.WriteLine("============在线缴费平台-测试账号=============");
//循环输出账号跟密码
foreach (Owner ower in ownerArr)
{
Console.WriteLine($"账号:{ower.Account,-30:D}密码:{ower.Password}");
}
Console.WriteLine("==============================================");
Console.WriteLine($"验证码:{VERIFICATION_CODE}");
Console.WriteLine("==============在线缴费平台-登录===============");
Console.WriteLine("请输入账号:");
string account = Console.ReadLine(); //获取账号
Console.WriteLine("请输入密码:");
string password = Console.ReadLine(); //获取密码
Console.WriteLine("请输入验证码:");
string verificationCode = Console.ReadLine(); //获取验证码
//判断登录的是哪个账号
foreach (Owner ower in ownerArr)
{
if (ower.Account.Equals(account) )
{
yzdx = ower;
break;
}
}
foreach (Payment payment in paymentArr)
{
if (payment.Account.Equals(yzdx.Account))
{
ggjfdx = payment;
break;
}
}
//调用登录方法
Register(account, password, verificationCode, VERIFICATION_CODE, yzdx, ggjfdx,ownerArr,paymentArr);
}
这就是我写这个在线缴费平台的所有代码,优化的地方还有很多,代码总体感觉比较冗余,数据无法进行存储,后期可以加上数据库进行结合。有的功能我个人感觉有的地方写的不太合理,各个模块之间没有充分进行联合,还是各自负责各自模块内的功能。以后有机会还可以再加几个功能,让这个小项目更完善。
下面展示所有代码(两个类方法上面有这里就不再展示了)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiaoXiangMu1
{
class Program
{
static void Main(string[] args)
{
Owner owner1 = new Owner(); //业主对象1
owner1.Account = "lilei@163.com"; //账号
owner1.Password = "123456"; //密码
owner1.PhoneNumber = "13866755466"; //手机号码
owner1.UserName = "李雷"; //用户姓名
owner1.RoomNo = "1-1-108"; //房号
owner1.Integral = 10; //积分
owner1.PhoneBalances = 50.0; //话费余额
owner1.UsedElectricity = 1080; //已用电量
owner1.SurplusElectrictiy = 10; //剩余电量
Owner owner2 = new Owner(); //业主对象2
owner2.Account = "liming@163.com"; //账号
owner2.Password = "123456"; //密码
owner2.PhoneNumber = "13920123597"; //手机号码
owner2.UserName = "李明"; //用户姓名
owner2.RoomNo = "1-1-102"; //房号
owner2.Integral = 20; //积分
owner2.PhoneBalances = 60.0; //话费余额
owner2.UsedElectricity = 1080; //已用电量
owner2.SurplusElectrictiy = 10; //剩余电量
Owner owner3 = new Owner(); //业主对象3
owner3.Account = "hanmeimei@163.com";//账号
owner3.Password = "123456"; //密码
owner3.PhoneNumber = "13711516894"; //手机号码
owner3.UserName = "韩梅梅"; //用户姓名
owner3.RoomNo = "1-1-203"; //房号
owner3.Integral = 50; //积分
owner3.PhoneBalances = 20.0; //话费余额
owner3.UsedElectricity = 1080; //已用电量
owner3.SurplusElectrictiy = 10; //剩余电量
Owner owner4 = new Owner(); //业主对象4
owner4.Account = "xiaoai@163.com"; //账号
owner4.Password = "123456"; //密码
owner4.PhoneNumber = "15201238912"; //手机号码
owner4.UserName = "小爱"; //用户姓名
owner4.RoomNo = "1-1-303"; //房号
owner4.Integral = 35; //积分
owner4.PhoneBalances = 45.0; //话费余额
owner4.UsedElectricity = 1080; //已用电量
owner4.SurplusElectrictiy = 10; //剩余电量
Owner owner5 = new Owner(); //业主对象5
owner5.Account = "lili@163.com"; //账号
owner5.Password = "123456"; //密码
owner5.PhoneNumber = "13605987421"; //手机号码
owner5.UserName = "李丽"; //用户姓名
owner5.RoomNo = "1-1-705"; //房号
owner5.Integral = 40; //积分
owner5.PhoneBalances = 40.0; //话费余额
owner5.UsedElectricity = 1080; //已用电量
owner5.SurplusElectrictiy = 10; //剩余电量
Owner[] ownerArr = new Owner[5] { owner1, owner2, owner3, owner4, owner5 }; //业主对象数组
Owner yzdx = owner1; //存业主对象名
Payment payment1 = new Payment(); //公共缴费对象1
payment1.Account = owner1.Account; //账号
payment1.PaymentCategory = ""; //缴费类别
payment1.Price = 1280; //金额
Payment payment2 = new Payment(); //公共缴费对象2
payment2.Account = owner2.Account; //账号
payment2.PaymentCategory = ""; //缴费类别
payment2.Price = 1560; //金额
Payment payment3 = new Payment(); //公共缴费对象3
payment3.Account = owner3.Account; //账号
payment3.PaymentCategory = ""; //缴费类别
payment3.Price = 6940; //金额
Payment payment4 = new Payment(); //公共缴费对象4
payment4.Account = owner4.Account; //账号
payment4.PaymentCategory = ""; //缴费类别
payment4.Price = 9990; //金额
Payment payment5 = new Payment(); //公共缴费对象5
payment5.Account = owner5.Account; //账号
payment5.PaymentCategory = ""; //缴费类别
payment5.Price = 1356; //金额
Payment[] paymentArr = new Payment[5] { payment1, payment2, payment3, payment4, payment5 }; //公共缴费对象数组
Payment ggjfdx = payment1; //存公共缴费对象名
const string VERIFICATION_CODE = "Bd4S"; //验证码
Console.WriteLine("============在线缴费平台-测试账号=============");
//循环输出账号跟密码
foreach (Owner ower in ownerArr)
{
Console.WriteLine($"账号:{ower.Account,-30:D}密码:{ower.Password}");
}
Console.WriteLine("==============================================");
Console.WriteLine($"验证码:{VERIFICATION_CODE}");
Console.WriteLine("==============在线缴费平台-登录===============");
Console.WriteLine("请输入账号:");
string account = Console.ReadLine(); //获取账号
Console.WriteLine("请输入密码:");
string password = Console.ReadLine(); //获取密码
Console.WriteLine("请输入验证码:");
string verificationCode = Console.ReadLine(); //获取验证码
//判断登录的是哪个账号
foreach (Owner ower in ownerArr)
{
if (ower.Account.Equals(account) )
{
yzdx = ower;
break;
}
}
foreach (Payment payment in paymentArr)
{
if (payment.Account.Equals(yzdx.Account))
{
ggjfdx = payment;
break;
}
}
//调用登录方法
Register(account, password, verificationCode, VERIFICATION_CODE, yzdx, ggjfdx,ownerArr,paymentArr);
}
/// <summary>
/// 登录方法
/// </summary>
public static void Register(string account, string password, string verificationCode, string VERIFICATION_CODE, Owner yzdx,Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
if (account != "" && password != "" && verificationCode != "")
{
if (account.Equals(yzdx.Account))
{
if (password.Equals(yzdx.Password))
{
if ((verificationCode.ToUpper()).Equals(VERIFICATION_CODE.ToUpper()))
{
Console.WriteLine("==================登录成功====================");
Console.WriteLine();
//调用显示选项方法
ShowOption();
//调用选择选项方法
SelectOption(yzdx, ggjfdx, ownerArr, paymentArr);
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:验证码错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:密码错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:账号错误");
}
}
else
{
Console.WriteLine("==================登录失败====================");
Console.WriteLine("失败原因:账号、密码、验证码不能为空");
}
}
/// <summary>
/// 显示选项方法
/// </summary>
public static void ShowOption()
{
Console.WriteLine("================在线缴费平台==================");
Console.WriteLine("|| ||");
Console.WriteLine("|| 1.缴水费 ||");
Console.WriteLine("|| 2.缴电费 ||");
Console.WriteLine("|| 3.缴物业费 ||");
Console.WriteLine("|| 4.缴手机话费 ||");
Console.WriteLine("|| Q.退出 ||");
Console.WriteLine("|| ||");
Console.WriteLine("==============================================");
}
/// <summary>
/// 选择选项方法
/// </summary>
public static void SelectOption(Owner yzdx, Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
Console.Write("请输入缴费类型:");
string PaymentType = Console.ReadLine(); //获取缴费类型
switch (PaymentType)
{
case "1":
//缴水费
WaterRate(yzdx, ggjfdx);
break;
case "2":
//缴电费
ElectricCharge(yzdx, ggjfdx);
break;
case "3":
//缴物业费
PropertyFee(yzdx, ggjfdx, ownerArr, paymentArr);
break;
case "4":
//缴手机话费
PhoneBill(yzdx, ggjfdx,ownerArr, paymentArr);
break;
case "Q":
//退出
Console.WriteLine("==================退出成功====================");
break;
default:
Console.WriteLine("没有该选项");
break;
}
}
/// <summary>
/// 缴水费方法
/// </summary>
public static void WaterRate(Owner yzdx, Payment ggjfdx)
{
ggjfdx.PaymentCategory = "水费"; //定义缴费类别
Console.WriteLine("=============================================");
Console.WriteLine("|| 欢迎进入水费管理界面! ||");
Console.WriteLine("=============================================");
Console.WriteLine();
Console.WriteLine("=======================水费类型=======================");
Console.WriteLine("|| 1.居民用水,2.行政用水,3.工商用水,4.餐饮用水 ||");
Console.WriteLine("======================================================");
Console.WriteLine("请输入水费类型:");
int waterType = int.Parse(Console.ReadLine()); //水费类型
Console.WriteLine("请输入用水量(立方米):");
int waterConsumption = int.Parse(Console.ReadLine()); //用水量
const double RESIDENT = 2.8; //居民用水
const double ADMINISTRATIVE = 3.9; //行政用水
const double INDUSTRY_AND_COMMERCE = 4.1; //工商用水
const double CATERING = 4.6; //餐饮用水
double sum = 0;
string jiaoFeiLeiXing = ""; //缴水费类型
switch (waterType)
{
case 1:
double residentSum = waterConsumption * RESIDENT; //居民水费
sum = residentSum;
jiaoFeiLeiXing = "居民用水";
break;
case 2:
double administrativeSum = waterConsumption * ADMINISTRATIVE; //行政水费
sum = administrativeSum;
jiaoFeiLeiXing = "行政用水";
break;
case 3:
double industry_and_commerceSum = waterConsumption * INDUSTRY_AND_COMMERCE; //工商水费
sum = industry_and_commerceSum;
jiaoFeiLeiXing = "工商用水";
break;
case 4:
double cateringSum = waterConsumption * CATERING; //餐饮水费
sum = cateringSum;
jiaoFeiLeiXing = "餐饮用水";
break;
default:
Console.WriteLine("没有该选项!");
break;
}
Console.WriteLine("应缴水费金额:{0,8:C2}元。", sum);
Console.WriteLine("是否进行缴费:Y:是,N:否?");
string isJiaoFei = Console.ReadLine(); //是否继续缴费
if (isJiaoFei == "Y") //判断是否继续缴费
{
Console.WriteLine($"水费缴费成功,业主姓名:{yzdx.UserName},缴费类型:{jiaoFeiLeiXing},缴费金额:{sum,8:C2}元。");
}
else
{
Console.WriteLine("放弃缴费!");
}
}
/// <summary>
/// 缴电费方法
/// </summary>
public static void ElectricCharge(Owner yzdx, Payment ggjfdx)
{
ggjfdx.PaymentCategory = "电费"; //定义缴费类别
Console.WriteLine("==============================================");
Console.WriteLine("|| 欢迎进入电费管理界面! ||");
Console.WriteLine("==============================================");
Console.WriteLine();
Console.WriteLine("=======================阶梯电价=======================");
Console.WriteLine("|| 第一阶梯0.56元,2160度及以下。 ||");
Console.WriteLine("|| 第二阶梯0.61元,2160-3120度。 ||");
Console.WriteLine("|| 第三阶梯0.86元,超过3120度。 ||");
Console.WriteLine("======================================================");
const double ONE = 0.56; //第一阶梯价钱
const double TWO = 0.61; //第二阶梯价钱
const double THREE = 0.86; //第三阶梯价钱
Console.WriteLine($"业主姓名:{yzdx.UserName},已用电量:{yzdx.UsedElectricity}度,剩余电量:{yzdx.SurplusElectrictiy}度");
Console.Write("请输入要购买的电量(单位:度):");
int purchase = int.Parse(Console.ReadLine()); //获取购买电量
double sum = 0; //应缴电费
//判断各个阶段应缴的电费
if (yzdx.UsedElectricity < 2160)
{
sum = ONE * purchase; //计算第一阶段应缴电费价钱
}
else if (yzdx.UsedElectricity >= 2160 && yzdx.UsedElectricity <= 3120)
{
sum = TWO * purchase; //计算第二阶段应缴电费价钱
}
else if (yzdx.UsedElectricity > 3120)
{
sum = THREE * purchase; //计算第三阶段应缴电费价钱
}
Console.WriteLine($"应缴电费:{sum,10:C2}元。");
Console.WriteLine("是否进行购买:Y:是,N:否?");
string isGouMai = Console.ReadLine(); //是否继续购买
if (isGouMai == "Y") //判断是否继续购买
{
Console.WriteLine($"购买成功,业主姓名:{yzdx.UserName},购买电量:{purchase}度,金额:{sum,10:C2}元,剩余电量:{yzdx.SurplusElectrictiy + purchase}度");
}
else
{
Console.WriteLine("放弃购买!");
}
}
/// <summary>
/// 缴物业费方法
/// </summary>
public static void PropertyFee(Owner yzdx, Payment ggjfdx, Owner[] ownerArr, Payment[] paymentArr)
{
ggjfdx.PaymentCategory = "物业费"; //定义缴费类别
Console.WriteLine("==============================================");
Console.WriteLine("|| 欢迎进入物业费管理界面! ||");
Console.WriteLine("==============================================");
Console.WriteLine();
Console.WriteLine("===================物业缴费===================");
Console.WriteLine("|| 1.物业缴费,2.打印凭证,3.缴费统计 ||");
Console.WriteLine("==============================================");
Console.WriteLine("请输入操作类型:");
int operationType = int.Parse(Console.ReadLine()); //操作类型
switch (operationType)
{
case 1:
//调用物业缴费方法
PropertyPayment(yzdx, ggjfdx);
break;
case 2:
//调用打印凭证方法
PrintCertificate(yzdx, ggjfdx);
break;
case 3:
//调用物业费统计方法
Statistics(ownerArr, paymentArr);
break;
default:
Console.WriteLine("没有该选项!");
break;
}
}
/// <summary>
/// 物业缴费方法
/// </summary>
public static void PropertyPayment(Owner yzdx, Payment ggjfdx)
{
Console.WriteLine("==========物业缴费方式==========");
Console.WriteLine("|| 1.季节缴:折扣100% ||");
Console.WriteLine("|| 2.半年缴:折扣90% ||");
Console.WriteLine("|| 3.年度缴:折扣80% ||");
Console.WriteLine("================================");
Console.WriteLine("请输入缴费方式:");
int paymentMethod = int.Parse(Console.ReadLine()); //缴费方式
Console.WriteLine("请输入房屋面积(平方米):");
int area = int.Parse(Console.ReadLine()); //房屋面积
const int PROPERTY_FEE = 2; //物业费单价
double propertyFeeSum = 0; //物业费
string type = ""; //缴费类型
switch (paymentMethod)
{
case 1:
type = "季度";
propertyFeeSum = area * PROPERTY_FEE * 1 * 3; //一季度3个月
break;
case 2:
type = "半年";
propertyFeeSum = area * PROPERTY_FEE * 0.9 * 6; //半年6个月
break;
case 3:
type = "整年";
propertyFeeSum = area * PROPERTY_FEE * 0.8 * 12; //一年12个月
break;
default:
Console.WriteLine("没有该选项!");
break;
}
Console.WriteLine($"你需要缴纳的物业费用:{propertyFeeSum}元。");
Console.WriteLine("是否确认缴费:Y:是,N:否?");
string isPayment = Console.ReadLine(); //是否确认缴费
if (isPayment == "Y") //判断是否确认缴费
{
Console.WriteLine($"缴费成功,姓名:{yzdx.UserName},房号:{yzdx.RoomNo},缴费方式:{type},缴费金额:{propertyFeeSum}元。");
}
else
{
Console.WriteLine("放弃缴费!");
}
}
/// <summary>
/// 打印凭证方法
/// </summary>
public static void PrintCertificate(Owner yzdx, Payment ggjfdx)
{
double discount = 8.6; //定义折扣
Console.WriteLine("========物业费缴费凭证========");
Console.WriteLine();
Console.WriteLine("==============================");
Console.WriteLine($"|| 账号:{yzdx.Account,18}||");
Console.WriteLine($"|| 姓名:{yzdx.UserName,16}||");
Console.WriteLine($"|| 房号:{yzdx.RoomNo,18}||");
Console.WriteLine($"|| 类别:{ggjfdx.PaymentCategory,15}||");
Console.WriteLine($"|| 折扣:{discount,18:F1}||");
Console.WriteLine($"|| 金额:{ggjfdx.Price,15:C2}元||");
Console.WriteLine("==============================");
}
/// <summary>
/// 物业费统计方法
/// </summary>
public static void Statistics(Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("******物业费统计平台******");
Console.WriteLine("物业缴费明细:");
for (int i = 0; i < ownerArr.Length; i++)
{
Console.WriteLine($"房号:{ownerArr[i].RoomNo},业主姓名:{ownerArr[i].UserName},缴费金额:{paymentArr[i].Price}元。");
}
}
/// <summary>
/// 缴手机话费方法
/// </summary>
public static void PhoneBill(Owner yzdx, Payment ggjfdx,Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("================================");
Console.WriteLine("||欢迎进入缴手机话费管理界面!||");
Console.WriteLine("================================");
Console.WriteLine();
Console.WriteLine("================================");
Console.WriteLine("|| 1.话费充值 ||");
Console.WriteLine("|| 2.积分发放 ||");
Console.WriteLine("================================");
Console.Write("请选择服务:");
string selectService = Console.ReadLine(); //选择服务
switch (selectService)
{
case "1":
//调用话费充值方法
PrepaidRefill(yzdx, ggjfdx,ownerArr, paymentArr);
break;
case "2":
//调用积分发放方法
PointRelease(yzdx);
break;
default:
Console.WriteLine("没有该选项服务!");
break;
}
}
/// <summary>
/// 话费充值方法
/// </summary>
public static void PrepaidRefill(Owner yzdx, Payment ggjfdx,Owner[] ownerArr, Payment[] paymentArr)
{
Console.WriteLine("============话费充值============");
Console.WriteLine("|| ||");
Console.WriteLine("|| Y.继续充值 ||");
Console.WriteLine("|| N.退出充值 ||");
Console.WriteLine("|| ||");
Console.WriteLine("================================");
bool isNo = true; //循环变量为真
bool isN = true; //用来记录判断手机号是否存在的容器
Owner dx = yzdx; //用来存该用户对象
while (isNo)
{
Console.WriteLine("请输入手机号码:");
string number = Console.ReadLine(); //手机号码
Console.WriteLine("请输入充值金额:");
int money = int.Parse(Console.ReadLine()); //充值金额
for (int i = 0; i < ownerArr.Length; i++)
{
if (ownerArr[i].PhoneNumber.Equals(number))
{
dx = ownerArr[i];
isN = true;
break;
}
isN = false;
}
if (isN == false)
{
Console.WriteLine("未找到该手机号码,无法进行充值!");
continue;
}
Console.WriteLine("充值成功!");
Console.WriteLine("充值前:");
Console.WriteLine($"姓名:{dx.UserName},手机号码:{number},余额:{dx.PhoneBalances + money}元,积分:{dx.Integral + (money / 10)}");
Console.WriteLine("充值后:");
Console.WriteLine($"姓名:{dx.UserName},手机号码:{number},余额:{dx.PhoneBalances + money}元,充值金额:{money}元,积分:{dx.Integral + (money / 10)},积分增加:{money / 10}");
Console.WriteLine("是否继续充值:Y:是,N:否?");
string isRecharge = Console.ReadLine(); //是否继续充值
if (isRecharge == "Y")
{
isNo = true;
}
else
{
isNo = false;
Console.WriteLine("退出充值!");
}
}
}
/// <summary>
/// 积分发放方法
/// </summary>
public static void PointRelease(Owner yzdx)
{
Console.WriteLine($"姓名:{yzdx.UserName},话费余额:{yzdx.PhoneBalances}元,积分:{yzdx.Integral}");
Console.WriteLine("请输入要充值的话费:");
int recharge = int.Parse(Console.ReadLine()); //充值
Console.WriteLine($"充值成功,姓名:{yzdx.UserName},话费余额:{yzdx.PhoneBalances + recharge}元,积分:{yzdx.Integral + recharge / 10},积分增加:{recharge / 10}");
}
}
}