1. 程式人生 > >第十三章 帶參方法 上機練習4 模擬賬戶查詢,實現取款取款

第十三章 帶參方法 上機練習4 模擬賬戶查詢,實現取款取款

package cn.jbit.test.JianDaTi;

public class testAccount {
	public static void main(String[] args) {
		Account Acc = new Account();
		Acc.Show();
	}
}
package cn.jbit.test.JianDaTi;

import java.util.Scanner;

public class Account {
	Scanner input = new Scanner(System.in);
	public double money = 0;// 初始餘額

	public void Show1() {// 初始化方法
		System.out.println("1.存款 2.取款 3.退出");
		System.out.println("請選擇您需要辦理的業務:");
	}

	public void Balance() {// 餘額查詢
		System.out.println("****當前餘額為" + money + "元****");

	}

	public void Draw(double a) {// 取錢
		if (a <= money) {
			money = money - a;
			System.out.println("取款成功");
			Balance();
		} else {
			System.out.println("卡內餘額不足");
			Balance();
		}

	}

	public void Deposit(double b) {// 存錢
		money = money + b;
		Balance();
	}

	public void Show() {
		Show1();
		int c;// 選項
		int d = 0;// 紀錄輸入次數
		do {
			c = input.nextInt();
			d++;
			if (c == 1) {
				System.out.println("1.存款");
				System.out.println("請輸入存款金額:");
				double b = input.nextDouble();
				Deposit(b);
				System.out.println("\n");
				Show1();
			} else if (c == 2) {
				System.out.println("2.取款");
				System.out.println("請輸入取款金額:");
				double a = input.nextDouble();
				Draw(a);
				System.out.println("\n");
				Show1();
			} else if (c == 3) {
				System.out.println("退出系統,謝謝使用!");
				break;
			} else {
				System.out.println("您的輸入有誤,請輸入1~3任意整數");
				System.out.println("\n");
				Show1();
				if (d == 3) {
					System.out.println("錯誤次數已滿,退出系統,謝謝使用!");
					break;
				}
			}

		} while (!"c".equals(3));
	}
}