C # Simple store cash register system v1.0 (2-1)
-
Original:
- Applications for benefits and a simple factory pattern programming objects (inheritance, polymorphism)
-
just now:
- Make a simple cash register application form
Before you can reference calculator essays
Create a form program
Client code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 商城收银软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d;//Declare a double variable, with a total calculated total Private void the button1_Click ( Object SENDER, EventArgs E) { double totalPrices = Convert.ToDouble (txtPrice.Text) * Convert.ToDouble (txtNum.Text); total = total + totalPrices; // each product sum calculated lbxList.Text = " Price: " + + txtPrice.Text " number: " + txtNum.Text + " total: " + totalPrices.ToString (); // display information lblResult.Text = total. the ToString (); // display the total count }
}
}
Achieve results
Now add a discount function
Client code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 商城收银软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d;//Declare a double variable, with a total calculated total Private void the button1_Click ( Object SENDER, EventArgs E) { double totalPrices = 0D; Switch (cbxType.SelectedIndex) { Case 0 : totalPrices = Convert.ToDouble (txtPrice.Text) * Convert.ToDouble (txtNum.Text); BREAK ; Case . 1 : totalPrices = Convert.ToDouble (txtPrice.Text) * Convert.ToDouble (txtNum.Text) * 0.8 ; BREAK ; case 2: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)*0.7; break; case 3: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)*0.5; break; } total = total + totalPrices; lbxList.Text = "单价:" + txtPrice.Text + "数量:" + txtNum.Text + " " + CbxType.SelectedItem + " Total: " + totalPrices.ToString (); lblResult.Text = total.ToString (); } Private void the Form1_Load ( Object SENDER, EventArgs E) { cbxType.Items.AddRange ( new new Object [] { " normal charge " , " discount of twenty " , " play Qizhe " , " half off " }); cbxType.SelectedIndex = 0 ; }
}
to sum up
Like Convert.ToDouble () many of these duplicate
Discount branch can be considered reconstructed
The client is instantiated only need to change out of operation
You can try using a simple factory implementation
Under an essay on the use of simple factory store cash register system to achieve v1.0