1. 程式人生 > >Java小程式:模擬ATM取款

Java小程式:模擬ATM取款

程式執行截圖:


程式碼:

import java.io.IOException;

/**
 * ATM機類
 * 
 * 檢視餘額
 * 
 * 取款
 * 
 * 存款
 * 
 * 退出系統
 * 
 * 
 * 
 */

public class ATM {
	static double yue = 1200.00;

	public static void main(String[] arg) {
		ATM localTest1 = new ATM();
		localTest1.ATM_Operate();
	}

	/**
	 * ATM機的操作
	 */
	private void ATM_Operate() {
		System.out.println("歡迎使用中國工商銀行ATM取款機");
		System.out.println("1、檢視餘額             2、取款");
		System.out.println("3、存款                0、退出");
		System.out.print("請輸入您需要的服務:");
		byte[] buffer = new byte[512];
		try {
			int count = System.in.read(buffer);// 返回實際讀取的位元組數
			System.out.print("您輸入的是:");
			for (int i = 0; i < count; i++) {
				System.out.print("" + (char) buffer[i]);
			}
			if ((char) buffer[0] == '1') {
				// 檢視餘額
				System.out.println("您的餘額是:¥" + yue + "元");
				System.out.println();
				ATM_Operate();
			} else if ((char) buffer[0] == '2') {
				// 取款
				withdrawal();
				System.out.println();
				ATM_Operate();
			} else if ((char) buffer[0] == '3') {
				// 存款
				deposit();
				System.out.println();
				ATM_Operate();
			} else if ((char) buffer[0] == '0') {
				// 退出
				System.out.println("您已經成功退出系統,謝謝你的使用");
				System.exit(0);
			} else {
				System.out.println("輸入不合法,請重新輸入");
				System.out.println();
				ATM_Operate();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 取款
	 * 
	 * @throws IOException
	 */
	private void withdrawal() throws IOException {
		byte[] buffer = new byte[512];

		System.out.print("請輸入您要取出的金額:¥");
		int count2 = System.in.read(buffer);// 返回實際讀取的位元組數
		System.out.print("您輸入的金額是:");
		for (int i = 0; i < count2 - 1; i++) {
			System.out.print("" + (char) buffer[i]);
		}
		System.out.println();
		// 字元0 ~ 9對應ASCII值48 ~ 57
		boolean flag = false;
		for (int i = 0; i < count2 - 1; i++) {
			if ((char) buffer[i] > 47 && (char) buffer[i] < 58) {
				if (i == count2 - 2) {
					flag = true;
				}
			} else {
				// 輸入的字元不是數值
				System.out.println("輸入不合法,請重新輸入");
				withdrawal();
				break;
			}
		}
		System.out.println();
		if (flag) {
			System.out.print("您已成功取出¥:");
			String num = "";
			for (int i = 0; i < count2 - 1; i++) {
				System.out.print("" + (char) buffer[i]);
				num += (char) buffer[i];
			}
			yue -= Double.valueOf(num);
			System.out.print(",現在餘額¥:" + yue);

		}
	}

	/**
	 * 存款
	 * 
	 * @throws IOException
	 */
	private void deposit() throws IOException {
		byte[] buffer = new byte[512];

		System.out.print("請輸入您要存入的金額:¥");
		int count2 = System.in.read(buffer);// 返回實際讀取的位元組數
		System.out.print("您輸入的金額是:");
		for (int i = 0; i < count2 - 1; i++) {
			System.out.print("" + (char) buffer[i]);
		}
		System.out.println();
		// 字元0 ~ 9對應ASCII值48 ~ 57
		boolean flag = false;
		for (int i = 0; i < count2 - 1; i++) {
			if ((char) buffer[i] > 47 && (char) buffer[i] < 58) {
				if (i == count2 - 2) {
					flag = true;
				}
			} else {
				// 輸入的字元不是數值
				System.out.println("輸入不合法,請重新輸入");
				withdrawal();
				break;
			}
		}
		System.out.println();
		if (flag) {
			System.out.print("您已成功存入¥:");
			String num = "";
			for (int i = 0; i < count2 - 1; i++) {
				System.out.print("" + (char) buffer[i]);
				num += (char) buffer[i];
			}
			yue += Double.valueOf(num);
			System.out.print(",現在餘額¥:" + yue);

		}
	}
}