# Help: Methods and Arguments

• 12-11-2010, 11:23 PM
whateverme
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

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.
• 12-12-2010, 12:22 AM
gcalvin
So this is the algorithm in pseudocode, right?
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-
• 12-12-2010, 12:30 AM
whateverme
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.
• 12-12-2010, 12:30 AM
gcalvin
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:
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-
• 12-12-2010, 12:38 AM
gcalvin
Some method declarations to get you started:
Code:

```        public int computeBasePrice(int quantity, int unitPrice) {                 // TODO write this part         }```
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():
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-
• 12-12-2010, 01:14 AM
whateverme
Thank you very much, Gary.
Will try to do it now.