Simulating ATM machines inserting input data into a database

Source: Internet
Author: User

ATM Abstract class

 Public Abstract classATM {Private Doublebalance; PrivateString Idcard; PrivateString password;  PublicString Getidcard () {returnIdcard; }     Public voidSetidcard (String idcard) { This. Idcard =Idcard; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     Public DoubleGetBalance () {returnbalance; }     Public voidSetbalance (Doublebalance) {         This. Balance =balance; }     Public voidbalance () {} Public BooleanCunkuan (Doublecun) {        if(Cun >0) {Balance+=Cun; System.out.println ("Deposit Success!" "); return true; }        Else{System.out.println ("The number of deposits you have entered is incorrect and will exit the service!" "); return false; }    }     Public BooleanQukuan (Doublequ) {        if(Qu <=balance) {Balance-=Qu; System.out.println ("Successful withdrawals!" "); return true; }        Else{System.out.println ("Withdrawals fail and will exit the service!" "); return false; }    }    //add an abstract method of opening an account     Public Abstract voidKaihu ();}

ATM subclass, and implement its method of opening an account

Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJava.util.Scanner; Public classChildatmextendsATM {//add an abstract class of account opening method and implement the method@Override Public voidKaihu () {System.out.println ("*********************"); System.out.println ("Your choice is the account opening function"); System.out.println ("Please enter your name:"); CHILDATM CS=NewChildatm (); Cs.setbalance (300); Scanner SC=NewScanner (system.in); String name=Sc.nextline ();  for(inti=1;i>0;i++) {System.out.println ("Please enter your ID number"); Scanner SC1=NewScanner (system.in); String Idcard=Sc1.nextline (); if(Idcard.length () ==18)            {   //determine the number of password entries                 for(i=0;i<3;i++) {System.out.println ("Please enter your password"); String Password=Sc.nextline (); intinput =integer.parseint (password); if(Input >=100000 && input <1000000) {System.out.println ("Successful account Opening");                        Cs.setidcard (Idcard);                        Cs.setpassword (password);  Break; }                    Else{System.out.println ("Please enter a 6-digit password"); } System.out.println ("The password you entered is incorrect, please re-enter" + "your number of entries and" + (2-i)); }                 Break; }            Else{System.out.println ("The ID number is not correct, please continue to enter"); }        }                    intAccount = (int) ((Math.random () *10) *100000); System.out.println ("Your account number is:" +Account ); //Accessing the database//1. Load driver, load the drive class provided by the database                Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); //2. Get the connection to the database: through the management of the database//URL: The address of the database//User: Username//Password: passwordConnection conn=drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test0816", "laoer123"); System.out.println ("Database connection succeeded"); //manipulating Databases//3. Obtain a statement of the operational databaseStatement st =conn.createstatement (); //4.DML Add Data//Perform an update operation//The return value represents the number of bars returned by the operation value Record//storing input data in a database                                    inti = 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 ("Operation data succeeded, return value =" +i); //turn off freeing resourcesSt.close ();                    Conn.close (); }                                 Catch(ClassNotFoundException |SQLException e)                {E.printstacktrace (); }            }}

ATM Test class

Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJava.util.Scanner;ImportUnionPay. CHILDATM; Public classTestbank { Public Static voidMain (string[] args) {System.out.println ("Bank self-service terminal system"); System.out.println ("********************"); System.out.println ("0---------------Account Opening"); System.out.println ("1---------------Query"); System.out.println ("2---------------Deposit"); System.out.println ("3---------------Withdrawals"); System.out.println ("4---------------Exit"); CHILDATM CA=NewChildatm ();  for(inti=1;i>0;i++) {System.out.println ("Please enter the features you want to select"); Scanner SC=NewScanner (system.in); intnum=Sc.nextint (); Switch(num) { Case0: Ca.kaihu ();  Break;
}
}
}
}

Simulating ATM machines inserting input data into a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.