Minggu, 31 Maret 2013

Task 2 (Horoscope)

Diposting oleh Unknown di 18.19
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

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

/**
 * @author user
 */
public class RamalanBintang extends MIDlet implements CommandListener {

    Display display;
    Form textForm;
    Command exitCommand = new Command("Exit", Command.EXIT, 1);
    Command hasilCommand = new Command("Result", Command.OK, 1);
    TextField nama = null;
    ChoiceGroup choice1 = null, choice2 = null, choice3 = null;
    Ticker ticker = new Ticker("Handa Sari Surbakti - Horoscope - Java Mobile");

    public RamalanBintang() {
        textForm = new Form("Horoscope");
        textForm.addCommand(exitCommand);
        textForm.addCommand(hasilCommand);
        textForm.setCommandListener(this);

        nama = new TextField("Silahkan Ketik Nama Anda (huruf kecil)", "", 64, TextField.ANY);
        choice1 = new ChoiceGroup("Pilh Tanggal Lahir Anda", Choice.POPUP);
        choice1.append("Pilih", null);
        choice1.append("01", null);
        choice1.append("02", null);
        choice1.append("03", null);
        choice1.append("04", null);
        choice1.append("05", null);
        choice1.append("06", null);
        choice1.append("07", null);
        choice1.append("08", null);
        choice1.append("09", null);
        choice1.append("10", null);
        choice1.append("11", null);
        choice1.append("12", null);
        choice1.append("13", null);
        choice1.append("14", null);
        choice1.append("15", null);
        choice1.append("16", null);
        choice1.append("18", null);
        choice1.append("19", null);
        choice1.append("20", null);
        choice1.append("21", null);
        choice1.append("22", null);
        choice1.append("23", null);
        choice1.append("24", null);
        choice1.append("25", null);
        choice1.append("26", null);
        choice1.append("27", null);
        choice1.append("28", null);
        choice1.append("29", null);
        choice1.append("30", null);
        choice1.append("31", null);

        choice2 = new ChoiceGroup("Pilih Bulan Lahir Anda", Choice.POPUP);
        choice2.append("Pilih", null);
        choice2.append("01", null);
        choice2.append("02", null);
        choice2.append("03", null);
        choice2.append("04", null);
        choice2.append("05", null);
        choice2.append("06", null);
        choice2.append("07", null);
        choice2.append("08", null);
        choice2.append("09", null);
        choice2.append("10", null);
        choice2.append("11", null);
        choice2.append("12", null);

        choice3 = new ChoiceGroup("Pilih Tahun Lahir Anda", Choice.POPUP);
        choice3.append("Pilih", null);
        choice3.append("1980", null);
        choice3.append("1981", null);
        choice3.append("1982", null);
        choice3.append("1984", null);
        choice3.append("1985", null);
        choice3.append("1986", null);
        choice3.append("1987", null);
        choice3.append("1988", null);
        choice3.append("1989", null);
        choice3.append("1990", null);
        choice3.append("1991", null);
        choice3.append("1992", null);
        choice3.append("1993", null);

        textForm.append(nama);
        textForm.append(choice1);
        textForm.append(choice2);
        textForm.append(choice3);
        textForm.setTicker(ticker);
    }

    public void result() {

        String[] kesehatan = {
            "Sakit", "Kurang Sehat", "Segera periksa ke Dokter", "Mantap"
        };
        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[] a;
        a = new int[l];
        int jlhNama = 0, jlhLahir = 0, x = 0, y = 0, z = 0;
        for (int i = 0; i < l; i++) {
            a[i] = ((int) letter[i]) - 96;
            jlhNama = jlhNama + a[i];
        }

        choice1.setSelectedIndex(x, true);
        choice2.setSelectedIndex(y, true);
        choice3.setSelectedIndex(z, true);
        jlhLahir = x + y + z;

        int SisaNama = jlhNama % 4;
        int SisaLahir = jlhLahir % 4;
        int akum = (SisaNama + SisaLahir) % 4;

        StringItem sehat = new StringItem("Kesehatan Anda = ", "", StringItem.PLAIN);
        StringItem uang = new StringItem("Keuangan Anda = ", "", StringItem.PLAIN);
        StringItem percintaan = new StringItem("Asmara Anda   = ", "", StringItem.PLAIN);
        StringItem pekerjaan = new StringItem("Karir Anda     = ", "", StringItem.PLAIN);
        if (SisaNama == 0) {
            sehat.setText(kesehatan[0]);
            pekerjaan.setText(karir[3]);
        }
        if (SisaNama == 1) {
            sehat.setText(kesehatan[1]);
            pekerjaan.setText(karir[2]);
        }
        if (SisaNama == 2) {
            sehat.setText(kesehatan[2]);
            pekerjaan.setText(karir[1]);
        }
        if (SisaNama == 3) {
            sehat.setText(kesehatan[3]);
            pekerjaan.setText(karir[0]);
        }
        if (SisaLahir == 0) {
            uang.setText(keuangan[0]);
        } else {
            if (SisaLahir == 1) {
                uang.setText(keuangan[1]);
            } else {
                if (SisaLahir == 2) {
                    uang.setText(keuangan[2]);
                } else {
                    uang.setText(keuangan[3]);
                }
            }
        }
        if (akum == 0) {
            percintaan.setText(asmara[0]);
        } else {
            if (akum == 1) {
                percintaan.setText(asmara[1]);
            } else {
                if (akum == 2) {
                    percintaan.setText(asmara[2]);
                } else {
                    percintaan.setText(asmara[3]);
                }
            }
        }
        textForm.append(sehat);
        textForm.append(uang);
        textForm.append(percintaan);
        textForm.append(pekerjaan);
        textForm.removeCommand(hasilCommand);
    }

    public void startApp() {

        if (display == null) {
            display = Display.getDisplay(this);
            display.setCurrent(textForm);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed(); // Exit
        }
        if (c == hasilCommand) {
            result();

        }
    }
}

OUTPUT PROGRAM:
This is the Form 1. Fill your Name with small word,  to fill your date birth Click  Pilih Tanggal lahir, And to fill your month birth Click pilih bulan Lahir Anda, to fill your year birth Click Pilih tahun lahit


The Display when you Filled


The display when you had fill your name and date birth


The calculation from my name and birh date



Explanation:



Line 2-6 are codes for java class that needed to import for this program.
It’s need four classes to imported. Java io*, Provides classes for input and output through data streams.

Line 18 – 25  Display input devices of the system, The user interface objects that are shown on the display device are contained within a Displayable object.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.
The call to setCommandListener() tells the system that the MIDlet (this)
should be told about any command action that happens on the TextBox.
A TextField is an editable text component that may be placed into a Form. It can be given a piece of text , the initial value. We use Textfield , form and command in this program (nama & tanggal lahir).

Line 31- 94 : Choose group, Choose the Date from 1-31 , choose Month from 1-12, Choose year 1980-1993

Line 103-180 : The Calculation horoscope from yout Birthdate and name 

Line 184 - 189 :  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.





0 komentar:

Posting Komentar

 

My PAGES Copyright © 2010 Design by Ipietoon Blogger Template Graphic from Enakei | web hosting