【学习记录】 Java ATM系统

系统介绍:

该系统账户处理全部使用ArrayList(动态数组)金额 账户信息等放在Count类里进行处理运算

主要功能:

  • 存款
  • 取款
  • 转账
  • 信息查询

账户功能:

  • 注册账户
  • 登录账户

主函数

public static void main(String args[])
    {
ArrayList<Count> countArrayList=new ArrayList<>();
        String User;
        String passWord;
        String name;
    while(true)
    {   Scanner input = new Scanner(System.in);
        int button=mainView(input);
        boolean flag=true;
        switch(button)
        { case 1:
            System.out.println("请输入用户名:");
            User = input.next();                      //登录输入用户名
            System.out.println("请输入密码:");
            passWord = input.next();                  //登录输入密码
            if(SystemLog(countArrayList,User,passWord))
            {
                int button1 =SecondView(input);
                while(flag)
                {if(button1==1)
                   { query(countArrayList,User);
                       button1 =SecondView(input);

                   }
                   else if(button1==2)
                   {getMoney(countArrayList,User,input);
                       button1 =SecondView(input);

                   }
                   else if(button1==3)
                    { deposit(countArrayList,User,input);
                        button1 =SecondView(input);

                    }
                   else if(button1==4)
                {
                    transfer(countArrayList,User,input);
                    button1 =SecondView(input);
                }
                   else if(button1==5)
                {
                    break;
                }

                }

            }
            break;
            case 2:
                System.out.println("请输入用户名:");
                User = input.next();                      //登录输入用户名
                System.out.println("请输入密码:");
                passWord = input.next();                  //登录输入密码
                System.out.println("请输入您的姓名:");
                name= input.next();                  //登录输入密码
                enroll(countArrayList,User,passWord,name);
                break;
                default: System.out.println("您输入的操作不存在");
                break;
        }
    }

    }

该项目分为两个界面

主界面 mainView   展示账户操作

 public static int mainView(Scanner input)
    {
        System.out.println("===========欢迎进入ATM系统=============");
        System.out.println("1.登录账户");
        System.out.println("2.注册账户");
        System.out.println("请选择您要进行的操作");

        int button = input.nextInt();
        return button;
    }

副界面 SecondView 展示ATM内部操作

    public static int SecondView(Scanner input)
    {
boolean flag= true;
int b=0;
        while (flag)
        {
            System.out.println("====================================");
            System.out.println("1.查询账户信息");
            System.out.println("2.取款");
            System.out.println("3.存款");
            System.out.println("4.转账");
            System.out.println("5.退出当前账户");
            System.out.println("请选择您要进行的操作");
            int button = input.nextInt();
            switch (button)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    flag = false;
                    return b=button;
                default:
                    System.out.println("您输入的操作不存在");
                    break;
            }
        }
        return b;
    }

系统登录

public static boolean SystemLog(ArrayList<Count> countArrayList,String User,String passWord)
{   boolean flag=false;
    Scanner input = new Scanner(System.in);Count tempCount = new Count();
    if(countArrayList.size()!=0)
    {
        for(int i=0;i<countArrayList.size();i++)
        {
            tempCount = countArrayList.get(i);
            if(tempCount.getPassWord().equals(passWord)&&tempCount.getUserName().equals(User))
            {System.out.println("登录成功!!");
              flag=true;
            }
        }
    }
    else
    {flag=false;
        System.out.println("登录失败!!");
    }
    return flag;
}

系统注册

public static boolean enroll(ArrayList<Count> countArrayList,String User,String passWord,String name)
{
    boolean flag=false;
    Scanner input = new Scanner(System.in);
    Count tempCount = new Count();
    tempCount.setUserName(User);
    tempCount.setPassWord(passWord);
    tempCount.setName(name);
    countArrayList.add(tempCount);
    return true;
}

查询功能单独编写 以便后续调用

public static void query(ArrayList<Count> countArrayList,String User)
{
int f = 0;
    Count tempCount = new Count();
    for(int i=0;i<countArrayList.size();i++)
    {  tempCount = countArrayList.get(i);
        if(tempCount.getUserName().equals(User))
        {   System.out.println("--------------------------------------------------");
            System.out.println("尊敬的"+tempCount.getName()+"用户您好,您的账户信息如下:");
            System.out.println("用户名:"+tempCount.getUserName());
            System.out.println("您的余额为:"+tempCount.getMoney()+"¥");
            System.out.println("--------------------------------------------------");

        }
    }


}

取款功能

public static void getMoney(ArrayList<Count> countArrayList,String User,Scanner input)
{   System.out.println("请输入取款金额:");
        int takeNumber=input.nextInt();
    Count tempCount = new Count();
    for(int i=0;i<countArrayList.size();i++)
    {  tempCount = countArrayList.get(i);
        if(tempCount.getUserName().equals(User))
        {
             if(tempCount.getMoney()>=takeNumber)
             {
                 tempCount.setMoney(tempCount.getMoney()-takeNumber);
             }
             else
             {
                 System.out.println("取款失败,金额不足!!!");
             }
        }
    }
}

存款功能

public static void deposit(ArrayList<Count> countArrayList,String User,Scanner input)
{
int p=-1;
    System.out.println("请输入存款金额:");
    int depositNumber=input.nextInt();
    Count tempCount=new Count();
    for(int i=0;i<countArrayList.size();i++)
    {tempCount =countArrayList.get(i);
        if(tempCount.getUserName().equals(User))
        {
            p=i;
        }
    }
       tempCount=countArrayList.get(p);
    tempCount.setMoney(tempCount.getMoney()+depositNumber);

    }

转账功能

public static void transfer(ArrayList<Count> countArrayList,String User,Scanner input)
{   int p=0;
    int w=0;
    System.out.println("请您输入对方的用户名:");
    String aimUser = input.next();
    System.out.println("请输入转账金额:");
    int depositNumber=input.nextInt();
    Count tempCount = new Count();
    Count aimTempCount = new Count();
    Count aimCount = new Count();
    for(int i=0;i<countArrayList.size();i++)
    {
        tempCount = countArrayList.get(i);
     if(tempCount.getUserName().equals(User))
     {
         p=i;
     }
        aimTempCount = countArrayList.get(i);
        if(aimTempCount.getUserName().equals(aimUser))
        {
            w=i;
        }
    }
    tempCount = countArrayList.get(p);
    aimTempCount = countArrayList.get(w);
        if(aimTempCount.getUserName().equals(aimUser))
        {
            if(depositNumber>tempCount.getMoney())
            {
                System.out.println("余额不足,转账失败!!");
            }
            else if(depositNumber<=tempCount.getMoney())
            {   tempCount.setMoney(tempCount.getMoney()-depositNumber);
                aimTempCount.setMoney(aimTempCount.getMoney()+depositNumber);
                System.out.println("转账成功!!!");
            }
        }



}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值