银行,客户,账户
银行 客户 账户
2023-09-14 09:05:15 时间
题目2
// 三个类之间的关系
// 它们之间的关系是正方形属于矩形的特例 所以需要继承矩形的属性
// 而立方体是在矩形的基础之上多了一个维度 所以需要在矩形的基础上加上一个属性:高
// 矩形类
class Rectangle{
private int width;
private int length;
public Rectangle() { }
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
}
// 需要注意的是 在这个题里面需要通过父类提供的set方法来为父类的私有成员赋值 这样才符合继承,封装的意义
// 正方形类
class Square extends Rectangle{
public Square(){ }
// 因为继承关系的原因 这里必须使用super 这样才符合面相对象思想
public Square(int width, int length) {
super(width, length);
}
}
// 立方体类
class Cube extends Rectangle{
// 在原有的基础之上加上一个height属性
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Cube(){}
public Cube(int height) {
this.height = height;
}
public Cube(int width, int length, int height) {
super(width, length);
this.height = height;
}
}
题目3
// 面相对象第一步 思考各个类之间的关系
// 题目里面说Bank中有多个Account 但是他们不是继承关系
// 所以在这里如果Bank,Customer想要调用Account的话需要把Account放在Bank类的前面 (一个技术细节)
// 需要思考的就是银行,银行客户,与客户的账户之间的关系
// 显然分析可得这里是IS-A法则 所以这三个类都是独立的
class Account{
private int id; // 账户id
private double money; // 账户金额
public Account() {}
public Account(int id, double money) {
this.id = id;
this.money = money;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
class Bank{
private Account [] accounts; // 银行的账户集 用一个account的数组表示
private int accountNum; // 银行账户的数量
public Bank(){}
public Bank(Account[] accounts){
this.accounts = accounts;
this.accountNum = accounts.length;
}
public Account[] getAccounts() {
return accounts;
}
public void setAccounts(Account[] accounts) {
this.accounts = accounts;
}
public int getAccountNum() {
return accountNum;
}
public void setAccountNum(int accountNum) {
this.accountNum = accountNum;
}
}
class Customer{
private Account account; // 该用户的账户
private double custmerMoney; // 用户的钱
// 由于题目说用户可以有一个账户 所以用户多了一个是否有账户的情况
private boolean flag; // 该用户是否有在银行开户 如果为false代表没有开户 反之为开户
public Customer() {
// 误差构造方法含义就是没有创建账户 那么就是false
this.flag = false;
}
public Customer(Account account, double custmerMoney) {
this.account = account;
this.custmerMoney = custmerMoney;
this.flag = true;
}
public Account getAccount() {
// 如果这个客户没有创建账户 那么就返回null
if (flag == false) return null;
return account;
}
public void setAccount(Account account) {
// 设置了账户 那么flag就为true了
this.account = account;
this.flag = true;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public double getCustmerMoney() {
return custmerMoney;
}
public void setCustmerMoney(double custmerMoney) {
this.custmerMoney = custmerMoney;
}
// 不论是存钱还是取钱 第一步判断是这个用户是否开户了 没有的话就是false
// 存钱和取钱的函数都是boolean 如果返回true那么就是成功了 反之就失败了
public boolean withdrawMoney(double money){ // 取钱
if (this.isFlag()) {
double tmoney = this.getAccount().getMoney(); // 这里先把变量存在一个临时表变量里面 这样的目的是可以减少函数间传递的频率
// 提高程序的运行效率(用c++的思想写java)
if (tmoney < money) return false; // 如果钱不够
else {
this.getAccount().setMoney(tmoney - money);
return true;
}
} else return false;
}
// 存钱的方法
public boolean saveMoney(int money){
// 先判断有没有账号
if (this.isFlag()) {
// 钱不够 然后就存钱失败
if (this.custmerMoney - money < 0) return false;
this.getAccount().setMoney(this.getAccount().getMoney() + money);
return true;
} else return false;
}
// 向另一个用户转账 设计为boolean类型 查看是否转账成功
public boolean transferAccount(Customer customer1, Customer customer2, double money){
// 这里是account1向account2转账
// 钱不够 不够转账
double money1 = customer1.getAccount().getMoney();
double money2 = customer2.getAccount().getMoney();
if(customer1.getAccount().getMoney() < money) return false;
customer1.getAccount().setMoney(money1 - money);
customer2.getAccount().setMoney(money2 + money);
return true;
}
}