Urgent Help with Booking Program

Help with booking program

Page 1 of 1

6 Replies - 608 Views - Last Post: 05 January 2009 - 11:02 AM Rate Topic: -----

#1 aLders  Icon User is offline

  • New D.I.C Head

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

Urgent Help with Booking Program

Posted 04 January 2009 - 09:39 AM

Hi there ive been asking a few questions about my program for a while and have not yet been able to work out what to do!!!
I need to hand my program in soon and cant work out how to return the values i want from my methods.

My program is below:


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

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String output, usersNum, address, date, lengthOfHire, numPeople, mountHelOutput, raceHelOutput;
		int num, bookAnother; 
		int mountBike = 0, mountSmall = 0, mountMed = 0, mountLarge = 0, mountHelmet = 0, helmetOutput, mountMale = 0, mountFemale = 0;
		int raceBike = 0, raceSmall = 0, raceMed = 0, raceLarge = 0, raceAccess = 0, raceMale = 0, raceFemale = 0, hapOutput;
		int childBike = 0;
		
		
		
			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");

				
		do {
			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;
				mountMale = mountMale + mountBikeSex();
				mountFemale = mountFemale + mountBikeSex();
				
				mountSmall = mountSmall + mountFrame();
				mountMed = mountMed + mountFrame();
				mountLarge = mountLarge + mountFrame();
				
				mountHelmet = mountHelmet + mountHel();
				
				
			}	
			
			if (num == 2) {
				
				raceBike = raceBike + 1;
				
				raceMale = raceMale + raceSex();
				raceFemale = raceFemale + raceSex();
				
				raceSmall = raceSmall + raceFrame();
				raceMed = raceMed + raceFrame();
				raceLarge = raceLarge + raceFrame();
				
				raceAccess = raceAccess + raceHel();
				
	
			else  
				childBike = childBike + 1;
				
				JOptionPane.showMessageDialog(null, "Helmet is included with bike and must be worn!", "A Message",
						JOptionPane.INFORMATION_MESSAGE);
			
			}
				while (num <1 || num > 3);
				System.exit(0);
				
		}
	}
			
	
		
	public static int mountBikeSex () {
			String output, usersNum;
			int num, Male = 0, Female = 0;
			
			output = "";
			output = "Select if bike is for male or female \n\n";
			output = output + "1 - Male \n";
			output = output + "2 - Female \n";
			
			do
			{
			usersNum = JOptionPane.showInputDialog(output);
			num = Integer.parseInt(usersNum);
			
			if (num == 1)
				Male = Male + 1;
			return Male;
			
			if (num == 2)
				Female = Female + 1;
			return Female;
			
			}while((num!=1)&&(num!=2));
			
			
			
		}
	
			

	public static int mountFrame (){
			String output, usersNum;
			int num, small, medium, large;
			
			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) {
					small = small + 1;
					
				}else if (num == 2) {
					medium = medium +1;
					
				}else if (num == 3) {
					large = large +1;
				}
				
				return small;
				return medium;
				return large;
		}
		
		
	public static int mountHel () {
			String mountHelOutput; 
			int helmetOutput, mountHel = 0;
			
			mountHelOutput = "Do you require a helmet?";
			helmetOutput = JOptionPane.showConfirmDialog(null, mountHelOutput, "Please Answer",
					JOptionPane.YES_NO_OPTION);
				if (helmetOutput == 0);
					mountHel = mountHel + 1;
					return mountHel;
					
		}
		
	public static int raceSex (){
			String output, usersNum;
			int num, Male, Female;
			
			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);
			Male = Male + 1; 
			if (num == 2);
			Female = Female + 1;
			
			return Male;
			return Female;
		}
		
	public static int raceFrame() {
			String output, usersNum;
			int num, small, medium, large;
			
			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) {
					small = small + 1;
					
				}else if (num == 2) {
					medium = medium +1;
					
				}else if (num == 3) {
					large = large +1;
				}
				
				return small;
				return medium;
				return large;
		}
		
	public static int raceHel() {
			String raceHelOutput; 
			int hapOutput, mountHel, raceAccess;
			
			raceHelOutput = "Do you require a helmet and panniers?";
			hapOutput = JOptionPane.showConfirmDialog(null, raceHelOutput, "Please Answer",
					JOptionPane.YES_NO_OPTION);
				if (hapOutput == 0);
					raceAccess = raceAccess + 1;
					return raceAccess;
				 
		}
	}
		// TODO Auto-generated method stub

	



Basically all i need to do is be able to return the values from my methods so that they can be added on.... sounds simple but ive been battling with it for a while now and cant seem to work it out!!

mountBikeSex is the only method i have been fiddling around with to try and get working, thats why my methods are all slightly different

Help will be MUCH appreciated and thanks in advance :)

aLders x

This post has been edited by aLders: 04 January 2009 - 09:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Urgent Help with Booking Program

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Urgent Help with Booking Program

Posted 04 January 2009 - 09:58 AM

you cant return more than one value in one call. i've seen u do that
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: Urgent Help with Booking Program

Posted 04 January 2009 - 11:00 AM

Yea i know that i cant, thats the problem. I dont know how to get the values i want back? Because only one is going to be selected i only really need one value to be returned but i cant work out how to do it...
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Urgent Help with Booking Program

Posted 04 January 2009 - 11:44 AM

Your big problem is that you're trying to do more in your methods than you need to. Also, I don't really get the whole Male+1 deal; it makes little sense. Ask the user for input, validate the input is within scope, then continue on with your program.

Here's an example. You should be able to reuse this in all int cases.
import javax.swing.JOptionPane;

public class Main {

	public static int getUserInt(String prompt, String [] items) {
		int num = 0;
		String output = prompt + "\n";
		
		int len = items.length;
		for(int i=0; i<len; i++) {
			output += "\n" + (i+1) + " - " + items[i];
		}
		
		for(;;)/> {  // spin until they get it right
			String result = JOptionPane.showInputDialog(output);
			try {
				num = Integer.parseInt(result);
				if (num>0 && num<=len) { break; }
			} catch (NumberFormatException ex) {
			}
			JOptionPane.showMessageDialog(null, 
				"Sorry, please select a value from the list",
				"Value Error",
			JOptionPane.ERROR_MESSAGE);
			
		}
		return num;
	}
	

	public static void main(String[] args) {
		int num = getUserInt(
				"Select the bike you wish to purchase", 
				new String [] { "Mountain Bike","Racing Bike","Childrens Bike"}
				);
		System.out.println("seleced: " + num);
	}
}



Define what you're looking for from the user. Make a class for it.
e.g.
class Bike {
	public int bikeType;
	public int sex;
	public int size;
	public boolean helmet;
}



I don't know what the deal is with all those totals, you only seem to be asking for one. Totals would be pretty useless for a booking program, you'd want info on each individual item.

Bike item = new Bike();
// load up the values based on user input
// ...
// record the item, move on
list.add(item);



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 aLders  Icon User is offline

  • New D.I.C Head

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

Re: Urgent Help with Booking Program

Posted 05 January 2009 - 06:47 AM

I dont really understand your program that you have written there..... The program is really basic but i jst cant work out how to return my values..
Was This Post Helpful? 0
  • +
  • -

#6 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Urgent Help with Booking Program

Posted 05 January 2009 - 09:56 AM

okay,,,, interesting... hahah nd waaaay over complicated.. but nyway ill break this down for u step by step

1) your do-loop setup really is not the best way to approach this nevermind the fact that u have multiple syntax errors (missing '{' and '}' )
so first thing.. the easiest way to set up ur loop is to say
while(true){
///ur code
}

than when ever the user selects a menu item (1,2,3) simply type break at the end of the if statement
if(num == 1){
//ur code
break;
}
if (num == 2){
//ur code
break;
}

much easier to code and control

2) a lot of the time ur type things like this:
output = output + ..
mountBike = mountBike + ...
this is rather clumbersome. the same effect can be achieved with the += operator:
output += .... same as output = output + ....
mountBike += 5 same as mountBike = mountBike + 5

3) now the part ur asking about... returning shit
this is ur code for mountBikeSex()
    public static int mountBikeSex () {
            String output, usersNum;
            int num, Male = 0, Female = 0;
           
            output = "";
            output = "Select if bike is for male or female \n\n";
            output = output + "1 - Male \n";
            output = output + "2 - Female \n";
           
            do
            {
            usersNum = JOptionPane.showInputDialog(output);
            num = Integer.parseInt(usersNum);
           
            if (num == 1)
                Male = Male + 1;
            return Male;
           
            if (num == 2)
                Female = Female + 1;
            return Female;
           
            }while((num!=1)&&(num!=2));
        }


notice that the return statements aren't actually part of the if statements :o
and... u did tht stupid do while loop thing. ill fix this for u.. nd u can use it as an example to fix the rest. the while loop is controlled by a simple boolean varible that tells the program whether to continue looping or not.

	public static int mountBikeSex () {
			String output, usersNum;
			int num, Male = 0, Female = 0;
			boolean isContinue = true;
			output = "";
			output = "Select if bike is for male or female \n\n";
			output += "1 - Male \n";
			output  += "2 - Female \n";
		   
		   while(isContinue){
				usersNum = JOptionPane.showInputDialog(output);
				num = Integer.parseInt(usersNum);
			   
				if (num == 1){
					Male += 1;
					isContinue = false;
				}
				else if (num == 2){
					Female += 1;
					isContinue = false;
				}
				else{
					// the program will continue to loop unless the user chooses a valid option
					// nothing is returned unless they choose 1 or 2
					// for this reason it is a good idea to hv an option to quit
				}
		   }
		if (Male > 0){
			return Male;
		}
		else if(Female > 0){
			return Female;
		}
		else {
			return -1;  //if there was an error this gives u a testable value
		}
	}



now this code works flawlessly but the way u call these methods is retarded :P

mountMale = mountMale + mountBikeSex();
mountFemale = mountFemale + mountBikeSex();
you dont actually need to distinguish between male and female in the method because u do it in the main method (actually doing it in the bike sex method would be useless)
when it comes down to it ur whole program realllly needs to be re written and thought about a whole bunch more

baavgai suggestion about creating your own bike class is defineatly the best way to approach this program.... however judging by your code thus far, creating ur own classes and such is slightly beyond ur level..

so instead.. we r going to create an array to store this info
from ur code it appears you 4 things u wont to store
type, sex,size, helmet

so we create an array for this like this
String[] bike = new String[4];

we will then add the appropriate info to the array
bike[0] = getBikeType();
bike[1] = getSex();
bike[2] = getBikeSize();
bike[3] = getHelmet();




now if u try and run this ull notice that u dont have ANY of the required methods so ull have to write them.
ill do the first one for you
public static String getBikeType () {
            String output, type="";
            int num;
            boolean isContinue = true;
          
            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";
           
           while(isContinue){
	            num = Integer.parseInt(JOptionPane.showInputDialog(output));
	           
	            if (num == 1){
	                type = "Mountain Bike";
	                isContinue = false;
	            }
	            else if (num == 2){
	                type = "Racing Bike";
	                isContinue = false;
	            }
	            else if (num == 3){
	            	type = "Children's Bike";
	            	isContinue = false;
	            }
	            else{
	            	// tell the user they entered an incorect choice
	            	// the program will continue to loop unless the user chooses a valid option
	                // nothing is returned unless they choose 1 or 2
	            	// for this reason it is a good idea to hv an option to quit
	            }
           }
           return type;
    }



THIS SHOULD LOOK VAGUELY FAMILIAR !
write the next 3 methods in a similar manner and u'll be almost done.

this is the program so far
import javax.swing.JOptionPane;
public class Testing {


    public static void main(String[] args) {
        String address, date, lengthOfHire, numPeople;
        
       
            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");
           
            String[] bike = new String[4];
            

            bike[0] = getBikeType();
      //      write these methods 
      //      bike[1] = getSex();        returns "male" or "female"     
      //      bike[2] = getBikeSize();   returns "small" "meduim" or "large"
      //      bike[3] = getHelmet();     returns "requires helmet" or "does not require helmet"
     
            
      //      do this and ill help u sort out the next bit

    }
           
   
       
    public static String getBikeType () {
            String output, type="";
            int num;
            boolean isContinue = true;
          
            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";
           
           while(isContinue){
	            num = Integer.parseInt(JOptionPane.showInputDialog(output));
	           
	            if (num == 1){
	                type = "Mountain Bike";
	                isContinue = false;
	            }
	            else if (num == 2){
	                type = "Racing Bike";
	                isContinue = false;
	            }
	            else if (num == 3){
	            	type = "Children's Bike";
	            	isContinue = false;
	            }
	            else{
	            	// tell the user they entered an incorect choice
	            	// the program will continue to loop unless the user chooses a valid option
	                // nothing is returned unless they choose 1 or 2
	            	// for this reason it is a good idea to hv an option to quit
	            }
           }
           return type;
    }
}



one thing 2 note.. right now this program will only work for ONE bike.
but when u finish those methods ill show u how to make it work for as many as u want :o

if you dont know what an array is, check out the tutorials section there's a pretty decent one there
Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2584
  • View blog
  • Posts: 15,612
  • Joined: 24-September 08

Re: Urgent Help with Booking Program

Posted 05 January 2009 - 11:02 AM

Why don't you try putting in some comments so I know what you're doing, and give a better description of your assignment
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1