Reputation: 1
I'm a beginner in java programming and I'm not so familiar with java oop. I am able to do it without java oop however the project requires me to use oop. I have to create a salon system that calculates the subtotal based on services and products bought from the salon.
This is what i have done for now:
public class main1
{
int servCode, prodCode, servQuantity, prodQuantity;
double servPrice, prodPrice, subtotal, tax, total;
String servName, prodName;
public void menu()
{
System.out.println(" PURPLE HAIR SALON ");
System.out.println("\t -------------------------------------------------------");
System.out.println();
System.out.println("\t SERVICE CODE \tSERVICES \tPRICE");
System.out.println("\t 101 \tHair Cut \t$ 35");
System.out.println("\t 102 \tHair Wash \t$ 25");
System.out.println("\t 103 \tHair Coloring \t$ 65");
System.out.println("\t 104 \tHair Treatment \t$ 58");
System.out.println("\t 105 \tDigital Perm \t$ 108");
System.out.println("\t 106 \tIon Perm \t$ 95");
System.out.println("\t 107 \tRebonding \t$ 88");
System.out.println();
System.out.println("\t PRODUCT CODE \tPRODUCTS \tPRICE");
System.out.println("\t 201 \tShampoo \t$ 18");
System.out.println("\t 202 \tConditioner \t$ 17");
System.out.println("\t 203 \tHair Mask \t$ 28");
System.out.println();
System.out.println("\t -------------------------------------------------------");
}
public void setServCode (double code1)
{
code1 = servCode;
}
public void setProdCode (double code2)
{
code2 = prodCode;
}
public void ServCode (double code1)
{
if (code1 == 101)
servPrice = 35;
else if (code1 == 102)
servPrice = 25;
else if (code1 == 103)
servPrice = 65;
else if (code1 == 104)
servPrice = 58;
else if (code1 == 105)
servPrice = 108;
else if (code1 == 106)
servPrice = 95;
else if (code1 == 107)
servPrice = 88;
else
System.out.println("Invalid Service Code");
}
public void ProdCode (double code2)
{
if (code2 == 201)
prodPrice = 18;
else if (code2 == 202)
prodPrice = 17;
else if (code2 == 203)
prodPrice = 28;
else
System.out.println("Invalid Product Code");
}
public int getServCode()
{
return servCode;
}
public int getProdCode()
{
return prodCode;
}
public void setServQuantity (int quantity1)
{
servQuantity = quantity1;
}
public void setProdQuantity (int quantity2)
{
prodQuantity = quantity2;
}
public int getServQuanity()
{
return servQuantity;
}
public int getProdQuantity()
{
return prodQuantity;
}
public void setSubtotal (double subtotal)
{
this.subtotal = subtotal;
}
public void setTax (double tax)
{
this.tax = tax;
}
public void setTotal (double total)
{
this.total = total;
}
public void Subtotal (double subtotal)
{
subtotal = (servPrice * servQuantity) + (prodPrice * prodQuantity);
}
public double getSubtotal()
{
return subtotal;
}
public double getTax()
{
tax = 0.06 * subtotal;
return tax;
}
public double getTotal()
{
total = subtotal + tax;
return total;
}
}
import java.util.*;
public class main1demo
{
public static void main (String [] args)
{
Scanner sc = new Scanner (System.in);
int servCode, prodCode, servQuantity, prodQuantity, o;
String servName, prodName;
double servPrice, prodPrice, subtotal = 0, tax = 0, total = 0;
main1 obj = new main1();
obj.menu();
System.out.println();
System.out.print("\tENTER SERVICE CODE : ");
servCode = sc.nextInt();
System.out.println();
System.out.print("\tENTER QUANTITY : ");
servQuantity = sc.nextInt();
System.out.println();
System.out.print("\tENTER PRODUCT CODE : ");
prodCode = sc.nextInt();
System.out.println();
System.out.print("\tENTER QUANTITY : ");
prodQuantity = sc.nextInt();
System.out.println();
System.out.println("\tSUBTOTAL : $ " + obj.getSubtotal());
System.out.println("\tTAX : $ " + obj.getTax());
System.out.println("\tTOTAL : $ " + obj.getTotal());
}
}
I want to find out the subtotal, tax and total but the output for them displayed as 0. This is what the output is like :
SUBTOTAL : $ 0.0 TAX : $ 0.0 TOTAL : $ 0.0
I hope you can help me solve this problem. Thank you.
Upvotes: 0
Views: 390
Reputation: 9427
You have an obvious problem in your setters:
public void setServCode (double code1)
{
code1 = servCode;
}
code1 is your local variable, you don't actually update the value of your instance variables.
An assignment in Java goes as follows:
variable = value;
So, you need to alter your setters as such:
public void setServCode (double code1)
{
servCode = code1;
}
EDIT:
A second issue, just because you have two variables (in different classes) with the same name, doesn't make them the same variable.
In your main method, you'll need to change the following:
servCode = sc.nextInt();
to:
obj.setServCode(sc.nextInt());
and remove the local servCode, ... variables in your main method. The same goes for the other variables.
Upvotes: 1