Advertisement
a1ananth

Java MVC Test Code

Oct 20th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Model {
  4.     public int takeInput(View view) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         view.printText("Enter input: ");
  8.         int input = sc.nextInt();
  9.  
  10.         return input;
  11.     }
  12. }
  13.  
  14. class Controller {
  15.     public void count(int from, int to, View view) {
  16.         String s = "Printing numbers from " + from + " to " + to + ".";
  17.         view.printText(s);
  18.  
  19.         if (from < to) {
  20.             while ((to-from) >= 0) {
  21.                 view.printText(new Integer(from).toString());
  22.                 from++;
  23.             }
  24.         }
  25.         else {
  26.             while ((from-to) >= 0) {
  27.                 view.printText(new Integer(from).toString());
  28.                 from--;
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. class View {
  35.     public void printText(String a) {
  36.         System.out.println(a);
  37.     }
  38. }
  39.  
  40. public class MainClass {
  41.     public static void main(String[] args) {
  42.  
  43.         /* Instantiate all the required classes */
  44.  
  45.         Model model = new Model();
  46.         Controller controller = new Controller();
  47.         View view = new View();
  48.  
  49.         /* Call model method to take input */
  50.  
  51.         int a = model.takeInput(view);
  52.         int b = model.takeInput(view);
  53.  
  54.         /* Call controller method to run the required process */
  55.  
  56.         controller.count(a, b, view);
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement