Java 编写一个类实现银行账户Account的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率rate”、“最小余额”,定义封装这些属性的方法。

编写一个类实现银行账户Account的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率rate”、“最小余额”,定义封装这些属性的方法。账号要自动生成。编写主类,使用银行账户类,输入、输出3个储户的上述信息。考虑:哪些属性可以设计成static属性。

Account.java


/*编写一个类实现银行账户Account的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率rate”、“最小余额”, 定义封装这些属性的方法。 账号要自动生成。编写主类,使用银行账户类,输入、输出3个储户的上述信息。 考虑:哪些属性可以设计成static属性。 */

public class Account 
{ 
    private int userID;//账号
    private int password;//密码
    private double deposit;//存款余额
    private double rate;//利率
    private double balance;//最小余额
    static int aotonomic_number=2021001;
    { 
        userID=aotonomic_number;
        System.out.println("账号为:"+userID);
        aotonomic_number++;
    }



    public double getBalance()
     { 
        return balance;
    }

    public void setBalance(double balance) 
    { 
        this.balance = balance;
    }
    public String toString() 
    { 
        return "Account [账号" + userID + ", 密码" + password + ", 存款余额"
                + deposit + ", 利率" + rate + ", 最小余额" + balance + "]";
    }

    public int getUserID()
     { 
        return userID;
    }

    public void setUserID(int userID) 
    { 
        this.userID = userID;
    }

    public int getPassword() 
    { 
        return password;
    }

    public void setPassword(int password) 
    { 
        this.password = password;
    }

    public double getDeposit()
     { 
        return deposit;
    }

    public void setDeposit(double deposit)
     { 
        this.deposit = deposit;
    }

    public double getRate() 
    { 
        return rate;
    }

    public void setRate(double rate)
     { 
        this.rate = rate;
    }
}

Test.java

import java.util.Scanner;
public class Test { 
    public static void main(String[] args) { 
        Account user1=new Account();
        Account user2=new Account();
        Account user3=new Account();
        Scanner reader = new Scanner(System.in);

        System.out.println("设置密码:");
        user1.setPassword(reader.nextInt());
        user2.setPassword(reader.nextInt());
        user3.setPassword(reader.nextInt());
        System.out.println("输入存款:");
        user1.setDeposit(reader.nextDouble());
        user2.setDeposit(reader.nextDouble());
        user3.setDeposit(reader.nextDouble());
        System.out.println("输入利率:");
        user1.setRate(reader.nextDouble());
        user2.setRate(reader.nextDouble());
        user3.setRate(reader.nextDouble());
        System.out.println("输入最小余额:");
        user1.setBalance(reader.nextDouble());
        user2.setBalance(reader.nextDouble());
        user3.setBalance(reader.nextDouble());
        System.out.println(user1.toString()+'\n'+user2.toString()+'\n'+user3.toString());
    }
}

    原文作者:RachelQin_
    原文地址: https://blog.csdn.net/qq_19716069/article/details/112860862
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞