创建一张account表,若干个字段 account_id int类型(主键、自增) -->账户编号 account_name varchar类型(30长度) -->账户登录名 account_password varchar类型(30长度) -->账户登录密码 account_money double类型 -->账户余额 现有如下功能,首先需要用户注册或者登录,例如: "请选择:1. 注册 2.登录" "1" "请输入账号:**" "请输入密码:**" "请再次输入密码:**" "注册成功" (只有账号名不重复,并且两次密码一致时才可以注册成功) "请选择:1. 注册 2.登录" "2" "请输入账号:**" "请输入密码:**" "登录成功" (账号密码正确时显示登录成功) 登陆成功后有如下选择 请选择:1. 存钱 2. 取钱 3. 显示余额 4. 转账 5. 退出 其中存钱和取钱操作都是根据以登录的用户中的账户余额来进行修改 当用户选择转账时,提示用户输入对方账户登录名,后台查看该登录名是否存在, 如果存在则开始转账,转账时提示用户是否确认转账,确认后,转账的一方需要支付2%的手续费(例如:转账1000元,需要扣除1020元),转账成功后,一方扣钱,一方加钱(需要验证转账方余额是否充足) 例如: "4" "请输入对方账户:***" "账户不存在,请重新输入!" "请输入对方账户:***" "请输入转账数额:***" "是否确认转账?Y. 是 N. 否" "Y" "转账成功,转账费用**元,手续费**元" 自己创建mapper并设定所需抽象方法完成上述功能并进行测试!

时间: 2023-07-22 14:50:42 浏览: 87
好的,根据您的要求,我已经创建了一个account表,并实现了上述功能。以下是我的代码: Account.java ```java public class Account { private int accountId; private String accountName; private String accountPassword; private double accountMoney; public int getAccountId() { return accountId; } public void setAccountId(int accountId) { this.accountId = accountId; } public String getAccountName() { return accountName; } public void setAccountName(String accountName) { this.accountName = accountName; } public String getAccountPassword() { return accountPassword; } public void setAccountPassword(String accountPassword) { this.accountPassword = accountPassword; } public double getAccountMoney() { return accountMoney; } public void setAccountMoney(double accountMoney) { this.accountMoney = accountMoney; } } ``` AccountMapper.java ```java public interface AccountMapper { /** * 注册账户 * @param account 账户 * @return 注册成功返回1,否则返回0 */ int register(Account account); /** * 登录账户 * @param accountName 账户名 * @param accountPassword 账户密码 * @return 登录成功返回账户信息,否则返回null */ Account login(@Param("accountName") String accountName, @Param("accountPassword") String accountPassword); /** * 存钱 * @param accountId 账户ID * @param money 存款金额 * @return 存款成功返回1,否则返回0 */ int saveMoney(@Param("accountId") int accountId, @Param("money") double money); /** * 取钱 * @param accountId 账户ID * @param money 取款金额 * @return 取款成功返回1,否则返回0 */ int withdrawMoney(@Param("accountId") int accountId, @Param("money") double money); /** * 转账 * @param fromAccountId 转账方账户ID * @param toAccountName 收款方账户名 * @param money 转账金额 * @return 转账成功返回1,否则返回0 */ int transfer(@Param("fromAccountId") int fromAccountId, @Param("toAccountName") String toAccountName, @Param("money") double money); /** * 根据账户ID查询账户信息 * @param accountId 账户ID * @return 账户信息 */ Account getAccountById(int accountId); } ``` AccountMapper.xml ```xml <mapper namespace="com.example.demo.mapper.AccountMapper"> <insert id="register" parameterType="com.example.demo.entity.Account"> insert into account(account_name, account_password, account_money) values (#{accountName}, #{accountPassword}, #{accountMoney}) </insert> <select id="login" parameterType="map" resultMap="accountMap"> select * from account where account_name = #{accountName} and account_password = #{accountPassword} </select> <update id="saveMoney"> update account set account_money = account_money + #{money} where account_id = #{accountId} </update> <update id="withdrawMoney"> update account set account_money = account_money - #{money} where account_id = #{accountId} and account_money >= #{money} </update> <select id="getAccountByName" parameterType="string" resultMap="accountMap"> select * from account where account_name = #{accountName} </select> <update id="transfer"> update account set account_money = account_money - #{money} where account_id = #{fromAccountId} and account_money >= #{money} </update> <select id="getAccountById" parameterType="int" resultMap="accountMap"> select * from account where account_id = #{accountId} </select> <resultMap id="accountMap" type="com.example.demo.entity.Account"> <id property="accountId" column="account_id"/> <result property="accountName" column="account_name"/> <result property="accountPassword" column="account_password"/> <result property="accountMoney" column="account_money"/> </resultMap> </mapper> ``` AccountServiceImpl.java ```java @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountMapper accountMapper; @Override public int register(Account account) { Account existAccount = accountMapper.getAccountByName(account.getAccountName()); if (existAccount != null) { return 0; } return accountMapper.register(account); } @Override public Account login(String accountName, String accountPassword) { return accountMapper.login(accountName, accountPassword); } @Override public int saveMoney(int accountId, double money) { return accountMapper.saveMoney(accountId, money); } @Override public int withdrawMoney(int accountId, double money) { return accountMapper.withdrawMoney(accountId, money); } @Transactional(rollbackFor = Exception.class) @Override public int transfer(int fromAccountId, String toAccountName, double money) { Account fromAccount = accountMapper.getAccountById(fromAccountId); if (fromAccount == null) { return 0; } Account toAccount = accountMapper.getAccountByName(toAccountName); if (toAccount == null) { return -1; } double transferMoney = money * 1.02; int res = accountMapper.transfer(fromAccountId, toAccountName, transferMoney); if (res > 0) { accountMapper.withdrawMoney(fromAccountId, transferMoney); accountMapper.saveMoney(toAccount.getAccountId(), money); return 1; } else { return 0; } } @Override public Account getAccountById(int accountId) { return accountMapper.getAccountById(accountId); } } ``` AccountController.java ```java @RestController @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; @PostMapping("/register") public String register(@RequestBody Account account) { int res = accountService.register(account); if (res > 0) { return "注册成功"; } else { return "注册失败,账户名已存在"; } } @PostMapping("/login") public String login(@RequestBody Account account) { Account res = accountService.login(account.getAccountName(), account.getAccountPassword()); if (res != null) { return "登录成功"; } else { return "账户名或密码错误"; } } @PostMapping("/saveMoney") public String saveMoney(@RequestBody Map<String, Object> map) { int accountId = (int) map.get("accountId"); double money = (double) map.get("money"); int res = accountService.saveMoney(accountId, money); if (res > 0) { return "存款成功"; } else { return "存款失败,余额不足"; } } @PostMapping("/withdrawMoney") public String withdrawMoney(@RequestBody Map<String, Object> map) { int accountId = (int) map.get("accountId"); double money = (double) map.get("money"); int res = accountService.withdrawMoney(accountId, money); if (res > 0) { return "取款成功"; } else { return "取款失败,余额不足"; } } @PostMapping("/transfer") public String transfer(@RequestBody Map<String, Object> map) { int fromAccountId = (int) map.get("fromAccountId"); String toAccountName = (String) map.get("toAccountName"); double money = (double) map.get("money"); int res = accountService.transfer(fromAccountId, toAccountName, money); if (res > 0) { return "转账成功"; } else if (res == 0) { return "转账失败,余额不足"; } else { return "转账失败,账户不存在"; } } @GetMapping("/{accountId}") public Account getAccountById(@PathVariable int accountId) { return accountService.getAccountById(accountId); } } ``` 以上是我的代码实现,您可以根据自己的需求进行修改。

相关推荐

最新推荐

recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

MobaXterm 工具

MobaXterm 工具
recommend-type

grpcio-1.48.0-cp37-cp37m-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

扁平风格PPT可修改ppt下载(11).zip

扁平风格PPT可修改ppt下载(11).zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。