Sunday, March 31, 2013

Code Program Java Mobile for Horoscope

This time i will share code program for horoscope. Actually i made this program because my lecturer ask me. Procedure of this program is user input their name and birth date and then the output will show their horoscope for a week about their health, finance, romance and career. Here is the code :

package marisa.lois.main;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.lang.*;
import java.io.*;

/**
 * @author ACER
 */

public class Ramalan extends MIDlet implements CommandListener
{
   Display display = null;
   Form ramalanForm = null;
   Command exitCommand = new Command("Exit", Command.EXIT, 1);
   Command ramalCommand = new Command("Ramal", Command.OK, 1);
   TextField nama = null;
   TextField tglLahir = null;

   public Ramalan()
   {
      ramalanForm = new Form("Ramalan Mingguan Anda");
      ramalanForm.addCommand(exitCommand);
      ramalanForm.addCommand(ramalCommand);
      ramalanForm.setCommandListener(this);

      nama = new TextField("Nama (dengan huruf kecil) :", "", 64, TextField.ANY);
      tglLahir = new TextField("Tanggal Lahir (dd/mm/yyyy) :", "", 64, TextField.ANY);
     
      ramalanForm.append(nama);
      ramalanForm.append(tglLahir);
   }

   public void hitung()
   {
      String[] kesehatan =
      {
         "Sakit", "Kurang Sehat", "Periksa ke Dokter", "Mantapsss"
      };
      String[] keuangan =
      {
         "Minus", "Cukup", "Baik", "Berlebih"
      };
      String[] asmara =
      {
         "Galau", "PDKT", "Berbunga", "Romantis"
      };
      String[] karir =
      {
         "Bermasalah", "Tersendat - sendat", "Normal", "Lancar"
      };

      String name = nama.getString();
      char[] letter = name.toCharArray();
      int l = letter.length;
      int[] pose;
      pose = new int[l];
      int jlhName = 0, jlhDate = 0;

      for (int i = 0; i < l; i++)
      {
         pose[i] = ((int) letter[i]) - 96;
         jlhName = jlhName + pose[i];
      }

      String birthDate = tglLahir.getString();
      String a = birthDate.substring(0, 2);
      String b = birthDate.substring(3, 5);
      String c = birthDate.substring(6);
      int x = Integer.parseInt(a);
      int y = Integer.parseInt(b);
      int z = Integer.parseInt(c);
      jlhDate = x + y + z;

      int modName = jlhName % 4;
      int modDate = jlhDate % 4;
      int modMed = (modName + modDate) % 4;

      StringItem health = new StringItem("Kesehatan Anda : ","",StringItem.PLAIN);
      StringItem finance = new StringItem("Keuangan Anda : ", "",StringItem.PLAIN);
      StringItem romance = new StringItem("Asmara Anda : ", "", StringItem.PLAIN);
      StringItem career = new StringItem("Karir Anda : ", "", StringItem.PLAIN);

      if (modName == 0)
      {
         health.setText(kesehatan[0]);
         career.setText(karir[3]);
      }
      if (modName == 1)
      {
         health.setText(kesehatan[1]);
         career.setText(karir[2]);
      }
      if (modName == 2)
      {
         health.setText(kesehatan[2]);
         career.setText(karir[1]);
      }
      if (modName == 3)
      {
         health.setText(kesehatan[3]);
         career.setText(karir[0]);
      }
      if (modDate == 0)
      {
         finance.setText(keuangan[0]);
      } else
      {
         if (modDate == 1)
         {
            finance.setText(keuangan[1]);
         } else
         {
            if (modDate == 2)
            {
               finance.setText(keuangan[2]);
            } else
            {
               finance.setText(keuangan[3]);
            }
         }
      }
      if (modMed == 0)
      {
         romance.setText(asmara[0]);
      } else
      {
         if (modMed == 1)
         {
            romance.setText(asmara[1]);
         } else
         {
            if (modMed == 2)
            {
               romance.setText(asmara[2]);
            } else
            {
               romance.setText(asmara[3]);
            }
         }
      }

      ramalanForm.append(health);
      ramalanForm.append(finance);
      ramalanForm.append(romance);
      ramalanForm.append(career);
      ramalanForm.removeCommand(ramalCommand);
   }

   public void startApp()
   {
      if (display == null)
      {
         display = Display.getDisplay(this);
         display.setCurrent(ramalanForm);
      }
   }

   public void pauseApp()
   {
   }

   public void destroyApp(boolean unconditional)
   {
   }

   public void commandAction(Command c, Displayable d)
   {
      if (c == exitCommand)
      {
         destroyApp(true);
         notifyDestroyed();
      }
      if (c == ramalCommand)
      {
         hitung();
      }
   }
}

Explanation:
row 2-6 are codes for java class that needed to import for this program.

row 14-19 are codes for instantiation class such as Display, Form, Command, TextField,etc. Class that instantiated outside of the constructor is Public and known at all function. And when you instantiated a class, it's better if you make it's value or null if you want to input it later.

row 21-33 is the constructor. At row 23, I made a new form and add two commands to it, and what ever command that user choose will be caught by .setCommandListener at row 26. I also made TextField nama and tglLahir as input mode and add it to form.

row 35-149 is hitung() function. In that function I caught what ever input from user and process that and then show the output. From row 37 to 52 there are String for horoscope that will be shown. At row 54, String name will catch the input. row 55-59 is the initialization for variable that needed for looping later. From row 61-65 are codes for looping the position of each name's characters and transform it to ASCII decimal and minus it to 96. From row 67-74 are codes for get the birth date and then transform it to nominal and sum it.
The procedure for this horoscope is get the modulus from both of name and birth date. I use modulus name for health and career, modulus birth date for finance. And for romance, I combine both. After that I select the condition with If....else which you can see from row 85 to 142. And last, it's shown on the form, but I remove ramalCommand, so that user can't change their horoscope.

row 151-158 are for starting the application and it's shown the form.

row 168-179 are codes that caught the command from row 26. If it catch exitCommand, function destroyApp will be called, but if it catch ramalCommand , function hitung will be called.


And the result will be like this :
User input nama and tanggal lahir at that TextField.

Choose Ramal Command to process.

The result. It's shown your health, finance, romance and career for a week.


EmoticonEmoticon