java bank card function

Bank cards are the cards we often use, and each bank card corresponds to a bank account.
The bank card contains the following 3 pieces of information:
username, password, and bank balance.
The bank card includes the following 3 functions:
1. Deposit: deposit a specified amount of money in the card
2. Withdraw money: withdraw a specified amount of money from the card Money
3. Query the balance: Query and return the balance in the card Requirements: 1. Define a bank card class, and define appropriate member variables and member methods in the class according to the above content 2. Instantiate the two in the main function of the main
class The initial information of the two cards is as follows:
(1) Bank card 1: user name
ZhangSan, password 123456, initial balance 6,800 yuan
(2) Bank card 2: user name Lisi,
password 654321, initial balance 8,200 yuan
3. In the main function of the main class, 2,000 yuan is withdrawn from bank card 1 and 3,200 yuan is deposited in bank card 2. The balances in the two bank cards are displayed separately.

package ad;

import java.text.MessageFormat;

 class BankCard{
    private String username;
    private String password;
    private int balance;
    public BankCard() {
    }
    public BankCard(String username, String password, int balance) {
        this.username = username;
        this.password = password;
        this.balance = balance;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
    public void saveMoney(int num){
        balance+=num;
    }
    public void drawMoney(int num){
        balance-=num;
    }
}
class Main{
    public static void main(String[] args) {
        BankCard b1=new BankCard("ZhangSan","123456",6800);
        BankCard b2=new BankCard("Lisi","654321",8200);
        b1.drawMoney(2000);
        b2.saveMoney(3200);
        System.out.println(MessageFormat.format("用户名:{0},余额:{1}",b1.getUsername(),b1.getBalance()));
        System.out.println(MessageFormat.format("用户名:{0},余额:{1}",b2.getUsername(),b2.getBalance()));
    }
}

Guess you like

Origin blog.csdn.net/m0_63715487/article/details/127705422