2 Replies - 2850 Views - Last Post: 09 February 2012 - 10:59 AM Rate Topic: -----

#1 casp3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-February 12

How to convert application to applet

Posted 09 February 2012 - 08:46 AM

package schoolapartments;

import java.util.Calendar;
import java.util.Scanner;
public class Main {
    
    public Main(){
}


    private ApartmentA[] typeA;
    private ApartmentB[] typeB;
    private Student[] student;
    private Scanner input=new Scanner(System.in);
    private Scanner input2=new Scanner(System.in);
    private Calendar calendar;
    public static void schoolapartment (String[] args) {
        Main ma=new Main();
        ma.initialiseArrays();
        ma.Menu();
    }

    private void Menu()
    {
        char selection;
        String choice;
        System.out.println("WELCOME TO UCTI APARTMENT SYSTEM");
        System.out.println("");
        System.out.println("[----------------------------]");
        System.out.println("[          MAIN MENU         ]");
        System.out.println("[----------------------------]");
        System.out.println("[ Press N - New Registration ]");
        System.out.println("[ Press U - Update Status    ]");
        System.out.println("[ Press R - Report           ]");
        System.out.println("[ Press S - Search           ]");
        System.out.println("[ Press Q - Quit System      ]");
        System.out.println("[----------------------------]");
        System.out.println("");
        choice=input.next();
        selection=choice.charAt(0);

        switch(selection)
        {
            case 'N':
            case 'n':
                registerStudentModule();
                break;
            case 'U':
            case 'u':
                updateModule();
                break;
            case 'R':
            case 'r':
                generateReportModule();
                break;
            case 'S':
            case 's':
                searchModule();
                break;
            case 'Q':
            case 'q':
                System.out.println("Thank you for using our system");
                System.out.println("Have a nice day");
                System.exit(0);
                break;
            default:
                System.out.println("Incorrect Selection");
                System.out.println("Please revise your selection");
                System.out.println("");
                Menu();
                break;
        }
    }
    public void registerStudentModule()
    {
        char choice;
        String str,studentID,studentName,master,deposite;
        boolean isMaster;
		System.out.println("");
		System.out.println("[-----------------------------------------------------------------]");
		System.out.println("[                        REGISTRATION MENU                        ]");
		System.out.println("[--------------------------------|--------------------------------]");
		System.out.println("[             Type A             |             Type B             ]");
		System.out.println("[--------------------------------|--------------------------------]");
		System.out.println("[ 2 Single bedroom               | 1 Master bedroom               ]");
		System.out.println("[                                | 2 Single bedroom               ]");
		System.out.println("[ Facilities: Laudry & Kitchen   | Facilities: No Laudry/Kitchen  ]");
		System.out.println("[ Monthly rental: RM300          | Month rental: RM280 (Master)   ]");
		System.out.println("[                                |               RM200 (Single)   ]");
		System.out.println("[-----------------------------------------------------------------]");
		System.out.println("");
        System.out.println("-----------New Student---------");
        System.out.println("  Press A - Apartment Type A   ");
        System.out.println("  Press B - Apartment Type B   ");
        System.out.println("-------------------------------");
        str=input.next();
        choice=str.charAt(0);

        switch(choice)
        {
            case 'A':
            case 'a':
                System.out.println("Enter Student ID (TP No.)");
				System.out.print("TP");
                studentID=input.next();
                System.out.println("Enter Student Name");
                studentName=input2.nextLine();
                System.out.println("Enter Security Deposite");
                deposite=input.next();
                for(int i=0;i<20;i++)
                {
                    if(typeA[i].isVaccant()==true)
                    {
                        typeA[i].setRooms();
                        for(int j=0;j<20;j++)
                        {
                            if(student[j].getID().equalsIgnoreCase("")==true)
                            {
                                student[j].setType(typeA[i].getApartmentID());
                                student[j].setRent(typeA[i].getRentAmount());
                                student[j].setID(studentID);
                                student[j].setName(studentName);
                                calendar=Calendar.getInstance();
                                calendar.add(Calendar.DATE, 30);
                                student[j].setExpiry(calendar.getTime().toString());
                                System.out.println("New Student booking has been made");
                                break;
                            }
                        }
                        break;
                    }
                }
                break;
            case 'B':
            case 'b':
                System.out.println("Enter Student ID");
                studentID=input.next();
                System.out.println("Enter Student Name");
                studentName=input2.nextLine();
                System.out.println("Enter Security Deposite");
                deposite=input.next();
                System.out.println("Master Roam(Y/N)");
                master=input.next();
                if(master.equalsIgnoreCase("Y"))
                    isMaster=true;
                else
                    isMaster=false;
                for(int i=0;i<10;i++)
                {
                    if(typeB[i].isVaccant()==true)
                    {
                        typeB[i].setRooms();
                        typeB[i].setMasterRoom(isMaster);
                        for(int j=0;j<20;j++)
                        {
                            if(student[j].getID().equalsIgnoreCase("")==true)
                            {
                                student[j].setType(typeA[i].getApartmentID());
                                student[j].setRent(typeA[i].getRentAmount());
                                student[j].setID(studentID);
                                student[j].setName(studentName);
                                calendar=Calendar.getInstance();
                                calendar.add(Calendar.DATE, 30);
                                student[j].setExpiry(calendar.getTime().toString());
                                System.out.println("New Student booking has been made");
                                break;
                            }
                        }
                        break;
                    }
                }
                break;
            default:
                System.out.println("Incorrect Selection");
                break;
        }
        System.out.println("Enter Y to add another student or M for Main Menu");
        str=input.next();
        if(str.equalsIgnoreCase("Y"))
            registerStudentModule();
        else
            Menu();
    }
    private void updateModule()
    {
        String apartmentID,choice;
        System.out.println("*******Update Module*******");
        System.out.println("*Please enter Apartment ID*");
        System.out.println("***************************");
        apartmentID=input.next();
        System.out.println("Enter A for Type A and B for Type B");
        choice=input.next();
        if(choice.equalsIgnoreCase("A"))
        {
            for(int i=0;i<20;i++)
            {
                if(typeA[i].getApartmentID().equalsIgnoreCase(apartmentID))
                {
                    if(typeA[i].isVaccant()==true)
                        System.out.println("The apartment is currently vaccant");
                    else
                        System.out.println("The apartment is currently Full");
                    System.out.println("Do you want to change the current Status of the appartment?(Y/N)");
                    choice=input.next();
                    if(choice.equalsIgnoreCase("Y"))
                    {
                        if(typeA[i].isVaccant()==true)
                            typeA[i].setVaccant(false);
                        else
                            typeA[i].setVaccant(true);
                        System.out.println("Apartment Status has been updated");
                        System.out.println();
                        break;
                    }
                    else
                        break;
                }
            }
            Menu();
        }
        else
        {
            for(int i=0;i<20;i++)
            {
                if(typeB[i].getApartmentID().equalsIgnoreCase(apartmentID))
                {
                    if(typeB[i].isVaccant()==true)
                        System.out.println("The apartment is currently vaccant");
                    else
                        System.out.println("The apartment is currently Full");
                    System.out.println("Do you want to change the current Status of the appartment?(Y/N)");
                    choice=input.next();
                    if(choice.equalsIgnoreCase("Y"))
                    {
                        if(typeB[i].isVaccant()==true)
                            typeB[i].setVaccant(false);
                        else
                            typeB[i].setVaccant(true);
                        System.out.println("Apartment Status has been updated");
                        System.out.println();
                        break;
                    }
                    else
                        break;
                }
            }
            Menu();
        }
    }
    private void generateReportModule()
    {
        int atype=0,btype=0;
        String str;
        for(int i=0;i<20;i++)
        {
            if(typeA[i].isVaccant()==true)
                atype=atype+1;
            if(typeB[i].isVaccant()==true)
                btype=btype+1;
        }
        System.out.println("********Report Mordule**********");
        System.out.println("*Apartment Type : Type A       *");
        System.out.printf("*Occupied       : %d Apartments*\n",atype);
        System.out.printf("*Vacant         : %d Apartments*\n",20-atype);
        System.out.println("********************************");
        System.out.println("*Apartment Type : Type B       *");
        System.out.printf("*Occupied       : %d Apartments*\n",btype);
        System.out.printf("*Vacant         : %d Apartments*\n",20-btype);
        System.out.println("*********************************");
        System.out.println("Enter any Key to continue");
        str=input.next();
        Menu();
    }
    private void searchModule()
    {
        String studentID,ans;
        boolean found=false;
        System.out.println("*****Search Module*******");
        System.out.println("*Please Enter Student ID*");
        System.out.println("*************************");
        studentID=input.next();
        for(int i=0;i<20;i++)
        {
            if(student[i].getID().equalsIgnoreCase(studentID))
            {
                found=true;
                System.out.printf("Student ID     : %s\n",student[i].getID());
                System.out.printf("Student Name   : %s\n",student[i].getName());
                System.out.printf("Apartment ID   : %s\n",student[i].getType());
                System.out.printf("Rent Amount    : %f\n",student[i].getRent());
                System.out.printf("Expiry Date    : %s\n",student[i].getExpiry());
                System.out.println("Enter any key to continue");
                ans=input.next();
                break;
            }
        }
        if(found==false)
        {
            System.out.println("You entered a wrong student ID, please enter correct student ID");
            searchModule();
        }
        Menu();
    }
    private void initialiseArrays()
    {
        typeA=new ApartmentA[20];
        typeB=new ApartmentB[20];
        student=new Student[20];
        for(int i=0;i<20;i++)
        {
            typeA[i]=new ApartmentA(String.valueOf((i+1)*100));
            typeB[i]=new ApartmentB(String.valueOf((i+1)*100));
            student[i]=new Student();
        }
    }
}



This is the "main" code which related to another 3 classes.i'm trying to convert to an applet. I am very new to Java and from what i got from Google is applet was based on .awt and swing. For my case, i don't know where to put those init(), start(), stop() etc.
It's very simple application that simulates a house/apartment service system . The application will do

Registration of student
Update (check out)
Report
Search

Basically i have made everything working its just the matter of running in in applet form which my teacher didn't taught me yet so my knowledge just based on books and webs.

So, where should should i begin with ?
(i noticed some website said that the application must be changed into swig, where should i put the command )
that i want to run it in an applet.

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert application to applet

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How to convert application to applet

Posted 09 February 2012 - 10:26 AM

It is not about putting init() and others here, it is about the whole design. What you have now is a command line application and you want to change it to GUI, that is what you have to start to, changing your application to swing. When looking at your code you will need to redesign a lot of things like menu()s and other to be swing instead. In short you will change the whole Main() class from what I see. That is why we prefer to separate between logic of the program from view part, this helps when you want to change your application from one view to another, you change the command line display to swing one without even touch the business part.

So to start you will need to know some about swing so you can create your own interface for the program:
Swing part1
Swing part2
Swing part3
Swing part4
Swing part5
And a lot of other swing tutorial.
After getting the concept of how to create your own GUI, the logic of Applet is very simple. It is a swing app running in browser so few concepts to add and you have your swing.

But also always think of separating logic from view so you can design good application that you can easly change its view
Was This Post Helpful? 1
  • +
  • -

#3 casp3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-February 12

Re: How to convert application to applet

Posted 09 February 2012 - 10:59 AM

:clap: Thank you very much. Now i have better understanding of what applet is, and know which part should i be working on now. Thank you again. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1