需求及思路分析
-
业务代码需求:
某公司要开发“银行管理系统”,请使用面向对象的思想,设计银行的储户信息,描述存款、取款业务。 -
储户类的思路分析:
属性:用户姓名、密码、身份证号、账号、帐户余额
方法:
1、 存款:验证账号是否有效,存款金额是否有效,如果有效修修改账户余额
2、 取款:验证账号是否有效,取款金额是否有效,余额是否够用,如果有效修改余额
类的方法图
- 这里存款和取款方法,遵循整存整取
代码示例
import java.util.Scanner;/** 测试方法*/
public class UserTest {public static void main(String[] args){//实例化对象,对所有属性赋值User one=new User("张三","123456",2000,"111111");//接收用户输入信息,调用存款的方法Scanner input =new Scanner(System.in);System.out.println("请输入账号:");String account=input.nextLine();System.out.println("请输入存款金额:");double money=input.nextDouble();one.saving(account, money);System.out.println("=============================");//接收用户输入信息,调用存款的方法System.out.println("请输入取款金额:");money=input.nextDouble();one.getting(account, money);}
}/*** 储户类*/
class User {// 用户姓名、密码、身份证号、账号、帐户余额(>0)private String name;// 储户姓名private String password;// 密码private double balance;// 存款余额private String account;// 帐号public String getName() { return name; }public void setName(String name) { this.name = name; }public String getPassword() { return password; }public void setPassword(String password) { this.password = password; }public double getBalance() { return balance; }public String getAccount() { return account; }public void setAccount(String account) { this.account = account; }// 账户余额大于0public void setBalance(double balance) {if (balance < 0)System.out.println("无效金额");elsethis.balance = balance;}public User() { }public User(String name, String password, double balance, String account) {this.name = name;this.password = password;this.balance = balance;this.account = account;}// 1、存款,验证账号是否有效,存款金额是否有效,如果有效修改账户余额public void saving(String acc, double money) {if (!this.account.equals(acc)) {System.out.println("账号错误");return ;}if (money < 0 || money % 100 !=0) {System.out.println("存款金额无效");return;}this.balance += money;System.out.println("存款成功!\n储户姓名:" + this.name + "\n帐号:" + this.account + "\n可用金额:" + this.balance);}// 2、取款,验证账号是否有效,取款金额是否有效,余额是否够用,如果有效修改余额public void getting(String acc, double money) {if (!this.account.equals(acc)) {System.out.println("账号错误");return;}if (money < 0 || money % 100 !=0) {System.out.println("取款金额无效");return;}if (balance - money < 0) {System.out.println("当前账户余额不足");return;}this.balance -= money;System.out.println("取款成功!\n储户姓名:" + this.name + "\n帐号:" + this.account + "\n可用金额:" + this.balance);}
}
运行截图