java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java ATM机

Java模拟实现ATM机

作者:点丶错了。。

这篇文章主要为大家详细介绍了Java模拟实现ATM机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Java模拟ATM机,供大家参考,具体内容如下

实现登录,查询,转账,取款,修改密码,退出功能。

源码

package bank;

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

//ATM类
public class Atm {
 private String[] user;//用户全部信息
 private double money;//修改钱数
 private double userMoney;//用户的钱
 private String newPassword;
 private String userInFo;
 private int index;
 private int a =0;
 private int count = 10;

 public void show(){//显示界面
 index = logIn();
 if(index != -1){
  working();
 }
 }

 private String[] newStringUser(String[] str){
 count=count+10;
 String[] newUser = new String[count];
 for(int i=0;i<a;i++)
  newUser[i] = str[i];
 return newUser;
 }

 private void getUser(){//从文件获取全部用户
 String str;
 String[] strings = new String[count];
 File file = new File("src\\bank\\user");
 FileReader fileReader = null;
 BufferedReader bufferedReader = null;
 try{
  fileReader = new FileReader(file);
  bufferedReader = new BufferedReader(fileReader);
  while((str = bufferedReader.readLine())!=null){
  if(a<=count)
   strings[a++] = str;
  else
   strings = newStringUser(strings);
  }
  user = new String[a];
  for(int i=0;i<a;i++)
  user[i] = strings[i];
  strings = null;
 }catch(Exception e){
  e.printStackTrace();
  if((fileReader!=null)&&(bufferedReader!=null)){
  try {
   bufferedReader.close();
   fileReader.close();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  }
 }
 }

 private int logIn(){//用户登录
 getUser();
 String name,password,user;
 String[] a;
 int i = 0;
 int number = -1;
 Scanner input = new Scanner(System.in);
 a:while(i<3){
  System.out.println("请输入用户名:");
  name = input.nextLine();
  System.out.println("请输入用户密码:");
  password = input.nextLine();
  user = name + "*" + password;
  for(int j=0;j<this.user.length;j++){
  a = this.user[j].split("\\*");
  userInFo = a[0]+"*"+a[1];
  if(userInFo.equals(user)){
   number = j;
   break a;
  }
  }
  i++;
  System.out.println("账户或密码错误请重新输入。。。");
 }
 if(number!=-1){
  System.out.println("登录成功");
  try{
  Thread.sleep(1000);
  }catch(Exception e){
  e.printStackTrace();
  }
 }
 else
  System.out.println("您已输入错误三次,卡已被吞!请到银行柜台询问!");
 return number;
 }

 private int anthorLogin(){//查询转账用户是否存在
 Scanner input = new Scanner(System.in);
 String antherUserName;
 String[] a;
 int x=-1;
 System.out.println("请输入要转账的用户名:");
 antherUserName = input.nextLine();
 for(int i=0;i<user.length;i++){
  a = this.user[i].split("\\*");
  if(a[0].equals(antherUserName)){
  x=i;
  break;
  }
 }
 return x;
 }

 private void show1(){
 System.out.println("**********************");
 System.out.println("\t欢迎使用ATM");
 System.out.println("1,账户余额查询\n2,存钱\n3,取钱\n4,转账\n5,修改用户密码\n6,退出系统\n");
 System.out.println("**********************");
 }

 private void changeUser(int x){//改变用户数组里的数据
 String[] str = user[index].split("\\*");
 if(x==1)
  user[index] = str[0]+"*"+newPassword+"*"+str[2];
 else
  user[index] = str[0]+"*"+str[1]+"*"+userMoney;
 }

 private void working(){//atm办理的业务
 String number;
 setMoney();
 do{
  show1();
  System.out.println("请输入要办理的业务序号:");
  Scanner input = new Scanner(System.in);
  number = input.nextLine();
  switch(number){
  case "1":
   look();
   break;
  case "2":
   saveMoney();
   break;
  case "3":
   getMoney();
   break;
  case "4":
   giveMoney();
   break;
  case "5":
   changePassword();
   break;
  case "6":
   System.out.println("欢迎下次光临!");
   write();
   break;
  default:
   System.out.println("您输入有误,请重新输入。。。。");
  }
 }while(!number.equals("6"));
 }

 private void setMoney(){
 String u = user[index];
 userMoney = Double.parseDouble(u.split("\\*")[2]);
 }

 private void look(){//办理查看余额业务
 System.out.println("用户余额为:"+userMoney);
 try{
  Thread.sleep(2000);
 }catch(Exception e){
  e.printStackTrace();
 }
 }

 private void saveMoney(){//办理存钱业务
 money = howMuch("存钱");
 userMoney = userMoney+money;
 changeUser(2);
 look();
 if(isContinue())
  saveMoney();
 }

 private void getMoney(){//办理取钱业务
 money = howMuch("取钱");
 if(money <= userMoney){
  userMoney = userMoney-money;
  changeUser(2);
  look();
  if(isContinue())
  getMoney();
 }
 else
  System.out.println("您的余额不足!");
 }

 private void giveMoney(){//办理转账业务
 int anthorIndex = anthorLogin();
 if(anthorIndex!=-1){
  money = howMuch("转账");
  if(money <= userMoney){
  userMoney = userMoney - money;
  changeUser(2);
  String anthorUser = user[anthorIndex];
  String[] str =anthorUser.split("\\*");
  double money1 = Double.parseDouble(str[2]);
  money = money + money1;
  user[anthorIndex] = str[0]+"*"+str[1]+"*"+money;
  System.out.println("转账成功!");
  look();
  }
  else
  System.out.println("您的余额不足!");
 }
 else
  System.out.println("该用户不存在。。。。");
 }

 private double howMuch(String str){
 System.out.println("欢迎办理"+str+"业务。。。。。。");
 System.out.println("请输入金额(只能是整数且是100的倍数,最多为10000):");
 Scanner input = new Scanner(System.in);
 double money = input.nextDouble();
 if(money%10==0)
  return money;
 else{
  System.out.println("您输入有误!");
  return 0.0;
 }
 }

 private void changePassword(){//办理修改密码业务
 System.out.println("请输入新密码:");
 Scanner input = new Scanner(System.in);
 newPassword = input.nextLine();
 changeUser(1);
 System.out.println("密码修改成功!");
 }

 private boolean isContinue(){
 System.out.println("是否继续办理该项业务?(请输入Y(y)/N(n))");
 Scanner input = new Scanner(System.in);
 String str = input.nextLine();
 if(str.equalsIgnoreCase("y"))
  return true;
 else
  return false;
 }

 private void write(){
 String str = "";
 String s;
 for(int i=0;i<user.length;i++){
  s = user[i];
  if(i!=user.length-1)
  str = str + s + "\n";
  else
  str = str + s;
 }
 File file = new File("src\\bank\\user");
 FileWriter out = null;
 try {
  out = new FileWriter(file);
  out.write(str);
  out.flush();
 } catch (IOException e) {
  e.printStackTrace();
 }finally{
  if(out != null){
  try {
   out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }
 }
}
package bank;

//银行类
public class Bank {

 private Atm atm = new Atm();

 public void welcome(User user){
 System.out.println("欢迎使用atm");
 user.useAtm(atm);
 }

}
package bank;

//用户类
public class User {

 public void useAtm(Atm atm){
 atm.show();
 }
}
//创建user文件当数据库
张三*456*100.0
李四*123*300.0
王五*789*200.0
package bank;

//测试类
public class Text {
 public static void main(String[] args){
 Bank bank =new Bank();
 User user = new User();
 bank.welcome(user);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文