c#三层构架之ATM
这是我之前拿来交期末作业的作品
这就分享一下下载链接,和接下来的博文
下载链接可以直接下载
博文可以传阅,欢迎大佬评论。
这是资源下载地址,没有积分可以上淘宝购买积分
c#三层构架之ATM
首先这个ATM是使用三层架构来写的,关于三层架构我是在csdn上学的。
想要具体了解三层架构的话可以游览一下链接。其次需要了解三层架构的相互之间的关系及引用。
https://www.cnblogs.com/liuqifeng/p/9150254.html
最后才开始以下的实践。
说一下思路:
ATM的主要功能是存钱,取钱,转账,查询,交易明细。
还有就是我这个设计数据库的时候没有考虑周到,给的数据类型时整形的。
这是我的失误,你们可以设计成浮点的类型。
查询,需要登录后才能查询。
存钱,要怎样才能存钱?起码需要一个账户,然后存钱的金额不能是负数。
取钱,取钱的金额不能为负却不能大于本金。
转账,要怎样才能进行转账?转账需要两个账户,还需要转账的金额(金额不能为负数,不能大于本金)。
交易明细,记录各各操作的内容,账号及金额附上时间。
先贴数据库的图
这是用户表字段设计
这是用户表的数据
这是数据表字段的设计,SFZ是用来找回密码的,MONEY是用来储存钱的,
RECORD是用来记录交易明细的。
这是数据表的数据
这是方案里的项目,建四个项目,以及各类,窗体。
他们之间的引用关系在上文的链接中有详细解说。
MODEL项目里的model.cs代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//bll引用ui,和dal
//ui引用bll,moodel
//dal引用model 和using
namespace MODEL
{
public class model
{
public string id { get; set; }
public string pwd { get; set; }
public string sfz { get; set; }
public string money { get; set; }
public string record { get; set; }
}
}
DAL项目中的dal.cs代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MODEL;
namespace DAL
{
public class dal
{
string count = "server=.;DATABASE=MyAtm;Integrated Security=true;";//连接数据库字符串
/*登陆*/
public model select(string ID, string PWD)
{
model mm = null;
try
{
SqlConnection con=new SqlConnection(count);
con.Open();
//string sql = "select * from data where id='"+ID+"'";
SqlCommand cmd = new SqlCommand("select * from userr WHERE ID='"+ID+"'",con);
SqlDataReader dr=cmd.ExecuteReader();
if (dr.Read())
{
if (mm == null) //这个必须写
{
mm = new model();
}
mm.id = dr["ID"].ToString().Trim();
mm.pwd = dr["pwd"].ToString().Trim();
}
con.Close();
}
catch (Exception ex){
}
return mm;
}
/*修改密码*/
public model update(string ID, string SFZ,string PWD)
{
model mm = null;
try
{
SqlConnection con = new SqlConnection(count);
con.Open();
string sql = "select * from data where id='" + ID + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (mm == null)
{
mm = new model();
}
mm.id = dr["id"].ToString().Trim();
mm.sfz = dr["sfz"].ToString().Trim();
dr.Close();
string sql0 = "update userr set pwd='" + PWD + "'where id='" + ID + "'";
SqlCommand cmd1 = new SqlCommand(sql0, con);
int n = cmd1.ExecuteNonQuery();
mm.pwd = PWD;
con.Close();
}
}
catch (Exception ex){
}
return mm;
}
/*查询余额*/
public model money_select(string ID)//money
{
model mm = new model();
SqlConnection con = new SqlConnection(count);
string sql = "select * from data where id='"+ID+"'";
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr=cmd.ExecuteReader();
if (dr.Read())
{
if (mm==null)
{
mm = new model();
}
mm.money=dr["money"].ToString().Trim();
}
con.Close();
return mm;
}
/*取款*/
public int i=0;
public string DT="";
/*用来储存当前操作的时间,与金额*/
public model draw(string ID,string DRAW)
{
model mm = null;
string DT_sql = "select * from DATA where id='"+ID+"'";
SqlConnection DT_con = new SqlConnection(count);
DT_con.Open();
SqlCommand DT_cmd = new SqlCommand(DT_sql, DT_con);
SqlDataReader DT_dr = DT_cmd.ExecuteReader();
model i_mm=new model();
if (DT_dr.HasRows)
{
DT_dr.Read();
/*-----------先读取之前转账的信息------------*/
DT += DT_dr["RECORD"].ToString().Trim();
i_mm.money = DT_dr["MONEY"].ToString().Trim();
}
DT_dr.Close();
DT_con.Close();
if (DRAW == "" || Convert.ToInt32(DRAW) > Convert.ToInt32(i_mm.money))
{
//model i_mm = null;
mm = i_mm;
return mm;
}
else
{
DT += "\n" + DateTime.Now.ToString("yyy-M-d H:m:s");
SqlConnection con = new SqlConnection(count);
con.Open();
string sql = "select * from data where id='" + ID + "'";//这个查询账号的信息,再进行读取余额
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
i = Convert.ToInt32(dr["money"].ToString().Trim());//自己的本钱/
}
con.Close();
int k = i - Convert.ToInt32(DRAW);//自己的本钱减去要取出来的钱,然后把k更新到数据可以里
//2018-12-6 14:3:53已取出100 2018-12-6 14:6:24 已取出10元
DT += " 已取出" + DRAW + "元";
string sqlup = "update data set money='" + k + "',RECORD='" + DT + "'where id='" + ID + "'";//执行sql语句
//string sql02 = "update DATA set mory='" + k + "',RECORD='" + NOW + "'where ID='" + textBox1.Text.Trim() + "'";
con.Open();
SqlCommand cmdup = new SqlCommand(sqlup, con);
int n = cmdup.ExecuteNonQuery();
con.Close();
mm = null;
mm = new model();
mm.money = Convert.ToString(i);
//要把已经计算好的金额更新到数据库中
return mm;
}
}
/*储存*/
public model Storage(string ID, string Money)
{
string DT_sql = "select * from DATA";
SqlConnection DT_con = new SqlConnection(count);
DT_con.Open();
SqlCommand DT_cmd = new SqlCommand(DT_sql, DT_con);
SqlDataReader DT_dr = DT_cmd.ExecuteReader();
int m = 0;
if (DT_dr.HasRows)
{
DT_dr.Read();
/*-----------先读取之前转账的信息------------*/
DT += DT_dr["RECORD"].ToString().Trim();
m = Convert.ToInt32(DT_dr["MONEY"].ToString().Trim());
}
DT_dr.Close();
DT_con.Close();
DT += "\n" + DateTime.Now.ToString("yyy-M-d H:m:s");
if (Money == "")
{
model i_mm = null;
return i_mm;
}
else
{
//model mm = new model();
model mm = null;
SqlConnection con = new SqlConnection(count);
con.Open();
string sql = "select * from data where id='" + ID + "'";//这个查询账号的信息,再进行读取余额
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
i = Convert.ToInt32(dr["money"].ToString().Trim());//自己的本钱/
}
con.Close();
int k = i + Convert.ToInt32(Money);//自己的本钱减去要取出来的钱,然后把k更新到数据可以里
DT += " 已存入" + Money + "元";
string sqlup = "update data set money='" + k + "',RECORD='"+DT+"'where id='" + ID + "'";//执行sql语句
con.Open();
SqlCommand cmdup = new SqlCommand(sqlup, con);
int n = cmdup.ExecuteNonQuery();
con.Close();
mm = null;
mm = new model();
mm.money = Convert.ToString(k);
//要把已经计算好的金额更新到数据库中
return mm;
}
}
/*转账*//*没有卡号能减少自己的本金,没有金额会报错*/
int n;
public model T_accounts(string ID, string YouID, string Ta)
{
/*有这账号,没有金额报错,会报错*///ok
/*,没有账号,会提示没有这个账号,但是有金额可以转账成功*/
string Ta_sql = "select * from DATA";
SqlConnection Ta_con = new SqlConnection(count);
Ta_con.Open();
SqlCommand Ta_cmd = new SqlCommand(Ta_sql, Ta_con);
SqlDataReader Ta_dr = Ta_cmd.ExecuteReader();
int m = 0;
if (Ta_dr.HasRows)
{
Ta_dr.Read();
/*-----------先读取之前转账的信息------------*/
DT += Ta_dr["RECORD"].ToString().Trim();
m = Convert.ToInt32(Ta_dr["MONEY"].ToString().Trim());
}
Ta_dr.Close();
Ta_con.Close();
DT += "\n" + DateTime.Now.ToString("yyy-M-d H:m:s");
if (Ta == ""||YouID=="" ||Convert.ToInt32(Ta) > Convert.ToInt32(m))
{
model mm = null;
return mm;
}
else
{
model mm = new model();
mm.id = YouID;
SqlConnection con = new SqlConnection(count);
con.Open();
string sql = "select * from data where id='" + ID + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
mm.money = dr["money"].ToString().Trim();//获取自己的余额
dr.Close();
}
/*n相当于自己的余额减去转账的金额--然后把这个n更新到数据库里面*/
DT += " 已转入" + YouID + ":"+Ta+"元";
n = Convert.ToInt32(mm.money) - Convert.ToInt32(Ta);
string n_sql = "update data set money='" + n + "',RECORD='"+DT+"'where id='" + ID + "'";
SqlCommand n_cmd = new SqlCommand(n_sql, con);
int i = n_cmd.ExecuteNonQuery();
string m_sql = "select * from data where id='" + YouID + "'";
SqlCommand m_cmd = new SqlCommand(m_sql, con);
SqlDataReader m_dr = m_cmd.ExecuteReader();
if (m_dr.Read())
{
mm.money = "";
mm.money = m_dr["money"].ToString().Trim();//获取自己的余额
}
m_dr.Close();
int k_1 = Convert.ToInt32(mm.money);
int k_2= Convert.ToInt32(Ta);
k_1 += k_2;
string q_sql = "update data set money='" + k_1 + "'where id='" + YouID + "'";
SqlCommand q_cmd = new SqlCommand(q_sql, con);
int j = q_cmd.ExecuteNonQuery();
con.Close();
return mm;
}
}
/*记录*/
public model record(string ID)
{
model mm = new model();
SqlConnection con = new SqlConnection(count);
string sql = "select * from data where id='" + ID + "'";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (mm == null)
{
mm = new model();
}
mm.record = dr["RECORD"].ToString().Trim();
}
con.Close();
return mm;
}
}
}
BLL中的bll.cs代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MODEL;
namespace BLL
{
public class bll
{
//登录方法
public int select(string ID, string PWD)
{
DAL.dal dal = new DAL.dal();
model mm = dal.select(ID, PWD);
if (mm != null && mm.id == ID)
{
if (mm.pwd == PWD)
{
return 0;//登陆成功
}
return 1;//密码错误
}
return 2;//没有这个账号
}
//修改密码
public int update(string ID, string SFZ,string PWD)
{
DAL.dal dal = new DAL.dal();
model mm = dal.update(ID,SFZ,PWD);
if (mm !=null && mm.id==ID)
{
if (mm.sfz==SFZ)
{
return 0;
}
return 1;
}
return 2;
}
//查询余额
public string money_select(string ID)
{
DAL.dal dal = new DAL.dal();
model mm = dal.money_select(ID);
return mm.money; //余额
}
//取款
public int draw(string ID,string DRAW)
{
DAL.dal dal = new DAL.dal();
model mm = dal.draw(ID,DRAW);
if (DRAW == "")
{
return 2;//不能为空
}
else
{
BLL.bll bll = new BLL.bll();
if (DRAW!=null)
{
if(Convert.ToInt32(DRAW)>Convert.ToInt32(mm.money))//100>1
{
return 1;
}
return 0;
//要取款的金额不能大于余额或小于负数
}
return 2;
}
//取款成功
}
/*储存*/
public int Storage(string ID, string Money)
{
DAL.dal dal = new DAL.dal();
model mm = dal.Storage(ID,Money);
if (Money == "")
{
return 1;//不能为空
}
else
{
//int i = Convert.ToInt32(mm.money);
BLL.bll bll = new BLL.bll();
//int m = Convert.ToInt32(bll.money_select(ID));
return 0;
}
}
/*转账*/
public int T_accounts(string ID, string YouID, string Ta)
{
DAL.dal dal = new DAL.dal();
model mm = dal.T_accounts(ID,YouID,Ta);
if (Ta==""&&YouID=="")
{
return 2;//对方帐号和要转账的金额不能为空
}
//int i = Convert.ToInt32(mm.money);
BLL.bll bll = new BLL.bll();
int m = Convert.ToInt32(bll.money_select(ID));//查询余额
if(mm!=null&&mm.id==YouID&&Ta!=null&YouID!=null)
{
if (Convert.ToInt32(Ta)>m)
{
return 0; //要转账的金额不能大于余额
} return 1; //转账成功
}
return 2;
//没有这个账号
}
/*交易明细*/
public string Record(string ID)
{
DAL.dal dal = new DAL.dal();
model mm = dal.record(ID);
return mm.record;
}
}
}
ul层的窗体界面如图
接下来是ul层中form1(登陆)的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MODEL;
namespace 三层架构之ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "卡号:";
label2.Text = "密码:";
button1.Text = "登陆";
button2.Text = "找回";
string ID = textBox1.Text;
string PWD = textBox2.Text;
//BLL.USERADO bb = new BLL.USERADO();//先实例化逻辑层
BLL.bll bb=new BLL.bll();
int uu = bb.select(ID,PWD);
if (uu==0)
{
Form3 f3 = new Form3(textBox1.Text);
label1.Visible = false;
label2.Visible = false;
button1.Visible = false;
button2.Visible = false;
textBox1.Visible = false;
textBox2.Visible = false;
f3.MdiParent = this;
f3.Show();
MessageBox.Show("登陆成功");
}
else if(uu==1)
{
MessageBox.Show("密码错误");
}
else if(uu==2)
{
MessageBox.Show("没有这个账号");
}
}
private void button2_Click(object sender, EventArgs e)
{
label1.Visible = false;
label2.Visible = false;
button1.Visible = false;
button2.Visible = false;
textBox1.Visible = false;
textBox2.Visible = false;
Form2 f2 = new Form2(this);
f2.MdiParent = this;
f2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
textBox2.UseSystemPasswordChar = true;
}
private void button3_Click(object sender, EventArgs e)
{
label1.Visible = false;
label2.Visible = false;
button1.Visible = false;
button2.Visible = false;
textBox1.Visible = false;
textBox2.Visible = false;
Form2 f2 = new Form2();
f2.Show(this);
}
}
}
form2(找回)的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 三层架构之ATM
{
public partial class Form2 : Form
{
Form1 _form;
public Form2()
{
InitializeComponent();
textBox3.UseSystemPasswordChar = true;
}
public Form2(Form1 form)
{
InitializeComponent();
_form = form;//得到传过来的Form1实例
}
string txt;
public Form2(string str)
{
InitializeComponent();
txt = str;
}
private void button1_Click(object sender, EventArgs e)
{
//textBox1.Text = txt;
//textBox1.ReadOnly = true;
string ID = textBox1.Text;
string PWD = textBox3.Text;
string SFZ = textBox2.Text;
BLL.bll bb = new BLL.bll();
int uu = bb.update(ID,SFZ,PWD);
if (uu==0)
{
MessageBox.Show("修改成功");
} if (uu == 1)
{
MessageBox.Show("身份证错误");
} if (uu == 2)
{
MessageBox.Show("没有这个账号");
}
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f1=new Form1();
f1.Owner=this;
f1.label1.Visible = true;
f1.label2.Visible = true;
f1.button1.Visible = true;
f1.button2.Visible = true;
f1.textBox1.Visible = true;
f1.textBox2.Visible = true;
//button3.Visible = true;
f1.Show();
_form.Hide();
}
}
}
form3(功能)代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MODEL;
namespace 三层架构之ATM
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
string txt;
public Form3(string str)
{
InitializeComponent();
txt = str;
}
/*查询余额*/
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "卡号:";
label2.Text = "余额:";
label2.Visible = true;
string ID = textBox1.Text;
textBox2.Visible = true;
label3.Visible = false;
textBox3.Visible = false;
BLL.bll bll = new BLL.bll();
string uu = bll.money_select(ID);
textBox2.Text = uu;
}
private void Form3_Load(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text = txt;
}
/*取款*/
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "卡号:";
label2.Text = "金额:";
label2.Visible = true;
label1.Visible = true;
textBox1.Visible = true;
textBox1.Text = txt;
string ID =textBox1.Text;
string DRAW =textBox2.Text;
BLL.bll bll = new BLL.bll();
int uu = bll.draw(ID,DRAW);
if (uu==0)
{
MessageBox.Show("取款成功");
}if(uu==1)
{
MessageBox.Show("输入的金额不能大于余额");
}if(uu==2)
{
MessageBox.Show("要取款的金额不能为空");
}
}
private void button1_MouseLeave(object sender, EventArgs e)
{
textBox2.Text = "";
}
/*存款*/
private void button3_Click(object sender, EventArgs e)
{
string ID = textBox1.Text;
string Money = textBox2.Text;
BLL.bll bll = new BLL.bll();
int uu = bll.Storage(ID,Money);
if(uu==0)
{
MessageBox.Show("存款成功");
} if (uu == 1)
{
MessageBox.Show("请输入要储存的金额");
}
//int uu=bll.
}
/*转账*/
private void button4_Click(object sender, EventArgs e)
{
string ID = textBox1.Text;
string YouID = textBox2.Text;
string Ta = textBox3.Text;
BLL.bll bll = new BLL.bll();
int uu = bll.T_accounts(ID,YouID,Ta);
if (uu==0)
{
MessageBox.Show("要转账的金额不能大于余额");
}if(uu==1)
{
MessageBox.Show("转账成功");
}if(uu==2)
{
MessageBox.Show("不存在这个卡号或金额不能为空");
}
}
/*鼠标进入控件事件*/
private void button4_MouseEnter(object sender, EventArgs e)
{
label2.Text = "对方卡号:";
label2.Visible = true;
textBox2.Visible = true;
label3.Text = "金额:";
label3.Visible = true;
textBox3.Visible = true;
textBox3.Width=100;
textBox3.Height=21;
}
/*鼠标进入控件事件*/
private void button2_MouseEnter(object sender, EventArgs e)
{
label2.Text = "金额:";
label2.Visible = true;
label3.Visible = false;
textBox3.Visible = false;
textBox2.Visible = true;
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
private void button3_MouseEnter(object sender, EventArgs e)
{
label2.Text = "金额:";
label2.Visible = true;
label3.Visible = false;
textBox3.Visible = false;
textBox2.Visible = true;
}
private void button5_Click(object sender, EventArgs e)
{
//textBox3.Size=(201, 67);
string ID = textBox1.Text;
model mm = new model();
BLL.bll bll = new BLL.bll();
string uu= bll.Record(ID);
textBox3.Text= uu;
}
private void button6_Click(object sender, EventArgs e)
{
this.Close();
}
private void button5_MouseEnter(object sender, EventArgs e)
{
label2.Visible = false;
textBox2.Visible = false;
label3.Text = "交易记录";
textBox3.Width = 201;
textBox3.Height = 67;
}
private void button5_MouseLeave(object sender, EventArgs e)
{
textBox3.Text = "";
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0); //关闭程序
}
}
}