java编写简单的ATM存取系统
新手练手必备~
密码账户为:
先创建账户类:
package cn.atm;
/**
* @author 偶my耶
*/
import java.io.*;
import com.project.project;
public class account {
private string number=null;
private string name=null;
private string password=null;
private double money=0.0;
public account(string number,string name,string password,double money){
this.number=number;
this.name=name;
this.password=password;
this.money=money;
}
public string getnumber() {
return number;
}
public void setnumber(string number) {
this.number = number;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public double getmoney() {
return money;
}
public void setmoney(double money) {
this.money = money;
}
protected void sub_balance(double mon) {
money-=mon;
}
protected void add_balancen(double mon) {
money+=mon;
}
}
然后建立操作类:
里面包含各种操作方法:
package cn.atm;
/**
* @author 偶my耶
*/
import java.io.*;
import com.project.project;
public class atm {
account act;
public atm(){
act=new account("0000", "test", "0000", 2000);
}
/*************欢迎界面*********************/
protected void welcome(){
string str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
system.out.print(str+"\n");
system.out.print(
"1.取款"+"\n"+
"2.查询"+"\n"+
"3.存款"+"\n"+
"4.退出"+"\n"
);
system.out.print(str+"\n");
}
/************登陆系统********************/
protected void load_sys() throws exception{
string card,pwd;
int counter=0;
bufferedreader br=new bufferedreader(new inputstreamreader(system.in));
do{
system.out.println("请输入你的卡号");
card=br.readline();
system.out.println("请输入你的密码");
pwd=br.readline();
if (!isright(card,pwd)) {
system.out.println("你的卡号或密码有误");
counter++;
}
else {
welcome();
sysopter();
}
}while(counter<5);
system.exit(1);
}
/**********系统操作提示******************/
protected void sysopter() throws exception {
int num;
bufferedreader br=new bufferedreader(new inputstreamreader(system.in));
system.out.println("请选择你要操作的项目(1--4)");
num=br.read();
switch (num) {
case 49:
getbalance();
break;
case 50:
inqu_iofo();
break;
case 51:
addbalance();
break;
case 52:
exit_sys();
break;
}
}
/**********信息查询*******************/
protected void inqu_iofo() throws exception{
// todo auto-generated method stub
string str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
system.out.print(str+"\n");
system.out.print(
"账号"+act.getnumber()+"\n"+
"姓名"+act.getname()+"\n"+
"余额"+act.getmoney()+"\n"+
str+"\n"
);
sysopter();
}
/**********存款******************/
public void addbalance() throws exception{
// todo auto-generated method stub
string str=null,str1;
bufferedreader br=new bufferedreader(new inputstreamreader(system.in));
do {
system.out.println("请输入存款数目");
str=br.readline();
double qu=double.valueof(str).doublevalue();
act.add_balancen(qu);
system.out.println("存款成功,你的账户余额为"+act.getmoney());
welcome();
sysopter();
} while (true);
}
/**********取款********************/
public void getbalance() throws exception{
// todo auto-generated method stub
string str=null,str1;
bufferedreader br=new bufferedreader(new inputstreamreader(system.in));
do {
system.out.println("请输入取款数目");
str=br.readline();
double qu=double.valueof(str).doublevalue();
if(qu>act.getmoney()){
system.out.println("余额不足请重新输入");
}
else {
act.sub_balance(qu);
system.out.println("取款成功你的账户余额未"+act.getmoney());
welcome();
sysopter();
}
} while (true);
}
/*********退出******************/
protected void exit_sys () {
// todo auto-generated method stub
system.out.println("安全退出!");
system.exit(1);
}
/**********卡号和密码是否正确********************/
protected boolean isright (string card, string pwd) {
// todo auto-generated method stub
if (act.getnumber().equals(card)&&act.getpassword().equals(pwd)) {
return true;
} else {
return false;
}
}
public static void main(string[] args) throws exception {
atm atm=new atm();
// atm.welcome();
atm.load_sys();
}
}
代码仅供参考
运行结果如下
好了,小伙伴们可以自己做个atm机了^_^