栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实验报告4:第二题银行账户类型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实验报告4:第二题银行账户类型

1.文件Account.java包含一个描述银行帐户的类的部分代码。把它保存到您的目录并研究它,看它有哪些方法。然后按照如下所述完成这个Account类。注意,你还不能测试你的方法直到你在问题2中写下ManageAccount。

   a、填充toString方法的代码,它应该返回一个字符串,包含姓名,帐号,和账户余额

   b、填充方法chargeFee的代码,这应该从帐户中扣除服务费(这个的意思是,每次取钱都要支付一定的服务费,或者叫手续费)。

   c、修改chargeFee方法的代码,使它不是返回void,而是返回一个新的账户余额。注意,你必须在两个地方做出修改。

   d、填写changeName方法的代码,使得它将一个字串作为参数,同时改变帐户上的名字为该字符串。

 

2、ManageAccounts.java文件包含一个外壳程序,使用上面定义的帐户类。将它保存到你的目录,按以下注视要求完成这个程序。

3、修改ManageAccounts的代码,使得在调用chargeFees后,输出账户余额。不是像在存款和取款后使用getBalance方法进行输出,而是使用从chargeFees方法返回的余额。你可以把余额存在一个变量中,然后通过这个变量进行输出,也可以把这个方法直接嵌入在一个println语句中。

// **************************************************
//   Account.java   author:wang
//
//   A bank account class with methods // change the name on, charge a fee
// **************************************************
import java.text.NumberFormat;
public class Account
{
    private double balance;
    private String name;
    private long acctNum;
    //----------------------------------------------
//Constructor -- initializes balance, //----------------------------------------------
    public  Account(double initBal, String owner,long number) {
        balance = initBal;
        name = owner;
        acctNum = number;
    }


    //----------------------------------------------
// Checks to see if balance is sufficient // If so, decrements balance by amount; //----------------------------------------------
    public void withdraw(double amount)
    {
        if (balance >= amount)
            balance -= amount;
        else
            System.out.println("Insufficient " +amount);
    }
//----------------------------------------------
// Adds deposit amount to balance.


//----------------------------------------------
        public void deposit(double amount)
        {
            balance += amount;
        }
//----------------------------------------------
// Returns balance.
//----------------------------------------------
        public double getBalance()
        {
            return balance;
        }
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
        public String toString()
        {
            NumberFormat fmt =NumberFormat.getCurrencyInstance();
            return acctNum + "t" + "name" +"t" +fmt.format(balance);
        }
//----------------------------------------------
// Deducts $10 service fee
//----------------------------------------------
        public void chargeFee(double fee)
        {
                balance=balance-10;
        }

//----------------------------------------------
// Changes the name on the account
//----------------------------------------------
        public void changeName(String newName)
        {
            name = newName;
        }
    }

// ****************************************************************
// ManageAccounts.java
//
// Use Account class to create and manage Sally and Joe's
// bank accounts
// ****************************************************************
public class ManageAccounts {
    public static void main(String[] args) {
        Account acct1, acct2;
//create account1 for Sally with $1000
        acct1 = new Account(1000, "Sally", 1111);
        //create account2 for Joe with $500
        acct2 = new Account(500, "Joe", 2222);
//deposit $100 to Joe's account
        acct2.deposit(100);
//print Joe's new balance (use getBalance())
        System.out.println("Joe balance after deposit" + acct2.getBalance());
//withdraw $50 from Sally's account
        acct1.withdraw(50);
//print Sally's new balance (use getBalance())
        System.out.println("Sally balance after withdraw" + acct1.getBalance());
//charge fees to both accounts
        acct1.chargeFee(10);
        acct2.chargeFee(10);
//change the name on Joe's account to Joseph

        acct2.changeName("Joseph");
//print summary for both accounts
        System.out.println(acct1);
        System.out.println(acct2);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号