自学内容网 自学内容网

java:题目:用Java实现简单的自取取款操作

 

import java.util.Scanner;
public class ATM {

    public static void main(String[] args)
    {
        //自主取款主类
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入账户号码:");
        String BankAccoutsr=sc.nextLine();/
        BankAccout3 newBankAccout=new BankAccout3(BankAccoutsr,0.0);
        boolean exit=false;
        while(!exit)
        {
            System.out.println("\n===== 自助取款操作 =====");
            System.out.println("1. 存款");
            System.out.println("2. 取款");
            System.out.println("3. 查看余额");
            System.out.println("4. 退出");
            System.out.print("请选择操作 (1-4): ");
            int choice=sc.nextInt();

            switch(choice) {
                case 1:
                    System.out.println("请输入存款金额:");
                    double depositAmount=sc.nextDouble();
                    newBankAccout.deposit(depositAmount);
                    break;
                case 2:
                    System.out.println("请输入你要取款的金额:");
                    double withdrawnumber =sc.nextDouble();
                    newBankAccout.withdraw(withdrawnumber);
                    break;
                case 3:
                    newBankAccout.displayAccountinfo();
                    System.out.println("你的余额还剩:"+newBankAccout);
                    break;
                case 4:
                    System.out.println("感谢你使用自主取款系统,再见!");
                    exit=true;
                    break;
                default:
                    System.out.println("无效的输入,请重新选择!");
                    break;

            }
        }
    }
}
class BankAccout3{
     private String BankAccout3;//账户号码
     private double balance;//余额

     //传参构造
    public BankAccout3(String BankAccout,double balance )
    {
        this.BankAccout3=BankAccout;
        this.balance=balance;
    }

    //存款
    public void deposit(double amount)
    {
        if(amount>0)
        {
            balance+=amount;
            System.out.println("存款成功,存入金额:"+amount);
        }
        else {
            System.out.println("存款金额必须大于0");
        }
    }

    //取款
    public void withdraw(double amount)
    {
        if(amount>0)
        {
            if(balance>=amount)
            {
                balance-=amount;
                System.out.println("取款成功,取出金额为:"+amount);
            }
            else{
                System.out.println("余额不足");
            }
        }
        else {
            System.out.println("取款金额必须大于0");
        }
    }

    //输出账户信息
    public void displayAccountinfo()
    {
        System.out.println("账户号码:"+ BankAccout3+"账户余额:"+balance);
    }

    //获取当前余额
    public double getBalance()
    {
        return balance;
    }
}


原文地址:https://blog.csdn.net/2303_81073778/article/details/143532644

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!