JAVA 简单银行系统的代码

JAVA 简单银行系统的代码,第1张

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStreamReader

public class AccountDemo {

public static double MONEY = 0// 初始化金额是100元。

public static void main(String[] args) {

final String USER_NAME = "zhangsan"// 用户名

final String PASSWORD = "123456"// 密码

while (true) {

System.out.print("请输入用户名:")

String user_name = getString()

System.out.print("请输入密码:")

String password = getString()

if (user_name != null &&user_name.equals(USER_NAME)

&&password != null &&password.equals(PASSWORD)) {

System.out.println("登陆成功!你要干什么?")

while (true) {

System.out.println("1:存款")

System.out.println("2:取款")

System.out.println("3:查询余额")

System.out.println("q:退出程序")

System.out.print("请选择:")

String userIn = getString()

int in = 0

if (userIn != null &&userIn.equals("1")) {

in = Integer.parseInt(userIn)

} else if (userIn != null &&userIn.equals("2")) {

in = Integer.parseInt(userIn)

} else if (userIn != null &&userIn.equals("3")) {

in = Integer.parseInt(userIn)

} else if (userIn != null

&&userIn.trim().toUpperCase().equals("Q")) {

in = 4

} else {

System.out.println("你输入的指令不正确!请重新输入。")

continue

}

switch (in) {

case 1:

double add_money = 0

while (true) {

System.out.print("请输入你要存入的金额:")

try {

add_money = Double.parseDouble(getString())

} catch (Exception e) {

System.out.println("金额输入不正确!")

continue

}

break

}

MONEY += add_money

System.out.println("存入的金额是" + add_money

+ "\r\n请选择你要的 *** 作:")

break

case 2:

double money = 0

while (true) {

System.out.print("请输入你要取出的金额:")

try {

money = Double.parseDouble(getString())

} catch (Exception e) {

System.out.println("金额输入不正确!")

continue

}

if (money >MONEY) {

System.out.println("取出的金额大于现有存款,请重新输入要取出的金额!")

continue

}

break

}

MONEY -= money

System.out.println("取出的金额是" + money + "\r\n请选择你要的 *** 作:")

break

case 3:

System.out.println("你的余额是:" + MONEY + "\r\n请选择你要的 *** 作:")

break

case 4:

System.out.println("程序退出!")

return

}

}

} else {

System.out.println("错误:用户名与密码不匹配!\r\n")

System.out.println("按任意键:重新输入用户名和密码。")

System.out.println("q:退出程序。")

System.out.print("请选择:")

String in = getString()

if (in.trim().toUpperCase().equals("Q")) {

break

}

}

}

}

public static String getString() {

String str = null

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

try {

str = br.readLine()

} catch (IOException e) {

e.printStackTrace()

}

return str

}

}

private int balance = 0

private  String username = "A"

private  String password = "B"

public void bank() {

    Scanner scan = new Scanner(System.in)

    String  temp

    while (true) {

        System.out.println("输入账号:")

        if (scan.hasNext()) {

            temp = scan.next()

            if (temp.equals(username)) {

                break

            } else {

                System.out.println("输入错误")

            }

        }

    }

    while (true) {

        System.out.println("输入密码:")

        if (scan.hasNext()) {

            temp = scan.next()

            if (temp.equals(password)) {

                break

            } else {

                System.out.println("输入错误")

            }

        }

    }

    System.out.println("登录成功")

    while (true) {

        System.out.println("输入 *** 作:")

        if (scan.hasNext()) {

            temp = scan.next()

            switch (temp) {

                case "存款":

                    int x = 0

                    while (true) {

                        System.out.println("输入存款金额:")

                        if (scan.hasNextInt()) {

                            x = scan.nextInt()

                            break

                        } else {

                            System.out.println("输入错误")

                            scan.next()

                        }

                    }

                    balance += x

                    break

                case "取款":

                    int y = 0

                    while (true) {

                        System.out.println("输入取款金额:")

                        if (scan.hasNextInt()) {

                            y = scan.nextInt()

                            if (balance < y) {

                                System.out.println("余额不足")

                                continue

                            }

                            break

                        } else {

                            System.out.println("输入错误")

                            scan.next()

                        }

                    }

                    balance -= y

                    break

                case "余额":

                    System.out.println("余额:" + balance)

                    break

                case "终止":

                    System.exit(0)

                default:

                    System.out.println("未知 *** 作")

            }

        }

    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11160089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存