java声明银行账户类Account

       声明银行账户类Account,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、销户等操作。

 示例一:

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Account {
    private String accountNumber;
    private String accountHolderName;
    private LocalDate openingDate;
    private String idNumber;
    private double balance;
    private List transactionHistory;

    public Account(String accountNumber, String accountHolderName, String idNumber) {
        this.accountNumber = accountNumber;
        this.accountHolderName = accountHolderName;
        this.openingDate = LocalDate.now();
        this.idNumber = idNumber;
        this.balance = 0.0;
        this.transactionHistory = new ArrayList<>();
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public String getAccountHolderName() {
        return accountHolderName;
    }

    public LocalDate getOpeningDate() {
        return openingDate;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public double getBalance() {
        return balance;
    }

    public List getTransactionHistory() {
        return transactionHistory;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            transactionHistory.add("Deposit: +" + amount);
            System.out.println("Deposit of " + amount + " successful. New balance: " + balance);
        } else {
            System.out.println("Invalid deposit amount.");
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            transactionHistory.add("Withdrawal: -" + amount);
            System.out.println("Withdrawal of " + amount + " successful. New balance: " + balance);
        } else {
            System.out.println("Invalid withdrawal amount or insufficient funds.");
        }
    }

    public void checkBalance() {
        System.out.println("Account balance: " + balance);
    }

    public void printTransactionHistory() {
        System.out.println("Transaction History:");
        for (String transaction : transactionHistory) {
            System.out.println(transaction);
        }
    }

    public void closeAccount() {
        accountNumber = "";
        accountHolderName = "";
        openingDate = null;
        idNumber = "";
        balance = 0.0;
        transactionHistory.clear();
        System.out.println("Account closed successfully.");
    }
}

在这个示例中,Account类具有以下成员变量和方法:

  • accountNumber:账户号码的字符串。
  • accountHolderName:储户姓名的字符串。
  • openingDate:开户时间的LocalDate对象。
  • idNumber:身份证号码的字符串。
  • balance:存款余额的双精度浮点数。
  • transactionHistory:交易历史记录的字符串列表。

构造函数Account用于初始化账户对象,需要提供账号、储户姓名和身份证号码,并自动设置开户时间为当前日期。初始余额为0,交易历史为空列表。

getAccountNumbergetAccountHolderNamegetOpeningDategetIdNumbergetBalance方法用于获取账户信息。

getTransactionHistory方法用于获取交易历史记录。

deposit方法用于存款操作。它接受一个参数amount表示存款金额。如果存款金额大于零,将存款金额添加到余额上,并将存款记录添加到交易历史中。然后输出存款成功的信息。否则,输出无效的存款金额的信息。

withdraw方法用于取款操作。它接受一个参数amount表示取款金额。如果取款金额大于零且小于等于余额,将取款金额从余额中扣除,并将取款记录添加到交易历史中。然后输出取款成功的信息。否则,输出无效的取款金额或余额不足的信息。

checkBalance方法用于查询账户余额,并输出到控制台。

printTransactionHistory方法用于打印交易历史记录到控制台。

closeAccount方法用于销户操作。它将关闭账户,并清空所有账户信息,包括账号、储户姓名、开户时间、身份证号码、余额和交易历史记录。然后输出销户成功的信息。

示例二:

import java.util.Scanner;
import java.util.ArrayList;
class Person {
    // Person类的创建

    private String name;
    private char xin;

    public String getName() {
        return this.name;
    }

    public char getXin() {
        return this.xin;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setXin(char xin) {
        this.xin = xin;
    }
}
// Person类的创建终
class GoodFriend extends Person {
    private String ophen;
    private String friend;

    public String getOphen() {
        return this.ophen;
    }

    public void setOphen(String ophen) {
        this.ophen = ophen;
    }

    public String getFriend() {
        return this.friend;
    }

    public void setFriend(String friend) {
        this.friend = friend;
    }

    public void friend(ArrayList friend_arr, int x, int y) {
        GoodFriend a=friend_arr.get(x);
        GoodFriend b=friend_arr.get(y);
        if(a.getFriend().equals(b.getName())){
            if(b.getFriend().equals(a.getName())){
                System.out.println(a.getName()+"和"+b.getName()+"的关系是朋友");
                main01(friend_arr);
            }else{
                System.out.println(a.getName()+"和"+b.getName()+"不是朋友关系");
                main01(friend_arr);
            }
        }else{
            System.out.println(a.getName()+"和"+b.getName()+"不是朋友关系");
            main01(friend_arr);
        }
    }

    public void output() {
        System.out.print(getName() + "\t");
        System.out.print(getXin() + "\t");
        System.out.print(getOphen() + "\t");
        System.out.print(getFriend()+ "\t");
        System.out.println();
    }

    public static void input(ArrayList friend_arr) {
        Scanner scan = new Scanner(System.in);
        int i = 1;
        System.out.println("**********欢迎注册**********");
        lablo:
        while (true) {
            GoodFriend p = new GoodFriend();
            System.out.print("输入姓名:");
            String name = scan.next();
            p.setName(name);
            System.out.print("输入 " + name + " 的性别:");
            char xin = scan.next().charAt(0);
            p.setXin(xin);
            System.out.print("输入 " + name + " 的电话号码:");
            String ophen = scan.next();
            p.setOphen(ophen);
            System.out.print("输入 " + name + " 的好朋友名:");
            String friend = scan.next();
            p.setFriend(friend);
            friend_arr.add(p);
            if (friend_arr.size() == i) {
                System.out.println("提示:  第" + i + "个人注册成功!!");
            } else {
                System.out.println("提示: " + name + "未能成功注册");
            }
            leblo:
            while(true){
                System.out.println("是否继续注册?请输入是或者否");
                String str = scan.next();
                if (str.equals("否")) {
                    break lablo;
                } else if (str.equals("是")){
                    i++;
                    System.out.println("第 " + i + " 个开始注册");
                    break leblo;
                }
                else{
                    System.out.println("提示: 输入有误!!请重新输入!!");
                }
            }
        }
    }

    public static void main01(ArrayList friend_arr){
        Scanner scan = new Scanner(System.in);
        System.out.println("*********已注册的信息**********");
        System.out.println("\n"+"编号"+"\t"+"姓名"+"\t"+"性别");
        for (int i = 0; i < friend_arr.size(); i++) {
            GoodFriend p1 = friend_arr.get(i);
            System.out.println(i + "\t" + p1.getName() + "\t" + p1.getXin());
        }
        while (true){
            System.out.println("---------------查询---------------");
            System.out.println("【1】查看所有详细信息");
            System.out.println("【2】查看朋友关系");
            System.out.println("【3】退出");
            System.out.print("请选择:");
            while(true){
                int number = scan.nextInt();
                switch (number) {
                    case 1:
                        for(int j =0;j 1) {
                            System.out.println("提示: 请选择想查看关系的信息的编号。");
                            System.out.print("请输入编号:");
                            int x = scan.nextInt();
                            System.out.print("请输入编号:");
                            int y = scan.nextInt();
                            GoodFriend p2 = new GoodFriend();
                            p2.friend(friend_arr,x, y);
                        } else {
                            System.out.println("提示:  当前注册信息只有" + friend_arr.size() + "不符合查看关系的条件!!");
                        }
                    case 3:
                        System.exit(0);
                    default:
                        System.out.println("提示: 选择有误请选择!!");
                }
            }
        }
    }

    public static void main(String[] args) {
        ArrayList friend_arr = new ArrayList<>();

        input(friend_arr);
        main01(friend_arr);

    }
}

你可能感兴趣的:(java)