Java 类Account模拟ATM机

Account.java

package account;
import java.util.Date;

public class Account 
{
	//dataField
	private int id = 0;
	private double balance;
	private double annualInterestRate;
	Date dateCreated;
	//constructor
	Account()
	{
		dateCreated = new Date();
	}
	Account(int id, double balance)
	{
		this.id = id;
		this.balance = balance;
		dateCreated = new Date();
	}
	//accessor
	int getId()
	{
		return id;
	}
	double getBalance()
	{
		return balance;
	}
	double getARate()
	{
		return annualInterestRate;
	}
	Date getCreatedDate()
	{
		return dateCreated;
	}
	//mutator
	void setId(int id)
	{
		this.id = id;
	}
	void setBalance(double ba)
	{
		this.balance = ba;
	}
	void setARate(double rate)
	{
		this.annualInterestRate = rate;
	}
	//method
	double getMonthlyInterestRate()
	{
		return this.annualInterestRate/12;
	}
	void withDraw(double money)
	{
		if(money > balance)
			System.out.println("Your balance is not enough!");
		else
			this.balance -= money; 
	}
	void deposite(double money)
	{
		this.balance += money;
	}
	public static void main(String[] args)
	{
		Account a = new Account(1122, 20000);
		a.setARate(4.5/100);
		a.withDraw(2500);
		a.deposite(3000);
		
		System.out.println("余额 " + a.getBalance());
		System.out.println("月利息 " + a.getMonthlyInterestRate());
		System.out.println("开户日期 " + a.getCreatedDate());
	}
}

ATM.java

package account;
import java.util.Scanner;

public class ATM 
{
	public static void main(String args[])
	{
		//construct 10 initial account
		Account[] atm = new Account[10];
		for(int i = 0; i< atm.length ; i++)
		{
			atm[i] = new Account((i+100),100);
		}
		
		//ATM机启动
		int id = 0;
		int choice = 0;
		Scanner input = new Scanner(System.in);
		while(true)
		{
			//输入账户
			while(!correct(id))
			{
				System.out.print("Plese enter a right ID:");
				id = input.nextInt();
				System.out.println();
			}
			//System.out.println("yes!");
			System.out.println("------------------------------");
			System.out.println("Main menu\r\n1:check balance");
			System.out.println("2:withdraw");
			System.out.println("3:deposite");
			System.out.println("4:exit");
			System.out.println("------------------------------");
			choice = input.nextInt();
			if(choice == 1)
			{
				System.out.println("Balance:" + atm[id - 100].getBalance());
			}
			else if(choice == 2)
			{
				System.out.println("Enter the money you want to withdraw");
				atm[id - 100].withDraw(input.nextDouble());
			}
			else if(choice == 3)
			{
				System.out.println("Enter the money you want to deposite");
				atm[id - 100].deposite(input.nextDouble());
			}
			else if(choice ==4)
			{
				id = 0;//id错误,重新输入id
			}
		}
	}
	
	public static boolean correct(int a)
	{
		return (a >= 100) && (a <= 109);
	}
}

 

你可能感兴趣的:(Java 类Account模拟ATM机)