I'm a beginner at java, and I've been given a project to write a simple java application that calculates the weekly Net Pay of a certain worker. Now I have written the code but in one class, I need to make this code based on the MVC architecture, can someone help?
Write an application that computes a worker’s weekly net pay. The inputs are the worker’s name, hours worked for the week, and hourly payrate. The output is the worker’s name and pay. The pay is computed as follows:
- gross pay is hours worked multiplied by payrate. A bonus of 50% is paid for each hour over 40 that was worked.
- a payroll tax of 22% is deducted from the gross pay
This is the code:
import javax.swing.*;
import java.text.*;
public class workersNetPay {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("Write the name of the worker:");
String hoursWorked = JOptionPane.showInputDialog("Write the hours worked during the week:");
double hW = new Double(hoursWorked).doubleValue();
String hourlyPayRate = JOptionPane.showInputDialog("Write the hourly pay rate:");
double hpr = new Double(hourlyPayRate).doubleValue();
double tax = 0.22;
double grossPay = (hW*hpr)-((hW*hpr)*tax);
double bonus = (grossPay * 0.5)+grossPay;
String euro = "\u20AC";
DecimalFormat formatter = new DecimalFormat("0.00");
if (hW > 40) {
JOptionPane.showMessageDialog(null, "Sir " + name + " your wage for this week is: " + formatter.format(bonus)+ euro);
} else {
JOptionPane.showMessageDialog(null,"Sir " + name + " your wage for this week is: " + formatter.format(grossPay)+ euro);
}
}
}
EDIT: Now I've written the model and the view but can someone help me with the controller
THE MODEL
public class Model {
private double workersNetPay;
public void calculate(double hoursWorked, double hourlyPayRate, double tax, double bonus, double grossPay) {
tax = 0.22;
if (hoursWorked> 40) {
JOptionPane.showMessageDialog(null, bonus = (grossPay * 0.5) + grossPay);
} else {
JOptionPane.showMessageDialog(null,grossPay = (hoursWorked* hourlyPayRate)-((hoursWorked*hourlyPayRate)-tax));
}
}
public double getWorkersNetPay() {
return workersNetPay;
}
}
THE VIEW
import java.awt.*;
import javax.swing.*;
public class view {
public String name() {
String name = JOptionPane.showInputDialog("Write your name");
return name;
}
public double hours() {
double h ;
String hW = JOptionPane.showInputDialog("Write the hours worked during the week:");
if (hW.matches("\\d+")) {
h = Double.parseDouble(hW);
} else {
throw new IllegalArgumentException("You should write a number:");
}
return h;
}
public double hourlyPay() {
double hp;
String hpr = JOptionPane.showInputDialog("Write the hourly pay rate:");
if (hpr.matches("\\d+")) {
hp = Double.parseDouble(hpr);
} else {
throw new IllegalArgumentException("You should write a number:");
}
return hp;
}
public void theResult(duoble result) {
JOptionPane.showMessageDialog(null, result, "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
