Results 1 to 6 of 6
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default Help: Methods and Arguments

    Hi I'm writing a program about Kitchen Sales.
    In this program, it will have to work out the total amount and the current unit price, the kind of fittings chosen from Basic, Standard and Deluxe and whether the units are to be installed or not.
    There will a method that works out the basic price for the number of cupboard units the user input. This method will then be passes the number of of units and the unit price and return the result of multiplying them together.
    The answer is passes back to the main method and it will print out this basic price.

    The result of the calculation should then be passes by the main method to a second method that works out the total cost taking into account the kind of cupboard installed. The cost is unchanged for basic units, is multiplied by 2 for standard units and multiplied by 3 for deluxe units.
    if the kitchen is to be installed(1000 added) otherwise nothing will be added to the cost

    This is what I have done

    Java Code:
    	public static void main(String[] param) 
    	{
    		fittedkitchen();
    		System.exit(0);
    	}
    	
    	public static void fittedkitchen() 
    	{
    		
    		String cupboardunits = getText("Please type in the number of cupboard units.");
    		int numofunits = Integer.parseInt(cupboardunits);
    		
    		String pricePerUnits = getText("Please type in the individual unit price.");
    		int unitPrice = Integer.parseInt(pricePerUnits);
    		
    		JOptionPane.showMessageDialog(null,"The basic price is " + calculatePrice(numofunits,unitPrice));
    		
    		String fittings = getText("Type in the fittings according to the number given:\n\n1.Basic\n2.Standard\n3.Deluxe");
    		
    		
    		int fitted = JOptionPane.showConfirmDialog(null, "Is the kitchen to be installed ?","", JOptionPane.YES_NO_OPTION);
    		int cost =0;
    		if(fittings.equalsIgnoreCase("B")){
    		 cost =1;
    		}else if(fittings.equalsIgnoreCase("S")){
    		 cost =2;
    		}else if(fittings.equalsIgnoreCase("D")){
    		 cost =3;
    		}
    		//if ((number-1) <= types.length) 
    		//{
    		int price = cost*calculatePrice(numofunits,unitPrice);
    		if(fitted == 0)
    			{
    				price = price + 1000;
    			}
    		else
    			{
    			
    			}
              	JOptionPane.showMessageDialog(null, price + " pounds");
    	
    		
    	}
    	public static int calculatePrice(int unit, int price){
    		return unit * price;
    	}
    	
    	public static String getText(String msg)
    	{
    		String txt = null;
    		while (txt == null || txt.trim().equals(""))
    		{
    			txt = JOptionPane.showInputDialog(msg);
    		}
    			return txt;
    	}
    Basically the main method should ask for the base price, number of units and pass them to the first method to work out the basic cost. This basic cost will be returned to main which prints it out. The main should then asks for the kind of fitting and whether it is to be installed or not and passes the information along with basic cost to the second method. It then returns the final overall estimate to the main.

    But the I only have one extra method to work the base price. How should I do the rest?

    Any help would be greatly appreciated.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    So this is the algorithm in pseudocode, right?
    Java Code:
            basePrice = quantity * unitPrice
            if fittings = Basic
                    price = basePrice
            else if fittings = Standard
                    price = basePrice * 2
            else if fittings = Deluxe
                    price = basePrice * 3
            else
                    bad value for fittings
            if installed
                    price += 1000
    See if you can write that in Java, and let us know what problems you have.

    -Gary-

  3. #3
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    what do you meant by that? I have written that operation in the program. what I need help is how to pass to another new methods and not compact everything in main method.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Sorry, I should have read your post more carefully before replying. Your problem is not with the algorithm, but with source code structure and code flow. Once again, in pseudocode:
    Java Code:
            main method:
                    get number of units
                    get unit price
                    call computeBasePrice method
                    print out base price
                    get fitting type
                    get installation (Yes/No)
                    call computeFinalPrice method
                    print out final price
    
            computeBasePrice method:
                    return numberOfUnits * unitPrice
    
            computeFinalPrice method
                    // pretty much the pseudocode from my other post
    Hope that helps.

    -Gary-

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Some method declarations to get you started:
    Java Code:
            public int computeBasePrice(int quantity, int unitPrice) {
                    // TODO write this part
            }
    Java Code:
            public int computeFinalPrice(int basePrice, String fittings, boolean installed) {
                    // TODO write this part
            }
    Since we don't really want to make our methods static, consider something like this in main():
    Java Code:
            public static void main(String[] args) {
                    Kitchen kitchen = new Kitchen();
                    // TODO code to get quantity and unit price
                    int basePrice = kitchen.computeBasePrice(quantity, unitPrice);
                    // TODO code to print base price
                    // TODO code to get fittings and install option
                    int finalPrice = kitchen.computeFinalPrice(basePrice, fittings, installed);
                    // TODO code to print final price
            }
    Hope that helps.

    -Gary-

  6. #6
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Testing arguments
    By and0rsk in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 12:17 PM
  3. Replies: 8
    Last Post: 09-23-2010, 01:07 AM
  4. Passign arguments
    By ninjalord918 in forum AWT / Swing
    Replies: 4
    Last Post: 08-04-2010, 12:11 AM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •