Assignment: Write a Java program that can be used to calculate and notify violat
ID: 3905238 • Letter: A
Question
Assignment: Write a Java program that can be used to calculate and notify violators of the fines and/consequences for road traffic breaches as shown in the schedule below Non-Moving Violation No driver's license 25.00 $20.00 $20.00 10.00 * Expired tag No registration No insurance Moving Violation * Speeding violation or speeding in construction zone Miles over the Speed Limit o 1-5 MPH o 6-9 MPH o 10-14 MPH o 15-19 MPH o 20-29 MPH o 30 MPH or over Warning $130.00 S205.00 $405.00 $455.00 S605.00 and court appearance * Speeding in school zone or speeding within a toll facility o 1-5 MPH O 6-9 MPH $155.00 $255.00 $305.00 S405.00 $455.00 o 10 14 MPH o 15-19 MPH o 20- 29 MPH The violation notice may comprise of any of the following: One or more of non-moving violations alone Combination of non-moving violations, and one kind of moving violation for excess speeding, or speeding in construction zone, but not both.Explanation / Answer
public interface TrafficViolations {
String getNonMovingViolation(Integer option);
String getCostAccordingToMiles(String name,int miles);
}
import java.util.Scanner;
public class TestTrafficViolations implements TrafficViolations{
public static void main(String[] args) {
// TODO Auto-generated method stub
TestTrafficViolations testTrafficViolations=new TestTrafficViolations();
System.out.println("1.Non Moving Violations.");
System.out.println("2.Non Moving Violations and school zone violation.");
System.out.println("3.Non Moving Violations and construction zone violation.");
System.out.println("4.School zone or construction zone violation.");
Scanner sc=new Scanner(System.in);
int option=sc.nextInt();
int finalval =0;
switch(option){
case 1:
finalval = getNonMovingVal(testTrafficViolations, sc);
System.out.println("Total violation amount "+finalval);
case 2:
finalval = getNonMovingVal(testTrafficViolations, sc);
System.out.println("Enter speed exceded");
Integer speed=sc.nextInt();
String s="";
String out=testTrafficViolations.getCostAccordingToMiles("SchoolZone", speed);
try{
finalval+=Integer.parseInt(out);
}
catch(Exception e){
s="Warning";
}
System.out.println("Total violation amount "+finalval + " "+s);
case 3:
finalval = getNonMovingVal(testTrafficViolations, sc);
System.out.println("Enter speed exceded");
Integer speed2=sc.nextInt();
String s2="";
String out2=testTrafficViolations.getCostAccordingToMiles("constructionzone", speed2);
finalval+=Integer.parseInt(out2);
System.out.println("Total violation amount "+finalval);
case 4:
System.out.println("Enter zone and Speed");
System.out.println("1. Construction zone violation");
System.out.println("2. School zone violation");
String arr[]=sc.next().split(" ");
String out3;
String s3="";
if(arr[0].equals("1"))
{
out3=testTrafficViolations.getCostAccordingToMiles("constructionzone", Integer.parseInt(arr[1]));
finalval=Integer.parseInt(out3);
}
else
{
out3=testTrafficViolations.getCostAccordingToMiles("schoolZOne", Integer.parseInt(arr[1]));
try{
finalval+=Integer.parseInt(out3);
}
catch(Exception e){
s3="Warning";
}
}
System.out.println("Total violation amount "+finalval + " "+s3);
}
}
private static int getNonMovingVal(
TestTrafficViolations testTrafficViolations, Scanner sc) {
getNonData();
String[] vals=sc.next().split(" ");
int finalval=0;
for (String string : vals) {
String[] val=testTrafficViolations.getNonMovingViolation(Integer.parseInt(string)).split("-");
finalval+=Integer.parseInt(val[0]);
}
return finalval;
}
private static void getNonData() {
System.out.println("Enter violations by following options");
System.out.println("1. No drivers licence");
System.out.println("2. Expired Tag");
System.out.println("3. No registration");
System.out.println("4. No insurance");
}
@Override
public String getNonMovingViolation(Integer option) {
// TODO Auto-generated method stub
switch(option){
case 1:
return "25-No drivers licence";
case 2:
return "20-Expired Tag";
case 3:
return "20-No registration";
case 4:
return "10-No insurance";
}
return null;
}
@Override
public String getCostAccordingToMiles(String name, int miles) {
// TODO Auto-generated method stub
if(miles>0&&miles<=5){
if(name.equalsIgnoreCase("constructionzone"))
return "Warning";
return "155";
}
else if(miles>5&&miles<=9){
if(name.equalsIgnoreCase("constructionzone"))
return "130";
return "255";
}
else if(miles>9&&miles<=14){
if(name.equalsIgnoreCase("constructionzone"))
return "205";
return "305";
}
else if(miles>14&&miles<=19){
return "405";
}
else if(miles>19&&miles<=29){
return "355";
}
else{
if(name.equalsIgnoreCase("constructionzone"))
return "605-Court Appearance";
}
return "Not Supported Data";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.