要求:通过Java控制台开发一个银行管理系统,使用MySQL作为后台数据,实现银行管理员工功能和顾客功能。具体要求如下: (1)管理员功能:登录、添加顾客、删除顾客、计算存储金额、富豪排行榜、退出 代码
时间: 2023-07-10 21:35:54 浏览: 92
好的,下面是一个简单的Java控制台程序,实现了银行管理系统的管理员功能:
```java
import java.sql.*;
public class BankManagementSystem {
// 定义数据库连接信息
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/bank";
static final String USER = "root";
static final String PASS = "password";
// 定义数据库连接对象和语句对象
static Connection conn = null;
static Statement stmt = null;
public static void main(String[] args) {
try{
// 注册JDBC驱动
Class.forName(JDBC_DRIVER);
// 打开数据库连接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 创建Statement对象
System.out.println("创建Statement对象...");
stmt = conn.createStatement();
// 进入管理员系统
adminSystem();
// 关闭数据库连接
stmt.close();
conn.close();
}catch(SQLException se){
// 处理JDBC错误
se.printStackTrace();
}catch(Exception e){
// 处理Class.forName错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("程序结束...");
}
// 管理员系统
public static void adminSystem() throws SQLException {
System.out.println("欢迎进入管理员系统!");
boolean flag = true;
while(flag) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 登录");
System.out.println("2. 添加顾客");
System.out.println("3. 删除顾客");
System.out.println("4. 计算存储金额");
System.out.println("5. 富豪排行榜");
System.out.println("6. 退出");
System.out.print("请输入操作编号:");
int choice = Integer.parseInt(System.console().readLine());
switch(choice) {
case 1:
adminLogin();
break;
case 2:
addCustomer();
break;
case 3:
deleteCustomer();
break;
case 4:
calculateBalance();
break;
case 5:
rankCustomers();
break;
case 6:
flag = false;
break;
default:
System.out.println("请输入正确的操作编号!");
}
}
}
// 管理员登录
public static void adminLogin() throws SQLException {
System.out.println("请输入管理员用户名和密码:");
System.out.print("用户名:");
String username = System.console().readLine();
System.out.print("密码:");
String password = System.console().readLine();
String sql = "SELECT * FROM admin WHERE username='" + username + "' AND password='" + password + "'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
System.out.println("登录成功!");
} else {
System.out.println("用户名或密码错误!");
}
rs.close();
}
// 添加顾客
public static void addCustomer() throws SQLException {
System.out.println("请输入顾客信息:");
System.out.print("姓名:");
String name = System.console().readLine();
System.out.print("身份证号:");
String idcard = System.console().readLine();
System.out.print("手机号:");
String phone = System.console().readLine();
System.out.print("账户名:");
String account = System.console().readLine();
System.out.print("密码:");
String password = System.console().readLine();
String sql = "INSERT INTO customer (name, idcard, phone, account, password, balance) VALUES ('" +
name + "','" + idcard + "','" + phone + "','" + account + "','" + password + "',0)";
int result = stmt.executeUpdate(sql);
if(result == 1) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
}
// 删除顾客
public static void deleteCustomer() throws SQLException {
System.out.println("请输入要删除的顾客身份证号:");
String idcard = System.console().readLine();
String sql = "DELETE FROM customer WHERE idcard='" + idcard + "'";
int result = stmt.executeUpdate(sql);
if(result == 1) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
}
// 计算存储金额
public static void calculateBalance() throws SQLException {
String sql = "SELECT SUM(balance) AS total FROM customer";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
double total = rs.getDouble("total");
System.out.println("存储金额总和为:" + total);
}
rs.close();
}
// 富豪排行榜
public static void rankCustomers() throws SQLException {
System.out.print("请输入要查询的前几名:");
int num = Integer.parseInt(System.console().readLine());
String sql = "SELECT * FROM customer ORDER BY balance DESC LIMIT " + num;
ResultSet rs = stmt.executeQuery(sql);
System.out.println("排行榜:");
while(rs.next()) {
String name = rs.getString("name");
double balance = rs.getDouble("balance");
System.out.println(name + ":" + balance);
}
rs.close();
}
}
```
注意,上述代码只实现了银行管理系统的管理员功能,顾客功能还需要您自己完成。同时,您需要根据您的具体情况修改数据库连接信息和SQL语句。祝您顺利完成!