课程名称:《软件体系结构与设计模式》
实验项目名称:基于MVC模式下的二手车拍卖平台设计

实验内容 按下列要求编写程序并上机调试运行:

  1. 拍卖系统要又三个显示界面,分别是车体图片,文字描述和出价区。
  2. 将系统分解成MVC三个类进行方法设计。
  3. M和V必须观察者模式进行设计。
  4. 使用SWING进行界面设计。
  5. 按钮点击,必须更新车型和图片,并用时间驱动结构进行设计。
  6. 额外要求:要求报价必须只能提高,不等减少,如减少弹出警告窗口。

代码15元1份,有需要联系+qq2421442475

以下写类图和代码

类图:

分析与代码展示:

其中,CarGUI提供用户输入界面,代表用户;CarModel为MVC体系结构中的模型部分(Model);BitView和SearchView代表视图部分(View);而Controller为控制器部分。
用户通过使用CarGUI图形界面在的列表选择待拍卖的车,单击“Search”按钮,获取车的图片和文字说明;然后输入竞拍价,单击“Bit”按钮,竞拍价将显示在拍卖价格显示区域上;若下次的拍卖价格比这次的价格低,则会弹出警告窗口消息框并会在拍卖价格显示区域上显示一条信息:“Bit price for”+CarName+“:”+“illegal bit price”。
程序运行结果图如下图所示。

图1

图2

图3

图4

Observer类

public interface Observer {public void update(Observable subject);
}

Observable类

public interface Observable {public void register(Observer obs);public void notifyObservers();
}

在该设计类图中设计了两个接口类:Observable与Observer,Model类实现Observable接口,而View类实现Observer接口。也就是说Model对象是被观察者而View对象是观察者。根据观察者机制,被观察者类应该包含registerObserver( )、notifyObservers( )方法,其功能是在被观察者对象中利用registerObserver( )加入被观察者对象,并且当被观察者的数据或者状态有了改变的时候,利用notifyObservers( )方法通知观察者。此时,观察者类中的update( )方法将自动运行,达到及时更新观察者的目的。

CarModel类

import javax.swing.*;
import java.util.ArrayList;public class CarModel implements Observable{private ArrayList<Observer> observers;private String carName;private String bitPrice;private ImageIcon icon;String [] carSelected = {"Honda Accord"," "," ","Made in 2005, used by the Department of defense.","Mileage: 60,000 miles.","5-speed automatic (25 city/36 highway) transmission?"," "," "," "," "};String [] car2Selected = {"Honda Civic"," "," ","Made in 2006,used by the New York State government ","Mileage 40,4000 miles.","5-speed manual (25 city/36 highway) transmission?","140-hp, 1.8-Liter, 16-Valve SOHC i-VTEC? 4-cylinder engine"," "};String [] car3Selected = {"Toyota Camry"," "," ","Made in 2003, used by the Washington State government","Mileage: 100,000 miles.","5-speed automatic (25 city/36 highway) transmission?","140-hp, 1.8-Liter, 16-Valve SOHC i-VTEC? 4-cylinder engine"," "};String [] car4Selected = {"Toyota Corolla"," "," ","Made in 2002, used by the Washington State government","Mileage: 120,000 miles.","5-speed automatic (25 city/36 highway) transmission?", "140-hp, 1.8-Liter, 16-Valve SOHC i-VTEC? 4-cylinder engine"," "};public CarModel() {observers = new ArrayList<Observer>();}public String getCarName() {return carName;}public String getBitPrice() {return bitPrice;}public void setCarName(String carName) {this.carName = carName;notifyObservers();}public void setBitPrice(String bitPrice) {this.bitPrice = bitPrice;notifyObservers();}public ImageIcon getImage() {return this.icon;}public void setImage(String file) {this.icon = new ImageIcon(file);notifyObservers();}@Overridepublic void register(Observer obs) {observers.add(obs);}@Overridepublic void notifyObservers() {for (int i = 0; i < observers.size(); i++) {observers.get(i).update(this);}}
}

BitView类

import javax.swing.*;public class BitView implements Observer{private final CarModel cm;private final CarGUI cg;public BitView(CarModel cm,CarGUI cg) {this.cm = cm;this.cg=cg;}@Overridepublic void update(Observable subject) {if ( subject == cm){String name= cm.getCarName();String p = cm.getBitPrice();String text=" Bit price for "+ name + " : "+ p;ImageIcon imIcon = cm.getImage();cg.setIcon(imIcon);//更新拍卖车图片区域cg.showPrice(text);//更新拍卖价格显示区域}}}

SearchView类

import javax.swing.*;public class SearchView implements Observer {private final CarModel cm;private final CarGUI cg;public SearchView(CarModel cm,CarGUI cg) {this.cm = cm;this.cg=cg;}@Overridepublic void update(Observable subject) {if ( subject == cm){ImageIcon imIcon = cm.getImage();cg.setIcon(imIcon);}}
}

其中,CarGUI提供了用户输入界面,代表用户。注意,在类CarGUI中,创建与初始化了CarModel、SearchView和BitView对象,并且调用CarModel中的register(Observer obs),分别将SearchView和BitView的对象注册到该CarModel对象。
①箱子布局管理,主要是Swing包中的Box来进行盒子布局,包括两种箱子:一种是水平箱子、一种是垂直箱子。构造方法如下:
Box box=Box.createHorizontalBox()//创建水平箱子对象
Box box=Box.createVertiaclBox()//创建垂直箱子对象
箱子创建好后,用add()把组件添加到箱子里面
②设置控件间隔的方法
Box.createHorizontalStrut(int width)//设置左右两个组件的宽度
Box.createVerticalStrut(int height)//设置上下两个组件的距离

CarGUI类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class CarGUI {private static CarModel carModel;private static BitView bitView;private static SearchView searchView;private JComboBox carSelector;String [] carNameList = {"Honda Accord-2005","Honda Civic-2006","Toyota Camry-2003","Toyota Corolla-2002"};String [] carSelected = {"Honda Accord"," "," ","Made in 2005, used by the Department of defense.","Mileage: 60,000 miles.","5-speed automatic (25 city/36 highway) transmission?"," "};JFrame frame = new JFrame("MVC pattern 二手车拍卖");JTextArea bitShownText = new JTextArea("Bit price for each car will be shown here",4,20);JTextField bitInputText = new JTextField("Offer your bit price",10);JLabel imgLabel = new JLabel();JList textList = new JList<>(carSelected);Icon icon=new ImageIcon("images\\Honda Accord-2005.jpg");//组装视图的函数public void init(){//注册被观察者与观察者carModel = new CarModel();bitView= new BitView(carModel,this);searchView = new SearchView(carModel, this);carModel.register(bitView);carModel.register(searchView);//窗口大小frame.setBounds(200,200,500,300);//组装顶部相关元素Box pBox = Box.createVerticalBox();//组装车的类型和下拉框Box uBox = Box.createHorizontalBox();JLabel  carlbl = new JLabel("Cars:");carSelector = new JComboBox<>(carNameList);carSelector.setMaximumRowCount(4);uBox.add(Box.createHorizontalStrut(50));uBox.add(carlbl);uBox.add(Box.createHorizontalStrut(10));uBox.add(carSelector);uBox.add(Box.createHorizontalStrut(10));//组装搜索和退出按钮Box btn2Box = Box.createHorizontalBox();JButton searchBtn = new JButton("Search");JButton exitBtn = new JButton("Exit");btn2Box.add(Box.createHorizontalStrut(5));btn2Box.add(searchBtn);btn2Box.add(Box.createHorizontalStrut(5));btn2Box.add(exitBtn);btn2Box.add(Box.createHorizontalStrut(100));//组装Bit和文本框Box btn1Box = Box.createHorizontalBox();JButton bitButton = new JButton("Bit");bitInputText.setBackground(Color.green);btn1Box.add(Box.createHorizontalStrut(5));btn1Box.add(bitButton);btn1Box.add(Box.createHorizontalStrut(20));btn1Box.add(bitInputText);btn1Box.add(Box.createHorizontalStrut(30));//组装左上部分pBox.add(Box.createVerticalStrut(10));pBox.add(uBox);pBox.add(Box.createVerticalStrut(20));pBox.add(btn2Box);pBox.add(Box.createVerticalStrut(20));pBox.add(btn1Box);pBox.add(Box.createVerticalStrut(20));//组装右上文本域JScrollPane bitInfo = new JScrollPane(bitShownText);//拍卖价格显示区域//组装顶部Box topBox = Box.createHorizontalBox();topBox.add(pBox);topBox.add(bitInfo);//组装左下imgLabel.setIcon(icon);JScrollPane imagePane = new JScrollPane(imgLabel);//组装右下JScrollPane bigSplitPane = new JScrollPane(textList);//组装底部相关元素Box baseBox= Box.createHorizontalBox();baseBox.add(imagePane);baseBox.add(bigSplitPane);//组装整体Box holeBOX = Box.createVerticalBox();holeBOX.add(topBox);holeBOX.add(baseBox);//添加监听器Controller buttonTeam = new Controller(this,carModel);searchBtn.addActionListener(buttonTeam);exitBtn.addActionListener(buttonTeam);bitButton.addActionListener(buttonTeam);frame.add(holeBOX);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();//调整此窗口的大小,以适合其子组件的首选大小和布局frame.setVisible(true);}public String getBitPrice(){return bitInputText.getText();}public String getCarName(){return carSelector.getSelectedItem().toString();// getSelectedItem():获得所选项的内容。}public void showPrice(String text){bitShownText.setText(text);}public void setIcon(ImageIcon icon) {imgLabel.setIcon(icon);imgLabel.validate();}public void setData(String[] name) {textList.setListData(name);//更新文字信息}
}

类CarModel封装了所有关于二手车拍卖的核心数据和提供给类Contoller和类SerachView、BitView调用的方法,该类保持了与其他三个类的高度的独立性。这里Conroller类实现了ActionListener接口。 在负责用户输人的类CarGUI中,所有的按钮Search 与Bit 都添加了Controller的对象。这样,当一个按钮对象被按下时所产生的事件触发了Controller对象的actionPerformed方法。在该方法中,将根据用户输入更新CarModel对象,然后在Controller类中,调用CarModel中的notifyObservers方法(此处为了节省代码量,在CarModel做了其他的处理,其效果是一样的)。而在notifyObservers方法中,则直接调用了SerachView和BitView中的update方法,实现了自动对被拍卖车的图片、文字信息以及拍卖价格的显示。

Controller类

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Controller implements ActionListener{private final CarGUI cg;private final CarModel cm;private int preprice = 0;private int preprice1 = 0;private int preprice2 = 0;private int preprice3 = 0;public Controller(CarGUI cg, CarModel cm) {this.cg = cg;this.cm = cm;}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("Search")) {String text3 = cg.getCarName();String text=" Bit price for each car will be shown here";switch (text3) {case "Honda Accord-2005":cm.setImage("images\\Honda Accord-2005.jpg");cg.setData(cm.carSelected);cg.showPrice(text);break;case "Honda Civic-2006":cm.setImage("images\\Honda Civic-2006.jpg");cg.setData(cm.car2Selected);cg.showPrice(text);break;case "Toyota Camry-2003":cm.setImage("images\\Toyota Camry-2003.jpg");cg.setData(cm.car3Selected);cg.showPrice(text);break;case "Toyota Corolla-2002":cm.setImage("images\\Toyota Corolla-2002.jpg");cg.setData(cm.car4Selected);cg.showPrice(text);break;}}if (e.getActionCommand().equals("Bit")) {String text = cg.getBitPrice();String text2 = cg.getCarName();String text4=" Bit price for "+ text2 + " : "+ " illegal bit price ";if (text2.equals("Honda Accord-2005")){if(Integer.parseInt(text) > preprice){preprice = Integer.parseInt(text);cm.setImage("images\\Honda Accord-2005.jpg");cg.setData(cm.carSelected);cm.setCarName(text2);cm.setBitPrice(text);}else {cg.showPrice(text4);JOptionPane.showMessageDialog(null,"竞拍价格比上次低!","提示",JOptionPane.WARNING_MESSAGE);}}if (text2.equals("Honda Civic-2006")){if(Integer.parseInt(text) > preprice1){preprice1 = Integer.parseInt(text);cm.setImage("images\\Honda Civic-2006.jpg");cg.setData(cm.car2Selected);cm.setCarName(text2);cm.setBitPrice(text);}else {cg.showPrice(text4);JOptionPane.showMessageDialog(null,"竞拍价格比上次低!","提示",JOptionPane.WARNING_MESSAGE);}}if (text2.equals("Toyota Camry-2003")){if(Integer.parseInt(text) > preprice2){preprice2 = Integer.parseInt(text);cm.setImage("images\\Toyota Camry-2003.jpg");cg.setData(cm.car3Selected);cm.setCarName(text2);cm.setBitPrice(text);}else {cg.showPrice(text4);JOptionPane.showMessageDialog(null,"竞拍价格比上次低!","提示",JOptionPane.WARNING_MESSAGE);}}if (text2.equals("Toyota Corolla-2002")){if(Integer.parseInt(text) > preprice3){preprice3 = Integer.parseInt(text);cm.setImage("images\\Toyota Corolla-2002.jpg");cg.setData(cm.car4Selected);cm.setCarName(text2);cm.setBitPrice(text);}else {cg.showPrice(text4);JOptionPane.showMessageDialog(null,"竞拍价格比上次低!","提示",JOptionPane.WARNING_MESSAGE);}}}if (e.getActionCommand().equals("Exit")) {System.exit(0);}}
}

基于MVC模式下的二手车拍卖平台设计相关推荐

  1. r.java美发更改_JAVA基于MVC模式下的理发管理系统

    每天记录学习,每天会有好心情.*^_^* 今天将为大家分析一个理发管理系统(该系统基于JAVA语言开发,设计过程中充分利用MVC设计模式.针对行业的管理需求,以日常管理为核心,构建集员工信息管理模块, ...

  2. 【案例分享】使用ActiveReports报表工具,在.NET MVC模式下动态创建报表

    提起报表,大家会觉得即熟悉又陌生,好像常常在工作中使用,又似乎无法准确描述报表.今天我们来一起了解一下什么是报表,报表的结构.构成元素,以及为什么需要报表. 什么是报表 简单的说:报表就是通过表格.图 ...

  3. 重磅!2019上半年中国互联网二手车拍卖平台研究报告发布!

    在政策以及市场消费购车观念变化的共同影响下,中国汽车交易市场中国新车交易发展开始放缓,2019年第一季度销量仅为637万辆,同比下降11.32%.与此同时,二手车的交易量增长了11.46%,二手车交易 ...

  4. MVC模式下My97DatePicker日期控件引用注意事项

    My97DatePicker日期控件之前在用webform模式开发的时候,只要 <script language="javascript" type="text/j ...

  5. servlet+javabean+jdbc+mysql基于MVC模式的课件管理系统,有三个表的增删改查和课件搜索、课件上传、课件下载功能, 具体功能请看界面上的导航条

    源码支持在idea.eclipse.myeclipse运行,数据库采用MySQL数据库,项目采用mvc设计模式开发,页面采用jsp+html+css+js完成. servlet+javabean+jd ...

  6. BIO、NIO、AIO 详解和基于BIO模式下即时通信

    第一章 BIO.NIO.AIO课程介绍 身边同学写的,没发布出来,我算是转载 1.1 课程说明 ​ 在Java的软件设计开发中,通信架构是不可避免的,我们在进行不同系统或者不同进程之间的数据交互,或者 ...

  7. bs模式Java web,基于BS模式的即时通讯系统的设计与实现(MyEclipse)

    基于BS模式的即时通讯系统的设计与实现(MyEclipse)(包含选题审批表,任务书,开题报告,中期检查表,毕业论文13000字,答辩记录,成绩评定册,源程序) 摘  要:即时通讯(Instant M ...

  8. 嵌入式linux音频播放器设计,基于嵌入式Linux下Madplay音频播放器设计论文.docx

    基于嵌入式Linux下Madplay音频播放器设计论文 滁州职业技术学院计算机应用技术专业毕业论文PAGE I 滁州职业技术学院信息工程系--2015届计算机应用专业毕业论文 姓 名: 周杰 班 级: ...

  9. asp毕业设计——基于asp+access的校园网上购物平台设计与实现(毕业论文+程序源码)——网上购物平台

    基于asp+access的校园网上购物平台设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于asp+access的校园网上购物平台设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦. ...

最新文章

  1. python判断 t1 树是否有与 t2 树拓扑结构完全相同的子树
  2. Spring Boot 2.5.5发布:开始支持Java 17了!
  3. C/C++语言中闭包的探究及比较
  4. Linux系统下手把手完成无人值守安装服务
  5. 2016年6月份学习总结,读书《向着光亮那方》
  6. python修复不了_如何修复Python代码?
  7. 吉比特java开发_JVM 吉比特后台 Java 开发实习生 20 分钟一轮游 _好机友
  8. 终于出手!谷歌母公司旗下GV风投首次投资AI芯片创业公司
  9. Vuex mutations的详细解读
  10. LINUX上开发ffmpeg程序,查看链接的库
  11. linux命令 sync,linux sync命令详解
  12. 读书笔记1:《C++沉思录》
  13. 身份证地区码数据表-SqlServer版
  14. **一些常用的字体英文名**
  15. 如何在Tomcat中发布网站
  16. 计算机如何远程控制对方手机,如何远程控制别人的电脑【图解】
  17. vs2015调试时无法显示QString变量的值,只显示地址
  18. MySQL中的排序与分页
  19. MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
  20. phpspider框架的使用

热门文章

  1. 使用s2i制作docker镜像
  2. Bregman Divergences on Riemannian Manifold 黎曼流形上的Bregman散度
  3. 【Unity】Entities 1.0 学习(一):Aspect
  4. 另辟蹊径,简单sojson分析
  5. 90后剁手党占了一半!天猫是如何成为奢侈品第一平台的?
  6. L2TP连接错误 789 解决方法
  7. 解决企业包`出现无法安装“XXX“请稍后再试`的问题
  8. mysql中判断一列在另一列中没有_Excel中判断一个表中的某一列的数据在另一列中是否存在...
  9. java商品库管理_java实现超市商品库存管理平台
  10. JVM 新生代为何需要两个 Survivor 空间?