Java实现ATM机

Java作业ATM机,使用单例模式,实现存取款,查询余额

import javax.swing.JOptionPane;
public class Main {
	public static mian(String[]args){
		ATM.getInstance(10);//初始化包含十个账户的ATM
		userName=JOptionPane.showInputDialog("用户名");//ÀûÓöԻ°¿òÊäÈëÓû§Ãû
		passWord=JOptionPane.showInputDialog("密码");//ÀûÓöԻ°¿òÊäÈëÃÜÂë
		
	}
}
class ATM{
	public static ATM instance;
	private double deposits[];
	private String userNames[];
	private String passwords[];
	private int currentId;//当前登陆的账户id
	private int accounts=0;//当前ATM机中的账户数量
	final double NON_EXISTENT=-1;
	private ATM(int MaxAccounts){
		deposits=new double[MaxAccounts];
		userNames=new String[MaxAccounts];
		passwords=new String[MaxAccounts];
		currentId=-1;
	}
	public boolean RegisterAnAccount(String acconut,String password){
		if (accounts==userNames.length) {
			//System.out.println("注册失败");
			return false;
		} else{
			userNames[accounts]=acconut;
			passwordS[acconuts]=password;
			return true;
		}
	}
	public double GetAccountBalance(String Name,String passWord) {	//获取账户余额
		if (currentId!=-1) {
			return deposits[currentId];
		}
		System.out.println("该账户不存在");
		return 0;
	}
	public boolean Withdraw(double money){//取钱
		if (currentId==-1 && money>deposits[currentId]) {
			return false;
		}else{
			deposits[currentId]-=money;
			return true;
		}
	}
	public boolean Deposit(double money){//存钱
		if (currentId!=NON_EXISTENT) {
			deposits[currentId]+=money;
			return true;
		}else{
			return false;
		}
	}
	private int FindAccount(String Name) {//查找是否存在该账户
		for(int id=0;id<userNames.length;++id){
			if(userNames[id]==Name) {
				return id;
			}
		} 
		return NON_EXISTENT;
	}
	private Boolean comparePassWord(String passWord) {return currentId!=-1? passWord==passwords[currentId]:false;}//确认登陆密码
	public static ATM getInstance(int MaxAccounts) {instance=new ATM(1);return instance;}//获取ATM单例
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值