使用Java语言进行面向对象设计:ATM柜员机模拟程序

使用Java语言进行面向对象设计:ATM柜员机模拟程序,第1张

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=0count<buttons.lengthcount++)

{

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=0count<buttons.lengthcount++)

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=0count<buttons.lengthcount++)

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)

}

}

lass consumer

class ATM     // ATM取款机

{

public:

    ATM(consumer& cn):cnsm(cn){}

 void welcome()        // 登陆界面

 bool check_passwd(char n[],char pwd[]) // 核对密码

 void change_passwd()     // 修改密码

    void fetchmoney()        // 取款

 void information()            // 查询信息

 void exitATM()            // 退出系统

 void functionshow()      // 功能界面

 void lock()              // 锁机

private:

 int times               // 记录密码输入次数

 consumer& cnsm

}

class consumer  // 用户

{

public:

 friend class ATM

 consumer(char Name[],char Num[],float Money,char Password[])

protected:

 char* get_name()              // 取得姓名

 char* get_num()               // 取得卡号

 char* get_passwd()            // 取得密码

 float get_money()             // 取得余额

 void set_passwd(char pwd[])   // 设置密码

 void set_money(float m)       // 取钱

private:  

 char passwd[8]    // 用户密码

 char name[20]     // 用户姓名

 char num[20]     

 float money

}

 

// ************************************

// *                                  *

// *     consumer类的成员函数 吵衡        *

// *                                  *

// ************************************

#include"function.h"

#include

consumer::consumer(char Name[],char Num[],float Money,char Password[])

{

   strcpy(name,Name)

   strcpy(num,Num)

   money=Money

   strcpy(passwd,Password)

}

float consumer::get_money()

{

 return money

}

char* consumer::get_name()

{

 return name

}

char* consumer::get_num()

{

 return num

}

char* consumer::get_passwd()

{

 return passwd

}

void consumer::set_money(float m)

{

 money-=m

}

void consumer::set_passwd(char pwd[])

{

 strcpy(passwd,pwd)

}

 

// ************************************

// *                                  物碰肢*

// *          ATM类的成员函数         *

// *                                  *

// ************************************

#include "function.h"

#include

#include

void ATM::welcome()

{

 times=0

 cout<<"$  欢迎使用若雪银行ATM自动取款机!~! "<< P>

 char pwd[8],num[20],ch

 int i=0

 do

 {

   i=0

   cout<<"请输入卡号:"

    do

    {

          cin.get(ch)

       num[i++]=ch

    }while(ch!='\n')

        num[i-1]='\0'

   i=0

   cout<<"请输入密码:"

    do

    {

          cin.get(ch)

       pwd[i++]=ch

    }while(ch!='\n')

        pwd[i-1]='\0'

    if(!check_passwd(num,pwd))

    {

      cout<<"你输入的卡号或密码有误,请重新输入"<

      times++

    }

    else

  罩世  {

      functionshow()

    }

 }while(times<3)

    lock()

}

bool ATM::check_passwd(char num[],char pwd[])

{

 if(strcmp(num,cnsm.get_num())==0&&strcmp(pwd,cnsm.get_passwd())==0)

  return true

 else

  return false

}

void ATM::functionshow()

{

   int n

   do

   {

     cout<<"请你输入相应的 *** 作序号进行 *** 作:"<<ENDL

     cout<<"1) 修改密码 "<      <<"2) 取款     "<      <<"3) 查询余额 "<         <<"4) 退出系统 "<

     cout<<"$ >\\"

     cin>>n

     while(n<1||n>4)

  {

      cout<<"请输入正确的 *** 作序号!"<

      cout<<"$ >\\"

      cin>>n

  }

 

     switch(n)

  {

      case 1:   change_passwd()   break

      case 2:   fetchmoney()      break

      case 3:   information()           break

      case 4:   exitATM()         break  

  }

   }while(true)

 

}

void ATM::change_passwd()

{

 char pwd[8],repwd[8]

 

 times=0

 do

 {

 cout<<"请输入旧密码:"

 cin>>pwd

    if(!check_passwd(cnsm.get_num(),pwd))

  times++

 else

  break

 }while(times<3)

 if(times==3)

  lock()

   

 int t=0

    do

 {

  cout<<"请输入新密码:"

  cin>>pwd

  cout<<"请再输入一次新密码:"

  cin>>repwd

  if((t=strcmp(pwd,repwd))!=0)

   cout<<"你输入的两次密码不一样,请从新输入!"<

 }while(t!=0)

 cnsm.set_passwd(pwd)

 cout<<"密码修改成功,请牢记!"<< P>

}

void ATM::fetchmoney()

{

   float m

   char ch

   do

   {

      cout<<"你要取多少钱:"<<"\N$>\\"<

      cin>>m

      while(m<=0)

   {

    cout<<"请输入正确的数字!"<

    cout<<"$ >\\ "

    cin>>m

   }

      if(cnsm.get_money()-m<0)

   {

     cout<<"对不起,你的余额不足!"<

   }

   else

   {

   cout<<" *** 作成功,请收好钱!"<<ENDL

         cnsm.set_money(m)

   }

   cout<<"是否要继续该项 *** 作:(Y/N) "<

   cout<<"$ >\\ "

   cin>>ch

   while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')

  {

     cout<<"$ >\\"

     cin>>ch

  }

   }while(ch=='y'||ch=='Y')

}

void ATM::information()

{

 cout<<"**********************************"<

 cout<<"*"<

 cout<<"*     用户姓名:"<<<ENDL

 cout<<"*     卡号:    "<<<ENDL

 cout<<"*     余额:     "<<<ENDL

 cout<<"**********************************"<< P>

}

void ATM::lock()

{

 cout<<"对不起,由于你的 *** 作有误,你的卡已经被没收! ?<<endl

 cout<<"请取卡……"<

 exit(0)

}

 

// ************************************

// *                                  *

// *          ATM.cpp                 *

// *                                  *

// ************************************

#include

#include"function.h"

void main()

{

  consumer c1("jim","12345",5200.3f,"123") // 先构造一个用户

  ATM atm(c1)

  atm.welcome()

}

#include

"stdio.h"对ATM机器的模拟就是一个对队列的模拟下面代码在VC6环境下调郑野试雹隐已经通过了其中有个缺陷就是因为代码执行速度过快导致二次执行根据时间随机出来的数字都是一样的因此你可以自己加上一个延迟子程序部分功能已经注释了#include

"stdlib.h"#include

"time.h"#define

OK

1#define

ERROR

0

typedef

struct

node{

int

number

struct

node*

next}*Lnode

typedef

struct

list{

node

*head,*rear}*Plist

//模拟

ATM开业bool

ListInit(Plist

list){

Lnode

p

p

=

(Lnode)malloc(sizeof(Lnode))

list->head

=

p

list->rear

=

list->head

list->head->next

=

NULL

if(list->head!=NULL)

return

ERROR

else

return

OK}

//模拟

有客户排队bool

ListInsert(Plist

list,int

number){

Lnode

p

p

=

(Lnode)malloc(sizeof(Lnode))

if(p==NULL)

return

ERROR

else

{

p->number

=

number

p->next

=

NULL

list->rear->next

=

p

list->rear

=

p

return

OK

}}

//模拟

客户办完事离开bool

ListDelete(Plist

list){

Lnode

p

if(list->head

==list->rear)

return

ERROR

else

{

p

=

list->head->next

list->head->next

=

p->next

list->rear

=

list->head

//

free(p)

return

OK

}}

void

sand(int*

gettime,int*

needtime){

srand(time(NULL))

*gettime

=

rand()%100

srand(time(NULL))

*needtime

=rand()%100}

//模拟客户到达事件void

CustomerArrived(Plist

list,int

gettime,int

needtime,int

kehu,int

time){

int

nextgettime,nextneedtime

sand(&nextgettime,&nextneedtime)

while(needtime>0

&&

nextgettime>0

&&

time>0)

{

needtime

--

nextgettime

--

time

--

}

if(nextgettime

==

0

&&

needtime>0

&&time>0)

{

kehu++

ListInsert(list,kehu)

while(needtime>0

&&

time>0)

{

needtime--

time

--

}

ListDelete(list)

CustomerArrived(list,nextgettime,nextneedtime,kehu,time)

}

if(needtime

==0

&&

nextgettime>0

&&

time>0)

{

ListDelete(list)

while(nextgettime>0

&&

time>0)

{

nextgettime--

time

--

}

kehu++

ListInsert(list,kehu)

//未删除

,list未传递进去

CustomerArrived(list,nextgettime,nextneedtime,kehu,time)

}

if(time

==0)

{

printf("ATM关门,请明天在来!\n")

return

}}

main(){

list

list

int

i

=

10000

/源丛厅/ATM机器每天工作时间

int

kehu

=

0

//客户标号

int

gettime,needtime

ListInit(&list)

//ATM开业

sand(&gettime,&needtime)

ListInsert(&list,kehu)

CustomerArrived(&list,gettime,needtime,kehu,i)

getchar()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12558608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存