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

📄 bank.java

📁 JAVA实现的银行系统,仿照ATM取款机的操作步骤
💻 JAVA
字号:
import java.io.*;
import java.util.*;

//创建银行系统主程序
public class bank{
	
	/*
	 *********声明程序变量*********
	 */
	 String name="";//用于管理用户名
	 String cardNumber="";//用于卡号管理
	 String passWord="";//用于密码的管理
	 String banlance="";//用于余额的管理
	 
	 //假定次管理系统最多可以存储100个用户,并且每个用户占据一行
	 String[][] record=new String[100][4];//利用二位数组来存储用户的所有信息
	 int count=0;//用于记录用户的输入个数
	 int num=0;//用于表明当前用户所处于的位置
	

 /*
  *************用于接收用户键盘的输入操作**********
  */
  public static String input(){
  	String input="";//用于用户输入的信息
  	try{
  		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  		input=in.readLine();
  	}
  	catch(IOException e){
  		System.out.println(e.getMessage());
  		System.exit(0);
  		
  	}
  	return input;
  	
  }
  /*
   ********用于文件的读操作********
   */
   public void readText(){
   
		//声明变量存储数组下标
		int i=0,j=0;
		//声明变量存储从文件中读取出的每行数据
		String str="";
		try{
			BufferedReader fr=new BufferedReader(new FileReader("bank.txt"));
			while((str=fr.readLine())!=null){
				StringTokenizer str1=new StringTokenizer(str);
				while(str1.hasMoreTokens()){
					record[i][j]=str1.nextToken();
					j++;
				}
				j=0;
				i++;
				count++;
			}
			fr.close();
		}
		catch(IOException e){
			System.out.println(e.getMessage());
		}
			
   }
   /*
	 *********文件的写操作*********
	 */
	public void writeText(){
		try{
			FileWriter fw=new FileWriter(new File("bank.txt"));
			for(int i=0;i<count;i++){
				String str=record[i][0]+" "+record[i][1]+" "+record[i][2]+" "+record[i][3];
				fw.write(str);
				fw.write("\r\n");
			}
			fw.flush();
			fw.close();
		}
		catch(IOException e){
			System.out.println(e.getMessage());
		}
	}
	
	
	/************主界面***********
	 */
	
   public void show(){
   	System.out.println("******************************");
   	System.out.println("***新用户注册   请输入  1*****");
   	System.out.println("***老用户       请输入  2*****");
   	System.out.println("***退出系统     请输入  3*****");
   	String input=input();
   	try{
   		int i=Integer.parseInt(input);
   		switch(i){
   			case 1:zhuCe();
   			       break;
   			case 2:dengLu();
   			       break;
   			case 3:System.exit(0);
   			default:System.out.println("输入错误,请重新输入");show();
        	             
   		}
   		
   	}catch(NumberFormatException e){
   		System.out.println("输入错误");show();}
   	
   	
   }
   /*
    ********新用户注册********
    */
    public void zhuCe(){
    	System.out.println("***欢迎新用户使用注册****");
    	boolean flag=true;
    	    
    	while(flag){
    		boolean flag1=true;
    		System.out.println("***请输入注册名****");
    		String name=input();
    		System.out.println("***请输入密码****");
    		String passWord=input();
    		while(flag1){
    		
    		boolean flag2=true;
    		System.out.println("***请输入卡号****");
    		String cardNumber=input();
    		
    		//验证卡号是否重复
    		for(int i=0;i<count;i++){
    			if(record[i][2].equals(cardNumber)){
    				System.out.println("次卡号已将被用,请重新输入卡号");
    				flag2=false;
    				
    				}
    			}
    //验证余额是否大于10
     while(flag2){
     System.out.println("请输入存款金额>=10yuan");
     String money=input();
     double money1=Double.parseDouble(money);
     if(money1<10)
     
     	{
        System.out.println("开户金额不足,请重新输入");	
        }
        else{
        record[count][0]=name;
        record[count][2]=cardNumber;
        record[count][1]=passWord;
        record[count][3]=money;
        num=count;
        count++;
        writeText();
        flag2=false;
		flag1=false;
		flag=false;
		
        System.out.println(name+"先生,恭喜您操作成功");
        System.out.println();
        System.out.println("************************************");
        System.out.println("********继续操作     1**************");
        System.out.println("********返回上一层   2**************");
        System.out.println();
        String input=input();
        int i=Integer.parseInt(input);
        switch(i){
        	case 1:showMain();
        	       break;
        	case 2:show();
        	       break;
        	
                 }
            }
            }
       	
}             
    		
    	}
    	}
    	
    /*
     *****************老用户登陆界面**************
     */
    public void showMain(){
    	 System.out.println();
    	 System.out.println("*********************");
    	 System.out.println("****存款       1****");
    	 System.out.println("****取款       2****");
    	 System.out.println("****查询       3****");
    	 System.out.println("****返回上一层 4****");
    	 System.out.println();
    	 String input=input();
    	 int i=Integer.parseInt(input);
    	 switch(i){
    	 	case 1:cunKuan();
    	 	       break;
    	 	case 2:quKuan();
    	 	       break;
    	 	case 3:chaXun();
    	 	       break;
    	 	case 4:show();
    	 	       break;
    	 }
    	 
    }
    
    
    /*
     ********老用户验证模块***********
     */
     public void dengLu(){
     System.out.println();
     boolean flag=true;
     int j=0;//表示没有次用户
     int h=0;
     while(flag){
        System.out.println("请输入卡号");
     	String cardNumber=input();
     	System.out.println("请输入密码");
     	String pwd=input();

     	
     		for(int i=0;i<count;i++){
				if((record[i][2].equals(cardNumber))&&(record[i][1].equals(pwd))){
					flag=false;
					num=i;
					System.out.println("**********欢迎您使用本系统**********");
					showMain();
					j++;
					break;
				}
				
			}
         

     	if(j==0){
     		System.out.println("无次账户,请重新输入");
     		h++;
     	}
     	if(h==3){
     	System.out.println("您已经输入次数到达三次,系统自动退出");	
     	System.exit(0);
     	}
     }
     }	
     
     /*
      **************存款模块**********
      */
      public void cunKuan(){
      System.out.println();
		boolean flag=true;
		while(flag){
			System.out.println("请输入存款金额:");
			String money=input();
			double money1=Double.parseDouble(money);
			if(money1>=0){
				String s=record[num][3];
				double money2=Double.parseDouble(s);
				money2=money2+money1;
				String balance=Double.toString(money2);
				record[num][3]=balance;
				writeText();
				flag=false;
				System.out.println("祝贺您,存款成功!");
				System.out.println();
	 			System.out.println("************************************");
	 			System.out.println(" ****   继续操作     请按  1   ****");
	 			System.out.println(" ****   退出系统     请按  2   ****");
	 			System.out.println("************************************");
	 			System.out.println();
	 			String input=input();
	 			int i=Integer.parseInt(input);
	 			switch (i){
	 				case 1:showMain();
	 					break;
	 				case 2:System.exit(0);
	 		 	}
			}
			else{
				System.out.println("请输入正确的存款金额!");
				System.out.println();
			}
		}
		
		
	}
	
	/*
	 ****************取款模快******************
	 */public void quKuan(){
	 	System.out.println();
		boolean flag=true;
		while(flag){
			System.out.println("请输入取款金额:");
			String money=input();
			double money1=Double.parseDouble(money);
			String s=record[num][3];
			double money2=Double.parseDouble(s);
			if(money1>=0){
				if((money2-money1)<10){
					System.out.println("您的余额不足,请重新输入!");
					System.out.println();
					
				}
				else{
					String balance=Double.toString((money2-money1));
					record[num][3]=balance;
					writeText();
					System.out.println("祝贺您,取款成功!");
					System.out.println();
				
					System.out.println("************************************");
	 				System.out.println(" ****   继续操作     请按  1   ****");
	 				System.out.println(" ****   退出系统     请按  2   ****");
	 				System.out.println("************************************");
	 				System.out.println();
	 				String input=input();
	 				int i=Integer.parseInt(input);
	 				switch (i){
	 					case 1:showMain();
	 						break;
	 					case 2:System.exit(0);
	 		 		}
				flag=false;
				}
				
			}
			else{
				System.out.println("输入正确的取款金额!");
				System.out.println();
			}
			
		}
		
		
	}
      
      /*
       *********************查询模块***************
       */
       public void chaXun(){
       	System.out.println();
		System.out.println("您的名称为:"+record[num][0]);
		System.out.println("您的密码是:"+record[num][1]);
		System.out.println("您的卡号为:"+record[num][2]);
		System.out.println("您的余额是:"+record[num][3]);
		System.out.println();
		System.out.println("************************************");
	 	System.out.println(" ****   继续操作     请按  1   ****");
	 	System.out.println(" ****   退出系统     请按  2   ****");
	 	System.out.println("************************************");
	 	System.out.println();
	 	String input=input();
	 	int i=Integer.parseInt(input);
	 	switch (i){
	 		case 1:showMain();
	 				break;
	 		case 2:System.exit(0);
	 	}
       }
    		
    		
    	
    	
    	
    	
    /*
    *******主类,接口*******
    */
   
   public static void main(String args[]){
   	 bank b=new bank();
   	 b.readText();
   	 System.out.println("*****************************");
   	 System.out.println("***                       ***");
   	 System.out.println("    欢迎使用银行管理系统     ");
   	 System.out.println("***                       ***");
   	 System.out.println("*****************************");
   	 
   	 /*
   	  ***********用户管理者登陆验证************
   	  */
   	  while(true){
   	  	System.out.println("请输入管理姓名");
   	  	String user=b.input();//存储管理者姓名
   	  	if(user.equals("c")){
   	          System.exit(0);
   	  		 }
   	    else{
   	    
   	  	System.out.println("请输入管理密码");
   	  	String pwd=b.input();//存储管理密码
   	  	if(pwd.equals("c")){
   	          System.exit(0);
   	  		 }
   	  	else{
   	  	
   	  	if((user.equals("admin"))&&(pwd.equals("admin"))){
   	  		b.show();
   	  		break;
   	  	}
   	  	else{
   	  		 
   	  System.out.println("您输入的密码不正确,请重新进行输入");
   	  	    }
   	  	    }
   	  	    }
   	  }
}
}

   	  	 

   
   

⌨️ 快捷键说明

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