My Code

class McPatternsPresenter { //This is the class that will handle the model <-> UI communication. private MenuModel model; private ArrayList<ChangeListener> listeners; private Formatter formatter; public McPatternsPresenter() { //code to read a file and load the menu items. model = new MenuModel("Menu.txt"); listeners = new ArrayList<ChangeListener>(); formatter = new Formatter(); } /* *Method adds "menuItem" to MenuModel, then updates the view's screen *Obserser Design Pattern */ public void add(MenuItem menuItem){ model.add(menuItem); ChangeEvent event = new ChangeEvent(this); for(ChangeListener lister : listeners) lister.stateChanged(event); } // clears the screen & model's data public void clear(){ model.clear(); ChangeEvent event = new ChangeEvent(this); for(ChangeListener lister : listeners) lister.stateChanged(event); } public void addChangeListener(ChangeListener listener){ listeners.add(listener); } // Instruction to display text public String getOrder(){ StringBuilder sb = new StringBuilder(); sb.append(formatter.formatHeader()); for(MenuItem item : model.getOrderList()) sb.append(formatter.formatOrder(item)); sb.append(formatter.formatFooter()); return sb.toString(); } /* *Factory Design Pattern */ public String getCreditCardNumber(String number){ CreditCard cc = CreditCardFactory.createCard(number); if(cc == null) return "Please Re-Enter"; return cc.display(); } /* Contains the menu. The GUI will ask for the List then makes JButtons array from it. */ public ArrayList<MenuItem> getMenuItem(){ return model.getMenuItem(); } } class MenuModel { // ArrayList hold MenuItem private ArrayList<MenuItem> menuItems; // Textfile name private String fileName = "Menu.txt"; // use as a simple test run private MenuReader reader; private ArrayList<MenuItem> order; /** * * @param file name */ public MenuModel(String f){ order = new ArrayList<MenuItem>(); fileName = f; // Use to read textfile reader = new MenuReader(fileName, '|'); reader.read(); menuItems = reader.getMenu(); } // update the menu & price public void add(MenuItem menuItem){ order.add(menuItem); } /** * @return Menu Order Size */ public int size(){ return order.size(); } // clear price and menu public void clear(){ order.clear(); } // Possible order available public ArrayList<MenuItem> getMenuItem(){ return menuItems; } // Get what was ordered public ArrayList<MenuItem> getOrderList(){ return order; } /** * @return String filename */ public String getFileName(){ return fileName; } } class McPatternsGUI{ private McPatternsPresenter presenter; public McPatternsGUI(McPatternsPresenter presenter) { this.presenter = presenter; presenter.attachView(this); showGUI(); } // Update JTextArea screen private void showGUI() { final JTextArea screen = new JTextArea(3, 20); JScrollPane qScroller = new JScrollPane(screen); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JFrame theFrame = new JFrame(); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theFrame.setLayout(new BorderLayout()); screen.setEditable(false); JPanel title = new JPanel(new BorderLayout()); title.setSize(500,500); title.add(new JLabel("Welcome to McPatterns"), BorderLayout.NORTH); screen.setText("Input..."); // Observer Design Pattern ChangeListener listener = new ChangeListener(){ @Override public void stateChanged(ChangeEvent e) { screen.setText(presenter.getOrder()); } }; presenter.addChangeListener(listener); JPanel orderPane = new JPanel(); orderPane.setLayout(new BoxLayout(orderPane, BoxLayout.PAGE_AXIS)); final JLabel orderDetails = new JLabel("Your order"); orderPane.setBorder(BorderFactory.createRaisedBevelBorder()); //orderPane.add(qScroller); orderPane.add(orderDetails); final JTextField ccEntry = new JTextField("Enter CC #"); JButton confirm = new JButton("Place Order"); confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Add the function to handle confirmed order //Talks to presenter to get credit caard String display = presenter.getCreditCardNumber(ccEntry.getText()); orderDetails.setText("Order confrimed for " + display); } }); JButton cancel = new JButton("Cancel Order"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Add the function to handle cancel order // Presenter will create Screen & Model presenter.clear(); orderDetails.setText("Order cancelled"); } }); orderPane.add(ccEntry); orderPane.add(confirm); orderPane.add(cancel); JPanel buttonPanel = new JPanel(); //buttonPanel.setLayout(new GridLayout(4,4,1,1));//new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); // Ask the presenter for the buttons to create // Iterate over the buttons and create them ArrayList<MenuItem> menuList = presenter.getMenuItem(); buttonPanel.setLayout(new GridLayout(menuList.size()/2,menuList.size()/2)); for(final MenuItem menuItem : menuList){ JButton button = new JButton(menuItem.getName()); buttonPanel.add(button); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { presenter.add(menuItem); } }); } buttonPanel.setLayout(new FlowLayout()); theFrame.add(title,BorderLayout.NORTH); theFrame.add(buttonPanel, BorderLayout.CENTER); theFrame.add(qScroller, BorderLayout.EAST); theFrame.add(orderPane, BorderLayout.SOUTH); theFrame.setSize(800, 600); theFrame.setLocationRelativeTo(null); theFrame.setVisible(true); } }
my main files.

1 Response

Hi :) cool that you sign up here.
So, what I would change if possible, I would rather pass to the constructor of ManuModel the MenuReader, and let the Presenter create the menureader. It's always good to think that you data provider can change so would be cool to create a public interface MenuReader with the methods that are mandatory, and create implementations, like ManuReaderFileStrategy implements MenuReader ( http://www.tutorialspoint.com/design_pattern/strategy_pattern.htm).

Another important advise is always prefer interfaces over implementation, so I would use List<MenuItem> instate ArrayList<MenuItem> in the signature of the methods :). because you could want to change the implementation of the list.

If you can change the MenuReader I would create a static factory to return a menu reader that reads from Menu.txt, something like
publis static MenuReader getDeafultReader(), than this guys internally make new MenuReader('Menu.txt') :)

And finally I would take of the attribute fileName from MenuModel, sinse it does not need to use anymore.
Doing all this your code would look like this -> https://codepad.co/snippet/tFppluyU
I think the rest is awesome :)
If you have any futher queestions you always can reach me by my e-mail kim.ae09@gmail.com
I hope I've helped

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.