银行系统小项目(java)

该博客展示了如何使用Java编程实现一个简单的银行账户类(Account)和客户类(Customer),包括存款、取款、查看余额等功能。用户通过控制台输入选择操作,实现与账户交互。示例中创建了一个名为颜碧莹的客户及其账户,并演示了不同操作的流程。
摘要由CSDN通过智能技术生成

//Account类

package com.power;

public class Account {
    private int id;
    private double balance;

    public Account(int id,double balance){
        this.balance = balance;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void withdraw(double amount){
        if(balance < amount){
            System.out.println("余额不足,取款失败!");
        }else{
            balance-=amount;
            System.out.println("成功取出:"+amount);
            System.out.println("当前余额为:"+balance);
        }
    }

    public void deposit(double amount){
        if(amount > 0){
            balance+=amount;
            System.out.println("当前余额为:"+balance);
        }
    }

    public void balance(){
        System.out.println("当前余额为:"+balance);
    }
}

//customer类

package com.power;

public class customer {
    private String name;
    private Account account;

    public customer(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

//测试类

package com.power;

import java.util.Scanner;

public class customertest {
    public static void main(String[] args) {
        customer cust = new customer("颜碧莹");

        Account acct = new Account(1000, 2000);

        cust.setAccount(acct);
        System.out.println("假定您的余额为2000元!");
        System.out.println("银行卡号为:" + cust.getAccount().getId() + ",姓名为:" + cust.getName());

        Scanner sq = new Scanner(System.in);
        System.out.println("请选择您所要进行的操作:");
        System.out.println("1:进行存款;");
        System.out.println("2:进行取款;");
        System.out.println("3:查看余额;");
        System.out.println("4:退出。");
        int choice = sq.nextInt();

        if (choice == 1) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的存款金额:");
            double amount_deposited = sc.nextDouble();
            cust.getAccount().deposit(amount_deposited);
        }
        if (choice == 2) {
            Scanner se = new Scanner(System.in);
            System.out.println("请输入您的取款金额:");
            double amount_withdrawed = se.nextDouble();
            cust.getAccount().withdraw(amount_withdrawed);
        }
        if (choice == 3) {
            cust.getAccount().balance();
        }
        if (choice == 4) {
            System.out.println("滚!");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小颜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值