创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbala)等功能。
package Test_2;
import java.util.Scanner;
public class Account {
private double Money;
public double getMoney() {
return Money;
}
public void setMoney(double money) {
Money = money;
}
public Account()
{
this.Money = 0.0;
}//默认构造函数
public Account(double a)
{
this.Money = a;
}//重载构造函数
public void Deposit(double a,Account n)
{
n.Money += a;
}//存
public void Withdraw(double a,Account n)
{
n.Money -= a;
}//取
public void getbalance(Account n)
{
System.out.println("您的账户余额为:" + n.Money + "元");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account();
System.out.println("输入您的初始金额:");
Scanner in=new Scanner(System.in);
double a1 = in.nextDouble();
account.setMoney(a1);
account.getbalance(account);
boolean flag = true;
while(flag)
{
System.out.println("选择您办理的业务:");
System.out.println("存款请输入1/取款请输入2/查询请输入3/退出请输入0");
int judge = in.nextInt();
if(judge == 0)
flag = false;
else if(judge == 1)
{
System.out.println("输入您所存的金额:");
double dep = in.nextDouble();
account.Deposit(dep, account);
}
else if(judge == 2)
{
System.out.println("输入您所取的金额:");
double with = in.nextDouble();
account.Withdraw(with, account);
}
else if(judge == 3)
{
account.getbalance(account);
}
else
{
System.out.println("输入有误,请重新输入");
}
}
}
}
写得太僵硬了。