Created
November 10, 2012 17:07
-
-
Save ah052070/4051737 to your computer and use it in GitHub Desktop.
Employee program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
//********************************************************** | |
// Assignment: OOP | |
// Username: amh11u | |
// Author: Alexander Hunting | |
// Creation date: 11/2/11 | |
// Completion time: 8 | |
// | |
// Honor Code: I pledge that this program represents my own program code. I worked with | |
// Terrence and Chris in designing and debugging my | |
// program. | |
//********************************************************* | |
public class amh11uTestDrive_A5 { | |
private static ArrayList<amh11uEmployee_A5> list = new ArrayList<amh11uEmployee_A5>(); | |
private static Scanner scan = new Scanner(System.in); | |
//Program Execution | |
public static void main(String[] args) { | |
//the while loop simply looks at a boolean value and if the value is true, it will run the inner block, if false it moves on | |
//This can be done by doing something like while(i>5) or simply placing while(true) | |
//Note that to exit the loop we have to call "return" (line 113) | |
while(true) { | |
System.out.println("Please indicate the type of employee by typing its number: "); | |
System.out.println("1 - Wage Worker"); | |
System.out.println("2 - Executive"); | |
System.out.println("3 - Exit"); | |
//Read | |
String input = scan.nextLine(); | |
if (input.equals("1")) { | |
//initialize name, wages | |
String name; | |
int hours, wages; | |
//Prompt and receive input for name | |
System.out.print("Enter employee name: "); | |
name = scan.nextLine(); | |
//Prompt and receive input for hours | |
System.out.print("Enter amount of hours: "); | |
//Since the scanner reads in a String and the amh11uEmployee_A5 class calls for an integer, we need to cast the input to an integer | |
//However what if the user inputs a non numeric such as the word "hello". During the casting to integer, the program crashes | |
//We use a try/catch block to look out and fix such a thing. | |
try{ | |
hours = Integer.parseInt(scan.nextLine()); | |
} catch (Exception e) { | |
//An exception is a generated error, so if an error is drawn, we disregard what the user inputed and | |
// set it to a default. I have chosen 0. | |
hours = 0; | |
} | |
//End try/catch | |
System.out.print("Enter employee wages: "); | |
//The same goes for wages, a user can type in "hello", which is legal for the scanner however "hello" cannot be converted to int | |
//bregin try block | |
try{ | |
wages = Integer.parseInt(scan.nextLine()); | |
} catch(Exception e) // End try block, start catch statement | |
{ | |
wages = 0; //Called only if an exception is thrown | |
} | |
//Since ArrayList type is amh11uEmployee_A5 any elements added need to be type amh11uEmployee_A5. | |
//We can legally call amh11uWageWorker_A5 because it extends the amh11uEmployee_A5 class | |
//See amh11uWageWorker_A5 file for understanding why this works | |
list.add(new amh11uWageWorker_A5(name, hours, wages)); | |
} else if (input.equals("2")) { | |
//Initialize name and salary | |
String name; | |
int salary; | |
//Prompt and receive employee name | |
System.out.print("Enter employee name: "); | |
name = scan.nextLine(); | |
//Like before we need to convert the String to an integer so a try/catch block is used. | |
System.out.print("Enter employee salary: "); | |
//Start try block | |
try{ | |
salary = Integer.parseInt(scan.nextLine()); | |
} catch (Exception e) //End try block, start catch statement only if exception is thrown | |
{ | |
salary = 0; //If exception is thrown, salary will be set to 0 | |
} | |
//Prompt and receive employee title | |
System.out.print("Enter employee title: "); | |
String title = scan.nextLine(); | |
//Like amh11uWageWorker_A5 we need to add to the ArrayList (type amh11uEmployee_A5) | |
list.add(new amh11uExecutive_A5(name, salary, title)); | |
} else if (input.equals("3")) { | |
//Sets i to 0 and iterates until it reaches size of ArrayList | |
for(int i = 0; i < list.size(); i++) | |
{ | |
//Set up a temp variable to hold the current value. This doesn't have to be done, we can simply call list.get(i) each time but it becomes time consuming typing it out each time. | |
amh11uEmployee_A5 temp = list.get(i); | |
//Checks whether the type of temp is extended to be an Excutive or amh11uWageWorker_A5 | |
if(temp instanceof amh11uWageWorker_A5) | |
{ //If temp is type amh11uWageWorker_A5, we use the toString method that was written in class amh11uEmployee_A5 | |
System.out.println(temp.toString()); | |
} | |
else //Else (must be type amh11uExcecutive_A5, but a simple check never hurts anything | |
if(temp instanceof amh11uExecutive_A5) | |
{ | |
//When amh11uExcecutive_A5 is created we need an extra field (title) extended off of amh11uEmployee_A5 | |
//We cast the temp variable t | |
amh11uExecutive_A5 e = (amh11uExecutive_A5) temp; | |
System.out.println(e.toString()); | |
} | |
} | |
return; //This is called after the user enters 3 and the employee list is printed | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class amh11uEmployee_A5 { | |
private int salary; | |
private String name; | |
public amh11uEmployee_A5(String name, int salary) | |
{ | |
this.name = name; | |
this.salary = salary; | |
} | |
//Returns name | |
public String getName() | |
{ | |
return name; | |
} | |
//Returns salary | |
public int getSalary() | |
{ | |
return salary; | |
} | |
//This is a method that we will use in the actual tester. When called, it will automatically write the info out. | |
public String toString() | |
{ | |
return ("Employee name: " + getName() + "\tEmployee salary: " + getSalary()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class amh11uExecutive_A5 extends amh11uEmployee_A5 { | |
private String title; | |
//Since we need to add a title variable to amh11uEmployee_A5, this is where extends truely comes into play | |
public amh11uExecutive_A5(String name, int salary, String title) | |
{ | |
super(name, salary); //Calls the amh11uEmployee_A5 constructor and sets the variables | |
this.title = title; //By adding this we have extended amh11uEmployee_A5 | |
} | |
public String getTitle() | |
{ | |
return title; | |
} | |
//Just like the amh11uEmployee_A5 class toString method, it will be called in the tester class. However, since we have a title variable to call | |
//we need to add it to the output. | |
//What's happening is simply it calls for the amh11uEmployee_A5 toString method and adds onto it. | |
public String toString() | |
{ | |
return super.toString() + "\tEmployee title: " + title; | |
} | |
}//End class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class amh11uWageWorker_A5 extends amh11uEmployee_A5{ | |
String name; | |
private int hours, wages; | |
public amh11uWageWorker_A5(String name, int hours, int wages) | |
{ | |
super(name, hours*wages); | |
//super() is calling the amh11uEmployee_A5 constructor and setting the variables name and salary... salary=hours*wages | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment