-->
当前位置:首页 > 题库

函数题:银行账户

Luz3年前 (2022-11-10)题库396
编写一个完整的Java程序。包含类Acount表示银行账户,具体要求如下:

Account包含以下成员

①属性:

1)id:私有,int型,表示账户编号;

2)balance:私有,int型,表示账户余额(不能为负数);

②方法:

1)Account(), 构造方法,id和balance都初始化为0;

2)Account(int id,int balance),构造方法,用参数设置账户编号和余额;

3)void setBalance(int balance):修改账户金额

4)int getBalance():返回账户金额

5)boolean withdraw(int money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;

6)void deposit(int money):向账户存储特定数额。

7)public String toString():将把当前账户对象的信息转换成字符串形式,例如id为123,余额为1000,返回字符串"The balance of account 123 is 1000"。

### 裁判测试程序样例:
c++
import java.util.Scanner;

public class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n,m;

Account a = new Account(input.nextInt(),input.nextInt());
n = input.nextInt();
for(int i=0; i < n; i++) {
String op;
int money;
op = input.next();
money = input.nextInt();
if(op.equals("withdraw")) {
if(a.withdraw(money)) {
System.out.println("withdraw " + money + " success");
} else {
System.out.println("withdraw " + money + " failed");
}
} else if(op.equals("deposit")) {
a.deposit(money);
System.out.println("deposit " + money + " success");
}
}
System.out.println(a.toString());
}
}

/* 请在这里填写答案 */


### 输入样例:

in
1 100
5
withdraw 200
withdraw 100
deposit 50
deposit 100
withdraw 200


### 输出样例:

out
withdraw 200 failed
withdraw 100 success
deposit 50 success
deposit 100 success
withdraw 200 failed
The balance of account 1 is 150






答案:若无答案欢迎评论