我有一个GUI Java程序的MVC设计。我在想,如果:
这个程序要求用户提供有关火车票和旅行的一些信息,并且基本上处理他们输入的卡片,如果卡是有效的,则发出收据。需求是,我使用MVC设计模式时,必须有3个模型、视图和控制器包。
我可以上传到谷歌驱动器,如果需要的话。
package edu.witc.TrainTicket.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import edu.witc.TrainTicket.model.*;
import edu.witc.TrainTicket.view.*;
public class TrainTicketController {
private MainForm view = null;
private Destination model = null;
private CreditCard model1 = null;
public TrainTicketController(MainForm mainform, Destination destination, CreditCard card){
this.view = mainform;
this.model = destination;
this.model1 = card;
//add listener
SubmitButtonListener submit = new SubmitButtonListener();
this.view.addSubmitListener(submit);
}
class SubmitButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//set variables
boolean validCard = false;
boolean validName = false;
boolean validPhone = false;
boolean validPaintSelect = false;
String destinationSelected = "";
int count = 0;
Destination destination = new Destination(view.jrbChicago,view.jrbNewYork, view.jrbSeattle,view.jrbSanFransisco, view.jtfName, view.jtfPhone);
CreditCard card = new CreditCard();
//validate fields
ValidateFields validate = new ValidateFields();
validName = validate.nameCheck(view.jtfName.getText());
if(validName)
{
count++;
validPhone = validate.validatePhone(view.jtfPhone.getText());
}
if(validPhone)
{
count++;
validCard = validate.hasText(card.getCardNumber(view.jtfCardNumber));
}
if(validCard)
count++;
//if(count < 3)
//displayError();
else
{
destinationSelected = destination.getRadioButtonValue(view.jrbChicago,view.jrbNewYork, view.jrbSeattle, view.jrbSanFransisco);
String cardNumber = card.getCardNumber(view.jtfCardNumber);
String name = destination.getCustName(view.jtfName);
String phone = destination.getPhoneNum(view.jtfPhone);
displayMessage(cardNumber, name, phone);
}
}
private void displayError() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
sb.append("Oops sorry, you typed something wrong");
JOptionPane.showMessageDialog(null, sb);
}
public void displayMessage(String cardNumber, String name, String phone){
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
NumberFormat f = NumberFormat.getCurrencyInstance();
sb.append("Hello! Here is your estimate for the paint job:\n");
sb.append("You chose the:" + cardNumber +name +phone );
JOptionPane.showMessageDialog(null, sb);
}
}
}package edu.witc.TrainTicket.controller;
public class ValidateCard {
public static boolean luhnVerify(String str) {
int sum = 0;
int value;
int idx = str.length(); // Start from the end of string
boolean alt = false;
while(idx-- > 0) {
// Get value. Throws error if it isn't a digit
value = Integer.parseInt(str.substring(idx, idx + 1));
if (alt) {
value *= 2;
if (value > 9) value -= 9;
}
sum += value;
alt = !alt; //Toggle alt-flag
}
return (sum % 10) == 0;
}
}package edu.witc.TrainTicket.controller;
import java.util.InputMismatchException;
import javax.swing.JRadioButton;
public class ValidateFields {
private int count = 0;
public ValidateFields(){
}
public boolean hasText(String wallSpace){
boolean isValid = false;
count = 0;
try
{
if(wallSpace.trim().length() > 0 && wallSpace.matches("[0-9]+"))
count++;
if(count == 1)
return isValid = true;
}
catch(InputMismatchException e)
{
e.printStackTrace();
}
return isValid;
}
public boolean nameCheck(String name){
boolean isValid = false;
count = 0;
try
{
if(name.trim().length() > 0 && name.matches("[a-zA-Z]+"))
count++;
if(count == 1)
return isValid = true;
}
catch(InputMismatchException e)
{
e.printStackTrace();
}
return isValid;
}
public boolean validatePhone(String phone){
boolean cleanPhone = false;
count = 0;
try
{
if(phone.replaceAll("\\D","").length() == 11 && phone.replaceAll("\\D","").matches("[0-9]+"))
count++;
if(count == 1)
return cleanPhone = true;
}
catch(InputMismatchException e)
{
e.printStackTrace();
}
return cleanPhone;
}
} package edu.witc.TrainTicket.model;
import javax.swing.JTextField;
public class CreditCard {
public CreditCard(){
}
public CreditCard(JTextField cardNumber){
String cardNum = getCardNumber(cardNumber);
}
public String getCardNumber(JTextField cardNumber){
return cardNumber.getText();
}
}//This class sets gets all the information that the user has entered.
package edu.witc.TrainTicket.model;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import edu.witc.TrainTicket.view.*;
public class Destination {
//Constructor with no arguments
public Destination() {
}
public Destination(JRadioButton chicago, JRadioButton newYork, JRadioButton seattle, JRadioButton sanFransisco, JTextField name, JTextField phone) {
String selectedDestination = getRadioButtonValue(chicago, newYork, seattle, sanFransisco);
String custName = getCustName(name);
String custPhone = getPhoneNum(phone);
}
public String getCustName(JTextField name){
return name.getText();
}
public String getPhoneNum(JTextField phone){
return phone.getText();
}
//get the type of paint
public String getRadioButtonValue(JRadioButton chicago, JRadioButton newYork, JRadioButton seattle, JRadioButton sanFransisco) {
String selected = "";
if(chicago.isSelected())
selected = "Chicago";
if(newYork.isSelected())
selected = "New York";
if(seattle.isSelected())
selected= "Seattle";
if(sanFransisco.isSelected())
selected= "San Fransisco";
//JOptionPane.showMessageDialog(null, selected);
return selected;
}
}package edu.witc.TrainTicket.view;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MainForm extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public JTextField jtfCardNumber = null;
public JTextField jtfName = null;
public JTextField jtfPhone = null;
public JRadioButton jrbChicago = null;
public JRadioButton jrbNewYork = null;
public JRadioButton jrbSeattle = null;
public JRadioButton jrbSanFransisco = null;
protected JPanel radioPanel = null;
protected JPanel jpLabels = null;
protected JPanel jpTextFields = null;
protected JButton jbtSubmit = null;
public MainForm(){
makeFrom();
}
private void makeFrom() {
// TODO Auto-generated method stub
jpLabels = new JPanel();
jpTextFields = new JPanel();
jtfCardNumber = new JTextField(16);
jtfName = new JTextField();
jtfPhone = new JTextField();
jrbChicago = new JRadioButton("Chicago");
jrbNewYork = new JRadioButton("New York");
jrbSeattle = new JRadioButton("Seattle");
jrbSanFransisco = new JRadioButton("San Fransisco");
jrbChicago.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(jrbChicago);
group.add(jrbNewYork);
group.add(jrbSeattle);
group.add(jrbSanFransisco);
jbtSubmit = new JButton("Submit");
jpTextFields.setLayout(new GridLayout(7,1,1,1));
jpLabels.setLayout(new GridLayout(7,1,1,1));
jpLabels.add(new JLabel("Name:"));
jpTextFields.add(jtfName);
jpLabels.add(new JLabel("Phone:"));
jpTextFields.add(jtfPhone);
jpLabels.add(new JLabel("Credit Card Number"));
jpTextFields.add(jtfCardNumber);
jpLabels.add(new JLabel("Please Select A Destination"));
jpLabels.add(jrbChicago);
jpLabels.add(jrbNewYork);
jpLabels.add(jrbSeattle);
jpLabels.add(jrbSanFransisco);
add(jpTextFields, BorderLayout.CENTER);
add(jpLabels, BorderLayout.WEST);
add(jbtSubmit, BorderLayout.SOUTH);
}
public void addSubmitListener(ActionListener click){
jbtSubmit.addActionListener(click);
}
public void displayMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
}package edu.witc.TrainTicket.view;
import javax.swing.JFrame;
import edu.witc.TrainTicket.controller.*;
import edu.witc.TrainTicket.model.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainForm view = new MainForm();
Destination model = new Destination();
CreditCard model1 = new CreditCard();
@SuppressWarnings("unused")
TrainTicketController controller = new TrainTicketController(view,model,model1);
view.setTitle("Paint Job Information");
view.setSize(500,300);
view.setResizable(false);
view.setLocationRelativeTo(null);
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setVisible(true);
}
}发布于 2013-10-18 05:11:53
这个方法不应该在模型public CreditCard(JTextField cardNumber)中,模型层中的public Destination(JRadioButton chicago, JRadioButton newYork, JRadioButton seattle, JRadioButton sanFransisco, JTextField name, JTextField phone)类应该只知道底层的接口(DAO或类似的东西)。如果它包含JTextField,而不是告诉您的业务模型您使用的是什么视图。直接在服务(模型)方法中从textfield发送文本。或者最好给他们发送一些DTO包装对象。
你滥用了ValidateCard和ValidateFields。ValidateFields属于控制器或视图层,ValidateCard属于模型层并检查业务需求。
你还复制了城市的名字。当您想要添加新城市时,您必须更改模型和视图。将它们放在一个地方(可能类似于Model类访问的CityRepository ),然后从那里获取城市。
因此,概述模型是商业人士的观点。它应该包含验证和执行您的业务任务(在您的例子中是一些traint icket处理),并且应该只知道数据模型。视图应该只知道控制器,并提供与之交互的接口,以及如何从用户控制器获得数据驱动流程--它从视图中获取数据,并将其封装在一些业务对象(如TrainTicket、User等)中。并将其传递给模型,从模型中获取输出并通过视图呈现出来。
https://codereview.stackexchange.com/questions/32844
复制相似问题