Skip to content

Commit 9240d15

Browse files
committed
All project in java done for the semester!
1 parent 442573e commit 9240d15

File tree

8 files changed

+575
-70
lines changed

8 files changed

+575
-70
lines changed

conditionals2/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
/lab5.class
1313
/proj5.class
1414
/test_kevin.class
15+
/Payroll.class
16+
/PayrollApp.class

conditionals2/Payroll.java

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import java.text.DecimalFormat;
2+
3+
/**
4+
This file provided to the student for modification
5+
The Payroll class stores data about an employee's pay
6+
*/
7+
8+
public class Payroll
9+
{
10+
private String name; // Employee name
11+
private int idNumber; // ID number
12+
private double payRate; // Hourly pay rate
13+
private double hoursWorked; // Number of hours worked
14+
15+
/**
16+
The constructor initializes an object with the
17+
employee's name and ID number.
18+
@param n The employee's name.
19+
@param i The employee's ID number.
20+
*/
21+
public Payroll(String n, int i)
22+
{
23+
name = n;
24+
idNumber = i;
25+
}
26+
27+
/*************************************************************************************************************
28+
* Description: Contructor to create a blank student for the user to add information into
29+
*
30+
* Parameters: none
31+
*
32+
* Return: none
33+
**************************************************************************************************************/
34+
public Payroll()
35+
{
36+
name = "";
37+
idNumber = 0000000;
38+
payRate = 00.00;
39+
hoursWorked = 0;
40+
}
41+
42+
/**
43+
The setName sets the employee's name.
44+
@param n The employee's name.
45+
*/
46+
public void setName(String n)
47+
{
48+
name = n;
49+
}
50+
51+
/**
52+
The setIdNumber sets the employee's ID number.
53+
@param i The employee's ID number.
54+
*/
55+
public void setIdNumber(int i)
56+
{
57+
58+
idNumber = i;
59+
//TODO error checking char or double
60+
}
61+
62+
/**
63+
The setPayRate sets the employee's pay rate.
64+
@param p The employee's pay rate.
65+
*/
66+
public void setPayRate(double p)
67+
{
68+
payRate = p;
69+
//TODO error checking char
70+
}
71+
72+
/**
73+
The setHoursWorked sets the number of hours worked.
74+
@param h The number of hours worked.
75+
*/
76+
public void setHoursWorked(double h)
77+
{
78+
hoursWorked = h;
79+
//TODO error checking char
80+
}
81+
82+
/**
83+
The getName returns the employee's name.
84+
@return The employee's name.
85+
*/
86+
public String getName()
87+
{
88+
return name;
89+
}
90+
91+
/**
92+
The getIdNumber returns the employee's ID number.
93+
@return The employee's ID number.
94+
*/
95+
public int getIdNumber()
96+
{
97+
return idNumber;
98+
}
99+
100+
/**
101+
The getPayRate returns the employee's pay rate.
102+
@return The employee's pay rate.
103+
*/
104+
public double getPayRate()
105+
{
106+
return payRate;
107+
}
108+
109+
/**
110+
The getHoursWorked returns the hours worked by the
111+
employee.
112+
@return The hours worked.
113+
*/
114+
public double getHoursWorked()
115+
{
116+
return hoursWorked;
117+
}
118+
119+
/**
120+
The getGrossPay returns the employee's gross pay.
121+
@return The employee's gross pay.
122+
*/
123+
public double getGrossPay()
124+
{
125+
return hoursWorked * payRate;
126+
}
127+
128+
/*************************************************************************************************************
129+
* Description: return all the information about and object in a string to be outputted to the screen
130+
*
131+
* Parameters: none
132+
*
133+
* Return:
134+
* temp - used to gather all the information about an object to be returned
135+
**************************************************************************************************************/
136+
public String toString()
137+
{
138+
String temp = "";
139+
DecimalFormat df = new DecimalFormat("#0.00"); // format for displaying final value
140+
141+
temp = "Employee Name: " + name + "\nID Number: " + idNumber + "\nPay: $" + df.format(getGrossPay()); // + "\n"
142+
return temp;
143+
144+
}
145+
} // end class
146+

conditionals2/PayrollApp.java

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import java.util.*;
2+
3+
public class PayrollApp {
4+
5+
private static ArrayList<Payroll> list = new ArrayList<Payroll>();
6+
7+
8+
/*************************************************************************************************************
9+
* Gets information for the Payroll object then displays them afterwards allows the user to remove one then
10+
* add another one, then display the resulting list
11+
*
12+
* Jacob Dokos
13+
*
14+
* Project 9
15+
**************************************************************************************************************/
16+
public static void main(String[] args) {
17+
18+
Scanner s = new Scanner(System.in);
19+
boolean rerunprog = false;
20+
21+
do
22+
{
23+
addToList(); // method to add to array
24+
String struserin = " ";
25+
26+
//gets input from user if they want to continue program
27+
System.out.print("\nRun program again? (y/n) ");
28+
struserin = s.nextLine();
29+
30+
if (struserin.charAt(0) == 'Y' || struserin.charAt(0) == 'y')
31+
{
32+
rerunprog = true;
33+
System.out.print("\n");
34+
}
35+
else if (struserin.charAt(0) == 'N' || struserin.charAt(0) == 'n')
36+
rerunprog = false;
37+
38+
39+
} while (rerunprog);
40+
41+
42+
//display all the employees
43+
displayList();
44+
45+
String temp = "";
46+
47+
boolean validID = false;
48+
do
49+
{
50+
System.out.print("Please enter an ID number: ");
51+
temp = s.nextLine();
52+
//if you find the ID # then remove it
53+
for (int i = 0; i < list.size(); i++)
54+
{
55+
if (list.get(i).getIdNumber() == Integer.parseInt(temp) )
56+
{
57+
list.remove(i);
58+
validID = true;
59+
}
60+
}
61+
if (!validID)
62+
System.out.println("Please enter a valid ID number");
63+
64+
} while (!validID);
65+
//get input from user
66+
67+
68+
//add in one more employee then display them all
69+
addToList();
70+
displayList();
71+
}
72+
73+
/*************************************************************************************************************
74+
* Description: Used to display all the objects in the arraylist to the screen
75+
*
76+
* Parameters: none
77+
*
78+
* Return: none
79+
**************************************************************************************************************/
80+
public static void displayList()
81+
{
82+
for (int i = 0; i < list.size(); i++)
83+
{
84+
System.out.println(list.get(i).toString());
85+
System.out.println();
86+
}
87+
88+
89+
}
90+
91+
/*************************************************************************************************************
92+
* Description: Gets input from the user to add to the array list.
93+
*
94+
* Parameters: none
95+
*
96+
* Return: none
97+
**************************************************************************************************************/
98+
public static void addToList()
99+
{
100+
Payroll temp = new Payroll();
101+
Scanner s = new Scanner(System.in);
102+
boolean success = false;
103+
while (!success)
104+
{
105+
System.out.print("What is the employees name? ");
106+
try
107+
{
108+
temp.setName(s.nextLine());
109+
if (temp.getName().length() == 0)
110+
throw new Exception();
111+
success = true;
112+
}
113+
catch (Exception e)
114+
{
115+
System.out.println("You must enter a valid name, please try again. ");
116+
117+
}
118+
}
119+
success = false;
120+
while (!success)
121+
{
122+
System.out.print("What is the employees ID? ");
123+
try
124+
{
125+
temp.setIdNumber(Integer.parseInt(s.nextLine()));
126+
success = true;
127+
128+
}
129+
catch (ClassCastException f)
130+
{
131+
System.out.println("Invalid character, please try again. ");
132+
}
133+
catch (NumberFormatException e)
134+
{
135+
System.out.println("Invalid character, please try again. ");
136+
}
137+
}
138+
success = false;
139+
while (!success)
140+
{
141+
System.out.print("What is the employees Pay Rate? ");
142+
try
143+
{
144+
temp.setPayRate(Double.parseDouble(s.nextLine()));
145+
success = true;
146+
147+
}
148+
catch (NumberFormatException e)
149+
{
150+
System.out.println("Invalid character, please try again. ");
151+
}
152+
}
153+
success = false;
154+
while (!success)
155+
{
156+
System.out.print("What is the employees hours worked? ");
157+
try
158+
{
159+
temp.setHoursWorked(Double.parseDouble(s.nextLine()));
160+
success = true;
161+
162+
}
163+
catch (NumberFormatException e)
164+
{
165+
System.out.println("Invalid character, please try again. ");
166+
}
167+
}
168+
169+
list.add(temp);
170+
171+
}
172+
173+
}

0 commit comments

Comments
 (0)