使用java代码模拟实现一个银行系统的部分功能,当客户在登录完成时,实现余额查询,转账功能,存取款,修改密码功能界面使用控制台实现,数据存储使用mysql数据库,Java操作数据库部分使用hutool工具类。
时间: 2023-10-07 13:13:21 浏览: 118
好的,以下是Java代码实现银行系统部分功能的示例:
首先,需要使用Hutool工具类连接MySQL数据库,代码如下:
```java
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlRunner;
public class BankSystem {
private static final String URL = "jdbc:mysql://localhost:3306/bank_system?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 其他操作...
}
}
```
接着,需要实现用户的登录功能,这里假设用户的账号为手机号,密码为6位数字,代码如下:
```java
import java.util.Scanner;
public class BankSystem {
// ...
private static final String TABLE_NAME = "user";
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 用户登录
Scanner scanner = new Scanner(System.in);
System.out.println("请输入手机号:");
String phone = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
Entity user = runner.queryOne(String.format("SELECT * FROM %s WHERE phone = ? AND password = ?", TABLE_NAME), phone, password);
if (user == null) {
System.out.println("手机号或密码错误,登录失败。");
return;
}
System.out.printf("欢迎,%s!%n", user.getStr("name"));
// 其他操作...
}
}
```
接下来,实现余额查询功能,代码如下:
```java
public class BankSystem {
// ...
private static final String ACCOUNT_TABLE_NAME = "account";
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 用户登录...
// 余额查询
Entity account = runner.queryOne(String.format("SELECT * FROM %s WHERE user_id = ?", ACCOUNT_TABLE_NAME), user.getInt("id"));
System.out.printf("您的账户余额为:%f元%n", account.getBigDecimal("balance"));
// 其他操作...
}
}
```
然后,实现转账功能,代码如下:
```java
public class BankSystem {
// ...
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 用户登录...
// 转账
System.out.println("请输入对方账号:");
String toPhone = scanner.nextLine();
Entity toUser = runner.queryOne(String.format("SELECT * FROM %s WHERE phone = ?", TABLE_NAME), toPhone);
if (toUser == null) {
System.out.println("对方账号不存在!");
return;
}
Entity toAccount = runner.queryOne(String.format("SELECT * FROM %s WHERE user_id = ?", ACCOUNT_TABLE_NAME), toUser.getInt("id"));
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("转账金额必须大于0!");
return;
}
Entity account = runner.queryOne(String.format("SELECT * FROM %s WHERE user_id = ?", ACCOUNT_TABLE_NAME), user.getInt("id"));
if (account.getBigDecimal("balance").doubleValue() < amount) {
System.out.println("余额不足!");
return;
}
runner.tx(() -> {
runner.update(String.format("UPDATE %s SET balance = balance - ? WHERE user_id = ?", ACCOUNT_TABLE_NAME), amount, user.getInt("id"));
runner.update(String.format("UPDATE %s SET balance = balance + ? WHERE user_id = ?", ACCOUNT_TABLE_NAME), amount, toUser.getInt("id"));
runner.insert("transfer_log", new Entity()
.set("from_user_id", user.getInt("id"))
.set("to_user_id", toUser.getInt("id"))
.set("amount", amount)
.set("time", System.currentTimeMillis()));
});
System.out.println("转账成功!");
// 其他操作...
}
}
```
接下来,实现存取款功能,代码如下:
```java
public class BankSystem {
// ...
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 用户登录...
// 存取款
Entity account = runner.queryOne(String.format("SELECT * FROM %s WHERE user_id = ?", ACCOUNT_TABLE_NAME), user.getInt("id"));
System.out.printf("您的账户余额为:%f元%n", account.getBigDecimal("balance"));
System.out.println("请选择操作类型:\n1. 存款\n2. 取款");
int type = scanner.nextInt();
if (type == 1) {
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("存款金额必须大于0!");
return;
}
runner.update(String.format("UPDATE %s SET balance = balance + ? WHERE user_id = ?", ACCOUNT_TABLE_NAME), amount, user.getInt("id"));
System.out.printf("存款成功!当前账户余额为:%f元%n", account.getBigDecimal("balance").add(new BigDecimal(amount)));
} else if (type == 2) {
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("取款金额必须大于0!");
return;
}
if (account.getBigDecimal("balance").doubleValue() < amount) {
System.out.println("余额不足!");
return;
}
runner.update(String.format("UPDATE %s SET balance = balance - ? WHERE user_id = ?", ACCOUNT_TABLE_NAME), amount, user.getInt("id"));
System.out.printf("取款成功!当前账户余额为:%f元%n", account.getBigDecimal("balance").subtract(new BigDecimal(amount)));
} else {
System.out.println("无效操作!");
}
// 其他操作...
}
}
```
最后,实现修改密码功能,代码如下:
```java
public class BankSystem {
// ...
public static void main(String[] args) {
// 连接数据库
SqlRunner runner = Db.use().setRunner(new SqlRunner(URL, USERNAME, PASSWORD));
// 用户登录...
// 修改密码
System.out.println("请输入旧密码:");
String oldPassword = scanner.nextLine();
if (!user.getStr("password").equals(oldPassword)) {
System.out.println("旧密码错误!");
return;
}
System.out.println("请输入新密码:");
String newPassword = scanner.nextLine();
runner.update(String.format("UPDATE %s SET password = ? WHERE id = ?", TABLE_NAME), newPassword, user.getInt("id"));
System.out.println("密码修改成功!");
// 其他操作...
}
}
```
以上是Java代码实现银行系统部分功能的示例,希望能够帮到你。需要注意的是,这里只是简单地演示了如何实现功能,实际开发中还需要考虑很多问题,如异常处理、并发控制等。
阅读全文