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

📄 account.java

📁 Account.java 主要用于用户的增删改数据(用户名+帐户余额)存于E盘accountdata.txt文件中 Bank.java 主要用于用户的增删改数据(用户名+卡号+密码)存于E盘bank
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					}else{
						//System.out.println(subbalance);
						//System.out.println(balance);
						replacestr=allstr.replaceFirst(nameline,name+"\t\t"+balance);
						this.clearData();
						raf.writeBytes(replacestr);
						System.out.println("取款成功!");
						System.out.println("按回车键继续......");
						br.read();
						account.userMenu2();
					}		
				}
				if(str==null){
					System.out.println("还没有帐户存款信息!");
					System.out.println("按回车键继续......");
					br.read();
					//AtmAccount account=new AtmAccount();
					account.userMenu2();
					br.close();
					raf.close();
				}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	protected void changePassword()//修改密码
	{
		String searchstr1="";//要修改的整个行数据
		String allstr="";//文件中读出的全部字符串
		String passwordstr="";//修改后的密码
		String str3="";//得到姓名和卡号的字符串
		int len=0;
		String replacepasswordstr=null;//修改后的全部字符串(修改密码的)
		boolean flag2=false;
		
		File f=new File("E:/bankdata.txt");
		if(f.exists()){
			try{
				BufferedReader br=new BufferedReader(new FileReader(f));
				BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
				RandomAccessFile raf=new RandomAccessFile(f,"rw");				
				String str=br.readLine();
				while(str!=null){
					String[] info=str.split("\n");						
					for(int i=0;i<info.length;i++)
					{
						if(info[i].indexOf(name)!=-1){							
							searchstr1=info[i];
							flag2=true;							
							}
						
						allstr+=info[i]+"\n";
					}					
					str=br.readLine();				
					}
				if(flag2==false){
					System.out.println("没有这个帐户");					
					System.out.println("\n按回车键继续....");
					br1.read();
					AtmAccount account=new AtmAccount();
					account.userMenu2();	
				}else{
					System.out.println("帐户存在!");
					System.out.println("按回车键继续......");
					br1.readLine();
					
					System.out.println("请修改密码:");
					passwordstr=br1.readLine();
					len=name.length()+1;//1是制表符占一个字符
					str3=searchstr1.substring(0,len+3);//向前找四个字符(但愿不要遇到密码字符)
					replacepasswordstr=allstr.replaceFirst(searchstr1,str3+"\t"+passwordstr);
					Bank bank=new Bank();
				    bank.clearData();
					raf.writeBytes(replacepasswordstr);
					System.out.println("帐户已成功修改!");
				}
				System.out.println("按回车键继续......");
				br.read();
				AtmAccount account=new AtmAccount();
				account.userMenu2();
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			try{
				f.createNewFile();
				this.changePassword();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	protected void searchDraw()//查询余额
	{
		boolean flag1=false;
		File f=new File("E:/accountdata.txt");
		if(f.exists()){
			try{
				BufferedReader br=new BufferedReader(new FileReader(f));
				BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
				String str=br.readLine();
					
				while(str!=null){
					String[] info=str.split("\n");
					for(int i=0;i<info.length;i++){
						if(info[i].indexOf(name)!=-1){
							System.out.println("用户名\t\t"+"帐户余额");
							System.out.println(info[i]);
							flag1=true;
						}
					}
					str=br.readLine();
				}
				if(flag1==false){
					System.out.println("帐户余额为0元!");
				}
				//br.close();
				System.out.println("按回车键继续......");
				br1.read();
				AtmAccount account=new AtmAccount();
				account.userMenu2();
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			try{
				f.createNewFile();
				this.searchDraw();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	protected void Transfer()//转帐
	{
		boolean flag1=false;
		boolean flag2=false;
		boolean flag3=false;
		String othername="";//输入的对方姓名
		String transdollar="";//转帐的金额
		String nameline="";//得到包含用户的行(自己)
		String othernameline="";//对方帐户的行数据
		String subbalance="";//原来帐户余额(自己)
		String othersubbalance="";//原来帐户余额(对方)
		String replacestr="";//自己更改后的全部字符串
		String otherreplacestr="";//对方的更改后的全部字符串
		String allstr="";//自己的全部字符串(未更改前)
		String otherallstr="";//对方的全部字符串(未更改前)
		double otherbalance=0.0d;//对方转帐后的钱
		
		AtmAccount account=new AtmAccount();
		
		File f=new File("E:/accountdata.txt");//用于记录用户 用户名和帐户余额
		File f1=new File("E:/bankdata.txt");
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			RandomAccessFile raf=new RandomAccessFile(f,"rw");
			BufferedReader br1=new BufferedReader(new FileReader(f));
			BufferedReader br2=new BufferedReader(new FileReader(f1));
			
			
			String br1str=br1.readLine();
			String br2str=br2.readLine();
			//System.out.println(br1str+br2str);
			while(br1str!=null){
				String[] info=br1str.split("\n");
				for(int i=0;i<info.length;i++){
					if(info[i].indexOf(name)!=-1){
						flag1=true;
						nameline=info[i];
					}
					allstr+=info[i]+"\n";
				}
				br1str=br1.readLine();
			}
			if(flag1==true){
				System.out.println("请输入对方的姓名:");
				othername=br.readLine();
				while(br2str!=null){//在bankdata.txt遍历,看对方帐户是否存在
					String[] info=br2str.split("\n");
					for(int j=0;j<info.length;j++){
						if(info[j].indexOf(othername)!=-1){
							flag2=true;
							othernameline=info[j];
						}
						otherallstr+=info[j]+"\n";
					}
					br2str=br2.readLine();
				}
				
				while(br1str!=null){//用于遍历accountdata.txt..看是否包含对方的帐户,若没有,则相当于先给他存款
					String[] info=br1str.split("\n");
					for(int j=0;j<info.length;j++){
						if(info[j].indexOf(othername)!=-1){
							flag3=true;							
						}						
					}
					br2str=br2.readLine();
				}
				
				if(flag2==true){
					System.out.println("输入正确,对方帐户存在!");
					System.out.println("请输入你的转帐金额:");
					transdollar=br.readLine();
					
					int len=name.length()+2;//2是两个\t的字符长度
					int otherlen=othername.length()+2;
					subbalance=nameline.substring(len);
					othersubbalance=othernameline.substring(otherlen);
					balance=(Double.parseDouble(subbalance)-Double.parseDouble(transdollar));//自己帐户中减掉转帐的钱					
					if(balance<0){
						System.out.println("帐户余额不足!");
						System.out.println("按回车键继续......");
						br.readLine();
						account.userMenu2();
					}else{
						
						replacestr=allstr.replaceFirst(nameline,name+"\t\t"+balance);
						this.clearData();
						raf.writeBytes(replacestr);					
					
						
						if(flag3==true){
							otherbalance=(Double.parseDouble(othersubbalance)+Double.parseDouble(transdollar));//对方帐户中加上转帐的钱	

							otherreplacestr=otherallstr.replaceFirst(othernameline, othername+"\t\t"+otherbalance);
							this.clearData();
							raf.writeBytes(otherreplacestr);
						}else{
							raf.seek(raf.length());							
							raf.writeBytes(othername+"\t\t"+String.valueOf(transdollar)+"\n");//否则直接给对方存款
						}

						raf.close();
						System.out.println("转帐成功!");
						System.out.println("按回车键继续......");
						br.read();
						account.userMenu2();
					}	
				}else{
					System.out.println("对方帐户不存在!");
					System.out.println(" 按回车键继续......");
					br.readLine();
					account.userMenu2();
				}
			}else{
				System.out.println("您的帐户余额为0元,无法转帐,请先存款!");
				System.out.println("按回车键继续......");
				br.readLine();
				account.userMenu2();
			}
			//System.out.println("test..");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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