java习题--->银行项目

1.创建 banking 包
2. 在 banking 包下创建 Account 类。该类必须实现上述 UML 框图中的模型。
a. 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或 即
时)余额。
b. 声明一个带有一个参数 (init_balance )的公有构造器 ,这个参数为
balance 属性赋值。
c. 声明一个公有方法 geBalance,该方法用于获取经常余额。
d. 声明一个公有方法 deposit,该方法向当前余额增加金额。
e. 声明一个公有方法 withdraw 从当前余额中减去金额。
3.打开TestBanking.java文件,按提示完成编写,并编译 TestBanking.java 文件。
4. 运行 TestBanking 类。可以看到下列输出结果:
Creating an account with a 500.00 balance
Withdraw 150.00
Deposit 22.50
Withdraw 47.62
The account has a balance of 324.88

package banking;

public class Account {

    private static double balance;  //这个属性保留了银行账户的当前余额
    private double amt;
    public Account(double init_balance){
        this.balance=init_balance;
    } 
    public double getBalance() {
        return this.balance;
    }
    public void  deposit(double amt){
         balance+=amt;
         System.out.println("Deposit "+amt+": true");

    }
    public boolean  withdraw(double amt){
        if(amt<balance){
         balance-=amt;
         System.out.println("Withdraw "+amt+": true");
         return true;

         }else{
             System.out.println("Withdraw "+amt+": false");
            return false; 
         }
    }
}
package banking;

public class TestBanking {

    public static void main(String[] args) {
        Customer customer=new Customer("Jane","Smith");
        //customer.setAccount(500.00);
        Account admin=new Account(500.00);  

        System.out.println("Creating the customer "+customer.getFirstName()+" "+customer.getLastName());
        System.out.println("Creating her account with a "+admin.getBalance()+"  balance");
        admin.withdraw(150.00);
        admin.deposit(22.50);
        admin.withdraw(47.62);
        admin.withdraw(400.00);
        System.out.println("Customer ["+customer.getLastName()+", "+customer.getFirstName()+"] has a balance of "+admin.getBalance());

    }
}
package banking;

public class Customer {
    private  String firstName;
    private  String lastName;
    private double  account;

    public Customer(String f,String l){
        this.firstName=f;
        this.lastName=l;
    }
    public String getFirstName(){
        return this.firstName;
    }
    public String getLastName() {
        return this.lastName;
    }
    public void setAccount(double account){
        this.account=account;
    }
    public double getAccount(){
        return this.account;
    }
}
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值