I'm new to Java and I'm writing a sample code wherein a user inputs employee info (name, position and monthly salary--which also calculates annual salary by doing monsal * 12). My set constructors work fine for the employee's name, but It won't give me the numbers. IT always shows up as 0.0 Also, employee2's name won't take input. Hereis my code.
Employee.java
//Jeremiah A. Walker //Create a class called Employee that includes three instance variables--Name(String), Position(String) and //Monthly Salary(double) public class Employee { private String name; //string name. I prefer to use a single name field as opposed to two or three. private String position;//position in the company; their job private double monsal;// the employee's monthly salary private double anusal;//annual salary private double raise;//10% raise to monthly salary //Sets the employee's name for us public void setname(String empname) { name = empname; }//end setname //returns name value for us to use public String getname() { return name; }//end getname //sets position for us public void setposition(String emppos) { position = emppos; }//end setposition //returns position value for us to use public String getposition() { return position; }//end getposition //set monthly salary for us. If monthly salary is negative or zero, it will default to zero public double setmonsal(double msal) { if (monsal > 0.0) monsal = msal; return monsal; }//end setmonsal //returns monthly salary for us public double getmonsal() { return monsal; }//end getmonsal //Multiplies monsal by 12 to give us anusal(annual salary) public double setanusal(double asal) { asal = monsal * 12; anusal = asal; return anusal; }//end setanusal //returns the value so we can use it. public double getanusal() { return anusal; }//end getanusal }//end class Employee
EmployeeTest.Java
//Jeremiah A. Walker //EmployeeTest-- Test application for employee class import java.util.*; public class EmployeeTest { //main method public static void main(String[] args) { Employee Employee1 = new Employee(); Employee Employee2 = new Employee(); //Scanner to get input from user Scanner input = new Scanner(System.in); String name; //employee's name String position;//employee's actual job double monsal; // employee's monthly pay double anu;// annual salary double Empraise1; double Empraise2; //Asks the user to input name for employee1 System.out.print("Enter name for employee1: ");//prompt name = input.nextLine();//user input for name System.out.print("Name entered was " + name + ".\n\n");//message to user that input was recieved Employee1.setname(name);//sets name for employee1 //Asks the user to input position for employee1 System.out.print("Enter position for employee1: ");//prompt position = input.nextLine(); //user input for position System.out.print("Position entered for " + name + " was " + position + ".\n\n");//message to user that input was recieved Employee1.setposition(position); //sets position for employee1 //Asks the user to input monthly salary for employee1 System.out.print("Enter monthly salary for employee1: ");//prompt monsal = input.nextDouble();//user input for monthly salary System.out.print("Monthly Salary entered for " + name + " was " + "$" + monsal + ".\n\n" ); //message to user that input was recieved Employee1.setmonsal(monsal);//sets monthly salary for employee1 Employee1.getanusal(); Empraise1 = Employee1.getmonsal() * 0.1 + Employee1.getmonsal(); //Asks the user to input name for employee2 System.out.print("Enter name for employee2: ");//prompt name = input.nextLine();//user input for name System.out.print("Name entered was " + name + ".\n\n");//message to user that input was recieved Employee2.setname(name);//sets name for employee1 //Asks the user to input position for employee2 System.out.print("Enter position for employee2: ");//prompt position = input.nextLine(); //user input for position System.out.print("Position entered for " + name + "was " + position + ".\n\n");//message to user$ Employee2.setposition(position); //sets position for employee1 //Asks the user to input monthly salary for employee2 System.out.print("Enter monthly salary for employee2: ");//prompt monsal = input.nextDouble();//user input for monthly salary System.out.print("Monthly Salary entered for " + name + " was " + "$" + monsal + ".\n\n" ); //mes$ Employee2.setmonsal(monsal);//sets monthly salary for employee1 Employee2.getanusal(); Empraise2 = Employee2.getmonsal() * 0.1 + Employee2.getmonsal(); //displays information we put in System.out.print("Employee1: " + Employee1.getname() + "\n" ); System.out.print("Position: " + Employee1.getposition() + "\n" ); System.out.print("Monthly Salary: " + Employee1.getmonsal() + "\n" ); System.out.print("Annual Salary: " + Employee1.getanusal() + "\n" ); System.out.print("Monthly Salary + 10% raise: " + Empraise1 + "\n" ); System.out.print("Employee2: " + Employee2.getname() + "\n" ); System.out.print("Position: " + Employee2.getposition() + "\n" ); System.out.print("Monthly Salary: " + Employee2.getmonsal() + "\n" ); System.out.print("Annual Salary: " + Employee2.getanusal() + "\n" ); System.out.print("Monthly Salary + 10% raise: " + Empraise2 + "\n" ); } }//end class
Output
Enter name for employee1: Jor-El
Name entered was Jor-El.
Enter position for employee1: CEO
Position entered for Jor-El was CEO.
Enter monthly salary for employee1: 100,000
Monthly Salary entered for Jor-El was $100000.0.
Enter name for employee2: Name entered was . [It gives me the prompt, but it doesn't let me input]
Enter position for employee2: CTO
Position entered for was CTO.
Enter monthly salary for employee2: 10203.23
Monthly Salary entered for was $10203.23.
Employee1: Jor-El
Position: CEO
Monthly Salary: 0.0
Annual Salary: 0.0
Monthly Salary + 10% raise: 0.0
Employee2:
Position: CTO
Monthly Salary: 0.0
Annual Salary: 0.0
Monthly Salary + 10% raise: 0.0
I don't know what's going on. Hoping you all can help. Thanks a lot
~J. Walker