5 Replies - 849 Views - Last Post: 12 November 2008 - 03:09 PM Rate Topic: -----

#1 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

How to create and call methods/functions

Posted 12 November 2008 - 04:03 AM

Hi there, i am a complete beginner with java and have been given work to do which is just a simple booking system. The code i currently have is shown below (i know there is alot of stuff not compete).
What i want to do is put seperate things in different methods and just call the methods to run depending on what button the user was to press. Ive scoured the internet but cant seem to find it anywhere and it must be somthing fairly simple?

Thanks in advance

aLders


package Booking;
import javax.swing.JOptionPane;
public class version1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String output, usersNum, address, date, lengthOfHire, numPeople, mountHelOutput;
		int num, bookAnother; 
		int mountbike = 0, mountsmall = 0, mountmed = 0, mountlarge = 0, mountHelmet = 0, helmetOutput, mountMale = 0, mountFemale = 0;
		int racebike = 0; 
		int childbike = 0;
		
		do {
		
		output = "Select one of the menu options \n\n";
		output = output + "1 - Proceed to Booking \n";
		output = output + "2 - About Edinburgh Hire \n";
		output = output + "3 - Exit \n";
		
		usersNum = JOptionPane.showInputDialog(output);
		
		num = Integer.parseInt(usersNum);
		if (num == 1) {
			address = JOptionPane.showInputDialog("Please enter your FULL address");
			date = JOptionPane.showInputDialog("Please enter date for hire");
			lengthOfHire = JOptionPane.showInputDialog("Please enter how long the hire will be for");
			numPeople = JOptionPane.showInputDialog("Please enter how many people the booking is for");
			
			while (num <1 || num > 3)
				output = "";
			output = "Select the bike you wish to purchase \n\n";
			output = output + "1 - Mountain Bike \n";
			output = output + "2 - Racing Bike \n";
			output = output + "3 - Childrens Bike \n";
			
			usersNum = JOptionPane.showInputDialog(output);
			num = Integer.parseInt(usersNum);
			
			if (num == 1) {
				mountbike = mountbike + 1;
					while (num < 1 || num > 2);
					output = "";
					output = "Select if bike is for male or female \n\n";
					output = output + "1 - Male \n";
					output = output + "2 - Female \n";
						
					usersNum = JOptionPane.showInputDialog(output);
					num = Integer.parseInt(usersNum);
					
					if (num == 1);
					mountMale = mountMale + 1; }
					else {
					mountFemale = mountFemale + 1;
				}
						while (num < 1 || num > 3)
							output = "";
						output = "What frame size do you require? \n\n";
						output = output + "1 - Small \n";
						output = output + "2 - Medium \n";
						output = output + "3 - Large \n";
						
						usersNum = JOptionPane.showInputDialog(output);
						num = Integer.parseInt(usersNum);
						
							if (num == 1) {
								mountsmall = mountsmall + 1;
								
							if (num == 2) {
								mountmed = mountmed +1;
								
							if (num == 3) {
								mountlarge = mountlarge +1;
							}
							}
							}
							mountHelOutput = "Do you require a helmet?";
							helmetOutput = JOptionPane.showConfirmDialog(null, mountHelOutput, "Please Answer",
									JOptionPane.YES_NO_OPTION);
								if (helmetOutput == 0);
									mountHelmet = mountHelmet + 1;
									bookAnother = JOptionPane.showConfirmDialog(null, "Do you wish to book another bike?", "Please Answer",
											JOptionPane.YES_NO_OPTION);
												if (bookAnother == 0);
													//restart booking
												else
													//display details and confirm
								if (helmetOutput == 1); 
									bookAnother = JOptionPane.showConfirmDialog(null, "Do you wish to book another bike?", "Please Answer",
											JOptionPane.YES_NO_OPTION);
												if (bookAnother == 0);
													//restart booking
												else
													//display details and confirm
									
									
			if (num == 2) {
				racebike = racebike + 1;
				
				
			if (num == 3) {
				childbike = childbike + 1;
			}
					}
			}
		
		{	
		}
		if (num == 2) {
		output = "Details about Edinburgh Hire";
		JOptionPane.showMessageDialog(null, output, "Menu System",
		JOptionPane.INFORMATION_MESSAGE);
	
		}
		while (num < 1 || num > 3);
		}
		while (num < 1 || num > 3);
	System.exit(0);
		 
		
		}
	}


		
	
	

		// TODO Auto-generated method stub

	




Is This A Good Question/Topic? 0
  • +

Replies To: How to create and call methods/functions

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: How to create and call methods/functions

Posted 12 November 2008 - 04:41 AM

This is not the answer to your question but the following part of your code is probably not functional in the way you want it..

if (num == 1) {
								mountsmall = mountsmall + 1;
								
							if (num == 2) {
								mountmed = mountmed +1;
								
							if (num == 3) {
								mountlarge = mountlarge +1;
							}
							}
							}



I mean, num can't be equal to 1, 2 & 3 at the same time and if num isn't equal to 1 then the later 2 if-statements will never be evaluated.

Your code in general is quite a bit of a mess..

I would try to get some structure on the code if I were you. Like you said yourself, you want methods to handle things, that's a pretty good idea. Start writing down what the methods should do, what information they need to handle their duty and what they should return.

This post has been edited by Gloin: 12 November 2008 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

Re: How to create and call methods/functions

Posted 12 November 2008 - 05:48 AM

Yea, i know its quite a mess lol not a logical mind tbh.

What is the code to switch to a different method? I dnt want to return values to the main method but simply change to a different method.

if (num == 1) {
								mountsmall = mountsmall + 1;
								
							if (num == 2) {
								mountmed = mountmed +1;
								
							if (num == 3) {
								mountlarge = mountlarge +1;
							}
							}
							}



How can that be changed? I thought that it would have worked ok? Or will i have to insert the if statements into each of the choices?

This post has been edited by aLders: 12 November 2008 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: How to create and call methods/functions

Posted 12 November 2008 - 10:44 AM

Use else if's:
    	 if (num == 1) {
             mountsmall = mountsmall + 1;
    	 }else if (num == 2) {
             mountmed = mountmed +1;
    	 } else if (num == 3) {
             mountlarge = mountlarge +1;
         }



To switch to a different method you have to create a method, and then call that method using the method name and providing the correct arguments.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to create and call methods/functions

Posted 12 November 2008 - 12:40 PM

Topic renamed to be more descriptive.
Was This Post Helpful? 0
  • +
  • -

#6 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

Re: How to create and call methods/functions

Posted 12 November 2008 - 03:09 PM

Thanks for your comments, much appreciated. Will see how it all goes!

Thanks again

aLders
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1