0% found this document useful (0 votes)
13 views

ATM Project 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ATM Project 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

--------------------------------mysql-

code-------------------------------------------------

show databases;

create database Project2;

use Project2;

show tables;

create table Accounts (Account_holder_name varchar(120) not null,ATM_pin int not


null unique, Amount int not null);
insert into Accounts values ("Geetha", 1819, 50000),("Dhilli",5274,20000),
("Vamsi",7268,30000),("Manoj",4352,35000),("Manikumar",6732,40000);

-------------------------------------java-
code-----------------------------------------------

package GG1.JdbcProject;

import java.util.*;
import java.sql.*;

class Project2 {
//method for balance checking:-
public int getBalance(int pin) throws Exception{

Connection con1 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

//assigning a variable for getting existing amount of given


account holder
String query = "select Amount from Accounts where ATM_pin =
?";

PreparedStatement st2 = con1.prepareStatement(query);


st2.setInt(1, pin);

ResultSet rs2 = st2.executeQuery();

rs2.next();
int existing_amount = rs2.getInt(1);

st2.close();
con1.close();

return existing_amount;
}

public void deposit(int pin,int balance_amount,Scanner var)


throws Exception {
System.out.println("Enter the deposit amount.");
int deposit_amount = var.nextInt();

//assigning a variable for update the deposit amount of


given account holder
String deposit = "update Accounts set Amount ="+
(balance_amount+deposit_amount)+" where ATM_pin = ?";

Connection con2 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

//PreparedStatement for updating deposit amount


PreparedStatement st2 = con2.prepareStatement(deposit);
st2.setInt(1,pin);
st2.executeUpdate();

st2.close();
con2.close();

public void withdrawal(int pin,int balance_amount,Scanner var,int


withdrawal_amount) throws Exception {

//assigning a variable for update the deposit amount of


given account holder
String deposit = "update Accounts set Amount ="+
(balance_amount-withdrawal_amount)+" where ATM_pin = ?";

Connection con3 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

//PreparedStatement for updating deposit amount


PreparedStatement st3 = con3.prepareStatement(deposit);
st3.setInt(1,pin);
st3.executeUpdate();

con3.close();
st3.close();

public void pinChange(int pin, Scanner var) throws Exception {


System.out.println("Enter your new pin");
int new_pin = var.nextInt();

//assigning a variable for update the deposit amount of


given account holder
String pin_change = "update Accounts set ATM_pin
="+new_pin+" where ATM_pin = ?";
Connection con4 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

//PreparedStatement for updating deposit amount


PreparedStatement st4 = con4.prepareStatement(pin_change);
st4.setInt(1,pin);
st4.executeUpdate();

st4.close();
st4.close();

public class Atm_machine2 {


public static void main(String[] args) throws Exception {

//object creation of project2 class.


Project2 p1 = new Project2();

Scanner var = new Scanner(System.in);

//load and registering the Driver.


Class.forName("com.mysql.jdbc.Driver");

//Establish the connection.


Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

//assiging a variable for getting all ATM_pin values


String Pins = "select ATM_pin from Accounts";

//Statement creation.
Statement st1 = con.createStatement();

//Resultset.
ResultSet rs1 = st1.executeQuery(Pins);

//storing the Atm_pin values in ArrayList.


ArrayList<Integer> Atm_pins1 = new ArrayList<Integer>();

while(rs1.next()) {
//adding the values in arraylist with using add method.
Atm_pins1.add(rs1.getInt(1));
}

//closing the coonection.


st1.close();
con.close();
System.out.println("Welcome to SBI ATM.");

boolean T = true;

while(T) {

System.out.println("Enter the Pin: ");


int pin = var.nextInt();

Connection con5 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Project2","root","Root123$
");

if (Atm_pins1.contains(pin)) {

//assigning a variable for getting existing amount of given


account holder
String query1 = "select Account_holder_name from Accounts where
ATM_pin = ?";

PreparedStatement st5 = con5.prepareStatement(query1);


st5.setInt(1, pin);

ResultSet rs5 = st5.executeQuery();

rs5.next();
String account_hoder_name = rs5.getString(1);

System.out.println("Account holder name: "+ account_hoder_name);

T = false;

boolean G = true;
while(G) {

G = false;

System.out.println("select a option ");


System.out.println("1 : balance checking: ");
System.out.println("2 : Deposit ");
System.out.println("3 : Withdrawal");
System.out.println("4 : Pin change");

int choice = var.nextInt();

int balance_amount = p1.getBalance(pin);

if(choice == 1) {

System.out.println("Amount: "+ balance_amount);


System.out.println("Thank you.");
}
else if(choice == 2) {

p1.deposit(pin,balance_amount,var);

System.out.println("Successfully deposit the amount");


System.out.println("Would you like to display the balance
amount on the screen.");
System.out.println("If Yes: Please enter 1 : ");
System.out.println("If No: Please enter 2 : ");
int s = var.nextInt();
if (s == 1) {
System.out.println("Amount: "+ p1.getBalance(pin));
System.out.println("Thank you.");
}
else {
System.out.println("Thank you.");
}
}

else if(choice == 3) {

System.out.println("Enter the withdrawal amount.");


int withdrawal_amount = var.nextInt();

if (withdrawal_amount<p1.getBalance(pin)) {

p1.withdrawal(pin,balance_amount,var,withdrawal_amount);
System.out.println("Successfully withdrawal the amount");
System.out.println("Would you like to display the balance
amount on the screen.");
System.out.println("If Yes: Please enter 1 : ");
System.out.println("If No: Please enter 2 : ");
int s1 = var.nextInt();
if (s1 == 1) {
System.out.println("Amount: "+ p1.getBalance(pin));
System.out.println("Thank you.");
}
else {
System.out.println("Thank you.");
}

}
else {
System.out.println("Insufficient balance!!!");
System.out.println("Check the balance :");
System.out.println("If Yes: Please enter 1 : ");
System.out.println("If No: Please enter 2 : ");
int s2 = var.nextInt();
if (s2 == 1) {
System.out.println("Amount: "+
p1.getBalance(pin));
System.out.println("Thank you.");
}
else {
System.out.println("Thank you.");
}
}
}
else if(choice == 4) {

p1.pinChange(pin,var);

System.out.println("Your pin changed successfully.");


}
else{
System.out.println("You selected the wrong option. Please
enter valid option.");
G = true;
}
}

}
else{
System.out.println("Please enter valid pin number.");
T=true;
}

}
}

----------------------------------------
output----------------------------------------

condition 1: checking balance amount :-

Welcome to SBI ATM.


Enter the Pin:
5274
Account holder name: Dhilli
select a option
1 : balance checking:
2 : Deposit
3 : Withdrawal
4 : Pin change
1
Amount: 20000
Thank you.

condition 2: deposite amount :-

Welcome to SBI ATM.


Enter the Pin:
1819
Account holder name: Geetha
select a option
1 : balance checking:
2 : Deposit
3 : Withdrawal
4 : Pin change
2
Enter the deposit amount.
2000
Successfully deposit the amount
Would you like to display the balance amount on the screen.
If Yes: Please enter 1 :
If No: Please enter 2 :
1
Amount: 70617
Thank you.

condition 3: withdrawal amount

Welcome to SBI ATM.


Enter the Pin:
1819
Account holder name: Geetha
select a option
1 : balance checking:
2 : Deposit
3 : Withdrawal
4 : Pin change
3
Enter the withdrawal amount.
1500
Successfully withdrawal the amount
Would you like to display the balance amount on the screen.
If Yes: Please enter 1 :
If No: Please enter 2 :
1
Amount: 69117
Thank you.

condition 4: ATM pin change

Welcome to SBI ATM.


Enter the Pin:
7368
Please enter valid pin number.
Enter the Pin:
5274
Account holder name: Dhilli
select a option
1 : balance checking:
2 : Deposit
3 : Withdrawal
4 : Pin change
4
Enter your new pin
1234
Your pin changed successfully.

You might also like