BRINGING THE PIECES TOGETHER IV – User Interaction

Today we will be concluding the set of posts I have been sharing on how to bring the pieces of our previous lessons together and write a meaningful code. We will focus on User Interaction but before we go ahead, let us have a recap of all we have done so far.

  • Setting up of IDEs,
  • Creation of packages, classes, methods, variables and identifies,
  • Brief overview of access modifiers
  • Declaration of constructors
  • Use of Java statements: If-Else and return statements
  • Implementation of an interface – declaration of an interface, addition of abstract methods to an interface and implementation of an interface.
  • Working with objects by creating instance of a class,
  • Encapsulation – making use of setters and getters and we were able to set parameters and get some parameters using this concept.
  • Calling up methods from implementing classes of an interface by creating object of the implementing class with reference to the interface

Without doubt, it has been a long lesson. If you are just joining us, you should start from the beginning. In this post, we will be concluding the set of tutorials by looking at the most important part of an application which is user interaction. A user of an application is anybody that makes use of an application, interaction is the communication between two or more objects therefore we can say that user interaction in an application is anything built into the application that makes it usable and understandable for human. No, we are not designing an interface but we will see how our application can communicate effectively with the user to ensure the user can easily navigate the application and use our AreaCalculator effectively.

Let us consider our application layout again as we usually do,

ProjectHeirachy2

A lot of ground have been covered so far. We have discussed about the purpose of each package and class and we have seen the code on ShapeVariableBean class of package com.bee.code.blog.ShapeCommon here. In our second lesson, we discussed package com.bee.code.blog.ShapeController, the interface and classes in this package here. In our third lesson however, we discussed package com.bee.code.blog.MoreShapes and we have seen the code on all the classes within this package. With these, we are left with only one class that has not been talked about which is the AreaCalculator class under the com.bee.code.blog.ShapeCommon package as shown above. In the first post, I pointed at it as the main class and explained what makes a main class, click here if you missed it as today’s  discussion will be on this.

AreaCalculator. Java class

AreaCalculator class is the main class within the application as it contains the main method which means this is the first class that will run when the application is executed. It handles the communication with the user, finds out what the user wants, sends the request to the appropriate method and returns the output to the user. See source code below:

The class makes use of the Java Scanner to receive input from the user of the application see on line16 below (to be discussed in details).

  1 package com.bee.code.blog.ShapeCommon;
  2 
  3 import com.bee.code.blog.MoreShapes.Parrallelogram;
  4 import com.bee.code.blog.MoreShapes.Rectangle;
  5 import com.bee.code.blog.MoreShapes.RegularPolygon;
  6 import com.bee.code.blog.MoreShapes.Square;
  7 import com.bee.code.blog.MoreShapes.Triangle;
  8 import java.util.Scanner;
  9 
 10 /**
 11  *
 12  * @author 'beecodeblog
 13  */
 14 public class AreaCalculator {
 15 
 16     static Scanner reader = new Scanner(System.in);
 17 
 18     public static void main(String[] args) {
 19         AreaCalculator calculator = new AreaCalculator();
 20         String response = "Y";
 21         
 22         while (response.equalsIgnoreCase("Y")) {
 23             calculator.computeArea();
 24             System.out.println("Do you want to compute the area of another shape. Type 'Y' to continue and 'N' to exit.");
 25             response = reader.next();
 26                     
 27         }
 28     }
 29 
 30     public void computeArea() {
 31         double area = 0.0;
 32 
 33         System.out.println("Welcome Math Professor: Please\n Type '1' - for Area of Parrallelogram\n Type '2' - for Area of Rectangle \n Type '3' - for Area of Square\n Type '4' - for Area of Triangle  ");
 34         System.out.println("Enter a number: ");
 35         final int n = reader.nextInt();
 36 
 37         switch (n) {
 38             case 1:
 39                 System.out.println("You selected area of a Parrallelogram, Type 'Y' to proceed and 'N' to terminate:");
 40                 String parrallelogram = reader.next();
 41                 if (parrallelogram.toUpperCase().equals("Y")) {
 42                     System.out.println("Enter base value: ");
 43                     double base = reader.nextDouble();
 44                     System.out.println("Enter height value: ");
 45                     double height = reader.nextDouble();
 46                     area = new Parrallelogram().doArea(base, height);
 47                     break;
 48                 } else {
 49                     return;
 50                 }
 51             case 2:
 52                 System.out.println("You selected area of a Rectangle, Type 'Y' to proceed and 'N' to terminate:");
 53                 String rectangle = reader.next();
 54                 if (rectangle.toUpperCase().equals("Y")) {
 55                     System.out.println("Enter base value: ");
 56                     double base = reader.nextDouble();
 57                     System.out.println("Enter height value: ");
 58                     double height = reader.nextDouble();
 59                     area = new Rectangle().doArea(base, height);
 60                     break;
 61                 } else {
 62                     return;
 63                 }
 64             case 3:
 65                 System.out.println("You selected area of a Square, Type 'Y' to proceed and 'N' to terminate:");
 66                 String square = reader.next();
 67                 if (square.toUpperCase().equals("Y")) {
 68                     double lenght = reader.nextDouble();
 69                     System.out.println("Enter lenght value: ");
 70                     area = new Square().doArea(lenght);
 71                     break;
 72                 } else {
 73                     return;
 74                 }
 75             case 4:
 76                 System.out.println("You selected area of a Triangle, Type 'Y' to proceed and 'N' to terminate:");
 77                 String triangle = reader.next();
 78                 if (triangle.toUpperCase().equals("Y")) {
 79                     System.out.println("Enter base value: ");
 80                     double base = reader.nextDouble();
 81                     System.out.println("Enter height value: ");
 82                     double height = reader.nextDouble();
 83                     area = new Triangle().doArea(base, height);
 84                     break;
 85                 } else {
 86                     return;
 87                 }
 88             default:
 89                 System.out.println("You have not selected any option- Is the shape a regular polygon? Type 'R' for Regular Polygon,  'I' for Irregular Polygon and 'N' to terminate:");
 90                 String regularPolygon = reader.next();
 91                 if (regularPolygon.toUpperCase().equals("R")) {
 92                     System.out.println("Enter base value: ");
 93                     double base = reader.nextDouble();
 94                     System.out.println("Enter height value: ");
 95                     double height= reader.nextDouble();
 96                     area = new RegularPolygon().doArea(base, height);
 97                     break;
 98                 } else if (regularPolygon.toUpperCase().equals("I")) {
 99                     System.out.println("Sorry, no implementation for Irregular Polygons...");
100                     break;
101                 } else {
102                      System.out.println("Thank you for using area of shapes calculator...");
103                     return;
104                 }
105         }
106         if(area!=0.0){
107         System.out.println("Area is : " + area);
108         }
109 
110     }
111 }
112 

The following interactions happen between the user of the application and the application when the application is executed.

  • Once the application runs, the application will ask the user to specify the shape the user is trying to calculate its area. See snippet below:

select1

  • Once a user enters the right number for the operation needed, the application pops up a message to confirm the option selected by the user

2

  • Once the user confirms his option by typing “Y”, the message below displays asking for the necessary parameter .

3

  • Then this message displays to request for the second value

 4

  • Once the required parameters have been supplied and the users clicks on enter, the area is calculated and the output is displayed as shown below.

5

  • Once the output is displayed, the message above follows asking the user if he would like to continue as shown above. And the cycle continues as long as the customer response remains “Y”.

6

No matter how complex the application you are building is, it is very important that you have a very good user interaction/interface as an unnecessarily complicated interface is of no good use to the user if they don’t know how to go about using it. Like my boss will always say, “always have it at the back of your mind whenever you are building an application as if it’s targeted at a 5 year old this way, you will make it as dummy proof as possible”. I hope you enjoyed the tutorial, please don’t forget to get in touch should you require any assistance on any of the tutorials. Happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s