银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期(Date),账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
package ch09;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Account user = new Account();
while (true){
System.out.println("请输入办理业务功能(1.开户 2.查看当前用户信息 3.存款 4.取款 5.设置密码 6.退出系统)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num){
case 1:
user.init();
System.out.println("用户创建成功");
break;
case 2:
user.detail();
break;
case 3:
System.out.print("请输入存款金额:");
double balance = input.nextDouble();
user.deposit(balance);
break;
case 4:
System.out.print("请输入取款金额:");
balance = input.nextDouble();
user.withdrawMoney(balance);
break;
case 5:
user.setPassword();
break;
case 6:
System.out.println("系统已退出");
System.exit(0);
}
}
}
}
package ch09;
import java.util.Date;
import java.util.Scanner;
public class Account {
private String id;
private String name;
private Date date;
private int password;
private double balance;
public Account() {
password = 123456;
id = "ab123456789";
this.balance = 0;
this.date = new Date();
}
public Account(String name, double balance) {
password = 123456;
id = "ab123456789";
this.name = name;
this.balance = balance;
this.date = new Date();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public double getBalance() {
return balance;
}
// 初始化账户
public void init(){
Scanner input = new Scanner(System.in);
System.out.print("请输入姓名:");
this.name = input.next();
System.out.print("请输入存入金额:");
this.balance = input.nextDouble();
}
// 账户信息
public void detail(){
System.out.printf("开户日期为:%s\n", getDate());
System.out.printf("当前用户id为:%s\n", getId());
System.out.printf("当前用户的名字为:%s\n", getName());
System.out.printf("当前余额为%f\n", getBalance());
}
// 存款
public void deposit(double balance) {
if(balance <= 0){
System.out.println("存款的钱不能小于等于0");
}else {
this.balance += balance;
System.out.println(this.balance);
System.out.printf("成功存入%.2f元, 存款后的余额为%.2f元\n", balance, this.balance);
}
}
// 取款
public void withdrawMoney(double balance){
if(balance > this.balance){
System.out.printf("余额不足, 无法取出, 当前余额为%f\n", this.balance);
}else if(balance < 0){
System.out.println("取出的钱不能小于0");
}else {
this.balance -= balance;
System.out.printf("成功取出%.2f元, 剩余余额为%.2f元\n", balance, this.balance);
}
}
// 变更密码
public void setPassword(){
System.out.print("请输入当前密码:");
Scanner input = new Scanner(System.in);
int password = input.nextInt();
if(this.password != password){
System.out.println("输入的密码有误, 请重新输入");
}else {
System.out.print("请输入新密码:");
int password1 = input.nextInt();
System.out.print("请再次输入新密码:");
int password2 = input.nextInt();
if (password1 == password2){
this.password = password1;
System.out.println("修改密码成功");
}
}
}
}
标签:小型,name,账户,银行,System,input,balance,public,out
From: https://www.cnblogs.com/Reina-love/p/16857656.html