java:简单银行管理系统

我们在学习C/C++的时候,会写银行管理系统,java也一样。
这次分享一下银行管理系统的代码和心得

1.类静态属性与方法

public class Bank1 {

    private int id;			//账号
    private double balance;			//余额
    private double rate;               //存款的年利率
    private int lastDate;               //上次变更的时期
    private double accumulation;			//余额按类累加之和
    private double Accumulate(int date)  {
        return accumulation+balance*(date-lastDate);
    }
    public Bank1(int date, int id, double rate) {
        this.id=id;
        this.rate=rate;
        date=0;

    }
    //记录一笔帐,date为日期,amount为金额,desc为说明
    private void Record(int date, double amount) {
        accumulation=Accumulate(date);
        lastDate=date;
        amount=Math.floor((amount*100+0.5)/100);
        balance+=amount;
        System.out.println(date+"\t#"+id+"\t"+amount+"\t"+balance);
    }
    //存入现金
    public void deposit(int date, double amount) {
        Record(date, amount);

    }
    //取出现金
    public void withdraw(int date, double amount) {
        if(amount>balance) {
            System.out.println("Error: not enough money");

        }
        else {
            Record(date, -amount);
        }
    }
    //结算利息,每年1月1日调用一次该函数
    public void settle(int date) {
        double interest=Accumulate(date)*rate/365;
        if(interest!=0) {
            Record(date, interest);
        }
        accumulation=0;
    }
    //显示账户信息
    public void show() {
        System.out.println("#"+id+"        "+"balance:"+balance);

    }
    public static void main(String[] args) {
        Bank1 bs0=new Bank1(1, 1235986, 0.015);
        bs0.deposit(5, 5000);
        bs0.withdraw(25,2000);
        bs0.settle(90);
        bs0.show();
    }

}

2.增添字符串和对象数组

public class SavingsAccount {
    private final String id;			//账号
    private double balance;		//余额
    private final double rate;		//存款的年利率
    private Date lastDate;		//上次变更余额的时期
    private double accumulation;	//余额按日累加之和
    private static double total;

    public SavingsAccount(Date date,String id, double rate) {
        lastDate=date;
        this.id=id;
        this.rate=rate;
        accumulation=0;
        balance=0;
        date.show();
        System.out.println("\t#"+id+" created");
    }
    public String getId() { return id; }
    public double getBalance() { return balance; }
    public double getRate() { return rate; }
    static double getTotal(){return total;}
    //记录一笔帐,date为日期,amount为金额,desc为说明
    void record(Date date, double amount,String desc)  {
        accumulation = accumulate(date);
        lastDate = date;
        amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
        balance += amount;
        total+=amount;
        date.show();
        System.out.println("\t#"+id+"\t"+amount+"\t"+balance+"\t"+desc);
    }
    //报告错误信息
    void error(StringBuffer msg) {System.out.println("Error(#"+id+"):"+msg);}
    //获得到指定日期为止的存款金额按日累积值
    double accumulate( Date date) {
        return accumulation + balance * date.distance(lastDate);
    }
    //存入现金
    public void deposit( Date date, double amount, String desc){record(date, amount, desc);};
    //取出现金
    public void withdraw(Date date, double amount,String desc){
        if (amount > getBalance())
            System.out.println("Error: not enough money");
        else
            record(date, -amount,desc);
    }
    //结算利息,每年1月1日调用一次该函数
    public void settle( Date date){
        double interest = accumulate(date) * rate/date.distance(new Date(date.getYear() - 1, 1, 1));
        if (interest != 0)
            record(date, interest, "interest");
        accumulation = 0;
    }
    //显示账户信息
    public void show(){
        System.out.println(id+"\tBalance: "+balance);
    }
    public static void main(String[] args){
        //起始日期
        Date date=new Date(2008, 11, 1);
        SavingsAccount account[]={new SavingsAccount (date, "S3755217", 0.015),
                new SavingsAccount (date, "02342342", 0.015)
        };
        //建立几个账户
        //SavingsAccount sa0=new SavingsAccount (date, "S3755217", 0.015);
        //SavingsAccount sa1=new SavingsAccount (date, "02342342", 0.015);

        //11月份的几笔账目
        account[0].deposit(new Date(2008, 11, 5), 5000, "salary");
        account[1].deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
        //12月份的几笔账目
        account[0].deposit(new Date(2008, 12, 5), 5500, "salary");
        account[1].withdraw(new Date(2008, 12, 20), 4000, "buy a laptop");

        //     //结算所有账户并输出各个账户信息
        System.out.println();

        account[0].settle(new Date(2009, 1, 1));
        account[0].show();
        System.out.println();
        account[1].settle(new Date(2009, 1, 1));
        account[1].show();
        System.out.println();
        System.out.println("Total: "+getTotal() );
    }


        /*Date date=new Date(2008, 11, 1);	//起始日期
        //建立几个账户
        SavingsAccount accounts[]={
        new SavingsAccount(date, "S3755217", 0.015),
        new SavingsAccount(date, "02342342", 0.015)
        };
	    int n = accounts.length; //账户总数
        //11月份的几笔账目
        accounts[0].deposit(new Date(2008, 11, 5), 5000, "salary");
        accounts[1].deposit(new Date( 2008, 11, 25), 10000, "sell stock 0323");
        //12月份的几笔账目
        accounts[0].deposit(new Date(2008, 12, 5), 5500, "salary");
        accounts[1].withdraw(new Date(2008, 12, 20), 4000, "buy a laptop");

        //结算所有账户并输出各个账户信息
        System.out.println();
        for (int i = 0; i < n; i++) {
            accounts[i].settle(new Date(2009, 1, 1));
            accounts[i].show();
            System.out.println();
        }
        System.out.println("Total: "+getTotal());*/
}
class Date {
    private int year;
    private int month;
    private int day;
    private int totalDays;
    private int DAYS_BEFORE_MONTH[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

    public Date(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        if (day <= 0 || day > getMaxDay()) {
            System.out.println("Invalid date: ");
            this.show();
            System.out.println();
            System.exit(1);
        }
        int years = year - 1;
        totalDays = years * 365 + years / 4 - years / 100 + years / 400
                + DAYS_BEFORE_MONTH[month - 1] + day;
        if (isLeapYear() && month > 2) totalDays++;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    public int getMaxDay() {
        if (isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
    }

    public boolean isLeapYear() {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    public void show() {
        System.out.println(getYear() + "-" + getMonth() + "-" + getDay());
    }

    int distance(Date date) {
        return totalDays - date.totalDays;
    }
}

3.继承和派生,抽象出父类,增添子类

public class Account {
    private String id;	//帐号
    private double balance;	//余额
    private static double total; //所有账户的总金额
    public static void main(String args[]){
        Date date=new Date(2008, 11, 1);	//起始日期
        //建立几个账户
        SavingsAccount sa1=new SavingsAccount(date, "S3755217", 0.015);
        SavingsAccount sa2=new SavingsAccount(date, "02342342", 0.015);
        CreditAccount ca=new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
        //11月份的几笔账目
        sa1.deposit(new Date(2008, 11, 5), 5000, "salary");
        ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
        sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
        //结算信用卡
        ca.settle(new Date(2008, 12, 1));
        //12月份的几笔账目
        ca.deposit(new Date(2008, 12, 1), 2016, "repay the credit");
        sa1.deposit(new Date(2008, 12, 5), 5500, "salary");
        //结算所有账户
        sa1.settle(new Date(2009, 1, 1));
        sa2.settle(new Date(2009, 1, 1));
        ca.settle(new Date(2009, 1, 1));
        //输出各个账户信息
        System.out.println();
        sa1.show(); System.out.println();
        sa2.show();System.out.println();
        ca.show(); System.out.println();
        System.out.println("Total: "+getTotal());
    }
    //供派生类调用的构造函数,id为账户
    protected Account(Date date, String id){
        this.id=id;
        balance=0;
        date.show();
        System.out.println("\t#" + id+ " created");
    }
    //记录一笔帐,date为日期,amount为金额,desc为说明
    protected void record( Date date, double amount,  String desc){
        amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
        balance += amount;
        total += amount;
        date.show();
        System.out.println("\t#" +id +"\t"+amount +"\t" +balance +"\t"+desc );
    }
    //报告错误信息
    protected void error( String msg) {
        System.out.println("Error(#" +id + "): "+msg);
    };
    public String getId() { return id; }
    public double getBalance() { return balance; }
    public static double getTotal() { return total; }
    //显示账户信息
    public void show() {
        System.out.print(id +"\tBalance: "+balance);
    }
}
class SavingsAccount extends Account { //储蓄账户类

    private Accumulator acc;    //辅助计算利息的累加器
    private double rate;        //存款的年利率

    //构造函数
    public SavingsAccount(Date date, String id, double rate) {
        super(date, id);
        this.rate = rate;
        acc = new Accumulator(date, 0);
    }

    public double getRate() {
        return rate;
    }

    //存入现金
    public void deposit(Date date, double amount, String desc) {
        record(date, amount, desc);
        acc.change(date, getBalance());
    }

    //取出现金
    public void withdraw(Date date, double amount, String desc) {
        if (amount > getBalance()) {
            error("not enough money");
        } else {
            record(date, -amount, desc);
            acc.change(date, getBalance());
        }
    }

    //结算利息,每年1月1日调用一次该函数
    public void settle(Date date) {
        double interest = acc.getSum(date) * rate	//计算年息
                / date.distance(new Date(date.getYear() - 1, 1, 1));
        if (interest != 0)
            record(date, interest, "interest");
        acc.reset(date, getBalance());
    }
}
class CreditAccount extends Account { //信用账户类

    private Accumulator acc;	//辅助计算利息的累加器
    private double credit;		//信用额度
    private double rate;		//欠款的日利率
    private double fee;			//信用卡年费

    double getDebt() {	//获得欠款额
        double balance = getBalance();
        return (balance < 0 ? balance : 0);
    }

    //构造函数
    public CreditAccount(Date date,  String id, double credit, double rate, double fee){
        super(date, id);
        this.credit=credit;
        this.rate=rate;
        this.fee=fee;
        acc=new Accumulator(date,0);
    };
    public  double getCredit() { return credit; }
    public double getRate() { return rate; }
    public  double getFee() { return fee; }
    public double getAvailableCredit()  {	//获得可用信用
        if (getBalance() < 0)
            return credit + getBalance();
        else
            return credit;
    }
    //存入现金
    public void deposit(Date date, double amount, String desc){
        record(date, amount, desc);
        acc.change(date, getDebt());
    }
    //取出现金
    public  void withdraw(Date date, double amount, String desc){
        if (amount - getBalance() > credit) {
            error("not enough credit");
        } else {
            record(date, -amount, desc);
            acc.change(date, getDebt());
        }
    }
    //结算利息和年费,每月1日调用一次该函数
    public  void settle(Date date){
        double interest = acc.getSum(date) * rate;
        if (interest != 0)
            record(date, interest, "interest");
        if (date.getMonth() == 1)
            record(date, -fee, "annual fee");
        acc.reset(date, getDebt());
    }

    public  void show() {
        super.show();
        System.out.println("\tAvailable credit:"+getAvailableCredit());

    }
}
class Date {	//日期类

    private int year;		//年
    private int month;		//月
    private int day;		//日
    private int totalDays;	//该日期是从公元元年1月1日开始的第几天

    int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    public Date(int year, int month, int day){
        this.year=year;
        this.month=month;
        this.day=day;
        if (day <= 0 || day > getMaxDay()) {
            System.out.println("Invalid date: ");
            show();
            System.out.println();
            System.exit(1);
        }
        int years = year - 1;
        totalDays = years * 365 + years / 4 - years / 100 + years / 400
                + DAYS_BEFORE_MONTH[month - 1] + day;
        if (isLeapYear() && month > 2) totalDays++;
    }	//用年、月、日构造日期
    public int getYear() { return year; }
    public int getMonth()  { return month; }
    public int getDay() { return day; }
    public int getMaxDay() {
        if (isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
    }		//获得当月有多少天
    public boolean isLeapYear()  {	//判断当年是否为闰年
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    public void show(){
        System.out.println(getYear()+"-"+getMonth()+"-"+getDay());

    }			//输出当前日期
    //计算两个日期之间差多少天
    public int distance(Date date) {
        return totalDays - date.totalDays;
    }
}
class Accumulator {    //将某个数值按日累加

    private Date lastDate;    //上次变更数值的时期
    private double value;    //数值的当前值
    private double sum;        //数值按日累加之和

    //构造函数,date为开始累加的日期,value为初始值
    public Accumulator(Date date, double value) {
        lastDate = date;
        this.value = value;
        sum = 0;
    }

    //获得到日期date的累加结果
    double getSum(Date date) {
        return sum + value * date.distance(lastDate);
    }

    //在date将数值变更为value
    void change(Date date, double value) {
        sum = getSum(date);
        lastDate = date;
        this.value = value;
    }

    //初始化,将日期变为date,数值变为value,累加器清零
    void reset(Date date, double value) {
        lastDate = date;
        this.value = value;
        sum = 0;
    }
}

4.多态特性

import java.util.Scanner;

public abstract class Account {

    private String id;	//帐号
    private double balance;	//余额
    private static double total; //所有账户的总金额
    public static void main(String[] args){
        Date date=new Date(2008, 11, 1);	//起始日期
        //建立几个账户
        Account sa1=new SavingsAccount(date, "S3755217", 0.015);
        Account sa2=new SavingsAccount(date, "02342342", 0.015);
        Account ca=new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
        Account accounts[] = { sa1, sa2, ca };
        System.out.println("(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
        char cmd;
        int n=accounts.length;
        do {
            //显示日期和总金额
            date.show();
            System.out.println("\tTotal: " + getTotal()+"\tcommand> ");
            int index, day;
            double amount;
            String desc;
            Scanner in= new Scanner(System.in );
            cmd=in.next().charAt(0);
            switch (cmd) {
                case 'd':	//存入现金
                    Scanner scanner1= new Scanner(System.in );
                    index=scanner1.nextInt();
                    Scanner scanner2= new Scanner(System.in );
                    amount=scanner2.nextInt();
                    Scanner scanner3= new Scanner(System.in );
                    desc=scanner3.nextLine();
                    accounts[index].deposit(date, amount, desc);
                    break;
                case 'w':	//取出现金
                    Scanner scanner4= new Scanner(System.in );
                    index=scanner4.nextInt();
                    Scanner scanner5= new Scanner(System.in );
                    amount=scanner5.nextInt();
                    Scanner scanner6= new Scanner(System.in );
                    desc=scanner6.nextLine();
                    accounts[index].withdraw(date, amount, desc);
                    break;
                case 's':	//查询各账户信息
                    for (int i = 0; i < n; i++) {
                        System.out.println("[" +i +"] ");
                        accounts[i].show();
                        System.out.println();
                    }
                    break;
                case 'c':	//改变日期
                    Scanner scanner7= new Scanner(System.in );
                    day=scanner7.nextInt();
                    if (day < date.getDay())
                        System.out.print("You cannot specify a previous day");
                    else if (day > date.getMaxDay())
                        System.out.print("Invalid day");
                    else
                        date = new Date(date.getYear(), date.getMonth(), day);
                    break;
                case 'n':	//进入下个月
                    if (date.getMonth() == 12)
                        date = new Date(date.getYear() + 1, 1, 1);
                    else
                        date =new  Date(date.getYear(), date.getMonth() + 1, 1);
                    for (int i = 0; i < n; i++)
                        accounts[i].settle(date);
                    break;
            }
        } while (cmd != 'e');
    }
    //供派生类调用的构造函数,id为账户
    protected Account(Date date, String id){
        this.id=id;
        balance=0;
        date.show();
        System.out.println("\t#" + id+ " created");
    }
    //记录一笔帐,date为日期,amount为金额,desc为说明
    protected void record( Date date, double amount,  String desc){
        amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
        balance += amount;
        total += amount;
        date.show();
        System.out.println("\t#" +id +"\t"+amount +"\t" +balance +"\t"+desc );
    }
    //报告错误信息
    protected void error( String msg) {
        System.out.println("Error(#" +id + "): "+msg);
    };
    public String getId() { return id; }
    public double getBalance() { return balance; }
    public static double getTotal() { return total; }
    //显示账户信息
    abstract void deposit(Date date, double amount, String desc);
    //取出现金,date为日期,amount为金额,desc为款项说明
    abstract void withdraw(Date date, double amount, String desc);
    //结算(计算利息、年费等),每月结算一次,date为结算日期
    abstract void settle(Date date);
    void show() {

    }
}
class SavingsAccount extends Account { //储蓄账户类

    private Accumulator acc;    //辅助计算利息的累加器
    private double rate;        //存款的年利率

    //构造函数
    public SavingsAccount(Date date, String id, double rate) {
        super(date, id);
        this.rate = rate;
        acc = new Accumulator(date, 0);
    }

    public double getRate() {
        return rate;
    }

    //存入现金
    public void deposit(Date date, double amount, String desc) {
        record(date, amount, desc);
        acc.change(date, getBalance());
    }

    //取出现金
    public void withdraw(Date date, double amount, String desc) {
        if (amount > getBalance()) {
            error("not enough money");
        } else {
            record(date, -amount, desc);
            acc.change(date, getBalance());
        }
    }

    //结算利息,每年1月1日调用一次该函数
    public void settle(Date date) {
        double interest = acc.getSum(date) * rate	//计算年息
                / date.distance(new Date(date.getYear() - 1, 1, 1));
        if (interest != 0)
            record(date, interest, "interest");
        acc.reset(date, getBalance());
    }
    public  void show() {
        super.show();

    }
}
class CreditAccount extends Account { //信用账户类

    private Accumulator acc;	//辅助计算利息的累加器
    private double credit;		//信用额度
    private double rate;		//欠款的日利率
    private double fee;			//信用卡年费

    double getDebt() {	//获得欠款额
        double balance = getBalance();
        return (balance < 0 ? balance : 0);
    }

    //构造函数
    public CreditAccount(Date date,  String id, double credit, double rate, double fee){
        super(date, id);
        this.credit=credit;
        this.rate=rate;
        this.fee=fee;
        acc=new Accumulator(date,0);
    };
    public  double getCredit() { return credit; }
    public double getRate() { return rate; }
    public  double getFee() { return fee; }
    public double getAvailableCredit()  {	//获得可用信用
        if (getBalance() < 0)
            return credit + getBalance();
        else
            return credit;
    }
    //存入现金
    public void deposit(Date date, double amount, String desc){
        record(date, amount, desc);
        acc.change(date, getDebt());
    }
    //取出现金
    public  void withdraw(Date date, double amount, String desc){
        if (amount - getBalance() > credit) {
            error("not enough credit");
        } else {
            record(date, -amount, desc);
            acc.change(date, getDebt());
        }
    }
    //结算利息和年费,每月1日调用一次该函数
    public  void settle(Date date){
        double interest = acc.getSum(date) * rate;
        if (interest != 0)
            record(date, interest, "interest");
        if (date.getMonth() == 1)
            record(date, -fee, "annual fee");
        acc.reset(date, getDebt());
    }

    public  void show() {
        super.show();
        System.out.println("\tAvailable credit:"+getAvailableCredit());

    }
}
class Date {	//日期类

    private int year;		//年
    private int month;		//月
    private int day;		//日
    private int totalDays;	//该日期是从公元元年1月1日开始的第几天

    int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    public Date(int year, int month, int day){
        this.year=year;
        this.month=month;
        this.day=day;
        if (day <= 0 || day > getMaxDay()) {
            System.out.println("Invalid date: ");
            show();
            System.out.println();
            System.exit(1);
        }
        int years = year - 1;
        totalDays = years * 365 + years / 4 - years / 100 + years / 400
                + DAYS_BEFORE_MONTH[month - 1] + day;
        if (isLeapYear() && month > 2) totalDays++;
    }	//用年、月、日构造日期
    public int getYear() { return year; }
    public int getMonth()  { return month; }
    public int getDay() { return day; }
    public int getMaxDay() {
        if (isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
    }		//获得当月有多少天
    public boolean isLeapYear()  {	//判断当年是否为闰年
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    public void show(){
        System.out.println(getYear()+"-"+getMonth()+"-"+getDay());

    }			//输出当前日期
    //计算两个日期之间差多少天
    public int distance(Date date) {
        return totalDays - date.totalDays;
    }
}
class Accumulator {    //将某个数值按日累加

    private Date lastDate;    //上次变更数值的时期
    private double value;    //数值的当前值
    private double sum;        //数值按日累加之和

    //构造函数,date为开始累加的日期,value为初始值
    public Accumulator(Date date, double value) {
        lastDate = date;
        this.value = value;
        sum = 0;
    }

    //获得到日期date的累加结果
    double getSum(Date date) {
        return sum + value * date.distance(lastDate);
    }

    //在date将数值变更为value
    void change(Date date, double value) {
        sum = getSum(date);
        lastDate = date;
        this.value = value;
    }

    //初始化,将日期变为date,数值变为value,累加器清零
    void reset(Date date, double value) {
        lastDate = date;
        this.value = value;
        sum = 0;
    }
}

以上就是几个版本的银行管理系统的代码了。

写该任务的心得:

(1)一个程序可以有多个类,但是只有主类(类名与class名相同的类)才能用public修饰,否则会出错,main方法也需要放在该类中。

(2)Java中所有的方法的定义与实现必须都放在一个类中,但是C/C++可以在类中定义,类外实现。

(3)C/C++中不允许类嵌套,但是Java可以有内部类和匿名内部类。

(4)在C/C++中,我们在if判断语句中习惯上 bool值false,true就是0或者1,但是在Java中二者不是相通的,否则会出错。

(5)用C/C++写银行管理系统,我们通常会以多文件的形式,模块化,加上头文件;在java中类似的实现是多个类,放在同一个包里面,不放在同一个包会出错。

(6)还需注意的是输入问题:C/C++的输出,cin,scanf都比较简单,在java中的输入需要scanner类
Scanner x = new Scanner(System.in);(构造一个Scanner对象,其传入参数为System.in )
然后输入,例如:int i = x.nextInt();//读取一个int数值,调用类的内部方法实现。参数类型不同,方法也不一样,需要注意和区分。

总结:用java实现简单银行管理系统不难,可能比C/C++还简单,主要是注意两者语言细节上的不同,关键还是在于写银行管理系统思路。

如有误,欢迎大家指正!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一个简单银行管理系统可以包括以下功能: 1. 用户注册和登录:用户可以通过注册创建账号,并且登录到系统中。 2. 存款和取款:用户可以向自己的账户中存入现金,或者从账户中取出现金。 3. 查询余额:用户可以查询自己账户的余额。 4. 转账:用户可以向其他用户的账户中转账。 以下是一个简单Java实现: 首先,我们需要定义一个用户类,包括用户的姓名、账号、密码、余额等属性: ```java class User { private String name; private String account; private String password; private double balance; // 构造函数 public User(String name, String account, String password, double balance) { this.name = name; this.account = account; this.password = password; this.balance = balance; } // getter 和 setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } ``` 接着,我们定义一个银行类,用于管理用户的账户信息: ```java class Bank { private List<User> userList = new ArrayList<>(); // 添加用户 public void addUser(User user) { userList.add(user); } // 查询用户 public User getUser(String account, String password) { for (User user : userList) { if (user.getAccount().equals(account) && user.getPassword().equals(password)) { return user; } } return null; } // 存款 public boolean deposit(String account, double amount) { User user = getUser(account, password); if (user != null) { user.setBalance(user.getBalance() + amount); return true; } return false; } // 取款 public boolean withdraw(String account, double amount) { User user = getUser(account, password); if (user != null && user.getBalance() >= amount) { user.setBalance(user.getBalance() - amount); return true; } return false; } // 转账 public boolean transfer(String account1, String account2, double amount) { User user1 = getUser(account1, password); User user2 = getUser(account2, password); if (user1 != null && user2 != null && user1.getBalance() >= amount) { user1.setBalance(user1.getBalance() - amount); user2.setBalance(user2.getBalance() + amount); return true; } return false; } } ``` 最后,我们可以在主函数中实现用户的注册、登录、存款、取款、查询余额和转账等功能: ```java public static void main(String[] args) { Bank bank = new Bank(); // 注册用户 User user1 = new User("张三", "123456", "123456", 1000); User user2 = new User("李四", "654321", "654321", 2000); bank.addUser(user1); bank.addUser(user2); // 用户登录 User user = bank.getUser("123456", "123456"); if (user != null) { System.out.println("欢迎您," + user.getName() + "!"); } else { System.out.println("账号或密码错误!"); } // 存款 if (bank.deposit("123456", 500)) { System.out.println("存款成功,当前余额为:" + user.getBalance()); } else { System.out.println("账号或密码错误!"); } // 取款 if (bank.withdraw("123456", 200)) { System.out.println("取款成功,当前余额为:" + user.getBalance()); } else { System.out.println("账号或密码错误或余额不足!"); } // 查询余额 System.out.println("您的账户余额为:" + user.getBalance()); // 转账 if (bank.transfer("123456", "654321", 100)) { System.out.println("转账成功,当前余额为:" + user.getBalance()); } else { System.out.println("账号或密码错误或余额不足!"); } } ``` 以上就是一个简单银行管理系统Java实现。当然,实际的银行管理系统还需要更多的功能和安全措施,这里只是提供一个简单的示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值