import java.util.Objects;
import java.util.Scanner;

public class BankS {
    public static void main(String[] args) {
        Bank bank=new Bank();
        bank.login();
    }
}

class Bank{
/**
 * 银行类
 * 属性: 个数
 */
    private User[] users=new User[5];//保存用户的数组
    private int num;//保存个数
    Scanner scan=new Scanner(System.in);
    Scanner input=new Scanner(System.in);
    //初始化方法:
    public void initialize(){
        //创建用户对象
        User user1=new User();
        user1.setCardNumber("622208800660518");
        user1.setIdentityCard("1234567890");
        user1.setUsername("榮十一");
        user1.setPassword("000222");
        user1.setPhone("13145200222");
        user1.setBalance(500000);
        //创建用户2对象
        User user2=new User("6220819990404518","2345678901","张桜沝","990404","13145200518",200000);
        users[0]=user1;
        users[1]=user2;
        num=2;
    }
    public Bank(){
        initialize();
    }

    //登录
    public void login(){
        int n=3;//设置3次输入账号和密码的次数
        do {
        System.out.println("请输入卡号");
        String cardNumber=scan.next();
        System.out.println("请输入密码");
        String password=scan.next();
        //遍历数组
        User u=null;//保存找到的用户

            for (int i=0;i<num;i++){
                if(users[i].getCardNumber().equals(cardNumber)&&users[i].getPassword().equals(password)){//判断账号和密码是否正确
                    u=users[i];
                    break;
                }else {

                    if (!users[i].getCardNumber().equals(cardNumber)){//判断账号是否正确
                        if (i==num-1){
                            System.out.println("输入的账号不正确");
                        }
                    }else if (users[i].getCardNumber().equals(cardNumber)&&!users[i].getPassword().equals(password)){//判断在账号正确的前提下,密码是否正确
                        if (i==num-1){
                            System.out.println("输入的密码不正确");
                        }
                    }
                }

            }
            //登录成功 进入菜单界面
            if (u!=null){
                System.out.println("登录成功");
                showMenu(u);
            }
            n--;
            System.out.println("请重新输入,你还有"+n+"次机会");
            if (n==0){//当n=0时,告诉用户,账号已被冻结
                System.out.println("密码错误三次,你的账号已被冻结,请前往银行办理解冻手续");
            }
        }while (n>0);//判断是否还能输入的账号和密码
    }
    //显示菜单
    public void showMenu(User u){

        System.out.println("===========欢迎进入xxx银行系统=======当前用户"+u.getCardNumber()+"==========");
        do {
            System.out.println("=========1.存款  2.取款  3.转账  4.查询余额  5.修改密码 0.退出========");
            int choice=input.nextInt();
            switch (choice){
                case 1:
                    this.save(u);
                    break;
                case 2:
                    this.withDraw(u);
                    break;
                case 3:
                    this.trans(u);
                    break;
                case 4:
                    this.query(u);
                    break;
                case 5:
                    this.modifyPassword(u);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入选项错误,请重新输入");
                    break;
            }
        }while (true);//直到输入正确指令后结束,否则一直循环
    }
    //存钱
    public void save(User u){
        System.out.println("请输入存款金额");
        double money=input.nextDouble();
        if (money>100){//判断存款是否大于100;若大于,则存款成功,若小于,则存款失败
            u.setBalance(u.getBalance()+money);
            System.out.println("存钱成功:余额"+u.getBalance());
        }else {
            System.out.println("存钱失败,金额小于一百,无法存入请重新存款...");
        }
    }
    //取钱
    public void  withDraw(User u){
        System.out.print("请输入取款金额:");
        double money=input.nextDouble();
        if (money>100){//若取款金额是否大于一百
            if (u.getBalance()>=money){//判断余额是否大于等于取款金额,若是,则取款成功,否则失败
                u.setBalance(u.getBalance()-money);
                System.out.println("取钱成功:余额"+u.getBalance());
            }else{
                System.out.println("取款失败,你的余额不足...");
            }
        }else {
            System.out.println("取钱失败,请重新输入");
        }
    }
    //转账
    public void trans(User u){
        System.out.print("请输入对方卡号:");
        String cardNumber=input.next();
        System.out.print("请输入转账金额:");
        double money=input.nextDouble();
        User ToUser=null;
        //判断对方的卡号是否存在
        for(int i=0;i<num;i++){
            if (users[i].getCardNumber().equals(cardNumber)){
                ToUser=users[i];
                break;
            }
        }
        //判断卡号是否为空
        if (ToUser!=null){//若不是,则开始转账。若是,则账号不存在,无法转账
            if (u.getBalance()>money){
                u.setBalance(u.getBalance()-money);//更新转账人用户的数据
                ToUser.setBalance(ToUser.getBalance()+money);//更新被转账用户人的数据
                System.out.println("转账成功....");
            }else {
                System.out.println("转账失败,余额不足....");
            }
        }else{
            System.out.println("对方卡号不存在,无法转账,请重新输入");
        }
    }
    //查询余额
    public void query(User u){
        System.out.println("卡号:"+u.getCardNumber()+"\n用户名:"+u.getUsername()+"\n余额:"+u.getBalance());
    }
    //修改密码
    public void modifyPassword(User u){

        do {
            System.out.print("请输入新的密码:");
            String newpassword=input.next();
            System.out.print("请确认新的密码:");
            String newpassword1=input.next();
            if (newpassword.length()==6&&newpassword1.length()==6){//判断两次设置的密码是否为六位数字  是 则开始判断密码是否相同,不是 则告诉用户,密码不符合要求,请重新输入
                if (Objects.equals(newpassword, newpassword1)){//判断两次设置的密码是否相同,相同则设置成功,不同则设置失败
                    u.setPassword(newpassword);//更新密码
                    System.out.println("修改密码成功...");
                    break;
                }else {
                    System.out.println("两次输入的密码不相同");
                }

            }else {
                System.out.println("输入的密码不符合要求,请重新输入由六位数字组合的密码");
            }
        }while (true);
    }
}

//用户类
class User{
    /**
     * 属性:卡号 身份证 用户名 密码 电话 余额
     */
    private String cardNumber;
    private String identityCard;
    private String username;
    private String password;
    private String phone;
    private double balance;

    //默认构造法
    public User(){}
    //带参构造法
    public User(String cardNumber, String identityCard, String username, String password, String phone, double balance) {
        this.cardNumber = cardNumber;
        this.identityCard = identityCard;
        this.username = username;
        this.password = password;
        this.phone = phone;
        this.balance = balance;
    }
    //get和set

    public String getCardNumber() {
        return cardNumber;
    }

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

    public String getIdentityCard() {
        return identityCard;
    }

    public void setIdentityCard(String identityCard) {
        this.identityCard = identityCard;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}