今天老師給了一份作業是收銀機,格式如下圖:
public partial class 收銀機 : Form
{
//先給total一個可以存放數字
static double total = 0;
//先給com一個可以存放文字訊息
string com="";
int cake1_1 = 100, cake2_2=55,cake3_3=60,cake4_4=400;
public 收銀機()
{
InitializeComponent();
Text = "收銀機";
}
private void button7_Click(object sender, EventArgs e) //計算的按鈕
{
//判斷金額欄是否有金額
if (textBox1.Text == "0")
{
MessageBox.Show("請先選擇項目!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//判斷付現空格是否有輸入金額
if (textBox2.Text == "0")
{
MessageBox.Show("請輸入應付金額!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox2.Select();
return;
}
double a =double.Parse( textBox2.Text);
double b = double.Parse(textBox1.Text);
a =a - b;
textBox3.Text =a.ToString();
//下面這邊是做照片右下角那邊的找零系統
int ipay, itotal, ichange;
int i500, i100, i50, i10, i1;
ipay = int.Parse(textBox2.Text);
itotal = int.Parse(textBox1.Text);
ichange = int.Parse(textBox3.Text);
if (ipay>=itotal)
{
ichange = ipay - itotal;
textBox3.Text = ichange.ToString();
i500 = ichange / 500;
label9.Text = i500.ToString();
ichange = ichange % 500;
i100 = ichange / 100;
label10.Text = i100.ToString();
ichange = ichange % 100;
i50 = ichange / 50;
label11.Text = i50.ToString();
ichange = ichange % 50;
i10 = ichange / 10;
label12.Text = i10.ToString();
ichange = ichange % 10;
label13.Text = ichange.ToString();
}
else
{
MessageBox.Show("金額不足","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private void cake1_Click(object sender, EventArgs e)
{
textBox2.Select();
total = total + cake1_1;
textBox1.Text = total.ToString();
com = com + "草莓蛋糕一個"+Environment.NewLine;
textBox4.Text = com;
}
private void cake2_Click(object sender, EventArgs e)
{
textBox2.Select();
total = total + cake2_2;
textBox1.Text = total.ToString();
com =com+ "歐雷塔蛋糕一個"+Environment.NewLine;
textBox4.Text = com;
}
private void cake3_Click(object sender, EventArgs e)
{
textBox2.Select();
total = total + cake3_3;
textBox1.Text = total.ToString();
com = com+"巧克力榛果蛋糕一個"+Environment.NewLine;
textBox4.Text = com;
}
private void cancel_Click(object sender, EventArgs e) //清除按鈕
{
textBox1.Text = "0";
textBox2.Text = "0";
textBox3.Text = "0";
total = 0;
textBox4.Text = null;
com = "";
cash1.Enabled = true;
cake1.Enabled = true;
cake2.Enabled = true;
cake3.Enabled = true;
cake4.Enabled = true;
card.Enabled = true;
cash1.Enabled = true;
}
private void mail_Click(object sender, EventArgs e) //E-mail按鈕
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("寄送的郵件帳號");
msg.To.Add("接收的郵件帳號");
//郵件標題編碼
msg.SubjectEncoding = System.Text.Encoding.UTF8;
//郵件標題
msg.Subject = "明細";
//郵件內容編碼
msg.BodyEncoding = System.Text.Encoding.UTF8;
//郵件內容是否為html
msg.IsBodyHtml = false;
//郵件內容
msg.Body = "標題";
//郵件優先級
msg.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
//gmail host&port
client.Host = "smtp.gmail.com";
client.Port = 587;
//gmail的smtp使用ssl
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("寄送的郵件帳號","寄送的郵件密碼");
client.Timeout = 20000;
try
{
client.Send(msg);
MessageBox.Show("信件已寄");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
msg.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
textBox2.Text = "0";
textBox3.Text = "0";
total = 0;
textBox4.Text = null;
com = "";
cash1.Enabled = true;
cake1.Enabled = true;
cake2.Enabled = true;
cake3.Enabled = true;
cake4.Enabled = true;
card.Enabled = true;
cash1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void cash1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
MessageBox.Show("請先選擇品項!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//將輸出金額乘9,而且傳回整數
if (card.Enabled == true)
{
double a = double.Parse(textBox1.Text) * 0.9;
a = Math.Round(a);
textBox1.Text = a.ToString();
}
card.Enabled = false;
cash1.Enabled = false;
cake1.Enabled = false;
cake2.Enabled = false;
cake3.Enabled = false;
cake4.Enabled = false;
textBox2.Select();
com = com + "現金打9折"+Environment.NewLine;
textBox4.Text = com;
}
private void card_Click(object sender, EventArgs e)
{
//判斷書出金額是否是0,如果是0跳出請先選擇品項
if (textBox1.Text == "0")
{
MessageBox.Show("請選擇品項!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//將輸出金額乘8,而且傳回整數
if (cash1.Enabled== true)
{
double a = double.Parse(textBox1.Text) * 0.8;
a = Math.Round(a);
textBox1.Text = a.ToString();
}
cash1.Enabled = false;
card.Enabled = false;
cash1.Enabled = false;
cake1.Enabled = false;
cake2.Enabled = false;
cake3.Enabled = false;
cake4.Enabled = false;
//把游標放到應付空格上
textBox2.Select();
//將文字訊息傳到清單
com = com + "刷卡打8折"+Environment.NewLine;
textBox4.Text = com;
}
private void cake4_Click(object sender, EventArgs e)
{
textBox2.Select();
total = total + cake4_4;
textBox1.Text = total.ToString();
com = com+ "黑森林蛋糕一個"+Environment.NewLine;
textBox4.Text = com;
//total = total + int.Parse(cake4.Text);
}
}
留言列表