# Thread: Help: Methods and Arguments

1. Member
Join Date
Oct 2010
Posts
51
Rep Power
0

## 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. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
5
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
if installed
price += 1000```
See if you can write that in Java, and let us know what problems you have.

-Gary-

3. Member
Join Date
Oct 2010
Posts
51
Rep Power
0
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. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
5
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. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
5
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. Member
Join Date
Oct 2010
Posts
51
Rep Power
0
Thank you very much, Gary.
Will try to do it now.

#### Posting Permissions

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