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

📄 account.java

📁 Account.java 主要用于用户的增删改数据(用户名+帐户余额)存于E盘accountdata.txt文件中 Bank.java 主要用于用户的增删改数据(用户名+卡号+密码)存于E盘bank
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.feng.atm;

import java.io.*;


public class Account {

	static String name=null;//注意把这里定义为静态变量,只要前面有程序把name的值改变了,后面的name值就是改变后的值,本程序把login里的name传到了Deposit方法里的
	String password=null;
	String number=null;//卡号
	double balance=0.0d;//余额
	
	boolean flag=false;//用于判断用户是否存在		
	
	public Account(){}
	
	public Account(String name,String password,String number,double balance)
	{
		this.name=name;
		this.password=password;
		this.number=number;
		this.balance=balance;

	}
	
	
	protected String Login()//客户登陆atm机 
	{	
		AtmAccount account=new AtmAccount();
		try{
			BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));			
			do{
				this.judgeUser();
				//System.out.println(name);
				
				if(flag==true){
					System.out.println("用户名和密码输入正确!");
					System.out.println("按回车键继续......");
					br1.read();
					//AtmAccount account=new AtmAccount();
					account.userMenu2();
				}else{
					for(int i=0;i<2;i++){
						int j=2-i;
						System.out.println("用户名或密码错误!您还有"+j+"次机会!");//注意java中不能把数学表达式放在输出中
						this.judgeUser();
						if(flag==true){
							System.out.println("用户名和密码输入正确!");
							System.out.println("按回车键继续......");
							br1.read();
							//AtmAccount account=new AtmAccount();
							account.userMenu2();
						}
						if(i==1){
							System.out.println("对不起,您三次输入的用户名或密码都不对,您的卡已被atm机吞了,程序被强行退出!\n" +
									"可能出错原因:\n"+
									"1,您三次输入的用户名或者密码有错误!\n"+
									"2,您还没开户,请到银行开户!");
							System.out.println("按回车键退出......");
							br1.read();
							System.exit(0);
						}
					}
				}				
			}while(true);
			}catch(Exception e){
				e.printStackTrace();
			}
		return name;
			
		}
	
	
	protected boolean judgeUser()//判断用户是否存在
	{		
		String usernameline=null;//包含用户名的行	
		
		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));
				String str=br.readLine();
			/*
				System.out.println("请输入用户名:");
				name=br1.readLine();
				*/
				this.inputName();
				System.out.println("请输入用户密码:");
				password=br1.readLine();
				while(str!=null){
					String[] info=str.split("\n");
					for(int i=0;i<info.length;i++){
						if(info[i].indexOf(name)!=-1 && info[i].substring(8).indexOf(password)!=-1){
							//System.out.println(info[i]);
							usernameline=info[i];
							flag=true;							
						}
					}
					str=br.readLine();
				}
				
				br.close();
				//br1.close();
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			try {
				f.createNewFile();
				this.judgeUser();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;//把这里的flag传到Login方法里
	}
	
	
	protected String inputName(){//输入用户名
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			System.out.println("请输入用户名:");
			name=br.readLine();
			//br.close();//注意这里的流不能关闭
		}catch(Exception e){
			e.printStackTrace();
		}
	
		return name;
	}
	
	protected String getName(){//得到用户名(登录时你输入的)
		return name;
	}
	
	protected void clearData(){//清除所有数据,写这个方法为了存取款用到的
		File f=new File("E:/accountdata.txt");//创建File实例
		if(f.exists())
		{
			try {
				PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(f)));
				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
				pw.write("");//向bank.txt中写入一个空字符串即可
				pw.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	

	
	protected void Deposit()//存款
	{
		boolean flag1=false;
		String nameline="";//得到包含该姓名的行
		String subbalance="";//原来帐户余额
		String allstr="";//文件中全部的字符串
		String replacestr="";//替换后余额的字符串
		
		AtmAccount account=new AtmAccount();
		
		File f=new File("E:/accountdata.txt");//accountdata.txt用来存储帐户余额信息
		try {
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));			
			RandomAccessFile raf=new RandomAccessFile(f,"rw");
			BufferedReader br1=new BufferedReader(new FileReader(f));
			
			String str=br1.readLine();
			System.out.println("请输入你的存款金额:");
			balance=Double.parseDouble(br.readLine());
			
			while(str!=null){
				String[] info=str.split("\n");
				for(int i=0;i<info.length;i++){
					if(info[i].indexOf(name)!=-1){
						nameline=info[i];
						flag1=true;
					}
					allstr+=info[i]+"\n";
				}
				str=br1.readLine();
			}
			if(flag1==true){
				int len=name.length()+2;//2是两个\t的字符长度
				subbalance=nameline.substring(len);
				balance=(Double.parseDouble(subbalance)+balance);
				//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){
				raf.seek(raf.length());
				String name=this.getName();			
				//System.out.println(name+"haha");
				raf.writeBytes(name+"\t\t"+String.valueOf(balance)+"\n");
				System.out.println("存款成功!");
				System.out.println("按回车键继续......");
				br.read();
				//AtmAccount account=new AtmAccount();
				account.userMenu2();
				br.close();
				raf.close();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	protected void Withdraw()//取款
	{		
			boolean flag1=false;
			String nameline="";//得到包含该姓名的行
			String subbalance="";//原来帐户余额
			String allstr="";//文件中全部的字符串
			String replacestr="";//替换后余额的字符串
			
			AtmAccount account=new AtmAccount();
			
			File f=new File("E:/accountdata.txt");//accountdata.txt用来存储帐户余额信息
			try {
				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));			
				RandomAccessFile raf=new RandomAccessFile(f,"rw");
				BufferedReader br1=new BufferedReader(new FileReader(f));
				
				String str=br1.readLine();
				System.out.println("请输入你的取款金额:");
				balance=Double.parseDouble(br.readLine());
				
				while(str!=null){
					String[] info=str.split("\n");
					for(int i=0;i<info.length;i++){
						if(info[i].indexOf(name)!=-1){
							nameline=info[i];
							flag1=true;
						}
						allstr+=info[i]+"\n";
					}
					str=br1.readLine();
				}
				if(flag1==true){
					int len=name.length()+2;//2是两个\t的字符长度
					subbalance=nameline.substring(len);
					balance=(Double.parseDouble(subbalance)-balance);
					if(balance<0){
						System.out.println("帐户余额不足!");
						System.out.println("按回车键继续......");
						br.readLine();
						account.userMenu2();

⌨️ 快捷键说明

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