在学java的小白
2022-01-19 23:07
采纳率: 100%
浏览 23
已结题

ATM系统转账后无法改变值

使用转账功能以后不能改变ArrayList的值
主方法



 package com.itheima.momey;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 周谋
 * 2022/1/19 16:35
 */
public class AtmSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Account s = new Account();
        ArrayList<Account> arr = new ArrayList<>();
        System.out.println("==============欢迎您进入到黑马ATM系统===============");
        lo:while(true){
            System.out.println("1. 登录账户");
            System.out.println("2. 注册账户");
            System.out.println("3. 退出系统");
            System.out.println("请您选择操作:");
            String choose = sc.next();
            switch(choose){
                case "1":
                    s.logIn(arr);
                    break;
                case "2":
                    s.register(arr);
                    break;
                case "3":
                    System.out.println("系统退出");
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的输入有误!");
                    break;
            }
        }
    }
}

###银行类
package com.itheima.momey;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * @author 周谋
 * 2022/1/19 16:23
 */
public class Account {
    Scanner sc = new Scanner(System.in);
    Random random = new Random();

    private String name;
    //账户名

    private String password;
    //账户密码

    private double money;
    //余额

    private double amount;
    //单次取现额度

    private int cardNumber;
    //卡号

    public Account(String name, String password, double amount, int cardNumber) {
        this.name = name;
        this.password = password;
        this.amount = amount;
        this.cardNumber = cardNumber;
    }

    public void setCardNumber(int cardNumber) {
        this.cardNumber = cardNumber;
    }

    public int getCardNumber() {
        return cardNumber;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getAmount() {
        return amount;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public double getMoney() {
        return money;
    }

    public Account() {
    }


    /**
     * 银行注册操作
     */
    public void register(ArrayList<Account> arr) {
        System.out.println("请您输入账户名称:");
        name = sc.next();
        while (true) {
            System.out.println("请您输入账户密码:");
            password = sc.next();
            System.out.println("请您确认输入密码:");
            String okPassword = sc.next();
            // 判断两次密码是否符合
            if (password.equals(okPassword)) {
                break;
            } else {
                System.out.println("两次输入密码不一致!");
            }
        }
        System.out.println("请您设置当次取现额度:");
        amount = sc.nextDouble();
        cardNumber = random.nextInt(99999999) + 10000000;
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i).cardNumber == cardNumber) {
                cardNumber = random.nextInt(99999999) + 10000000;
                i = 0;
            }
        }
        System.out.println("恭喜您," + this.name + "先生/女士" + "您开户完成,您的卡号是:" + this.cardNumber);
        arr.add(new Account(this.name, this.password, this.amount, this.cardNumber));
    }


    /**
     * 登录界面
     */
    public void logIn(ArrayList<Account> arr) {
        System.out.println("===============欢迎您进入到登录操作===============");
        boolean empty = arr.isEmpty();
        if (empty) {
            System.out.println("当前系统无任何账户,请先注册再登录!");
            return;
        }
        System.out.println("请您输入登录的卡号:");
        cardNumber = sc.nextInt();
        int a = 1;
        for (int i = 0; i < arr.size(); i++) {
            if (a == 1) {
                System.out.println("请您输入登录的密码:");
                password = sc.next();
                a--;
            }
            if (arr.get(i).cardNumber == cardNumber) {
                if (arr.get(i).password.equals(password)) {
                    System.out.println("欢迎你:" + arr.get(i).name + "先生/女士进入系统,您可以办理你的业务了!");
                    interFace(arr, i);
                    return;
                } else {
                    System.out.println("您输入的密码有误!");
                    i--;
                    a++;
                }
            } else if (i == arr.size() - 1) {
                System.out.println("您的卡号有误!");
            }
        }
    }

    /**
     * 银行操作系统主界面
     */
    public void interFace(ArrayList<Account> arr, int i) {
        lo:
        while (true) {
            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("6. 退出");
            System.out.println("7. 注销账户");
            System.out.println("请您输入操作命令:");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    inquire(arr, i);
                    break;
                case "2":
                    deposit(arr, i);
                    break;
                case "3":
                    withdrawMoney(arr, i);
                    break;
                case "4":
                    transfer(arr, i);
                    break;
                case "5":
                    revise(arr, i);
                    break;
                case "6":
                    System.out.println("欢迎下次继续光临!!");
                    break lo;
                case "7":
                    arr.remove(i);
                    System.out.println("您的账户以注销!");
                    break;
                default:
                    System.out.println("您的输入有误!");
                    break;
            }
        }
    }

    /**
     * 查询操作
     */
    public void inquire(ArrayList<Account> arr, int i) {
        System.out.println("===============您当前账户详细信息如下===============");
        System.out.println("卡号:" + arr.get(i).cardNumber);
        System.out.println("户主:" + arr.get(i).name);
        System.out.println("余额:" + arr.get(i).money);
        System.out.println("单次提现额度:" + arr.get(i).amount);
    }

    /**
     * 存款操作
     */
    public void deposit(ArrayList<Account> arr, int i) {
        System.out.println("===============欢迎进入账户存款操作===============");
        System.out.println("请您输入存款余额:");
        double money = sc.nextDouble();
        arr.get(i).money += money;
        inquire(arr, i);
    }

    /**
     * 取款操作
     */
    public void withdrawMoney(ArrayList<Account> arr, int i) {
        System.out.println("===============欢迎进入账户取款操作===============");
        while (true) {
            System.out.println("请您输入取款的金额:");
            money = sc.nextDouble();
            if (money > arr.get(i).amount) {
                System.out.println("您当前取款金额超过了每次金额!");
            } else {
                if (money > arr.get(i).money) {
                    System.out.println("您的余额不足!当前余额是:" + arr.get(i).money);
                } else {
                    arr.get(i).money -= money;
                    System.out.println("您当前取钱完成,请拿走你的钱,当前余额是:" + arr.get(i).money);
                    break;
                }
            }
        }
    }

    /**
     * 转账操作
     */
    public void transfer(ArrayList<Account> arr, int i) {
        int j = 0;
        if (arr.get(i).money <= 0) {
            System.out.println("您自己都没钱,就别转了吧!");
        } else {
            if (arr.size() <= 1) {
                System.out.println("当前系统中没有其他账户可以转账,去注册一个吧!");
            } else {
                lo:
                while (true) {
                    System.out.println("请您输入对方卡号:");
                    cardNumber = sc.nextInt();
                    for (; j < arr.size(); j++) {
                        if (arr.get(j).cardNumber == arr.get(i).cardNumber) {
                            String name = arr.get(j).name.substring(1);
                            String name2 = arr.get(j).name.substring(0, 1);
                            System.out.println("请您确认[*" + name + "]的姓氏来确认");
                            System.out.println("请您输入对方姓氏:");
                            String lastName = sc.next();
                            if (lastName.equals(name2)) {
                                while (true) {
                                    System.out.println("请您输入转账的金额(您最多可以转账:" + arr.get(i).money + "元:");
                                    this.money = sc.nextDouble();
                                    if (money > arr.get(i).money) {
                                        System.out.println("你不听话,没有这么多钱可以转!");
                                    } else {
                                        arr.get(i).money -= this.money;
                                        arr.get(j).money += this.money;
                                        System.out.println("您以完成转账!您当前还剩余:" + arr.get(i).money);
                                        break lo;
                                    }
                                }

                            } else {
                                System.out.println("您输入对方的信息有误!");
                                break;
                            }
                        } else if (j == arr.size() - 1) {
                            System.out.println("您输入的卡号不存在!");
                        }
                    }

                }
            }
        }
    }

    /**
     * 修改密码
     */
    public void revise(ArrayList<Account> arr, int i) {
        System.out.println("请输入新密码:");
        this.password = sc.next();
        while (true) {
            System.out.println("请再次输入密码:");
            String password = sc.next();
            if (this.password.equals(password)) {
                break;
            } else {
                System.out.println("输入有误!");
            }
        }
        arr.get(i).password = this.password;
    }
}

arr.get(i).money -= this.money;
arr.get(j).money += this.money;
System.out.println("您以完成转账!您当前还剩余:" + arr.get(i).money);

问题应该出现在这三行代码之间 当我删除 arr.get(j).money += this.money;时转账的账户就能正常扣钱
我想要达到的结果
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • 最佳回答
    
    arr.get(i).money -= this.money;
    arr.get(j).money += this.money;
    改为
    Account a = arr.get(i);
    a.money -= this.money; 
    arr.set(i,a);
    Account b = arr.get(j);
    b.money += this.money; 
    arr.set(j,b);
    
    
    评论
    解决 无用
    打赏 举报 编辑记录

相关推荐 更多相似问题