📄 bank.java
字号:
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
//import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
public class Bank {
// private ArrayList<Account> accounts;
private DBOperator dbOperator;
/**
* Constructs a bank with no customers.
*/
public Bank() {
dbOperator = new DBOperator();
// addCustomer( "xiaoming", "meiyuan", null,
// "123456789", "123456", 89.12);
// try{
// Account ac = findCustomer("123456","123456");
// System.out.println(ac);
// System.out.println(ac.getBalance());
// }
// catch(Exception ex){System.out.println(ex.toString()); }
// accounts = new ArrayList<Account>();
}
/**
* Reads the customer numbers and pins and initializes the bank accounts.
*
* @param filename
* the name of the customer file
*/
// public void readCustomers(String filename) throws IOException {
// Scanner in = new Scanner(new FileReader(filename));
// boolean done = false;
// while (in.hasNext()) {
// String number = in.next();// todo........................
// String pin = in.next();// todo..........................
// Account c = new Account(number, pin);
// addCustomer(c);
// }
// in.close();
// }
/**
* Adds a customer to the bank.
*
* @param c
* the customer to add
*/
// public void addCustomer(Account c) {
// accounts.add(c);
// }
public void addCustomer(String name, String address, String password,
String account, String pin, double balance) {
Account c = new Account(name, address, password, account, pin, balance);
c.addAccount();
}
public synchronized boolean deposit(String Account, double amount)
throws SQLException {
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn.prepareStatement("UPDATE Accounts"
+ " SET Balance = Balance + ?" + " WHERE Num = ?");
stat.setDouble(1, amount);
stat.setString(2, Account);
stat.executeUpdate();
Account c = null;
String query = "select * from Accounts where Num = ? ";
stat = conn.prepareStatement(query);
stat.setString(1, Account);
ResultSet result = stat.executeQuery();
if (result.next()) {
c = new Account(result.getString("Guest"), result
.getString("Address"), null, result.getString("Num"),
result.getString("pin"), result.getDouble("Balance"));
c.addOperator("deposit", amount);
conn.close();
return true;
}
return false;
}
/**
* Finds a customer with a given number and PIN.
*
* @param customerNumber
* the customer number
* @param pin
* the personal identification number
* @return the matching customer, or null if none found
*/
public Account findCustomer(String customerNumber, String pin)
throws SQLException {
Connection conn = dbOperator.getConnection();
try {
Account c = null;
String query = "select * from Accounts where Num = ? ";
PreparedStatement stat = conn.prepareStatement(query);
stat.setString(1, customerNumber);
// Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery();
// System.out.println(result.next());
if (result.next() && pin.equals(result.getString("pin")))
c = new Account(result.getString("Guest"), result
.getString("Address"), null, result.getString("Num"),
pin, result.getDouble("Balance"));
return c;
} finally {
conn.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -