Skip to content

Instantly share code, notes, and snippets.

@aortiz49
Created January 25, 2020 20:27
Show Gist options
  • Save aortiz49/48cb21af72002e1db756a6ffd198311a to your computer and use it in GitHub Desktop.
Save aortiz49/48cb21af72002e1db756a6ffd198311a to your computer and use it in GitHub Desktop.
MVC Example
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* University of the Andes
* Department of Systems Engineering
* Licensed under Academic Free License version 2.1
* Lab 0: Sample
* Author: Andy Ortiz
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package controller;
// -------------------------------------------------------------
// Imports
// -------------------------------------------------------------
import model.logic.Model;
import view.View;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Controller class to render the view and update the model.
*/
public class Controller {
// -------------------------------------------------------------
// Attributes
// -------------------------------------------------------------
/**
* A model.
*/
private Model model;
/**
* A view.
*/
private View view;
// -------------------------------------------------------------
// Constructor
// -------------------------------------------------------------
/**
* Creates the project view and the project model
*/
public Controller() {
model = new Model();
view = new View();
}
// -------------------------------------------------------------
// Methods
// -------------------------------------------------------------
/**
* Prints the user options and updates the view using the model.
*
* @throws InputMismatchException If the user inputs an incorrect number sequence.
*/
public void run() throws InputMismatchException {
try {
Scanner reader = new Scanner(System.in);
boolean end = false;
while (!end) {
view.displayMenu();
int option = reader.nextInt();
switch (option) {
case 0:
// Display option 0
view.displayOp0Menu();
// read name from input
String name = reader.next();
// set name in model
model.setName(name);
// display name in view
view.displayOp0Data(name);
break;
case 1:
// Display option 1
view.displayOp1Menu();
// read age from input
int age = reader.nextInt();
// set age in model
model.setAge(age);
// display age in view
view.displayOp1Data(age);
break;
case 2:
// Display option 2
view.displayOp2Menu();
// get info from model
String info = model.getName()+"-"+model.getAge();
// display info in view
view.displayOp2Data(info);
break;
// Invalid option
default:
view.badOption();
end = true;
break;
}
}
} catch (InputMismatchException e) {
run();
}
}
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* University of the Andes
* Department of Systems Engineering
* Licensed under Academic Free License version 2.1
* Lab 0: Sample
* Authors: Andy Ortiz
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package main;
import controller.Controller;
/**
* The main class.
*/
public class Main {
/**
* Starts the controller.
*
* @param args Arguments.
*/
public static void main(String[] args) {
Controller controller = new Controller();
controller.run();
}
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* University of the Andes
* Department of Systems Engineering
* Licensed under Academic Free License version 2.1
* Lab 0: Sample
* Author: Andy Ortiz
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package model.logic;
// -------------------------------------------------------------
// Imports
// -------------------------------------------------------------
import java.io.*;
/**
* Definition of the world model
*/
public class Model {
// -------------------------------------------------------------
// Attributes
// -------------------------------------------------------------
/**
* String used to store name
*/
private String name;
/**
* Integer used to store age
*/
private int age;
// -------------------------------------------------------------
// Constructor
// -------------------------------------------------------------
/**
* Constructs a new model
*/
public Model() {
name = "Kyle";
age = 20;
}
// -------------------------------------------------------------
// Methods
// -------------------------------------------------------------
/**
* Returns the current name
* @return name
*/
public String getName() {
return name;
}
/**
* Sets the current name to a given name
* @param pName name to be set
*/
public void setName(String pName) {
name = pName;
}
/**
* Returns the current age
* @return age
*/
public int getAge() {
return age;
}
/**
* Sets the current age to a given age
* @param pAge age to be set
*/
public void setAge(int pAge) {
age = pAge;
}
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* University of the Andes
* Department of Systems Engineering
* Licensed under Academic Free License version 2.1
* Lab 0: Sample
* Author: Andy Ortiz
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
package view;
/**
* Publishes the view for the user.
*/
public class View {
// -------------------------------------------------------------
// Constants
// -------------------------------------------------------------
private static final String RESET = "\u001B[0m";
private static final String BLUE = "\u001B[34m";
private static final String DYELLOW = "\u001B[38;5;214m";
private static final String PINK = "\u001B[38;5;205m";
private static final String WEIRDRED = "\u001B[38;5;203m";
private static final String ORANGE = "\u001B[38;5;208m";
private static final String LIGHTBLUE = "\u001B[38;5;50m";
private static final String GREEN = "\u001B[38;5;84m";
private static final String PRETTYPURPLE = "\u001B[38;5;213m";
private static final String BBLUE = "\u001B[38;5;87m";
private static final String DARKORANGE = "\u001B[38;5;202m";
// -------------------------------------------------------------
// Displays
// -------------------------------------------------------------
/**
* Displays the user menu.
*/
public void displayMenu() {
System.out.println(DYELLOW + " **==========================**");
System.out.println(DYELLOW + " || ==== MENU ==== ||");
System.out.println(DYELLOW + " ||" + BBLUE + " 0. Set name " + DYELLOW + " ||");
System.out.println(DYELLOW + " ||" + GREEN + " 1. Set age " + DYELLOW + " ||");
System.out.println(DYELLOW + " ||" + PINK + " 2. Get Info " + DYELLOW + " ||");
System.out.println(DYELLOW + " **==========================**\n");
// display hint
this.displayHint();
System.out.print(BLUE + "Input -> \n\n" + RESET);
}
/**
* Displays an error message to the user if the option selected is invalid.
*/
public void badOption() {
System.out.println("\u001B[31m########## \n?Invalid option !! \n########## \u001B[0m");
}
/**
* Print a hint to the user to select an option.
*/
public void displayHint() {
System.out.println(
BLUE + "Enter the number corresponding to the option, the press the Return "
+ "key: (e.g ., 1,2..):\n" + RESET);
}
// -------------------------------------------------------------
// Option 0
// -------------------------------------------------------------
/**
* Print option 0 menu.
*/
public void displayOp0Menu() {
System.out.println(DYELLOW + "====== Set Name ======");
System.out.println(LIGHTBLUE + "Enter name: ");
}
/**
* Print option 0 data.
*/
public void displayOp0Data(String pName) {
System.out.println(LIGHTBLUE + "Name set to: " + RESET + pName);
System.out.println(DYELLOW + "======================\n");
}
// -------------------------------------------------------------
// Option 1
// -------------------------------------------------------------
/**
* Print option 1 menu.
*/
public void displayOp1Menu() {
System.out.println(DYELLOW + "====== Set Age ======");
System.out.println(LIGHTBLUE + "Enter Age: ");
}
/**
* Print option 1 data.
*/
public void displayOp1Data(int pAge) {
System.out.println(LIGHTBLUE + "Age set to: " + RESET + pAge);
System.out.println(DYELLOW + "======================\n");
}
// -------------------------------------------------------------
// Option 2
// -------------------------------------------------------------
/**
* Print option 2 menu.
*/
public void displayOp2Menu() {
System.out.println(DYELLOW + "====== Get Info ======");
}
/**
* Print option 2 data.
*/
public void displayOp2Data(String pInfo) {
System.out.println(LIGHTBLUE + "Name: " + RESET + pInfo.split("-")[0]);
System.out.println(LIGHTBLUE + "Age: " + RESET + pInfo.split("-")[1]);
System.out.println(DYELLOW + "======================\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment