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 = 0private 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("未知 *** 作")
}
}
}
package com.lw.thread
/*
银行账户类Account(不能透支),
包含账扰纯号id(10~16位数字),密码password(6位数字),户主姓名name,余额balence
*/
public class Account {
private String id
private int password
private String name
private double balence
public String getId() {
return id
}
public void setId(String id) {
this.id = id
}
public int getPassword() {
return password
}
public void setPassword(int password) {
this.password = password
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public double getBalence() {
return balence
}
public void setBalence(double balence) {
this.balence = balence
}
/*
* 默认构造账户信息为:1111111111111111,666666,钱三多,888888.88。
*/
public Account() {
super()
this.id = "1111111111111111"
this.password = 666666
this.name = "钱乎拿三多"
this.balence = 888888.88
}
/*
* 另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。
*/
public Account(String id, int password, String name, double balence) {
this.id = id
this.password = password
this.name = name
this.balence = balence
}
/*
* 查询余额
*/
public static double selectMoney(Account account) {
return account.getBalence()
}
/*
* 存钱
*/
public static String setMoney(Account account, double balence) {
if (balence <0) {
return "存钱失败,请正确放入!"
}
double d = balence + account.getBalence()
account.setBalence(d)
return "您存入了" + balence + "元,现账户余额为+" + d
}
/*
* 取钱
*/
public static String getMoney(Account account, double balence) {
double d = account.getBalence()
if (balence >d) {
return "您的余缓顷咐额不足!"
}
account.setBalence(d - balence)
return "您取出了" + balence + "元,现账户余额为+" + account.getBalence()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)