import javax.swing.JOptionPane;
public class Account //账户类
{
private String name; //储户姓名
private double balance; //账户余额
public Account(String name,double balance)
{
this.name = name;
this.balance =balance;
}
public String getName() //返回账户名
{
return name;
}
public double balance() //查看账户余额
{
return balance;
}
public boolean put(double value) //存款操作,参数为存入金额
{
if (value>0)
{
this.balance += value; //存款操作使余额值增加
return true;
}
return false;
}
public double get(double value) //取款操作,参数为取款金额,返回实际取到金额
{
if (value>0)
{
if (value<=this.balance)
this.balance -= value; //取款操作使余额值减少
else //账户余额不够所取时
{
value = this.balance; //取走全部余额
this.balance = 0;
}
return value; //返回实际取款额
}
return 0;
}
}
class Account_ex{
public static void main(String args[]){
Account user=new Account("张三",2000);
String str;
int b;
String s=JOptionPane.showInputDialog("您好,存款选择1,取款选择2");
int xz=Integer.parseInt(s);
if(xz==1)
{
str=JOptionPane.showInputDialog("输入您要存入的数额");
b=Integer.parseInt(str);
JOptionPane.showMessageDialog(null, "请放入钞票!");
user.put(b);
JOptionPane.showMessageDialog(null,user.getName()+"\n您的余额为"+user.balance());
}
else{
str=JOptionPane.showInputDialog("输入您要取出的数额");
b=Integer.parseInt(str);
if(b>user.balance())
{
JOptionPane.showMessageDialog(null, "余额不足");
}
else
{
JOptionPane.showMessageDialog(null, "请取出钞票!");
user.get(b);
JOptionPane.showMessageDialog(null,user.getName()+"\n账户余额为"+user.balance());
}
}
}
}
取消
评论