⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bank.java

📁 老师留的一个银行系统的作业
💻 JAVA
字号:
package TestBank;

import java.util.*;
import java.io.*;

/**
 * 实现描述银行的类Bank,
 * 记录系统中现有哪些储户,(可用数组实现但注意越界),
 * 定义了生成储户的函数append,
 * 按照账户删除储户的函数Delete,
 * 按账号查询储户的函数query,并显示结果。
 * @author 
 * Oct 24, 2007 5:56:39 PM
 */
public class Bank extends HashMap{
	
	private static long id = 0; 
	
	public Account append(Account account) {
		id ++;
		Account a =  (Account)put(new Long(id), account);
		if (a == null) 
			System.out.println("Append OK");
		else 
			System.out.println("Override OK");
		return a;
	}
	
	public Account append(String name, double sum) {
		Bank.id ++;
		Account account = new Account(id , name ,sum);
		Account a =  (Account)put(new Long(id), account);
		if (a == null) 
			System.out.println("Append OK");
		else 
			System.out.println("Override OK");
		return a;
	}
	
	public boolean delete(long id) {
		Account account =  (Account)remove(new Long(id));
		if (account != null) {
			System.out.println("Delete OK ");
			return true;
		}else {
			System.out.println("Delete Failed");
			return false;
		}
	}
	
	public Account query(long id) {
		Account account =  (Account)get(new Long(id));
		if (account != null) {
			System.out.println(account.show());
			return account;
		} else {
			System.out.println("Account failed!");
			return account;
		}
	}
	
	/*
	 * 编写main函数,测试上述所要求的各种功能,
	 * 即可以根据菜单命令增加,删除,和查询储户,以及储户存款和取款操作。
	 */
	public static void main(String[] args) {
		
		System.out.println("Input 'a' for append");
		System.out.println("Input 'f' for search");
		System.out.println("Input 'r' for remove");
		System.out.println("Input 'x' for exit");
		
		Bank icbc = new Bank();
		InputStreamReader stdin = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(stdin);
		String str = "";

		try{
			str = br.readLine();
		}catch(IOException e){
			System.out.println(e.toString());
			System.exit(0);
		}

		while (str.compareToIgnoreCase("x") != 0) {
			
			if (str.compareToIgnoreCase("a") == 0) {
				
				String name = "";
				double sum = 0;
				try{
					// input name and money
					System.out.print("Input Name :");
					name = br.readLine();
					System.out.println("Input sum :");
					sum = Double.parseDouble(br.readLine());
					//System.out.println("sum :" + sum);
				}catch(IOException e){
					System.out.println(e.toString());
					System.exit(0);
				}
				
				if (sum >= 0)
					icbc.append(name, sum);
				else 
					System.out.println("Input error!");
				
			} else if (str.compareToIgnoreCase("r") == 0) {
				
				// input id 
				int id = 0;
				try{
					// input name and money
					System.out.print("Input deleted id :");
					id = Integer.parseInt(br.readLine());
					
				}catch(IOException e){
					System.out.println(e.toString());
					System.exit(0);
				}
				if (id > 0)
					icbc.delete(id);
				else 
					System.out.println("Input error!");
				
			} else if (str.compareToIgnoreCase("f") == 0) {
				
				// input id
				int id = 0;
				try{
					// input name and money
					System.out.print("Input find id :");
					id = Integer.parseInt(br.readLine());
					
				}catch(IOException e){
					System.out.println(e.toString());
					System.exit(0);
				}
				
				if (id > 0) {
					Account ac = icbc.query(id);
					if (ac == null) {
						System.out.println("No this record");
						continue;
					}
					
					System.out.println("Input 's' for save");
					System.out.println("Input 'w' for withdraw");
					
					String input = "";
					try{
						input = br.readLine();
					}catch(IOException e){
						System.out.println(e.toString());
						System.exit(0);
					}
					
					if (input.compareToIgnoreCase("s") == 0) {
						
						System.out.println("Input money for save");
						double sum = 0;
						try{
							// input money
							sum = Double.parseDouble(br.readLine());
							
						}catch(IOException e){
							System.out.println(e.toString());
							System.exit(0);
						}
						
						if (sum >= 0)
							ac.save(sum);
						else 
							System.out.println("Input error!");
						
					} else if (input.compareToIgnoreCase("w") == 0) {
						
						System.out.println("Input money for withdraw");
						double sum = 0;
						try{
							// input money
							sum = Double.parseDouble(br.readLine());
							
						}catch(IOException e){
							System.out.println(e.toString());
							System.exit(0);
						}
						
						if (sum >= 0)
							ac.withdraw(sum);
						else 
							System.out.println("Input error!");
					}
				}
				else 
					System.out.println("Input error!");
			}
			
			try{
				str = br.readLine();
			}catch(IOException e){
				System.out.println(e.toString());
				System.exit(0);
			}
		} // while
	}// main
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -