4.The "Clear" Button should erase the contents of all the fields and the textArea. It should also hide the car related fields and labels, as shown in Figure 2. 5.The "Exit" button should close the application by simply calling System.exit(0);
4.The "Clear" Button should erase the contents of all the fields and the textArea. It should also hide the car related fields and labels, as shown in Figure 2.
5.The "Exit" button should close the application by simply calling System.exit(0);
Here are the classes:
EmployeeController class:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
public class EmployeeController {
@FXML
private Label NameEM;
@FXML
private Label MakerEM;
@FXML
private Label PriceEM;
@FXML
private Button handlehascar;
@FXML
private Button AddButton;
@FXML
private TextArea TextAreaBox;
}
EmployeeApp Class:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class EmployeeApp extends Application {
@Override
public void start(Stage stage) throws Exception{
Pane root = FXMLLoader.load(getClass().getResource("/view/EmployeeView.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("Employee App");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
EmployeeController class:
package controller;
public class EmployeeController {
}
Automobile Class:
package model;
public class Automobile {
private String maker;
private double price;
private double mileage;
private static int count;
public Automobile(String maker, double price) {
this.maker = maker;
setPrice(price);
this.mileage=400000-0.7*this.price;//mileage inversely proportional to the price.
count++;
}
public String getMaker() {
return maker;
}
public void setMaker(String maker) {
this.maker = maker;
}
public void move(double averageSpeed, double hours){
double drivenMiles=hours*averageSpeed;
this.mileage += drivenMiles;
if(this.price-0.25*drivenMiles > 4500)
this.price -= 0.25*drivenMiles;
}
public double getMileage() {
return mileage;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = (price > 4600 && price <= 349000)? price: 4600;
}
public static int getCount() {
return count;
}
public String getCondition() {
if (price < 16000)
return "Poor";
else if (price < 75000)
return "Good";
else
return "Excellent";
}
public String toString() {
return String.format("Make: %-10s Mileage: %,.1f km Price: QR%,.0f Condition: %-12s",
maker, mileage, price, getCondition());
}
}
Employee Class:
package model;
public class Employee {
private String name;
private Automobile vehicle;
public Employee(String name, Automobile vehicle) {
this.name = name;
this.vehicle = vehicle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Automobile getVehicle() {
return vehicle;
}
public void setVehicle(Automobile vehicle) {
this.vehicle = vehicle;
}
public boolean hasVehicle() {
return vehicle!=null;
}
public String toString() {
String str = String.format("Name: %s\n", name);
if(hasVehicle())
str+="Vehicle details:\n"+vehicle;
return str;
}
}
EmployeeRepo Class:
package repository;
import java.util.ArrayList;
import java.util.List;
import model.Automobile;
import model.Employee;
public class EmployeeRepo {
private static List<Employee> employees = new ArrayList<>();
public static List<Employee> getEmployees() {
return employees;
}
public static void addEmployee(Employee emp) {
if (emp != null)
employees.add(emp);
}
public static void initializeList() {
addEmployee(new Employee("Ahmed Taha", null));
addEmployee(new Employee("Nasri Khalifa", new Automobile("Ford", 2340)));
addEmployee(new Employee("Mohamed Othman", new Automobile("Nissan", 47500)));
addEmployee(new Employee("Hanan Ali", null));
addEmployee(new Employee("Khalid Ahmed", null));
addEmployee(new Employee("Suad Fahad", new Automobile("Ford", 67300)));
addEmployee(new Employee("Abdalla Ibrahim", new Automobile("Toyota", 54000)));
addEmployee(new Employee("Sulaiman Abdo", new Automobile("Toyota", 307500)));
addEmployee(new Employee("Sayed Khalifa", new Automobile("Chevy", 25750)));
addEmployee(new Employee("Sara Mansoor", new Automobile("Chevy", 36980)));
addEmployee(new Employee("Saad Khamis", new Automobile("Mercedes", 68500)));
}
}
EmployeeView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.EmployeeController">
</VBox>
You need edit EmployeeView and EmployeeController classes only.
Step by step
Solved in 2 steps