




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象技术大作业 1. 需求分析1.1. 问题描述动售票系统在生活中非常常见,是一个典型的有限状态机模型,与之相似的还有银行自动柜员机、影院自助售票机、学校餐厅自助圈存机等等。用JAVA语言设计 “地铁自动售票”仿真程序,是对JAVA程序设计的一次综合实践。设计的“地铁自动售票”仿真系统要具备自助购票机的基本功能,模拟实现现实生活中中自助购票的几个步骤,和正常处理现实中可能遇到的所有情况。1.2. 对问题的理解要编写“地铁自动售票”仿真系统,首先需要要了解有限状态机的相关知识。要让“地铁自动售票”仿真系统正常运作,可以把一次购票过程看作系统的一个运行周期,再将这一个周期分为有限个数的状态,根据外界的操作在这些状态之间作一定规律的状态转移。所以,程序的关键是如何模拟售票系统在用户操作时状态的切换。2. 程序设计2.1. 概要设计这次的程序设计中运用JAVA GUI设计,通过图形界面来实现人机交互。程序主要分为了三个部分,第一部分是用以人机交互的的图形界面;第二部分是按照实际操作步骤分成的六大状态,包括“待机”状态,“选择车站”状态,“支付”状态,“找零”状态,“打印车票”状态,以及响应其他操作的“提示”状态;第三部分是实现各种状态下的转移。在状态切换方面,利用JAVA GUI设计,建立一个空白窗口框架(Frame)和对应系统六个状态的面板(Panel),在不同的状态设置窗口框架显示对应该状态的的面板,再对该面板中的按钮和文本框进行监听,以获取输入的数据和响应用户操作对应的状态切换。这样,虽然用户看到的只是其中一个面板,实际在各个状态都有一个对应的面板,这样极大方便了对各种状态下的时间响应。实际设计程序的时候要充分考虑各程序之间的数据共用,因此建立一个机器类,在类中新建具体的窗口对象,并定义记录用户支付、选择情况的变量,最后还需构造其他供类使用和修改本类中对象属性及数据的方法。控制状态转移由控制台(Controlor)类定义,其中构造设置面板显示的方法,及获取和修改机器类中对象属性和变量的方法。2.2. 详细设计2.2.1状态转移图“地铁自动售票”仿真系统的基本状态包括“待机”状态、“选择车站”、状态、“支付”状态、“找零”状态、“打印车票”状态、“提示”状态,依据用户的操作进行状态之间的转移,其状态转移图如图 2.2-1 所示。图 2.2-12.2.2设计思路程序的设计主要思路如下:1) 建立一个窗口框架类,用以新建放置面板的框架对象2) 建立一个面板(Panel)类windoes作为六个状态对应面板的父类,在父类里抽象定义各个成员(按钮,选择框)的监听事件响应方法;3) 建立继承windows类的六个界面类:主页界面类,选择车站界面类,支付界面类,找零界面类,车票打印界面类,提示界面类,并给出每个界面的初始化方法,和界面中按钮及车站选择界面中的选择框的事件响应方法;4) 建立机器类,在类里面新建六个界面类的具体对象,再建立方便其他类使用,修改各个面板的方法,最后定义记录支付金额、车站编号、车站名称等数据的变量,以及访问这些变量的方法。5) 建立一个控制台类,类里面包含设置窗口框架放置哪个面板的方法、获取机器类中数据的方法。6) 建立自动售票机类,类里面包含主函数,主函数新建了窗口框架类、机器类、控制台类等对象。 2.2.3程序分析(类、方法、成员属性说明)panel类:各个界面类的父类方法:panel();界面初始化方法mainAction(),sureAction(),cancelAction();按钮监听事件响应抽象方法StandbyState类,ChoiceState类,ChargeState类,ChangeState类PrintState类,Caution类: 界面类方法:mainAction(),sureAction(),cancelAction();按钮监听事件响应具体方法controlor类; 控制状态转移的控制台方法:setstandbystate(),setchoicestate(),setchargestate(),setchangestate()setprintstate(),setcaution(String s1,String s2,String s3);设置及面方法reset();重置方法charge();计算车费的方法machine类方法:getstandbystate(); getchargestate();getchangestate(); getcaution()getchoicestate(); getprintstate(); 供其他类获取界面变量的方法printtick(String s1,String s2,boolean b): 打印车票的方法其他供外界设置本类变量的方法 ticket类; 车票”小窗口”类ticket_Vendor类; 售票机类3. 代码清单1.indows类package windows;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;public class windows extends JFrame private static final long serialVersionUID = 1L;private JPanel contentPane;JPanel st;public windows() setTitle(Xian Metro 西安地铁);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(380, 120, 600, 430);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);setContentPane(contentPane);contentPane.setLayout(null);setVisible(true);public void setstate(JPanel jp)st = jp; contentPane.add(jp);public JPanel getstate()return st;2 panel类package windows;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JTextField;import javax.swing.JLabel;import java.awt.Color;import java.awt.TextArea;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.UIManager;import javax.swing.SwingConstants;import java.awt.Font;import javax.swing.DefaultComboBoxModel; public abstract class panel extends JPanel private static final long serialVersionUID = 1L; public JTextField chargeField; public JTextField changeField; protected JButton button_mian; protected JButton button_sure; protected JButton buttun_cancel; public TextArea Screen; public JComboBox comboBox;public panel() setForeground(Color.BLACK); setLayout(null);/主屏幕 Screen = new TextArea(,10,5,TextArea.SCROLLBARS_NONE); Screen.setBounds(42, 45, 378, 265); Screen.setEditable(false); Screen.setFont(new Font(新宋体, Font.ITALIC, 25);Screen.setBackground(new Color(0, 255, 255);add(Screen);/主页键JButton button_mian = new JButton(u4E3Bu9875);button_mian.setForeground(new Color(0, 51, 255);button_mian.setBounds(454, 45, 93, 36);button_mian.setFont(new Font(楷体, Font.BOLD, 20);button_mian.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) mainAction(););button_mian.setBackground(UIManager.getColor(PopupMenu.background);add(button_mian);/确定键JButton button_sure = new JButton(u786Eu5B9A);button_sure.setForeground(Color.RED);button_sure.setBounds(454, 133, 93, 36);button_sure.setFont(new Font(楷体, Font.BOLD, 20);button_sure.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) sureAction(););add(button_sure);/取消键JButton buttun_cancel = new JButton(u53D6u6D88);buttun_cancel.setForeground(new Color(102, 204, 0);buttun_cancel.setBounds(454, 213, 93, 36);buttun_cancel.setFont(new Font(楷体, Font.BOLD, 20);buttun_cancel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) cancelAction(););add(buttun_cancel);/支付窗口chargeField = new JTextField(0);chargeField.setBounds(105, 342, 66, 21);chargeField.setBackground(new Color(255, 250, 240);chargeField.setHorizontalAlignment(SwingConstants.CENTER);add(chargeField);chargeField.setColumns(10);/找零窗口changeField = new JTextField(0);changeField.setBounds(318, 342, 66, 21);changeField.setHorizontalAlignment(SwingConstants.CENTER);changeField.setBackground(new Color(255, 250, 240);add(changeField);changeField.setColumns(10);JLabel label_charge = new JLabel(u6295u5E01);label_charge.setForeground(new Color(0, 0, 0);label_charge.setBounds(59, 345, 36, 15);add(label_charge);JLabel label_change = new JLabel(u627Eu96F6);label_change.setBounds(272, 343, 36, 18);add(label_change);/车站选择窗口comboBox = new JComboBox();comboBox.setFont(new Font(幼圆, Font.BOLD, 13);comboBox.setModel(new DefaultComboBoxModel(new String u8F66u7AD9u9009u62E9);comboBox.setMaximumRowCount(5);comboBox.setBounds(454, 283, 93, 21);add(comboBox); /主页键 响应方法 在子类中具体定义abstract public void mainAction();/确定键 响应方法abstract public void sureAction();/取消键 响应方法abstract public void cancelAction();3 StandbyState类package state;import ticket_Vendor.controlor;import windows.panel;public class StandbyState extends panelprivate static final long serialVersionUID = 1L;public StandbyState()this.setBounds(0, 0, 580, 380);this.changeField.setText(0);this.chargeField.setText(0); this.Screen.setText(nnn 你好,nn 欢迎使用西安地铁自动售票机nn 按确定键继续);/主页键 响应方法 在子类中具体定义public void mainAction()/确定键 响应方法 public void sureAction()controlor.setchoicestate();/取消键 响应方法public void cancelAction()4 ChoiceState类package state;import javax.swing.DefaultComboBoxModel;import ticket_Vendor.controlor;import windows.panel;public class ChoiceState extends panelprivate static final long serialVersionUID = 1L; public ChoiceState()this.setBounds(0, 0, 580, 380);this.changeField.setText(0); boBox.setModel(new DefaultComboBoxModel(new String u5317u82D1, u8FD0u52A8u516Cu56ED, u884Cu653Fu4E2Du5FC3, u51E4u57CEu4E94u8DEF, u5E02u56FEu4E66u9986, u5927u660Eu5BABu897F, u9F99u9996u539F, u5B89u8FDCu95E8, u5317u5927u8857, u949Fu697C, u6C38u5B81u95E8, u5357u7A0Du95E8, u4F53u80B2u573A, u5C0Fu5BE8, u7EACu4E00u8857, u4F1Au5C55u4E2Du5FC3);this.Screen.setText(nnnnn 请选择你要去往的车站);/主页键 响应方法 public void mainAction()controlor.setstandbystate();/确定键 响应方法 public void sureAction() controlor.setchargestate(); /取消键 响应方法public void cancelAction() controlor.setstandbystate();5 ChargeState类package state;import ticket_Vendor.controlor;import windows.panel;public class ChargeState extends panelprivate static final long serialVersionUID = 1L;public ChargeState()this.setBounds(0, 0, 580, 380);public void mainAction()controlor.setstandbystate();public void sureAction()controlor. setchangestate();public void cancelAction()controlor.setstandbystate();6 ChangeState类package state;import ticket_Vendor.controlor;import windows.panel;public class ChangeState extends panelprivate static final long serialVersionUID = 1L;public ChangeState()this.Screen.setText(nnnnn 找零页面测试);this.setBounds(0, 0, 580, 380);this.changeField.setText(0);this.changeField.setText(0);public void mainAction()public void sureAction()controlor.setprintstate();public void cancelAction()controlor.setcaution(nnn 您选择了取消 nn 已支付的+controlor.paid()+元已退回到找零口nn 按确定键继续购票,controlor.paid(),0);7 PrintState类package state;import ticket_Vendor.controlor;import ticket_Vendor.machine;import windows.panel;public class PrintState extends panel private static final long serialVersionUID = 1L;public PrintState()this.Screen.setText(nnnn 车票已打印nn 请按任意键取票);this.setBounds(0, 0, 580, 380); this.changeField.setText(PrintState);/主页键 响应方法 public void mainAction()machine.printtick(,false);controlor.setstandbystate();/确定键 响应方法public void sureAction()machine.printtick(,false);controlor.setstandbystate();/取消键 响应方法public void cancelAction()machine.printtick(,false); controlor.setstandbystate();8 Caution类package state;import ticket_Vendor.controlor;import windows.panel;public class Caution extends panelprivate static final long serialVersionUID = 1L;public Caution()this.setBounds(0, 0, 580, 380);this.chargeField.setText(0);/主页键 响应方法 public void mainAction() controlor.setstandbystate();/确定键 响应方法 public void sureAction() controlor.setstandbystate(); /取消键 响应方法public void cancelAction() controlor.setstandbystate();9 machine类package ticket_Vendor;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import state.Caution;import state.ChangeState;import state.ChargeState;import state.ChoiceState;import state.PrintState;import state.StandbyState;import ticket.ticket;import windows.panel;import windows.windows;public class machine windows window = new windows();static StandbyState standbystate= new StandbyState();static ChoiceState choicestate= new ChoiceState();static ChangeState changestate= new ChangeState();static ChargeState chargestate=new ChargeState();static PrintState printstate= new PrintState();static Caution caution =new Caution();static ticket tic = new ticket();static int index = 0; /所选车站编号 static String station = null; /所选车站 public static void choicestateaddlistener() boBox.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) index = boBox.getSelectedIndex(); station = boBox.getSelectedItem().toString(); System.out.println(index);); /对外提供数据的方法public static panel getstandbystate()return standbystate;public static panel getchoicestate()return choicestate;public static panel getchargestate()return chargestate;public static panel getchangestate()return changestate;public static panel getprintstate()return printstate;public static panel getcaution()return caution;/改变窗口显示的方法public static void setchangefield(panel p,String s1,String s2)p.changeField.setText(s1);p.chargeField.setText(s2);public static String chargestate_chargefieldget()return chargestate.chargeField.getText(); public static int index() return index; public static String station()return station;public static void setindex(int a)index = a;public static void setstation(String s)station = s;public static void printtick(String s1,String s2,boolean b)tic.textField.setText(s1);tic.textField_1.setText(s2);tic.setVisible(b);10 controlor类package ticket_Vendor;import windows.panel;import windows.windows;public class controlor public static windows window = new windows();public static void control()window.setstate(standbystate(); / 设置为待机状态public static void setstandbystate()reset();window.remove(window.getstate();window.setstate(standbystate();/ 设置为选择车票状态public static void setchoicestate()window.remove(window.getstate();window.setstate(choicestate();/ 设置为支付状态public static void setchargestate()if(machine.station()!=null)chargestate().Screen.setText(nnn 您选择了: + machine.station()+ 站+nn 需要要支付 +charge()+ 元,nn 请从投币口支付。);window.remove(window.getstate();window.setstate(chargestate();else setcaution(nnn 抱歉!nn 您还没选择车站,nn 按任意键重新购票。,0,0);/设置为 找零状态public static void setchangestate()if(Integer.parseInt(paid()=0)setcaution(nnnn 您未进行支付,nn 请按任意键重新购票。,controlor.paid(),0);else if(Integer.parseInt(paid()-Integer.parseInt(charge()=0) changestate().Screen.setText(nn 您支付了 +paid()+ 元,nn 找您 +change()+ 元,+nn 按确定键打印车票,nn 按取消键取消购票。); machine.setchangefield(changestate(),change(),0); window.remove(window.getstate(); window.setstate(changestate(); else setcaution(nnn 抱歉!支付不足,nn 钱已退回。nn 按确任意键重新购票,controlor.paid(),0); /设置为 打印车票 状态public static void setprintstate()window.remove(window.getstate();machine.setchangefield(printstate(),0,0);window.setstate(printstate();machine.printtick( 北客站, machine.station()+站,true);/提示窗口public static void setcaution(String s1,String s2,String s3)window.remove(window.getstate();window.setstate(caution(s1,s2,s3);/复位的方法 public static void reset() machine.setchangefield(standbystate(),0,0); machine.setchangefield(choicestate(),0,0); machine.setchangefield(changestate(),0,0); machine.setchangefield(chargestate(),0,0); machine.setchangefield(printstate(),0,0); machine.setindex(0); machine.setstation(null); /获取数据的方法public static panel standbystate()return machine.getstandbystate();public static panel choicestate()machine.choicestateaddlistener();return machine.getchoicestate();public static panel chargestate()return machine.getchargestate();public static panel changestate()return machine.getchangestate();public static panel printstate()return machine.getprintstate();public static panel caution(String s1,String s2,String s3)machine.caution.Screen.setText(s1);machine.caution.changeField.setText(s2);machine.caution.chargeField.setTe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业家精神代际传递-洞察及研究
- 第八单元《词义的辨析和词语的使用》教案(表格式)统编版高中语文必修上册
- 2025企业租赁合同协议书模板
- 党史教育线上考试题库及答案
- 2025【合同范本】铁路运输合同范本
- 2025养殖场山地租赁合同
- 冲压安全生产培训资料课件
- 2025租赁合同模板示例
- 八月快递安全培训总结课件
- 2025化工卧式泵买卖合同书
- 颅脑外伤(共61张PPT)
- 项目部材料管理制度要点
- 消防安全检查记录表(完整详细版)1
- winmodv工厂可接受性测试、虚拟调试过程控制实时仿真
- 消费者行为学第01章导论
- 教学课件 金属学与热处理-崔忠圻
- 铁道概论全套课件
- 部编版二年级语文上册全册教案及反思
- 北师大版五年级数学上册全册教案含反思
- 西门子燃气轮机技术介绍开
- 阅兵英语课件
评论
0/150
提交评论