1. 程式人生 > >模擬自助取款機操作--java小程式

模擬自助取款機操作--java小程式

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Scanner;

public class Account {
	public int balance;
	public int Staticbalance;
	protected int AccountNumber;
	protected String IDcard;
	protected String Name;
	protected String Adrees;
	protected String password;

	public Account() {
	}

	public Account(String IDcard, String Name, String Adress, String password,
			int balance) {
		this.IDcard = IDcard;
		this.Name = Name;
		this.Adrees = Adress;
		this.password = password;
		this.balance = balance;
	}

	public void setStaticbalance(int Staticbalance) {
		this.Staticbalance = Staticbalance;
	}

	public void setAccountNumber() {
		AccountNumber = (int) (Math.random() * 1000000000);
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	public void setIDcard(String IDcard) {
		this.IDcard = IDcard;
	}

	public void setName(String Name) {
		this.Name = Name;
	}

	public void setAdrees(String Adress) {
		this.Adrees = Adress;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return Name;
	}

	public String getAdrees() {
		return Adrees;
	}

	public String getIDcard() {
		return IDcard;
	}

	public int getAccountNumber() {
		return AccountNumber;
	}

	public int getStaticbalance() {
		return Staticbalance;
	}

	public String getPassword() {
		return password;
	}

	public int getbalance() {
		return balance;
	}

	public void setbalance(int balance) {
		this.balance = balance;
	}

	public void setAccount() throws UnsupportedEncodingException {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入姓名:");
		String q = sc.nextLine();
		setName(q);
		System.out.println("請輸入身份證:");
		String p = sc.nextLine();
		setIDcard(p);
		System.out.println("請輸入地址:");
		String o = sc.nextLine();
        o = new String(o.getBytes("GBK"),Charset.defaultCharset()); //輸入中文,要轉換編碼格式
		setAdrees(o);
		setBalance(100);
		setAccountNumber();
	}

	public void deposite() {
		System.out.println("請輸入你需要存的現金:");
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		System.out.println("1.活期 2.定期");
		int s = sc.nextInt();
		if (s == 1)
			balance = balance + w;
		else {
			Staticbalance = Staticbalance + w;
			System.out.println("最低存一年 一年內不可取");
		}
	}

	public void withdraw() {
		System.out.println("請輸入你要取得錢");
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		if (balance > w)
			balance = balance - w;
		else
			System.out.println("您的餘額不足!\n您的活期為" + balance + "定期為"
					+ Staticbalance);
	}

}

import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws UnsupportedEncodingException {
		char choise;
		int i = 0;		
		boolean flag;//判斷標識
		Scanner sc = new Scanner(System.in);
		Account[] array = new Account[100];
		for (int n = 0; n < 100; n++) {
			array[n] = new Account("asd", "asd", "asd", "asd", 1);
		}
		do {			
			System.out.println("------------------------------------------------");
            System.out.println("|       Welcome to the BUU IT Wisdom Bank      |");
            System.out.println("|              What may I help you?            |");
            System.out.println("|             1.開戶                          |");
            System.out.println("|             2.銷戶                          |");
            System.out.println("|             3.按姓名查詢              |");
            System.out.println("|             4.按賬號查詢              |");
            System.out.println("|             5.存錢                          |");
            System.out.println("|             6.取錢                          |");
            System.out.println("------------------------------------------------");
            System.out.println("");
            System.out.println("|               Enter choice 1-6:              |");
            System.out.println("");
            choise=sc.next().charAt(0);
			System.out.println();

			switch (choise) {
			case '1':
				System.out.println("---1.開戶---");
				array[i] = new Account();
				array[i].setAccount();
				i++;
				break;
			case '2':
				System.out.println("---2.銷戶---");
				System.out.println("請輸入要刪除的姓名或賬號:");
				String t = sc.next();
					flag = false;
					for (int y = 0; y < 100; y++) {
						
						if (array[y].getName().equals(t)) {

							System.out.println("刪除成功");
							flag = true;
							break;
						} else {
							continue;
						}
					}
				    if(flag == false){
				    	System.out.println("找不到");
				    }
				    break;
			case '3':
				System.out.println("---3.按姓名查詢---");
				System.out.println("請輸入要查詢的姓名:");
				String u = sc.next();
				flag = false;
				for (int y = 0; y < 100; y++) {
					if (array[y].getName().equals(u)) {
						System.out.println("查詢的姓名為:"+array[y].getName());
						
						System.out.println("您的賬號為:"+array[y].getAccountNumber());
						
						System.out.println("您的餘額為"+array[y].getbalance());
						
						System.out.println("地址為:"+array[y].getAdrees());
						
						flag = true;
						break;
					} else{
						continue;
					}
				}
				if(flag == false){
					System.out.println("不存在");
				}
				
				break;
			case '4':
				System.out.println("---4.按賬號查詢 ---");
				System.out.println("請輸入要查詢的賬號:");
				int m = sc.nextInt();
				flag = false;
				for (int y = 0; y < 100; y++) {
					if (array[y].getAccountNumber() == m) {
						System.out.println("查詢的姓名:" + array[y].getName());
						
						System.out.println("您的賬號為:" + array[y].getAccountNumber());
						
						System.out.println("您的餘額為" + array[y].getbalance());
						
						System.out.println("地址為:" + array[y].getAdrees());
						flag = true;
					} else
						break;
				}
				if(flag == false){
					System.out.println("不存在");
				}
				break;
			case '5':
				System.out.println("---5.存錢---");
				System.out.println("請輸入您的賬號:");
				int qw = sc.nextInt();
				for (int y = 0; y < 100; y++) {
					if (array[y].getAccountNumber() == qw) {
						array[y].deposite();
					} else
						break;
				}
				break;
			case '6':
				System.out.println("---6.取錢 ---");
				System.out.println("請輸入您的賬號:");
				Scanner qwe = new Scanner(System.in);
				int qwr = qwe.nextInt();
				//qwe.close();
				for (int y = 0; y < 100; y++) {
					if (array[y].getAccountNumber() == qwr) {
						array[y].withdraw();
					} else
						break;
				}
				break;
			case '7':
				System.out.println("Quit Succsess");
				sc.close();
				break;
			default:
				System.out.println("illegl entry");
			}

		} while (choise != '7');

	}
}
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
1
 
---1.開戶---
請輸入姓名:
test
請輸入身份證:
321122333313455532
請輸入地址:
杭州市西湖區人民大道
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
3
 
---3.按姓名查詢---
請輸入要查詢的姓名:
test
查詢的姓名為:test
您的賬號為:80586447
您的餘額為100
地址為:杭州市西湖區人民大道
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
4
 
---4.按賬號查詢 ---
請輸入要查詢的賬號:
80586447
查詢的姓名:test
您的賬號為:80586447
您的餘額為100
地址為:杭州市西湖區人民大道
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
5
 
---5.存錢---
請輸入您的賬號:
80586447
請輸入你需要存的現金:
3
1.活期 2.定期
1
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
4
 
---4.按賬號查詢 ---
請輸入要查詢的賬號:
80586447
查詢的姓名:test
您的賬號為:80586447
您的餘額為103
地址為:杭州市西湖區人民大道
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
6
 
---6.取錢 ---
請輸入您的賬號:
80586447
請輸入你要取得錢
5
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
4
 
---4.按賬號查詢 ---
請輸入要查詢的賬號:
80586447
查詢的姓名:test
您的賬號為:80586447
您的餘額為98
地址為:杭州市西湖區人民大道
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
2
 
---2.銷戶---
請輸入要刪除的姓名或賬號:
test
刪除成功
------------------------------------------------
|       Welcome to the BUU IT Wisdom Bank      |
|              What may I help you?            |
|             1.開戶                          |
|             2.銷戶                          |
|             3.按姓名查詢              |
|             4.按賬號查詢              |
|             5.存錢                          |
|             6.取錢                          |
------------------------------------------------
 
|               Enter choice 1-6:              |
 
7
 
Quit Succsess