Let's walkthrough an example of building an application in Java with a "model-view" style.
The view in a model-view style application is essentially the user interface... it has the responsibility of presenting menu options and any output to the user, as well as accepting commands from the user. Our view code will go in our main function. The view code will call model functions to perform any work on our application's data.
The model in our model-view style application will be an object that contains the data our application is manipulating. We will need to create a class and associated instance variables and methods to implement our model.
In this example, we'll make an application that allows a user to control a television. The user will have the option of moving the channel up or down, and the volume up or down. The television functionality we will implement as a class (our model), and the interface that allows the user to manipulate the television we will implement in our main function (the view). We'll also give the user the ability to "create a new tv", i.e. create a new television object. We'll present the user with the current state of the television, and a menu of options, and we'll allow them to keep selecting options until they decide to exit.
To start off, create a new Java project using IntelliJ (the name doesn't matter), you can pick the "Hello, World" template if you like and you should end up with a main method that looks like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
First, let's get rid of the "Hello, World!" and replace it with a menu that our users can use to manipulate the TV. We use the string concatenation operator (+) to split up our menu across multiple lines, and we use the newline character ("\n") to put each line on a new line for the user.
public class Main {
public static void main(String[] args) {
// Menu for our user interface
System.out.print(
"1) Create a new TV \n" +
"2) Move channel up \n" +
"3) Move channel down \n" +
"4) Move volume up \n" +
"5) Move volume down \n" +
"6) Exit \n" +
"Your Choice: "
);
}
}
Next let's accept input from the user. We'll create a Scanner object called input for accepting user input. And remember that to do this we have to add import java.util.*; to the top of our file to ensure the Scanner is available to our main function. We create a variable called choice for storing the user's choice, and we call the nextInt() method of the Scanner to accept the user's choice which we output back to them.
import java.util.*;
public class Main {
public static void main(String[] args) {
// for reading user inputs from the command line
Scanner input = new Scanner(System.in);
// stores user's menu choice
int choice;
// Menu for our user interface
System.out.print(
"1) Create a new TV \n" +
"2) Move channel up \n" +
"3) Move channel down \n" +
"4) Move volume up \n" +
"5) Move volume down \n" +
"6) Exit \n" +
"Your Choice: "
);
// read in the user's choice, print it back to them
choice = input.nextInt();
System.out.println("Option " + choice + " selected!");
}
}
Next let's put our menu code in a do-while loop so that after the user selects an option, we ask them for their next option. We'll set our do-while loop to exit when the user picks the exit option.
import java.util.*;
public class Main {
public static void main(String[] args) {
// for reading user inputs from the command line
Scanner input = new Scanner(System.in);
// stores user's menu choice
int choice;
do {
// Menu for our user interface
System.out.print(
"1) Create a new TV \n" +
"2) Move channel up \n" +
"3) Move channel down \n" +
"4) Move volume up \n" +
"5) Move volume down \n" +
"6) Exit \n" +
"Your Choice: "
);
// read in the user's choice, print it back to them
choice = input.nextInt();
System.out.println("Option " + choice + " selected!");
// keep accepting inputs until the user decides to exit
} while (choice != 6);
System.out.println("Exiting!");
}
}
Use IntelliJ to create a new Java Class called Television. To do this you can right-click on the src folder, then mouse-over "New", then pick "Java Class".
When the dialog box comes up, enter "Television" as the Class Name and hit OK.
IntelliJ will create a class file called Television that will initially contain a basic class definition:
public class Television {
}
We want our television to keep track of the current channel and volume. We'll create two instance variables to keep track of the channel and volume, and initially set them both to zero.
public class Television {
int channel = 0;
int volume = 0;
}
We want to show the current state of the television every time we present the menu to the user. One nice way we can do this is to create a toString() method for our Television class. The toString() method is expected to return a String representation of the object, and once implemented, we can use our object reference variables in String concatenation operations and other places that Strings are expected. We implement our toString() method below and return a string that outputs the current channel and volume (with some nice formatting).
public class Television {
int channel = 0;
int volume = 0;
public String toString()
{
// output the current channel and volume
return "\n" +
"**************************************** \n" +
"* Channel: " + this.channel + " \n" +
"* Volume: " + this.volume + " \n" +
"****************************************";
}
}
Now in our view, we will create a Television object and store it in our reference variable tv. Above our menu, we output the current state of tv with a System.out.println(), which will now work because we have implemented the toString() method.
import java.util.*;
public class Main {
public static void main(String[] args) {
// for reading user inputs from the command line
Scanner input = new Scanner(System.in);
// stores user's menu choice
int choice;
// Television object reference
Television tv = new Television();
do {
// output the current television state
System.out.println(tv);
// Menu for our user interface
System.out.print(
"1) Create a new TV \n" +
"2) Move channel up \n" +
"3) Move channel down \n" +
"4) Move volume up \n" +
"5) Move volume down \n" +
"6) Exit \n" +
"Your Choice: "
);
// read in the user's choice, print it back to them
choice = input.nextInt();
System.out.println("Option " + choice + " selected!");
// keep accepting inputs until the user decides to exit
} while (choice != 6);
System.out.println("Exiting!");
}
}
If we run our application, we should now get output like this:
****************************************
* Channel: 0
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 2
Option 2 selected!
****************************************
* Channel: 0
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 6
Option 6 selected!
Exiting!
Next let's implement methods to move up and down the channel and volume of our television.
public class Television {
int channel = 0;
int volume = 0;
public void channelUp() { this.channel++; }
public void channelDown() { this.channel--; }
public void volumeUp() { this.volume++; }
public void volumeDown() { this.volume--; }
public String toString()
{
// output the current channel and volume
return "\n" +
"**************************************** \n" +
"* Channel: " + this.channel + " \n" +
"* Volume: " + this.volume + " \n" +
"****************************************";
}
}
And finally, let's have our main function (view) call our object (model) to carry out these different actions depending on the user's choice. It carries out these actions by calling the associated Television object method. Note that in the case of creating a new television, we create a new object all together (the original television, having no reference to it any longer, is no longer accessible and will be removed from memory).
import java.util.*;
public class Main {
public static void main(String[] args) {
// for reading user inputs from the command line
Scanner input = new Scanner(System.in);
// stores user's menu choice
int choice;
// Television object reference
Television tv = new Television();
do {
// output the current television state
System.out.println(tv);
// Menu for our user interface
System.out.print(
"1) Create a new TV \n" +
"2) Move channel up \n" +
"3) Move channel down \n" +
"4) Move volume up \n" +
"5) Move volume down \n" +
"6) Exit \n" +
"Your Choice: "
);
// read in the user's choice, print it back to them
choice = input.nextInt();
System.out.println("Option " + choice + " selected!");
// Carry out the user's choice
if (choice == 1) tv = new Television();
else if (choice == 2) tv.channelUp();
else if (choice == 3) tv.channelDown();
else if (choice == 4) tv.volumeUp();
else if (choice == 5) tv.volumeDown();
// keep accepting inputs until the user decides to exit
} while (choice != 6);
System.out.println("Exiting!");
}
}
When we run our program we should now be able to create and manipulate Television objects! See the example below.
Note that this example is just giving you the basic idea of a model and view style application. There are other things we either need to do or should do:
****************************************
* Channel: 0
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 2
Option 2 selected!
****************************************
* Channel: 1
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 4
Option 4 selected!
****************************************
* Channel: 1
* Volume: 1
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 4
Option 4 selected!
****************************************
* Channel: 1
* Volume: 2
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 3
Option 3 selected!
****************************************
* Channel: 0
* Volume: 2
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 5
Option 5 selected!
****************************************
* Channel: 0
* Volume: 1
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 1
Option 1 selected!
****************************************
* Channel: 0
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 2
Option 2 selected!
****************************************
* Channel: 1
* Volume: 0
****************************************
1) Create a new TV
2) Move channel up
3) Move channel down
4) Move volume up
5) Move volume down
6) Exit
Your Choice: 6
Option 6 selected!
Exiting!