亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java模擬ATM功能的詳細解析

發布時間:2020-07-21 14:29:49 來源:億速云 閱讀:176 作者:小豬 欄目:編程語言

這篇文章主要講解了java模擬ATM功能的詳細解析,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

有三個類:Test.java、Customer.java、CustomerDAO.java、ATMSystem.java

ATMSystem.java實現模擬ATM的功能

Customer.java創建一個用戶對象

CustomerDAO.java實現連接數據庫

Test為主程序 

ATMSystem.java:

package ATM;
 
import java.util.Scanner;
 
public class ATMSystem 
{
 CustomerDAO dao=new CustomerDAO();
 Scanner input=new Scanner(System.in);
 private int id;
 public ATMSystem()
 {
 
 }
 public void Regist()
 {
 System.out.println("注冊卡號:");
 int myid=input.nextInt();
 System.out.println("注冊用戶名:");
 String myname=input.next();
 System.out.println("注冊密碼:");
 String mypwd=input.next();
 System.out.println("輸入存入金額:");
 int mymoney=input.nextInt();
 dao.Regist(myid, mypwd, myname, mymoney);
 }
 public boolean login()
 {
 System.out.println("請輸入密碼:");
 String mypwd=input.next();
 id=dao.login(mypwd);
 if(id==-1)
 {
  System.out.println("登錄失敗!");
  return false;
 }
 else
 {
  System.out.println("登陸成功!");
  return true;
 }
 }
 public void query(int id)
 {
 Customer cust=dao.query(id);
 System.out.println("********************");
 System.out.println("卡號:"+cust.getid());
 System.out.println("余額:"+cust.getmoney());
 System.out.println("用戶名:"+cust.getname());
 System.out.println("********************");
 }
 public void save()
 {
 System.out.println("輸入存款金額:");
 int money=input.nextInt();
 dao.update_save(id, money);
 }
 public void get()
 {
 System.out.println("********************");
 System.out.println("輸入取款金額:");
 int money=input.nextInt();
 dao.update_get(id, money);
 System.out.println("********************");
 }
 public void change()
 {
 System.out.println("請輸入轉賬卡號:");
 int inid=input.nextInt();
 System.out.println("請輸入轉賬金額:");
 int money=input.nextInt();
 dao.change(id, inid, money);
 }
 public void begin()
 {
 int num=-1;
 int Num=0;
 System.out.println("請選擇功能:");
 System.out.println("1.注冊      2.登錄");
 Num=input.nextInt();
 switch(Num)
 {
 case 1:
  Regist();
  break;
 case 2:
  if(login())
  {
  while(num!=0)
  {
   System.out.println("********************");
   System.out.println("歡迎歡迎,熱烈歡迎");
   System.out.println("請選擇功能:");
   System.out.println("1.存款     2.取款   3.查詢   4.轉賬  0.退出");
   System.out.println("********************");
   num=input.nextInt();
   switch(num)
   {
   case 1:
   save();
   break;
   case 2:
   get();
   break;
   case 3:
   query(id);
   break;
   case 4:
   change();
   break;
   case 0:
   System.out.println("謝謝使用!");
   break;
   }
  }
  }
  else
  {
  System.out.println("登錄失敗,賬號、密碼錯誤!");
  }
 break;
 default:
  break;
 }
 }
}

Customer.java

package ATM;
 
public class Customer 
{
 private int id;
 private String pwd;
 private int money;
 private String name;
 public int getid()
 {
 return id;
 }
 public void setid(int newid)
 {
 id=newid;
 }
 public String getpwd()
 {
 return pwd;
 }
 public void setpwd(String newpwd)
 {
 pwd=newpwd;
 }
 public String getname()
 {
 return name;
 }
 public void setname(String newname)
 {
 name=newname;
 }
 public int getmoney()
 {
 return money;
 }
 public void setmoney(int newmoney)
 {
 money=newmoney;
 }
}

CustomerDAO.java:

package ATM;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
public class CustomerDAO
{
 private Connection conn=null;
 private java.sql.PreparedStatement p=null;
 public CustomerDAO()
 {
 try {
  Class.forName("org.gjt.mm.mysql.Driver");
 } catch (ClassNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 try {
  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "");
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
 public int login(String mypwd)
 {
 String sql="select * from mysql where pwd = ?";
 try 
 {
  p=conn.prepareStatement(sql);
  p.setString(1, mypwd);
  ResultSet rs=p.executeQuery();
  if(rs.next())
  {
  return rs.getInt("id");
  }
  rs.close();
  p.close();
  conn.close();
 } 
 catch (SQLException e) 
 {
  e.printStackTrace();
 }
 return -1;
 }
 public Customer query(int id)
 {
 Customer cust=new Customer();
 try
 {
  String sql="select * from mysql where id = ?";
  p=conn.prepareStatement(sql);
  p.setInt(1, id);
  ResultSet rs=p.executeQuery();
  if(rs.next())
  {
  cust.setid(rs.getInt("id"));
  cust.setmoney(rs.getInt("money"));
  cust.setname(rs.getString("name"));
  cust.setpwd(rs.getString("pwd"));
  }
 }
 catch (Exception e) {
  // TODO: handle exception
 }
 return cust;
 
 }
 public void update_get(int id,int money) 
 {
 String sql="select * from mysql where id = ?";
 try 
 {
  p=conn.prepareStatement(sql);
  p.setInt(1, id);
  ResultSet rs=p.executeQuery();
  if(rs.next())
  {
  if(rs.getInt("money")<money)
  {
   System.out.println("余額不足!");
  }
  else
  {
   sql="update mysql set money = money - &#63; where id = &#63;";
   p=conn.prepareStatement(sql);
   p.setInt(1, money);
   p.setInt(2, id);
   p.executeUpdate();
   System.out.println("取款成功!");
  }
  }
 }
 catch (Exception e)
 {
  // TODO: handle exception
 }
 }
 public void update_save(int id,int money)
 {
 String sql="update mysql set money = money + &#63; where id = &#63;";
 try 
 {
  p=conn.prepareStatement(sql);
  p.setInt(1, money);
  p.setInt(2, id);
  p.executeUpdate();
  System.out.println("存款成功!");
 }
 catch (Exception e) 
 {
  // TODO: handle exception
 }
 }
 public void change(int myid,int inid,int money)
 {
 try
 { String sql="select * from mysql where id = &#63;";
  p=conn.prepareStatement(sql);
  p.setInt(1, inid);
  ResultSet rs=p.executeQuery();
  if(rs.next())
  {
  sql="select * from mysql where id = &#63;";
  p=conn.prepareStatement(sql);
  p.setInt(1, myid);
  rs=p.executeQuery();
  if(rs.next())
  {
   if(rs.getInt("money")<money)
   {
   System.out.println("余額不足!");
   return;
   }
   else
   {
   sql="update mysql set money = money - &#63; where id = &#63;";
   p=conn.prepareStatement(sql);
   p.setInt(1, money);
   p.setInt(2, myid);
   p.executeUpdate();
   sql="update mysql set money = money + &#63; where id = &#63;";
   p=conn.prepareStatement(sql);
   p.setInt(1, money);
   p.setInt(2, inid);
   p.executeUpdate();
   }
  }
  }
  else
  {
  System.out.println("卡號錯誤!");
  }
 }
 catch (Exception e) {
  // TODO: handle exception
 }
 }
 public void Regist(int myid,String mypwd,String myname,int mymoney)
 {
 String sql;
 try
 {
  sql="select * from mysql where id = &#63;";
  p=conn.prepareStatement(sql);
  p.setInt(1, myid);
  ResultSet rs=p.executeQuery();
  if(rs.next())
  {
  System.out.println("此卡已注冊,請勿重復注冊!!!!");
  return;
  }
 }
 catch (Exception e)
 {
  // TODO: handle exception
 }
 
 try 
 {
  sql="insert into mysql(id,pwd,money,name) values(&#63;,&#63;,&#63;,&#63;)";
  p=conn.prepareStatement(sql);
  p.setInt(1, myid);
  p.setString(2, mypwd);
  p.setInt(3, mymoney);
  p.setString(4, myname);
  p.executeUpdate();
  System.out.println("注冊成功!");
 }
 catch (Exception e) 
 {
  // TODO: handle exception
 }
 }
}

Test.java:

package ATM;
 
public class Test 
{
 public static void main(String[] args) 
 {
 // TODO Auto-generated method stub
 ATMSystem ATM=new ATMSystem();
 while(true)
 {
  ATM.begin();
 }
 }
 
}

看完上述內容,是不是對java模擬ATM功能的詳細解析有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

丹寨县| 雷波县| 长沙市| 永靖县| 达州市| 长岛县| 阿坝| 株洲县| 黄陵县| 封开县| 太白县| 固阳县| 上思县| 鄂温| 青冈县| 利津县| 河南省| 垫江县| 江北区| 通江县| 麻城市| 茌平县| 合作市| 普定县| 富顺县| 泰宁县| 东乌珠穆沁旗| 屯留县| 水富县| 乐安县| 金寨县| 稷山县| 绥阳县| 双城市| 英吉沙县| 布拖县| 长治县| 开江县| 新平| 密山市| 吉水县|