求助:能帮忙看看我这个代码吗,有更好的方式吗?
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请依次按提示输入:");
Console.WriteLine("输入购买金额:");
var inputMoney = double.Parse(Console.ReadLine());
Console.WriteLine("输入购买数量:");
var inputNumbers = int.Parse(Console.ReadLine());
Console.WriteLine("@输入是否是会员: 0:非会员; 1:银卡会员; 2:金卡会员;");
var inputVip = int.Parse(Console.ReadLine());
var Person1 = new CashContext(inputMoney, inputNumbers,inputVip);
Console.ReadLine();
}
}
//现金收取父类
internal abstract class CashReap
{
public abstract double AcceptCash(double money);
}
//正常收费子类
internal class CashNormal : CashReap
{
public override double AcceptCash(double money)
{
return money;
}
}
//打折子类
internal class CashDiscount : CashReap
{
private double _moneyDiscount = 1d;
public int ProductNumbers { get; set; }
public CashDiscount(double moneyDiscount)
{
_moneyDiscount = moneyDiscount;
}
public override double AcceptCash(double money)
{
return money*_moneyDiscount;
}
}
//返现子类
internal class CashRebate : CashReap
{
private double _moneyCondition = 0.0d;
private double _moneyReturn = 0.0d;
public CashRebate(double moneyCondition, double moneyReturn)
{
_moneyCondition = moneyCondition;
_moneyReturn = moneyReturn;
}
public float TotalPrices { get; set; }
public override double AcceptCash(double money)
{
double result = money;
if (money >= _moneyCondition)
{
result = money - _moneyReturn;
}
return result;
}
}
//VIP类
internal class VipDiscount : CashReap
{
private double _moneyDiscount = 1d;
public VipDiscount(double discountRate)
{
_moneyDiscount = discountRate;
}
public override double AcceptCash(double money)
{
return money * _moneyDiscount;
}
}
//现金收取工厂
internal class CashContext
{
private CashReap _cReap;
public CashContext(double totalMoney,int productnumbers,int isVip)
{
//购买数量的判断
if (productnumbers == 1)
{
var cs1 = new CashDiscount(0.9);
_cReap = cs1;
}
else if (productnumbers >= 2)
{
var cs2 = new CashDiscount(0.7);
_cReap = cs2;
}
var temp1 = _cReap.AcceptCash(totalMoney);
//购买金额的判断
if (temp1 >= 100 && temp1 < 200)
{
var cs1 = new CashRebate(100, 10);
_cReap = cs1;
}
if (temp1 >= 200 && temp1 < 300)
{
var cs2 = new CashRebate(200, 20);
_cReap = cs2;
}
if (temp1 >= 300)
{
var cs3 = new CashRebate(300, 50);
_cReap = cs3;
}
var temp2 = _cReap.AcceptCash(totalMoney);
//VIP的判断
switch (isVip)
{
case 0:
var cs1 = new VipDiscount(1);
_cReap = cs1;
break;
case 1:
var cs2 = new VipDiscount(0.9);
_cReap = cs2;
break;
case 2:
var cs3 = new VipDiscount(0.8);
_cReap = cs3;
break;
}
Console.WriteLine("应付总额为: "+GetResult(temp2)+" 元");
}
public double GetResult(double money)
{
return _cReap.AcceptCash(money);
}
}
请问这里的if else可以换为更简便的写法嘛?这代码有什么问题没有啊,刚学完策略模式+工程模式;要运用OOP思想 --------------------编程问答-------------------- if这里不复杂阿,如果不用if 那用case ,这里条件不多,还是if可以 --------------------编程问答--------------------
但是在判断区间那里还是必须要使用if啊。。 --------------------编程问答-------------------- 代码逻辑不清晰:
1. 部分打折和返现的业务逻辑写到了现金收取工厂中
2. 打折、返现、VIP之间关系混乱,首先你要明确这三者之间的关系。
CashContext功只需要协调打折、返现、VIP三者之间的关系,例如多少钱的返现多少、多少数量的打几折这种逻辑是在打折和返现类中进行判断的。 --------------------编程问答--------------------
请问if那里需要怎么修改呢 --------------------编程问答-------------------- 建议按以下结构设计代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请依次按提示输入:");
Console.WriteLine("输入购买金额:");
var inputMoney = double.Parse(Console.ReadLine());
Console.WriteLine("输入购买数量:");
var inputNumbers = int.Parse(Console.ReadLine());
Console.WriteLine("@输入是否是会员: 0:非会员; 1:银卡会员; 2:金卡会员;");
var inputVip = int.Parse(Console.ReadLine());
var Person1 = new CashContext(inputMoney, inputNumbers,inputVip);
Console.ReadLine();
}
}
//现金收取父类
internal abstract class CashReap
{
public abstract double AcceptCash(double money);
}
//打折子类
internal class CashDiscount : CashReap
{
......
public int ProductNumbers { get; set; } // 此处需要增加根据数量自动换算折扣率的代码
......
}
//返现子类
internal class CashRebate : CashReap
{
......
public float TotalPrices { get; set; } // 此处需要增加根据总金额自动换算返还金额的算法
......
}
//VIP类
internal class VipDiscount : CashReap
{
......
}
//现金收取工厂
internal class CashContext
{
private CashReap _cReap;
public CashContext(double totalMoney,int productnumbers,int isVip)
{
// 活支折扣
CashDiscount cs1 = new CashDiscount();
cs1.productnumbers == 1;
totalMoney = cs1.AcceptCash(totalMoney);
// VIP折扣
VipDiscount cs2 = new VipDiscount(isVip);
totalMoney = cs2.AcceptCash(totalMoney);
// 返现
CashRebater cs3 = new CashRebate();
totalMoney = cs3.AcceptCash(totalMoney)
Console.WriteLine("应付总额为: "+ totalMoney.tostring() +" 元");
}
}
补充:.NET技术 , C#