Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Implementation MVC of problem

Implementation MVC of problem

Scheduled Pinned Locked Moved Design and Architecture
helpjavaasp-netarchitecturequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fell0206
    wrote on last edited by
    #1

    Everybody, I have some MVC of problem, I reference more information, and I write the Model, the Controller and more than one View, but I don't know my code is consistent with MVC? Please give some help! Thank you~

    import java.util.*;

    public class Initial {

    /\*\*
     \* @param args
     \*/
    
    public static void main(String\[\] args) {
    	// TODO Auto-generated method stub
    	View v = new View("View");
    	View2 v2 = new View2("View2");
    	Model m = new Model();
    	Controller c = new Controller(m);
    	c.add(v);
    	c.add(v2);
    
    	v.display();
    	v2.display();
    
    	c.showAryList();
    }
    

    }

    public class Model {
    private enum States {
    Add, Subtract
    };

    private States state;
    
    public Model() {
    }
    
    public void changeToAdd() {
    	this.state = States.Add;
    }
    
    public void changeToSubtract() {
    	this.state = States.Subtract;
    }
    
    public int caculate(int x, int y) {
    	int currentInt = 0;
    	if (this.state == States.Add) {
    		currentInt = add(x, y);
    	}
    
    	if (this.state == States.Subtract) {
    		currentInt = substract(x, y);
    	}
    
    	return currentInt;
    }
    
    public int add(int x, int y) {
    	return x + y;
    }
    
    public int substract(int x, int y) {
    	return x - y;
    }
    

    }

    import java.util.*;

    public class Controller {

    // private Viewable v;
    private Model m;
    private List aryList;
    
    public Controller(Model m) {
    	// TODO Auto-generated constructor stub
    	this.m = m;
    	aryList = new ArrayList();
    }
    
    public void add(Viewable v) {
    	aryList.add(v);
    	System.out.println(v.getName() + "add!");
    	v.addController(this);
    }
    
    public void removeView(Viewable v) {
    	aryList.remove(v);
    	System.out.println(v.getName() + "remove!");
    }
    
    public void sumNumber(Viewable v, int N1, int N2) {
    	this.m.changeToAdd();
    
    	if (aryList.contains(v)) {
    		((Viewable) this.aryList.get(aryList.indexOf(v))).show(this.m
    				.caculate(N1, N2));
    	}
    	removeView(v);
    }
    
    public void substractNumber(Viewable v, int N1, int N2) {
    	this.m.changeToSubtract();
    
    	if (aryList.contains(v)) {
    		((Viewable) this.aryList.get(aryList.indexOf(v))).show(this.m
    				.substract(N1, N2));
    	}
    }
    
    public void showAryList() {
    	for (int i = 0; i < aryList.size(); i++) {
    		System.out.println(aryList.get(i).getClass());
    	}
    }
    

    }

    public interface Viewable {
    // final String name;
    public String getName();

    public void addController(Controller c);
    
    public void show(int result);
    

    }

    import java.util

    R 1 Reply Last reply
    0
    • F fell0206

      Everybody, I have some MVC of problem, I reference more information, and I write the Model, the Controller and more than one View, but I don't know my code is consistent with MVC? Please give some help! Thank you~

      import java.util.*;

      public class Initial {

      /\*\*
       \* @param args
       \*/
      
      public static void main(String\[\] args) {
      	// TODO Auto-generated method stub
      	View v = new View("View");
      	View2 v2 = new View2("View2");
      	Model m = new Model();
      	Controller c = new Controller(m);
      	c.add(v);
      	c.add(v2);
      
      	v.display();
      	v2.display();
      
      	c.showAryList();
      }
      

      }

      public class Model {
      private enum States {
      Add, Subtract
      };

      private States state;
      
      public Model() {
      }
      
      public void changeToAdd() {
      	this.state = States.Add;
      }
      
      public void changeToSubtract() {
      	this.state = States.Subtract;
      }
      
      public int caculate(int x, int y) {
      	int currentInt = 0;
      	if (this.state == States.Add) {
      		currentInt = add(x, y);
      	}
      
      	if (this.state == States.Subtract) {
      		currentInt = substract(x, y);
      	}
      
      	return currentInt;
      }
      
      public int add(int x, int y) {
      	return x + y;
      }
      
      public int substract(int x, int y) {
      	return x - y;
      }
      

      }

      import java.util.*;

      public class Controller {

      // private Viewable v;
      private Model m;
      private List aryList;
      
      public Controller(Model m) {
      	// TODO Auto-generated constructor stub
      	this.m = m;
      	aryList = new ArrayList();
      }
      
      public void add(Viewable v) {
      	aryList.add(v);
      	System.out.println(v.getName() + "add!");
      	v.addController(this);
      }
      
      public void removeView(Viewable v) {
      	aryList.remove(v);
      	System.out.println(v.getName() + "remove!");
      }
      
      public void sumNumber(Viewable v, int N1, int N2) {
      	this.m.changeToAdd();
      
      	if (aryList.contains(v)) {
      		((Viewable) this.aryList.get(aryList.indexOf(v))).show(this.m
      				.caculate(N1, N2));
      	}
      	removeView(v);
      }
      
      public void substractNumber(Viewable v, int N1, int N2) {
      	this.m.changeToSubtract();
      
      	if (aryList.contains(v)) {
      		((Viewable) this.aryList.get(aryList.indexOf(v))).show(this.m
      				.substract(N1, N2));
      	}
      }
      
      public void showAryList() {
      	for (int i = 0; i < aryList.size(); i++) {
      		System.out.println(aryList.get(i).getClass());
      	}
      }
      

      }

      public interface Viewable {
      // final String name;
      public String getName();

      public void addController(Controller c);
      
      public void show(int result);
      

      }

      import java.util

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #2

      I doubt that anyone will want to wade through your code on the off-chance it might be problematic. The purpose of the forum is to answer questions relating to specific issues not for peer review.

      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups