简单地 ATM系统
//用户类
public class account {
private String id; //账号
private String password="888888";//密码
private double balance=0;//余额
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() //复写toString()方法 用于输出账户信息
{
return "账号:"+id+"\n密码:"+password+"\n余额:"+balance;
}
}
//创建ATM类异常
public class ATMException extends Exception {
String name;
ATMException(String name)
{
this.name = name;
}
public String toString() //返回异常的内容
{
return name;
}
}
import java.util.Scanner;
public class ATMDemo {
//static String st="";//用于存放新建的账号 需要赋空值 否则账号前会有 null
static Scanner in = new Scanner(System.in); //需要static
static int chioce; //接收用户的输入
//account p;//创建账户引用 并没有指向对象
static account p = new account();//创建账户对象
//修改密码的方法
public static void changeword()
{
String s="";
System.out.println("请输入您的6位数字新密码:");
try
{
s = in.next();
if(s.length()!=6)
throw new ATMException("您的输入不合法");
else
System.out.println("恭喜密码修改成功。");
}
catch(ATMException e)
{
System.out.println(e.toString());
}
p.setPassword(s);
}
//取钱的方法
public static void getMoney()
{
//局部变量 必须初始化 否则编译出错
int set = 0;//用户存入的金额
System.out.println("请输入您的取钱金额:");
try
{
set = in.nextInt(); //如果这里定义set的话 它的作用范围只在这try的大括号内
if(set>p.getBalance())
throw new ATMException("您的输入不合法");
else
System.out.println("取钱成功,您得金库少了"+set+"元钱");
}
catch(ATMException e)
{
System.out.println(e.toString());
}
p.setBalance(p.getBalance()-set);
}
//存钱的方法
public static void saving()
{
//局部变量 必须初始化 否则编译出错
int set = 0;//用户存入的金额
System.out.println("请输入您的存钱金额:");
try
{
set = in.nextInt(); //如果这里定义set的话 它的作用范围只在这try的大括号内
if(set<=0)
throw new ATMException("您的输入不合法");
else
System.out.println("存钱成功,您多了"+set+"元钱");
}
catch(ATMException e)
{
System.out.println(e.toString());
}
p.setBalance(p.getBalance()+set);
}
//开户方法
public static void newAccount()
{
//局部变量 必须初始化否则编译出错 String成员变量没初始化 系统默认null
String st = ""; //用于存放 随机生成的账号
//随机生成一个6位数的账户名
for(int i=0 ;i<6 ;i++)
{
int a = (int)(Math.random()*100)%10;//随机产生0到9的整数 这个的区别((int)Math.random()*100)%10 永远是0
st+=a;
}
p.setId(st);
System.out.println("开户成功\n您的账户信息是:");
System.out.println(p.toString());
}
//系统菜单
public static void menu()
{
System.out.println("\t1 开户");
System.out.println("\t2 查询余额");
System.out.println("\t3 存款");
System.out.println("\t4 取款");
System.out.println("\t5 修改密码");
System.out.println("\t6 显示用户信息");
System.out.println("\t7 退出系统");
chioce = in.nextInt();
}
public static void main(String[] args) {
System.out.println("+++++++++++欢迎进入《》 钱的世界+++++++++++++");
menu();
do
{
if(chioce==7)
{
break;
}
switch(chioce)
{
case 1:
newAccount();
break;
case 2:
System.out.println("您的余额是:"+p.getBalance());
break;
case 3:
saving();
break;
case 4:
getMoney();
break;
case 5:
changeword();
break;
case 6:
System.out.println(p.toString());
break;
}
System.out.println("1 返回主菜单 <====请选您需要的操作====> 7 推出系统");
chioce = in.nextInt();
if(chioce==1)
menu();
}while(chioce!=7);
System.out.println("欢迎下次光临/再见");
}
}
输出》》》》
+++++++++++欢迎进入《》 钱的世界+++++++++++++
1 开户
2 查询余额
3 存款
4 取款
5 修改密码
6 显示用户信息
7 退出系统
1
开户成功
您的账户信息是:
账号:485841
密码:888888
余额:0.0
1 返回主菜单 <====请选您需要的操作====> 7 推出系统
44444444