• 模拟ATM机将输入的数据插入数据库


    ATM抽象类

    public abstract class ATM {
        
        private double balance;
        private String idcard;
        private String password;
        public String getIdcard() {
            return idcard;
        }
        public void setIdcard(String idcard) {
            this.idcard = idcard;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public double getBalance() {
            return balance;
        }
        public void setBalance(double balance) {
            this.balance = balance;
        }
        public void balance()
        {
            
        }
        public boolean cunkuan(double cun)
        {
            if(cun >0)
            { 
                balance += cun;
                System.out.println("存款成功!");
                return true;
            }
            else
            {
                System.out.println("您输入的存款数不正确,将退出该服务!");
                return false;
            }
        }
        public boolean qukuan(double qu)
        {
            if(qu <= balance)
            {   balance -=qu;
                System.out.println("取款成功!");
                return true;
            }
            else
            {
                System.out.println("取款失败,将退出该服务!");
                return false;
            }
        }
        //添加开户的抽象方法
        public abstract void kaihu();
    
    }

    ATM子类,并实现其开户的方法

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    public class ChildATM extends ATM {
     
        //添加抽象类的开户方法并实现该方法
        @Override
        public void kaihu() {
            System.out.println("*********************");
            System.out.println("您选择的为开户功能");
            System.out.println("请输入您的姓名:");
            ChildATM cs= new ChildATM();
            cs.setBalance(300);
            Scanner  sc = new Scanner(System.in);
            String name=sc.nextLine();
            for(int i=1;i>0;i++)
            {
                System.out.println("请输入您的身份证号");
                Scanner sc1= new Scanner(System.in);
                String idcard=sc1.nextLine();
                if(idcard.length()==18)
                {   //判断密码输入的次数
                    for(i=0;i<3;i++)
                    {
                        System.out.println("请输入您的密码");
                        String password =sc.nextLine();
                        int input =Integer.parseInt(password) ;
                        if(input >=100000 && input <1000000)
                        {
                            System.out.println("开户成功");
                            cs.setIdcard(idcard);
                            cs.setPassword(password);
                            break;
                        }
                        else
                        {
                            System.out.println("请输入6位数的密码");
                        }
                        System.out.println("您输入的密码有误,请重新输入"+"您的输入次数还有"+(2-i));
                        
                    }
                    break;
                }
                else
                {
                    System.out.println("身份证位数不正确,请继续输入");
                    
                }
            }        
                int account =(int)((Math.random()*10)*100000);
            System.out.println("您的账号为:"+account);
            //访问数据库
                    //1.加载驱动,加载数据库提供的驱动类
                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        //2.获取数据库的连接 :通过数据库的管理
                        //url:数据库的地址
                        //user:用户名
                        //password:密码
                        Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "laoer123");
                        System.out.println("数据库连接成功");
                        //操作数据库
                        //3.获得操作数据库的声明
                        Statement st = conn.createStatement();
                        //4.DML增加数据
                        //执行更新操作
                        //返回值代表该操作值记录返回的条数
                        //将输入的数据存入数据库中
                    
                        int i = st.executeUpdate("insert into account (a_id,a_name,a_password,a_idcard,a_balance)"+"values('"+account+"','"+name+"','"+cs.getPassword()+"','"+cs.getIdcard()+"','"+cs.getBalance()+"')");
                        System.out.println("操作数据成功,返回值="+i);
                        //关闭释放资源
                        st.close();
                        conn.close();
                        } 
                    
                    catch (ClassNotFoundException | SQLException e) {
                            e.printStackTrace();
                    }
            
    
        }
    
    }

    实现查询账户数据

     public void cha()
        {
             System.out.println("*******************");
            System.out.println("您选择的是查询功能");
            System.out.println("请输入您的卡号");
            ChildATM cs= new ChildATM();
            Scanner sc= new Scanner(System.in);
             String kahao=sc.nextLine();
             //判断卡号是否正确
             try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "laoer123");
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery("select * from account where a_id='"+kahao+"'");
                //查询用户输入卡号的结果集
               if(rs.next())
               {
                   String ss = rs.getString("a_password");
                   for(int i=0;i<3;i++)
                   {
                       System.out.println("请输入您的密码");
                       Scanner sc2 = new Scanner(System.in);
                       String mima=sc2.nextLine();
                       if(ss.equals(mima))
                       {
                           System.out.println("姓名:"+rs.getString("a_name")+"账号:"+rs.getString("a_id")+"密码:"+rs.getString("a_password")+"身份证号:"+rs.getString("a_idcard")+"余额:"+rs.getString("a_balance"));
                           break;
                       }
                       else
                       {
                           System.out.println("请重新输入密码");
                       }
                   }
               }
               else
               {
                   System.out.println("您输入的卡号不存在!");
               }
                st.close();
                conn.close();
                } 
            
            catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
            }
    
        }

    实现存款

     public  void cunkuan()
      {
          System.out.println("*******************");
          System.out.println("您选择的是存款功能");
          System.out.println("请输入您的卡号");
          ChildATM cs= new ChildATM();
            Scanner sc= new Scanner(System.in);
          String kahao=sc.nextLine();
          //判断卡号是否正确
          try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "laoer123");
              Statement st = conn.createStatement();
              ResultSet rs = st.executeQuery("select * from account where a_id='"+kahao+"'");
              //查询用户输入卡号的结果集
             if(rs.next())
             {
                 String ss = rs.getString("a_password");
                 for(int i=0;i<3;i++)
                 {
                     System.out.println("请输入您的密码");
                     Scanner sc2 = new Scanner(System.in);
                     String mima=sc2.nextLine();
                     if(ss.equals(mima))
                     {
                         System.out.println("姓名:"+rs.getString("a_name")+"账号:"+rs.getString("a_id")+"密码:"+rs.getString("a_password")+"身份证号:"+rs.getString("a_idcard")+"余额:"+rs.getString("a_balance"));
                         System.out.println("请输入您要存款的金额");
                         double cun=sc2.nextDouble();
                         //执行语句
                         if(cun>0)
                         {
                             double money =rs.getDouble("a_balance");
                             money+=cun;
                             String sql ="update account set a_balance="+money+"where a_id ='"+kahao+"'";
                             st.executeUpdate(sql);
                             System.out.println("存款成功");
                         }
                         else
                         {
                             System.out.println("存款失败!");
                         }
                         break;
                     }
                     
                     else
                     {
                         System.out.println("请重新输入密码");
                     }
                 }
             }
             else
             {
                 System.out.println("您输入的卡号不存在!");
             }
              st.close();
              conn.close();
              } 
          
          catch (ClassNotFoundException | SQLException e) {
                  e.printStackTrace();
          }
      
      }

     实现取款

     public  void qukuan()
      {
          System.out.println("*******************");
          System.out.println("您选择的是存款功能");
          System.out.println("请输入您的卡号");
          ChildATM cs= new ChildATM();
            Scanner sc= new Scanner(System.in);
          String kahao=sc.nextLine();
          //判断卡号是否正确
          try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "laoer123");
              Statement st = conn.createStatement();
              ResultSet rs = st.executeQuery("select * from account where a_id='"+kahao+"'");
              //查询用户输入卡号的结果集
             if(rs.next())
             {
                 String ss = rs.getString("a_password");
                 for(int i=0;i<3;i++)
                 {
                     System.out.println("请输入您的密码");
                     Scanner sc2 = new Scanner(System.in);
                     String mima=sc2.nextLine();
                     if(ss.equals(mima))
                     {
                         System.out.println("姓名:"+rs.getString("a_name")+"账号:"+rs.getString("a_id")+"密码:"+rs.getString("a_password")+"身份证号:"+rs.getString("a_idcard")+"余额:"+rs.getString("a_balance"));
                         System.out.println("请输入您要存款的金额");
                         double qu=sc2.nextDouble();
                         //执行语句
                       double money =rs.getDouble("a_balance");
                         if(qu< money)
                         {
                             
                             money-=qu;
                             String sql ="update account set a_balance="+money+"where a_id ='"+kahao+"'";
                             st.executeUpdate(sql);
                             System.out.println("取款成功");
                         }
                         else
                         {
                             System.out.println("取款失败!");
                         }
                         break;
                     }
                     
                     else
                     {
                         System.out.println("请重新输入密码");
                     }
                 }
             }
             else
             {
                 System.out.println("您输入的卡号不存在!");
             }
              st.close();
              conn.close();
              } 
          
          catch (ClassNotFoundException | SQLException e) {
                  e.printStackTrace();
          }
      
      }

    ATM测试类

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    import unionpay.ChildATM;
    
    public class TestBank {
        
        public static void main(String[] args) {
            System.out.println("银行自助终端系统");
            System.out.println("********************");
            System.out.println("0---------------开户");
            System.out.println("1---------------查询");
            System.out.println("2---------------存款");
            System.out.println("3---------------取款");
            System.out.println("4---------------退出");
            ChildATM ca= new ChildATM();
            for(int i=1;i>0;i++){
                System.out.println("请输入您要选择的功能");
                Scanner sc = new Scanner(System.in);
                int num=sc.nextInt();
                switch(num)
                {
                    case 0:
                    ca.kaihu();
                    break;
             
                    case 1:
                    ca.cha();
                    break;
                            case 2:
                ca.cunkuan();
                break;            
             case 3:
                        ca.qukuan();
                    break;
                    case 4:
                        System.out.println("您选择的是退出功能,系统将要退出");
                    return;
                
                    default:
                        System.out.println("您选择的功能不正确,请重新选择");
                        break;

    }
    }
    }
    }

     

     

     

  • 相关阅读:
    spring学习总结009 --- 重复id或name的bean定义允许覆盖allowBeanDefinitionOverriding
    spring学习总结008 --- IOC流程图
    spring学习总结007 --- IOC容器级生命周期接口
    spring学习总结006 --- Bean级生命周期接口
    spring学习总结005 --- IOC容器启动源码(事件机制)
    字体图标
    pycharm永久激活
    Linux常用命令
    Android Studio 更新后导入旧项目Bug解决
    Ubuntu更新源
  • 原文地址:https://www.cnblogs.com/miracle-0807/p/5983342.html
Copyright © 2020-2023  润新知