HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Menu Model class using Model-View-Presenter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
presenterviewmenuusingclassmodel

Problem

The following code is for my Object Oriented Programming class. My program simulate the cashier's machine to order food. The code uses Model View Presenter. This class is the model.

I have an ArrayList to hold the possible order possible. The View uses it to create JButtons.

I have an ArrayList to hold data to know what is ordered.

I am wondering if I followed Model-View-Presenter correctly.

Is this class holding too much responsibility.

```
public class MenuModel {
// ArrayList hold MenuItem
private ArrayList menuItems;
// Textfile name
private String fileName = "Menu.txt";
// Use to read textfile
private MenuReader menuReader;
// Data to keep menu's price and the menu's order
//Keep for what was ordered
private ArrayList order;
//Listeners
private ArrayList listeners;
private Formatter formatter;

/**
*
* @param file name
*/
public MenuModel(String f){
listeners = new ArrayList();
formatter = new Formatter();
order = new ArrayList();
fileName = f;
menuItems = new ArrayList();
menuReader = new MenuReader(fileName, '|', menuItems);
menuReader.read();
}

public void addChangeListener(ChangeListener listener){
listeners.add(listener);
}
// update the menu & price
public void add(MenuItem menuItem){
order.add(menuItem);
ChangeEvent event = new ChangeEvent(this);
for(ChangeListener lister : listeners)
lister.stateChanged(event);
}
/**
*
* @return Menu Order Size
*/
public int size(){
return menuOrder.size();
}

// clear price and menu
public void clear(){

order.clear();
ChangeEvent event = new ChangeEvent(this);
for(ChangeListener lister : listeners)
lister.stateChanged(event);

}
// Text to be sent to the screen
public String display(){
StringBu

Solution

I would separate the responsibility to load the file from this class.I think this class have more responsibilities of a Presenter than a model class. A good indicator of that is the method display and the method clear and seems to me that you are using the Observer pattern to inform the view that the model has changed, and this seems to be the work of a middle-man class (Presenter).

So the only 2 thing I would change is to create a class to make the load of the file, and I would use for-each instead iterator, seems to me that iterator is something used much more in C++ than in Java.

I hope this can help :)

Context

StackExchange Code Review Q#125688, answer score: 2

Revisions (0)

No revisions yet.