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.
MenuReader menuReader = FileReaderStrategy.getFromDefaultFile('|');
model = new MenuModel(menuReader);
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(); }
}
//////////////////// HERE IS THE MAIN MODIFICATION ////////////////////////////////////
//If and only if you can modify the MenuReader and transform it in a interface///////
public interface MenuReader{
public void read();
public List<MenuItem> getMenu();
}
public class FileReaderStrategy implements MenuReader{
private List<MenuItem> items;
private String fileName;
private String sep;
private FileReaderStrategy(String fileName, String sep){
this.fileName = fileName;
this.sep = sep;
}
@Override
public void read(){
//do your reading XD
}
@Override
public List<MenuItem> getMenu(){
//never return the reference :)
return new ArrayList<>(this.items);
}
public static FileReaderStrategy getFromDefaultFile(String sep){
return new FileReaderStrategy(fileName, sep);
}
public static FileReaderStrategy getFromFile(String file, String sep){
return new FileReaderStrategy(file, sep);
}
}
//////////////////////////////////////////////////////////////////////////////
class MenuModel {
// ArrayList hold MenuItem
private ArrayList<MenuItem> menuItems;
private ArrayList<MenuItem> order;
public MenuModel(MenuReader menuReader){
order = new ArrayList<MenuItem>();
menuReader.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 List<MenuItem> getMenuItem(){ return new ArrayList<>(menuItems); }
// Get what was ordered
//Interfaces over implementation
public List<MenuItem> getOrderList(){ return new ArrayList<>(order); }
}
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);
}
}
[Refactor of https://codepad.co/snippet/5HUv42PF].
1 Response
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.