1. 就是用java编写一个ATM的程序
你是做实习还是做真实项目,如果是真实项目,估计够呛,因为ATM这些要用嵌入式开发,Java需要环境,而且效率低,如果仅仅是做练习,你可以用JFrame来实现 或者swing,其实说白这些不过是一些框架类库,你安装了这些框架类库,然后用一个类去继承窗体类,其他的和一些窗体软件开发是一样的,用的是事件机制,说白了就是观察者设计模式。
2. 如何用Java编写模拟ATM取款机的程序
import java.io.IOException;
/**
* ATM机类
*
* 查看余额
*
* 取款
*
* 存款
*
* 退出系统
*
*
*
*/
public class ATM {
static double yue = 1200.00;
public static void main(String[] arg) {
ATM localTest1 = new ATM();
localTest1.ATM_Operate();
}
/**
* ATM机的操作
*/
private void ATM_Operate() {
System.out.println("欢迎使用中国工商银行ATM取款机");
System.out.println("1、查看余额 2、取款");
System.out.println("3、存款 0、退出");
System.out.print("请输入您需要的服务:");
byte[] buffer = new byte[512];
try {
int count = System.in.read(buffer);// 返回实际读取的字节数
System.out.print("您输入的是:");
for (int i = 0; i < count; i++) {
System.out.print("" + (char) buffer[i]);
}
if ((char) buffer[0] == '1') {
// 查看余额
System.out.println("您的余额是:¥" + yue + "元");
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '2') {
// 取款
withdrawal();
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '3') {
// 存款
deposit();
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '0') {
// 退出
System.out.println("您已经成功退出系统,谢谢你的使用");
System.exit(0);
} else {
System.out.println("输入不合法,请重新输入");
System.out.println();
ATM_Operate();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 取款
*
* @throws IOException
*/
private void withdrawal() throws IOException {
byte[] buffer = new byte[512];
System.out.print("请输入您要取出的金额:¥");
int count2 = System.in.read(buffer);// 返回实际读取的字节数
System.out.print("您输入的金额是:");
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9对应ASCII值48 ~ 57
boolean flag = false;
for (int i = 0; i < count2 - 1; i++) {
if ((char) buffer[i] > 47 && (char) buffer[i] < 58) {
if (i == count2 - 2) {
flag = true;
}
} else {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入");
withdrawal();
break;
}
}
System.out.println();
if (flag) {
System.out.print("您已成功取出¥:");
String num = "";
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
num += (char) buffer[i];
}
yue -= Double.valueOf(num);
System.out.print(",现在余额¥:" + yue);
}
}
/**
* 存款
*
* @throws IOException
*/
private void deposit() throws IOException {
byte[] buffer = new byte[512];
System.out.print("请输入您要存入的金额:¥");
int count2 = System.in.read(buffer);// 返回实际读取的字节数
System.out.print("您输入的金额是:");
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9对应ASCII值48 ~ 57
boolean flag = false;
for (int i = 0; i < count2 - 1; i++) {
if ((char) buffer[i] > 47 && (char) buffer[i] < 58) {
if (i == count2 - 2) {
flag = true;
}
} else {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入");
withdrawal();
break;
}
}
System.out.println();
if (flag) {
System.out.print("您已成功存入¥:");
String num = "";
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
num += (char) buffer[i];
}
yue += Double.valueOf(num);
System.out.print(",现在余额¥:" + yue);
}
}
}
3. JAVA模拟ATM机
import java.util.*;
public class ATM {
static int people=123456;
static int mima=123456;
static int yue=10000;
static Scanner in=null;
public ATM(int people,int mima){
this.mima=mima;
}
public static void main(String[] args) {
chongxindenglu();
int count=1;
while(count!=0){
System.out.println("1、查询余额");
System.out.println("2、取款");
System.out.println("3、存款");
System.out.println("4、修改密码");
System.out.println("5、重新登录");
count=in.nextInt();
switch(count){
case 1: System.out.println(10000); System.out.println(); break;
case 2: quKuan(); break;
case 3: cunKuan(); break;
case 4: xiugai(); break;
case 5: chongxindenglu();
}
}
}
public static void quKuan(){
System.out.println("请输入取款金额:");
int qukuanjine=in.nextInt();
if(qukuanjine>5000){
System.out.println("取款金额不能大于5000"+"\n");
}else{
if(qukuanjine%100!=0){
System.out.println("取款金额必须为100的整逗凳数倍"+"\n");
}else{
yue=yue-qukuanjine;
System.out.println("您取出了"+qukuanjine+"元亏指指,您现在的余额为:"+yue+"元"+"\n");
}
}
}
public static void cunKuan(){
System.out.println("请输入存款金额");
int cunkuanjine=in.nextInt();
if(cunkuanjine<0){
System.out.println("存款金额不能小于0");
}else{
yue=yue+cunkuanjine;
System.out.println("您已成功存入:"+cunkuanjine+"元,您现在总余额为:"+yue+"元"+"\n");
}
}
public static void xiugai(){
System.out.println("请输入您的密码:");
int mima1=in.nextInt();
if(mima1==mima){
System.out.println("请输入新密码:");
mima=in.nextInt();
System.out.println("您的销配密码已修改为:"+mima+"\n");
}else{
System.out.println("密码输入错误:"+"\n");
}
}
public static void chongxindenglu(){
in=new Scanner(System.in);
System.out.println("请输入登录帐号:");
int denglupeople=in.nextInt();
System.out.println("请输入登录密码:");
int denglumima=in.nextInt();
if(denglupeople==people && denglumima==mima)
System.out.println("帐号登录成功"+"\n");
else
System.out.println("密码输入错误,登录失败"+"\n");
}
}
4. 使用Java语言进行面向对象设计:ATM柜员机模拟程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AtmSystem extends JFrame implements ActionListener
{
private JPanel buttonPanel=new JPanel();
private JPanel area1Panel=new JPanel();
private JPanel area2Panel=new JPanel();
private JTextField note=new JTextField(33);
private JButton buttons[];
private JButton dot = new JButton(".");
private JButton doublezero = new JButton("00");
private JButton ok = new JButton("确定");
private JButton exit = new JButton("退出");
private JButton cancel = new JButton("取消");
private JButton change=new JButton("更正");
private JButton getmoney = new JButton("取款");
private JButton search = new JButton("查询余额");
private JButton changekey = new JButton("修改密码");
private JButton putmoney= new JButton("存款");;
private boolean flag0, flag1, flag2,flag3,flag4;
int status=0;
int k,s,k1;
String str1="\t欢迎使用ATM柜员机!";
String str2="\t请输入用户名:";
String str3="\t请输入密码:";
String str4="\t请输入取款金额:";
String str5="\t请输入存款金额:";
String str6="\t请输入新密码:";
String str7="\t您的余额为:";
String str8="\t用户名错误,请重新输入:";
String str9="\t密码错误,请重新输入:";
String str10="\t余额不足,请重新输入:";
String str11="\t密码修改成功!";
String store1="",store2="";
double moneysum=10000;
double getMoney;
String username="123456";
String password="123456";
public AtmSystem()
{
super("ATM柜员机");
Container container=getContentPane();
buttons=new JButton[10];
dot.addActionListener(this);
doublezero.addActionListener(this);
change.addActionListener(this);
cancel.addActionListener(this);
ok.addActionListener(this);
exit.addActionListener(this);
changekey.addActionListener(this);
putmoney.addActionListener(this);
getmoney.addActionListener(this);
search.addActionListener(this);
area2Panel.add(changekey);
area2Panel.add(putmoney);
area2Panel.add(getmoney);
area2Panel.add(search);
area2Panel.setLayout(new GridLayout(1,3));
container.add(area2Panel,BorderLayout.NORTH);
note.setText(str1);
note.setEditable(false);
area1Panel.add(note);
buttonPanel.setLayout(new GridLayout(4,4));
for(int count=0;count<buttons.length;count++)
{
buttons[count]=new JButton(""+count);
buttons[count].addActionListener(this);
buttonPanel.add(buttons[count]);
}
buttonPanel.add(doublezero);
buttonPanel.add(dot);
buttonPanel.add(change);
buttonPanel.add(cancel);
buttonPanel.add(ok);
buttonPanel.add(exit);
container.add(buttonPanel,BorderLayout.SOUTH);
container.add(area1Panel);
area1Panel.setVisible(true);
area2Panel.setVisible(false);
setSize(400,250);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JButton operation = (JButton) event.getSource();
if(operation==ok)
{
if(status==0)
{
note.setText(""+str2);
status=1;
}
else if(flag4==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag4=false;
}
else if(status==1&&k==1)
{
note.setText(""+str3);
status=2;
k=0;
}
else if(status==1&&k==0)
{
note.setText(""+str8);
store1="";
}
else if(status==2&&k1==1)
{
k1=0;
area1Panel.setVisible(false);
area2Panel.setVisible(true);
status=3;
}
else if(status==2&&k1==0)
{
note.setText(""+str9);
store2="";
}
else if(status==3)
{
if(flag0==true)
{
getMoney=Double.valueOf(store1).doubleValue();
if(getMoney<moneysum)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag0=false;
moneysum=moneysum-getMoney;
}
else
{
area2Panel.setVisible(false);
area1Panel.setVisible(true);
note.setText(""+str10);
store1="";
}
}
else if(flag1==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag1=false;
}
else if(flag2==true)
flag2=false;
else if(flag3=true)
{
flag4=true;
note.setText(""+str11);
password=store2;
flag3=false;
}
}
}
else if(operation==cancel)
{
if(status==1)
{
note.setText(""+str1);
status=0;
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str2);
status=1;
store1="";
store2="";
}
else if(status==3&&flag2)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag2=false;
store1="";
store2="";
}
else if(status==3&&flag0)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag0=false;
store1="";
store2="";
}
else if(status==3&&flag3)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag3=false;
store1="";
store2="";
}
}
else if(operation==change)
{
if(status==1)
{
note.setText(""+str2);
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str3);
store1="";
store2="";
}
else if(status==3&&flag2)
{
note.setText(""+str5);
store1="";
store2="";
}
else if(status==3&&flag0)
{
note.setText(""+str4);
store1="";
store2="";
}
else if(status==3&&flag3)
{
note.setText(""+str6);
store1="";
store2="";
}
}
else if(operation==doublezero&&status==3&&flag0==true)
{
note.setText(note.getText()+"00");
}
else if(operation==dot&&status==3&&flag0==true)
{
note.setText(note.getText()+".");
}
else if(operation==getmoney)
{
note.setText(""+str4);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store1="";
flag0=true;
}
else if(operation==search)
{
note.setText(""+str7+moneysum);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag1=true;
}
else if(operation==putmoney)
{
note.setText(""+str5);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag2=true;
}
else if(operation==changekey)
{
note.setText(""+str6);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store2="";
flag3=true;
}
else if(operation==exit)
{
note.setText(""+str1);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
status=0;
flag0=flag1=flag2=flag3=flag4=false;
store1="";
store2="";
k=k1=s=0;
}
else if(status==1||(status==3&&flag2==true)||(status==3&&flag0==true))
{
for(int count=0;count<buttons.length;count++)
if (operation== buttons[count])
{
note.setText(note.getText() + count);
store1+=event.getActionCommand();
}
user(store1);
}
else if(status==2||(status==3&&flag3==true))
{
for(int count=0;count<buttons.length;count++)
if (operation== buttons[count])
{
note.setText(note.getText()+ "*");
store2+=event.getActionCommand();
}
key(store2);
}
}
public void user(String load)
{
if(username.equals(load))
{
k=1;
s=1;
}
}
public void key(String load)
{
if(password.equals(store2))
{
k1=1;
}
}
public static void main(String args[])
{
AtmSystem application=new AtmSystem();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
5. java atm机代码
package arraylist;
import java.util.Scanner;
public class AtmDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Boolean flag = true;
int times = 0;
while(flag){
times++;
if(times == 4){
System.out.println("密码错误,请取卡");
break;
}
System.out.println("请输入你的密码");
String password = sc.next();
if(password.equals("111111")){
Boolean moneyflag = true;
while(moneyflag){
System.out.println("请输入金额");
int number = sc.nextInt();
if(number >= 0 && number <= 1000 && number % 100 == 0){
System.out.println("用户取了" + number + "元。交易完成");
moneyflag = false;
}else{
System.out.println("请重新输入金额");
}
}
break;
}else{
continue;
}
}
}
}
6. 用java做一个简易的ATM机具体流程在下面
代码如下
packageBaiD;
importjava.util.Scanner;
publicclassATM{
/*1提示请输入密码然后直接进入下一步。密码6位限制(限制方法用“最小大于100000最大小于999999”这样限制)
2.提示密码正确还是错误密码直接弄成“123456”错误返回上一步循环方法用for循环。
3.密码输入正确后进入下一步提示5个选项(1.余额查询“基础10000”2.取款3存款4.退出)
4.进行取款或者存款之后要回到第三步重新选择(余额和取款存款相关联)
备注:用键盘输入的方法用scanner*/
privatestaticintmoney=10000;//全局变量余额默认10000
publicstaticvoidmain(Stringargs[])
{
for(;;){//for循环,有意思吗?
System.out.println("请输入密码:");
Scannerinput=newScanner(System.in);
intpw=input.nextInt();
if(Checkpw(pw)){
System.out.println("密码正确。");
Next();
}
elseSystem.out.println("密码错误!");
}
}
publicstaticbooleanCheckpw(intpw)
{
if(pw==123456)returntrue;//固定密码就不需要限制位数了,反正不符合就错
else
returnfalse;
}
publicstaticvoidNext(){
do{
System.out.println("请选择你需要的功能:");
System.out.println("1.余额查询2.取款3.存款4.退出");
intvalue=newScanner(System.in).nextInt();
switch(value){
case1://查询余额
System.out.println("您的余额为"+money+"元");
break;
case2://取款
System.out.println("请输入取款金额:");
intgetnum=newScanner(System.in).nextInt();
if(getnum<0)System.out.println("输入金额有误!");
elseif(getnum>money)System.out.println("余额不足.");
else{money=money-getnum;System.out.println("取款成功,余额为"+money);}
break;
case3://存款
System.out.println("请输入存款金额:");
intpushnum=newScanner(System.in).nextInt();
if(pushnum<0)System.out.println("输入金额有误!");
else{money=money+pushnum;System.out.println("存款成功,余额为"+money);}
break;
case4://退出
System.out.println("谢谢使用!");
System.exit(0);
break;
default:
System.out.println("输入有误");
break;
}
}while(true);
}
}
2、运行效果