Question: I need help for java: It's a speeding ticket calculator. Here are the classes that needs to be created. I need to write for the
I need help for java: It's a speeding ticket calculator. Here are the classes that needs to be created.
I need to write for the data element class -ticket, and driverClass - ticketDriver. The other two is already given. The code given will be at the bottom.
Assigment description: The DMV has asked you to build an application that will calculate the fees for a speeding tickets and then print a notice that will be mailed to the violator.
Data Element Class Ticket
Student Created
Include private instance variables to store information needed for a ticket. Select the correct data types for each one. The speed and speed limit will be integers and that the school zone and work zone will be Boolean.
A constructor that takes the name, speed, speed limit, school zone and work zone.
Another constructor that takes the name, speed and speed limit. The school zone and work zone will be set to false.
Getters and setters for the private instance variables.
A public method calculateFine which calculates and returns the fine (as a double) based on the table in Assignment Details.
A public method printNotice which returns a String with the contents of the Notice. Make sure that your fine is formatted in decimal format. Please see the Assignment Details for exact wording/format. Use your name instead of Professor Kartchner.
A private method generateTicketNum which randomly generates a ticket number between 100000 and 999999 inclusively.
A private method generateCourtDate which randomly generates a day between 1 and 31 inclusively. All court dates will take place in October 2018.
A private method ticketType which returns a string of either Payable or Must Appear based on the table in Assignment Details.
A toString method that returns a string representation of a Ticket, including the ticket number and ticket type. No specific format required.
The method headers must match the Javadoc provided for you for the Ticket class.
Driver Class TicketDriver
Student created
This is the driver class for Ticket that contains a main method.
The driver is responsible to:
print a header
ask the user for information about a ticket.
Print out the notice for that ticket using the methods from the Ticket class.
Allow user to add another ticket.
When user is finished entering tickets, prints Exiting the Ticket Manager
Refer to the program sample run for more clarification.
Data Validation. The following data must be validated:
Speed > 0
Speed Limit >0 and <= 80
Add any necessary methods to modularize your code.
GUI Driver Class TicketGUIDriver
Provided
Uses methods of the ticket class
Use as a way to test your cacluateFine and printNotice methods
Introduction to a Graphical User Interface (GUI) driver
Include the dmv.jpg in the same package as your .java files for the image to display.
Test Class TicketTestDriver
Provided
This is the driver class for testing the methods of the Ticket class. This has been given to you to help you test the methods of your Ticket class.
TicketTestDriver.java:
import java.util.Scanner; /** * * This is a driver to test the Ticket class * @author Professor Kartchner */ public class TicketTestDriver { public static void main(String[] args) { Ticket ticket1, ticket2, ticket3; ticket1 = new Ticket("Bob Brown",36, 25, true, false); ticket2 = new Ticket("Betty Boop",66,45,false, true); ticket3 = new Ticket("Edward Cullen",88,45); //test the getName if(!"Bob Brown".equals(ticket1.getName())) print("getName Error 1"); if(!"Betty Boop".equals(ticket2.getName())) print("getName Error 2"); //test the getSpeed if(!(36==ticket1.getSpeed())) print("getSpeed Error 1"); if(!(66==ticket2.getSpeed())) print("getSpeed Error 2"); if(!(88==ticket3.getSpeed())) print("getSpeed Error 3"); //test the getSpeedLimit if(!(25==ticket1.getSpeedLimit())) print("getSpeedLimit Error 1"); if(!(45==ticket2.getSpeedLimit())) print("getSpeedLimit Error 2"); if(!(45==ticket3.getSpeedLimit())) print("getSpeedLimit Error 3"); //test the isSchoolZone if(!(ticket1.isSchoolZone()==true)) print("isSchoolZone Error 1"); if(!(ticket2.isSchoolZone()==false)) print("isSchoolZone Error 2"); if(!(ticket3.isSchoolZone()==false)) print("isSchoolZone Error 3"); //test the isWorkZone if(!(ticket1.isWorkZone()==false)) print("isWorkZone Error 1"); if(!(ticket2.isWorkZone()==true)) print("isWorkZone Error 2"); if(!(ticket3.isWorkZone()==false)) print("isWorkZone Error 3"); //test calculateFine if(!(310.0==ticket1.calculateFine())) print("calculateFine Error 1"); if(!(660.0==ticket2.calculateFine())) print("calculateFine Error 2"); if(!(675.0==ticket3.calculateFine())) print("calculateFine Error 3"); //test getTicketType if(!"PAYABLE".equals(ticket1.getTicketType())) print("getTicketType Error 1"); if(!"PAYABLE".equals(ticket2.getTicketType())) print("getTicketType Error 2"); if(!"MUST APPEAR".equals(ticket3.getTicketType())) print("getTicketType Error 3"); //test printNotice String result = ticket1.printNotice(); Scanner scan = new Scanner(result); if(!"Department of Motor Vehicles".equals(scan.nextLine())) print("printNotice Error 1"); scan.nextLine(); //Automated Traffic Enforcement scan.nextLine(); scan.nextLine(); //two blank lines if(!"Dear Bob Brown,".equals(scan.nextLine())) print("printNotice Error 2"); scan.nextLine(); // blank line if(!(scan.nextLine().contains("fine of $310.00"))) print("printNotice Error 3"); scan.nextLine();//receiving this notice String line = scan.nextLine(); if(!(line.contains("for going 36 MPH in a 25 MPH school zone"))) print("printNotice Error 4"); scan.nextLine(); //blank line line = scan.nextLine(); if(!(line.contains("Ticket Type: PAYABLE"))) print("printNotice Error 5"); scan.close(); //test toString String result1 = ticket1.toString(); if(!(result1.contains("Bob Brown"))) print("toString Error 1"); if(!(result1.contains("36"))) print("toString Error 2"); if(!(result1.contains("25"))) print("toString Error 3"); if(!(result1.contains("PAYABLE"))) print("toString Error 4"); //test setName ticket1.setName("Ronald Reagan"); if(!"Ronald Reagan".equals(ticket1.getName())) print("setName Error 1"); //test setSpeed ticket2.setSpeed(68); if(!(68==ticket2.getSpeed())) print("setSpeed Error 1"); //test setSpeedLimit ticket3.setSpeedLimit(50); if(!(50==ticket3.getSpeedLimit())) print("setSpeedLimit Error 1"); //test setSchoolZone ticket1.setSchoolZone(false); if(!(ticket1.isSchoolZone()==false)) print("setSchoolZone Error 1"); ticket2.setSchoolZone(true); if(!(ticket2.isSchoolZone()==true)) print("setSchoolZone Error 2"); //test setWorkZone ticket1.setWorkZone(true); if(!(ticket1.isWorkZone()==true)) print("setWorkZone Error 1"); ticket2.setWorkZone(false); if(!(ticket2.isWorkZone()==false)) print("setWorkZone Error 2"); print("Test complete"); } public static void print(String message) { System.out.println(message); } } TicketGUIDriver.java:
//package assignment2; /** * This program provides a GUI through which you can enter the members of a basketball team by name and position * @author modified from version by Professor Kartchner * @version 1.0 */ import java.text.NumberFormat; import javafx.application.Application; //import javafx.event.ActionEvent; //import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.*; //import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.*; import javafx.scene.text.Font; public class TicketGUIDriver extends Application{ private static TextField lastNameField, firstNameField, speedField, speedLimitField, fineField; private Label firstNameLabel, lastNameLabel, speedLabel, speedLimitLabel, fineLabel; private Button calculateFine, printNotice, exit, clearButton; private Ticket ticket; private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); private TextArea textArea; //private Image icon; private boolean isWorkZone, isSchoolZone; public static void main(String[] args){ launch(args); } public void start(Stage stage) { //labels firstNameLabel = new Label("First Name:"); firstNameLabel.setFont(new Font(16)); lastNameLabel = new Label("Last Name:"); lastNameLabel.setFont(new Font(16)); speedLabel = new Label("Speed traveling"); speedLabel.setFont(new Font(16)); speedLimitLabel = new Label("Speed Limit"); speedLimitLabel.setFont(new Font(16)); Insets insets = new Insets(10); VBox labels = new VBox(); VBox.setMargin(firstNameLabel, insets); VBox.setMargin(lastNameLabel, insets); VBox.setMargin(speedLabel, insets); VBox.setMargin(speedLimitLabel, insets); labels.setAlignment(Pos.CENTER_RIGHT); labels.getChildren().addAll(firstNameLabel, lastNameLabel,speedLabel, speedLimitLabel); //textfields firstNameField = new TextField(); lastNameField = new TextField(); speedField = new TextField(); speedLimitField = new TextField(); VBox fields = new VBox(); VBox.setMargin(firstNameField, insets); VBox.setMargin(lastNameField, insets); VBox.setMargin(speedField, insets); VBox.setMargin(speedLimitField, insets); fields.setAlignment(Pos.CENTER_LEFT); fields.getChildren().addAll(firstNameField, lastNameField, speedField, speedLimitField); //Labels and textfields HBox speedInfo = new HBox(); speedInfo.getChildren().addAll(labels, fields); //Work Zone Radio Buttons VBox workZoneBox = new VBox(); workZoneBox.setStyle("-fx-border-color: gray;"); workZoneBox.setPadding(new Insets(10,10,10,10)); Label positionLabel = new Label("Within a Work Zone?"); positionLabel.setFont(new Font(16)); String [] workZoneLabels = {"Yes", "No"}; ToggleGroup workZoneGroup = new ToggleGroup(); RadioButton [] workRadioButtons = new RadioButton[workZoneLabels.length]; for (int i=0; i < workZoneLabels.length; i++) { workRadioButtons[i] = new RadioButton(workZoneLabels[i]); workRadioButtons[i].setToggleGroup(workZoneGroup); workRadioButtons[i].setPadding(new Insets(10,10,10,10)); } workZoneBox.getChildren().add(positionLabel); workZoneBox.getChildren().addAll(workRadioButtons); //School Zone Radio Buttons VBox schoolZoneBox = new VBox(); schoolZoneBox.setStyle("-fx-border-color: gray;"); schoolZoneBox.setPadding(new Insets(10,10,10,10)); Label schoolPositionLabel = new Label("Within a School Zone?"); schoolPositionLabel.setFont(new Font(16)); String [] schoolZoneLabels = {"Yes", "No"}; ToggleGroup schoolZoneGroup = new ToggleGroup(); RadioButton [] schoolRadioButtons = new RadioButton[schoolZoneLabels.length]; for (int i=0; i < workZoneLabels.length; i++) { schoolRadioButtons[i] = new RadioButton(schoolZoneLabels[i]); schoolRadioButtons[i].setToggleGroup(schoolZoneGroup); schoolRadioButtons[i].setPadding(new Insets(10,10,10,10)); } schoolZoneBox.getChildren().add(schoolPositionLabel); schoolZoneBox.getChildren().addAll(schoolRadioButtons); HBox radioBox = new HBox(); radioBox.setStyle("-fx-border-color: gray;"); radioBox.setPadding(new Insets(10,10,10,10)); radioBox.getChildren().addAll(workZoneBox, schoolZoneBox); //Fine label and textfield fineLabel = new Label("Fine"); fineLabel.setFont(new Font(16)); fineField = new TextField(); HBox fineBox = new HBox(); fineBox.setStyle("-fx-border-color: gray;"); fineBox.setPadding(new Insets(10,10,10,10)); HBox.setMargin(fineLabel, insets); HBox.setMargin(fineField, insets); fineBox.getChildren().addAll(fineLabel, fineField); //Textarea for the notice VBox noticeBox = new VBox(); textArea = new TextArea(); textArea.setPrefColumnCount(30); textArea.setPrefRowCount(15); textArea.setPadding(new Insets(10,10,10,10)); VBox.setMargin(noticeBox, new Insets(10,10,10,10)); noticeBox.setAlignment(Pos.CENTER); noticeBox.getChildren().add(textArea); //buttons HBox buttonBox = new HBox(); buttonBox.setPadding(new Insets(10,10,10,10)); calculateFine = new Button("Calculate Fine"); HBox.setMargin(calculateFine, insets); calculateFine.setOnAction( event -> { //Gather information from textfields and radio buttons String firstName = firstNameField.getText(); String lastName = lastNameField.getText(); int speed = Integer.parseInt(speedField.getText()); int speedLimit = Integer.parseInt(speedLimitField.getText()); if (workRadioButtons[0].isSelected()) isWorkZone = true; else isWorkZone = false; if (schoolRadioButtons[0].isSelected()) isSchoolZone = true; else isSchoolZone = false; //create a Ticket object ticket = new Ticket(firstName+" "+lastName, speed, speedLimit, isWorkZone, isSchoolZone); //display the fine in the fine textfield double fine = ticket.calculateFine(); fineField.setText(currencyFormat.format(fine)); }); exit = new Button("Exit"); HBox.setMargin(exit, insets); exit.setOnAction( event -> { System.exit(0); }); clearButton = new Button("Clear"); HBox.setMargin(clearButton, insets); clearButton.setOnAction( event -> { //clear fields firstNameField.setText(""); lastNameField.setText(""); speedField.setText(""); speedLimitField.setText("");; textArea.setText(""); fineField.setText(""); }); printNotice = new Button("Print Notice"); HBox.setMargin(printNotice, insets); printNotice.setOnAction( event -> { //Gather information from textfields and radio buttons String firstName = firstNameField.getText(); String lastName = lastNameField.getText(); int speed = Integer.parseInt(speedField.getText()); int speedLimit = Integer.parseInt(speedLimitField.getText()); if (workRadioButtons[0].isSelected()) isWorkZone = true; else isWorkZone = false; if (schoolRadioButtons[0].isSelected()) isSchoolZone = true; else isSchoolZone = false; //Create ticket object ticket = new Ticket(firstName+" "+lastName, speed, speedLimit, isWorkZone, isSchoolZone); textArea.setText(ticket.printNotice()); }); buttonBox.getChildren().addAll(calculateFine, printNotice, clearButton, exit); //main panel VBox mainPanel = new VBox(); VBox.setMargin(speedInfo,insets); Label imageLabel = new Label("Department of Motor Vehicles"); imageLabel.setFont(new Font(16)); try{ //if image file can be found, set up image ImageView image = new ImageView("dmv.jpg"); image.setFitHeight(70); image.setFitWidth(100); HBox imageBox = new HBox(); imageBox.setAlignment(Pos.CENTER); HBox.setMargin(imageBox, new Insets(10,10,10,10)); HBox.setMargin(image, new Insets(10,10,10,10)); imageBox.getChildren().addAll(image,imageLabel); mainPanel.getChildren().add(imageBox); }catch (Exception e) { //if image file can't be found HBox imageLabelBox = new HBox(); imageLabelBox.setAlignment(Pos.CENTER); HBox.setMargin(imageLabelBox, new Insets(10,10,10,10)); imageLabelBox.getChildren().addAll(imageLabel); mainPanel.getChildren().add(imageLabelBox); } mainPanel.getChildren().addAll(speedInfo, radioBox, fineBox, buttonBox, textArea); Scene scene = new Scene(mainPanel); stage.setScene(scene); stage.setTitle("Speeding Ticket Utility"); stage.show(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
