一个简单的ATM机系统

用链表结构在文件中存储用户信息,实现了ATM机的基本功能,其中还有些问题没有搞懂,代码存在冗余,细节还存在一些问题等,还要继续努力!
package ATMSystem;

import java.io.Serializable;

/**
 * 
 * @author zz384dian
 * @description 用户信息
 */
public class User implements Serializable{

	public String username;
	public String password;
	public double money;
	
	public User(){
		
	}
	
	public User(String username, String password, double money) {
		super();
		this.username = username;
		this.password = password;
		this.money = money;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(money);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (Double.doubleToLongBits(money) != Double.doubleToLongBits(other.money))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
	
}
package ATMSystem;
/**
 * 
 * @author zz384dian
 * @description 输入操作有误异常
 */
public class OperatorException extends Exception{

    public OperatorException(){
		
	}
	
	public OperatorException(String message){
		super(message);
	}
}
package ATMSystem;
/**
 * 
 * @author zz384dian
 * @description money不够异常
 */
public class MoneyNotEnough extends Exception{

	public MoneyNotEnough(){
		
	}
	
	public MoneyNotEnough(String message){
		super(message);
	}
}
package ATMSystem;
/**
 * @author zz384dian
 * @question 1、链表在注册时会进行二次存储,换成新的链表,读出时出现问题    解决:在加载时读出原文件中的链表后进行追加
 *           2、写入文件,如果是true,则会在文件中加上注明链表的东西;如果是false,则在原链表上追加????,flase是覆盖????
 * @description 实现ATM机的主要功能
 *              1、取款
 *              2、存款
 *              3、转账
 *              4、查询余额
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.Scanner;

public class ATMectype {

	private User currentUser = null;//当前用户
	private User otherUser = null;//转账用户
	private boolean flag = false;
	private LinkedList llOut = null;//整个系统中利用链表在文件中存储信息
	private Scanner sc = new Scanner(System.in);

	public ATMectype(){
		
	}
	
	//将主函数中的链表对ATM初始化时传入
	public ATMectype(LinkedList llOut){
		this.llOut = llOut;
	}
	
	//注册用户
	public void register(String username, String password){
		User user = new User(username, password, 0);
		llOut.add(user);
		mainmenu();
	}
	
	/*
    //写入文件
	public void addUser(){
		
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try{
			fos = new FileOutputStream("d://ATMuser/user.txt", false);//true和flase追加和覆盖问题?????
			oos = new ObjectOutputStream(fos);
			oos.writeObject(llOut);
			oos.flush();
			oos.close();
			fos.close();
		}catch(Exception e){
			System.out.println("-------------fail!!!!----------");
			//e.printStackTrace();
		}
		mainmenu();
	}
	*/
	
	//将修改后的内容写入文件
	public void write(){
		
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try{
			fos = new FileOutputStream("d://ATMuser/user.txt", false);//true和flase追加和覆盖问题???????
			oos = new ObjectOutputStream(fos);
			oos.writeObject(llOut);
			oos.flush();
			oos.close();
			fos.close();
		}catch(Exception e){
			System.out.println("-------------fail!!!!----------");
			//e.printStackTrace();
		}
		mainmenu();
	}
	
	/*
	//将链表从文件中读出
	@SuppressWarnings("unchecked")
	public LinkedList readO(){
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		
		try{
			fis = new FileInputStream("d://ATMuser/user.txt");
			ois = new ObjectInputStream(fis);
			
			llOut = (LinkedList)ois.readObject();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				ois.close();
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//return llIn;
		return llOut;
	}
	*/
	
	//登录
	public boolean login(String username,String password){

		for (User user : llOut) {
			if(user.username.equals(username)&&user.password.equals(password)){
				flag = true;
				currentUser = user;
				break;
			}
		}
		if(flag == true){
			System.out.println("登陆成功");
			return true;
		}else{
			System.out.println("用户名或密码不正确!!!,重新输入");
			menuTwo();
		}
		return false;
	}
		

	//取款
	public void getMoney(double money){
		double tempMoney = currentUser.money;
		try{
			equal(money);
			currentUser.money -= money;
		}catch(MoneyNotEnough e){
			System.out.println("取款失败,余额不足");
			currentUser.money = tempMoney;
		}
	}
	
	//存款
	public void setMoney(double money){
		currentUser.money += money;
		System.out.println("存款成功");
	}
	
	//转账
	public void transportMoney(String username,double money){
		 
		for (User user : llOut){
			if(user.username.equals(username)){
				flag = true;
				otherUser = user;
				break;
			}
		}
		
		double tempOther = otherUser.money;
		double tempCurrent = currentUser.money;
		
		if(flag == true){
			try{
				equal(money);
				otherUser.money += money;
				currentUser.money -= money;
				System.out.println("转账成功");
		    }catch(Exception e){
		    	System.out.println("转账失败");
		    	otherUser.money = tempOther;
		    	currentUser.money = tempCurrent;
		    }
		}else{
			System.out.println("输入用户有误,请重新输入");
			username = sc.next();
			money = sc.nextDouble();
			transportMoney(username, money);
		}	
	}

	//查询余额
	public void currentMoney(){
		System.out.printf("余额为%.2f\n",currentUser.money);
	}
	
	//判断余额
	public void equal(double money) throws MoneyNotEnough{
		if(money > currentUser.money)
			throw new MoneyNotEnough("余额不足");
	}

	//登录还是注册菜单
	public void mainmenu(){
		String username = null;
		String password = null;
		String s = null;
		System.out.println("请选择1.注册或2.登录");
		int c = 0;
		try{
			c = sc.nextInt();
			OperatorTwo(c);
			switch(c){
			case 1:
				System.out.println("请输入用户名:");
				username = sc.next();
				System.out.println("请输入密码:");
				password = sc.next();
				register(username, password);
				break;
			case 2:
				System.out.println("请输入用户名:");
				username = sc.next();
				System.out.println("请输入密码:");
				password = sc.next();
				boolean b = login(username, password);
				if(b == true){
					menu();
				}
			}
		}catch(OperatorException e){
			System.out.println("输入有误,重新输入!!!!");
			mainmenu();
		}
	}
	//副菜单
	public void menu(){
		
		String username = null;
		String password = null;
		double money = 0;
		
		System.out.println("请选择想执行的操作");
		System.out.println("1.取款");
		System.out.println("2.存款");
		System.out.println("3.转账");
		System.out.println("4.查询余额");
		System.out.println("5.退出登录");
        int s = 0;
        try{
        	s = sc.nextInt();
        	Operator(s);
        	switch(s){
            case 1:
            	System.out.println("请输入取款金额");
            	money = sc.nextDouble();
            	getMoney(money);
            	menu();
            	break;
            case 2:
            	System.out.println("请输入存款金额");
            	money = sc.nextDouble();
            	setMoney(money);
            	menu();
            	break;
            case 3:
            	System.out.println("请输入转账用户和金额");
            	username = sc.next();
            	money = sc.nextDouble();
            	transportMoney(username, money);
            	menu();
            	break;
            case 4:
            	currentMoney();
            	menu();
            	break;
            case 5:
            	write();//最后退出时将所有用户信息更新到文件中
            	break;
            }
        }catch(OperatorException e){
        	System.out.println("输入有误,重新输入!!!!");
        	menu();
        }  
	}
	
	//副副菜单
	public void menuTwo(){
		String username = null;
		String password = null;
		
		System.out.println("请输入用户名:");
		username = sc.next();
		System.out.println("请输入密码:");
		password = sc.next();
		boolean b = login(username, password);
		if(b == true){
			menu();
		}
	}
	
	//判断输入操作数是否有误
	public void Operator(int a) throws OperatorException{
		if(a>5||a<0)
			throw new OperatorException("输入有误,请重新输入");
	}
	
	public void OperatorTwo(int a) throws OperatorException{
		if(a>2||a<0)
			throw new OperatorException("输入有误,请重新输入");
	}
}

package ATMSystem;
/**
 * @author zz384dian
 * @description 测试ATM机的主函数
 */
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.LinkedList;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedList llOut = new LinkedList();//传入ATM中的链表
		LinkedList llIn = new LinkedList();
		
		//读出文件中的链表
		llIn = read();
		for (User user : llIn) {
			llOut.add(user);
		}
		
		ATMectype atm = new ATMectype(llOut);//读取一遍记录????
        System.out.println("欢迎使用ATM机");
        atm.mainmenu();
	}
	
	//读取文件中链表
	public static LinkedList read(){
		LinkedList ll = null;
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		
		try{
			fis = new FileInputStream("d://ATMuser/user.txt");
			ois = new ObjectInputStream(fis);
			
			ll = (LinkedList)ois.readObject();

		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				ois.close();
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return ll;
	}

}




你可能感兴趣的:(java小练习)