Title: Make a small program for checking out in a mall. Possible situations include: normal fees, 90% discount, 70% discount, 300% discount, 50% discount and so on.
The interface is as follows:
Analysis:
First let's write a parent CashSuper for collecting money.This parent class is used to include various other charging methods: normal charging, discount discount, discount discount at 20%, discount at 10%, discount at 300 minus 50, 400 minus 70, 500 minus 100, etc. Although discount discount discounts are different, they are similar in type, so we can divide normal charging, discount discount discount and full discount into three different categories:CashNormal, CashRebate, CashReturn.
CashSuper is a parent for inheritance, so we set abstract to be overridden, and then the three subclasses it contains all call to one parameter: the price of the goods actually needed to be received, so all we need to do is pass in the same parameter: acceptMoney.
1 abstract class CashSuper 2 { 3 public abstract double acceptCash(double acceptMoney); 4 }
Then there's the normal charge: CashNormal
The first parameter he gets from his parent is the price of the commodity he actually needs to receive. His normal fees do not give any benefit, so he can simply return the value passed in from his parent.
1 class CashNormal : CashSuper 2 { 3 public override double acceptCash(double acceptMoney) 4 { 5 return acceptMoney; 6 } 7 }
Discount Offer: CashRebate
Similar to normal fees, he inherits from his parent CashSuper and gets parameters from his parent to get the price of the goods he actually needs to receive, but what he needs to achieve is a discount on the goods, so he needs to define a discount preference parameter himself so that when someone calls him, he can pass in the discount parameter and he can offer a discount on the original price.Feedback to users.
1 class CashRebate : CashSuper 2 { 3 //This is it. cashrebate Property of 4 private double monRebate = 1; 5 6 //call CashRebate When you need to pass in preferences from outside 7 public CashRebate(string moneyRebate) 8 { 9 this.monRebate = double.Parse(moneyRebate); 10 } 11 12 public override double acceptCash(double acceptMoney) 13 { 14 return acceptMoney * monRebate; 15 } 16 }
Full discount: CashReturn
This is similar to a discount offer except that it has two parameters: the level of the full discount and the amount of the discount.Therefore, two parameters can be defined for this class.
1 class CashReturn : CashSuper 2 { 3 //This is it. cashreturn Property of 4 private double CashLevel = 0; 5 private double MoneyReturn = 0; 6 7 //To call a function externally, it must be public 8 public CashReturn(string level,string MonReturn) 9 { 10 this.CashLevel = double.Parse(level); 11 this.MoneyReturn = double.Parse(MonReturn); 12 } 13 14 public override double acceptCash(double acceptMoney) 15 { 16 double result = acceptMoney; 17 if (acceptMoney >= CashLevel) 18 { 19 result = acceptMoney - Math.Floor(acceptMoney / CashLevel) * MoneyReturn; 20 } 21 return result; 22 } 23 }
Now we have several offers, but we need to decide when we need to invoke which one. This is through the user's choice, the user will transfer the selected offers to us, and we can decide which one to invoke. This is using the simple factory mode, encapsulating all the offers and making further invocations.
1 class CashFactory 2 { 3 //CashSuper Now it's like this double And so on, the return value is CashSuper 4 public static CashSuper createCashAccept(string type) 5 { 6 CashSuper cs = null; 7 8 switch (type) 9 { 10 case "Normal Charge": 11 cs = new CashNormal(); 12 break; 13 case "Full 300 minus 50": 14 cs = new CashReturn("300", "50"); 15 break; 16 case "Full 500 minus 100": 17 cs = new CashReturn("500", "100"); 18 break; 19 case "400 minus 70": 20 cs = new CashReturn("400", "70"); 21 break; 22 case "Full 900 minus 200": 23 cs = new CashReturn("900", "200"); 24 break; 25 case "20% discount": 26 cs = new CashRebate("0.8"); 27 break; 28 case "10% discount": 29 cs = new CashRebate("0.9"); 30 break; 31 case "Seventy percent discount": 32 cs = new CashRebate("0.7"); 33 break; 34 } 35 return cs; 36 } 37 }
Finally, call the above function in the user interface.
1 private void ok_button_Click(object sender, EventArgs e) 2 { 3 /**You need to know two functions on the outside side 4 * 1.CashFactory.createCashAccept,This is to determine which offer is invoked for each purchase 5 * 2.csuper.acceptCash,This is to get the benefit of each offer 6 */ 7 CashSuper csuper = CashFactory.createCashAccept(typecomboBox.SelectedItem.ToString()); 8 9 totalPrices = csuper.acceptCash(double.Parse(unitPrice_textBox.Text) * double.Parse(amount_textBox.Text)); 10 total += totalPrices; 11 listBox1.Items.Add("Unit Price:" + unitPrice_textBox.Text.ToString() + " Number:" + amount.ToString() + " " + typecomboBox.SelectedItem.ToString() + " Total:" + totalPrices.ToString()); 12 resultLabel.Text = total.ToString(); 13 }