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(); } } }
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; } } }}
时间: 2024-10-25 20:13:18