Use Java programs to simulate bank ATM machines to realize simple applications including deposit, withdrawal, inquiry, and transfer functions

(1) Imagine the relevant operation sand and which entities in the real world? One is a depositor and the other is an ATM machine. Because
of this according to the principles of object-oriented abstraction, it can be abstracted into two classes: class representatives depositor account information, the bank representative of
ATM type ATM machine. Plus a main class (responsible for instantiating objects of other classes).
(2) Account class record depositors card number, name, password, and account balances and other information, and provides a get method of obtaining each
value of the attribute, there is provided sub_Balance of account balance properties () method and add_Balance () method to simulate Balance Increased
added, Reduced functionality.
Note: In order to strengthen the control access to the attribute data, consider defining it as private.
(3) ATM type simulates the main functions of ATM machines. Based on the understanding of bank ATM machines, consider designing the following main
methods.
① Welcome() method: welcome display function.
② Load_Sys() method: login function.
③ SysOpter() method: Schedule tasks according to user input.
④ Inqu_Info() method: query user account.
5 BetBalance() method: Withdraw money.
AddBalance(): Deposit.
⑦ isBalance() method: judge whether the balance is sufficient.
⑧ isRight() method: Determine whether the card number and password are correct.

Account.java

/*Account类封装储户信息及部分功能*/
import java.io.*;
public class Account {
    
    
	private String number=null;			//卡号
	private String name=null;			//客户姓名
	private String password=null;		//客户密码
	private double money=0.0;			//余额
	/*构造方法,以生成多个储户信息*/
	public Account(String number,String name,String password,double money) {
    
    
		this.number=number;
		this.name=name;
		this.password=password;
		this.money=money;
	}
	protected String get_number() {
    
    
		return number;
	}
	protected String get_Name() {
    
    
		return name;
	}
	protected String get_Password() {
    
    
		return password;
	}
	public double get_Money() {
    
    
		return money;
	}
	protected void sub_Balance(double mon) {
    
    
		money-=mon;
	}										//余额减少
	protected void add_Balance(double mon) {
    
    
		money+=mon;
	}										//余额增加
}

ATM.java

import java.io.*;
class ATM {
    
    
	Account act;
	Account act2;
	public ATM() {
    
    
		act=new Account("000","text","111",5000);
		act2=new Account("222","text","333",5000); 
	}
	/*********欢迎界面*********/
	protected void Welcome() {
    
    
		String str="---------------------";
		System.out.print(str+"\n");
		System.out.print("1.取款."+"\n"+"2.查询."+"\n"+"3.存款."+"\n"+"4.退出系统"+"\n"+"5.转账");
		System.out.print(str+"\n");
	}
	/*********登录系统
	 * @throws Exception *********/
	protected void Load_Sys() throws Exception {
    
    
		String card,pwd;
		int counter=0;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));		//创建标准输入和输出流
		do {
    
    
			System.out.println("请输入您的卡号:");
			card=br.readLine();							//读取键盘信息
			System.out.println("请输入您的密码:");
			pwd=br.readLine();
			if(!isRight(card,pwd)) {
    
    
				System.out.println("您的卡号和密码有误:");
				counter++;
			}else
				SysOpter();
		}while(counter<3);
		System.exit(1);									//应用退出
	}
	protected void SysOpter()throws Exception{
    
    
		int num;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请选择您要操作的项目(1-5):");
		num=br.read();								//num为ascll码转换的整数
		switch(num) {
    
    
		case 49:
			BetBalance();
			break;
		case 50:
			Inqu_Info();
			break;
		case 51:
			AddBalance();
		case 53:
			ZhuanZhang();
		case 52:
			Exit_Sys();
			break;
		}
		System.exit(1);
	}
	/**********信息查询**********/
	protected void Inqu_Info()throws Exception{
    
    
		System.out.print("------------------------\n"+"账号:"+act.get_number()+"姓名;"+act.get_Name()+"余额:"+act.get_Money()+"\n"+"------------------------\n");
		SysOpter();
	}
	/**********取款**************/
	public void BetBalance() throws Exception{
    
    
		String str=null,str1;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		do {
    
    
			System.out.println("请输入取款项目:");
			str=br.readLine();
			double qu=Double.valueOf(str).doubleValue();				//将字符串转换为double类型
			if(qu>act.get_Money()) {
    
    
				System.out.println("余额不足,请重新输入你要取的数目:");
			}else {
    
    
				act.sub_Balance(qu);
				System.out.println("取款成功,您账户的余额为:"+act.get_Money());
				Welcome();
				SysOpter();
			}
		}while(true);
	}
	public void AddBalance() throws Exception{
    
    
		String str=null,str1;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		do {
    
    
			System.out.println("请输入存款项目:");
			str=br.readLine();
			double qu=Double.valueOf(str).doubleValue();
			act.add_Balance(qu);
			System.out.println("存款成功,您账户的余额为:"+act.get_Money());
			Welcome();
			SysOpter();	
		}while(true);
	}
	public boolean isBalance() {
    
    
		if (act.get_Money()<0) {
    
    
			return false;
		}
	return true;
	}

	protected boolean isRight(String card,String pwd) {
    
    
		if (act.get_number().equals(card)&&act.get_Password().equals(pwd))
			return true;
		else
			return false;
	}
	protected boolean isRight2(String card,String pwd) {
    
    
		if (act2.get_number().equals(card)&&act.get_Password().equals(pwd))
			return true;
		else
			return false;
	}

	protected void Exit_Sys() {
    
    
		System.out.println("感谢您使用本系统,再见!");
		System.exit(1);
	}
	/*转账功能*/
	protected void ZhuanZhang() throws Exception {
    
    
		String card,pwd;
		int counter=0;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		do {
    
    
			System.out.println("请输入您要转账的卡号:");
			card=br.readLine();
			System.out.println("请输入您的密码:");
			pwd=br.readLine();
			if(!isRight2(card,pwd)) {
    
    
				System.out.println("您的转账卡号和密码有误:");
				counter++;
			}else
				do {
    
    
					System.out.println("请输入转账的金额:");
					String str = br.readLine();
					double qu=Double.valueOf(str).doubleValue();
					if(qu>act.get_Money()) {
    
    
						System.out.println("余额不足,请重新输入你要转账的数目:");
					}else {
    
    
						act.sub_Balance(qu);
						double qu1=Double.valueOf(str).doubleValue();
						act2.add_Balance(qu1);
						System.out.println("转账成功,您账户的余额为:"+act.get_Money());
						System.out.println("存款成功,账户2的余额为:"+act2.get_Money());
						Welcome();
						SysOpter();
					}
				}while(true);
		}while(counter<3);
		System.exit(1);
		
	}
}

ATMTest.java

public class ATMTest {
    
    
		public static void main(String[] args) throws Exception{
    
    
			ATM atm=new ATM();
			atm.Welcome();
			atm.Load_Sys();
		}
	}

operation result:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/The_Handsome_Sir/article/details/108904044