Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have five different class: Menu.java , Competition.java , Dogs.java , Courses.java , and AppDriver.java. I have posted all classes separatly. I have a bunch

I have five different class:" Menu.java" ," Competition.java" , "Dogs.java" ," Courses.java" , and "AppDriver.java". I have posted all classes separatly. I have a bunch of errors and couldn't figure out what is going on, I need some expert to fix my homework. Thank you in advance.

//Menu.java

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; import javax.swing.JOptionPane;

public class Menu {

Competition comp; // Menu-------------------------------------------------- public void runMenu() { comp = new Competition(); String input = JOptionPane.showInputDialog("1. Print report " + "2. Add a Dog " + "3. Exit " + "Enter a choice: "); while (!input.equals("3")) { switch(input) { case "1": printReport(); break; case "2": addDog(); break; case "3": System.out.println("Good bye!"); System.exit(0); default: System.out.println("Invalid choice"); break; } input = JOptionPane.showInputDialog("1. Print report " + "2. Add a Dog " + "3. Exit " + "Enter a choice: "); } } public void printReport() { System.out.println("Choose the course:"); System.out.println("Enter G for Gamblers, J for Jumpers, or T for Titling"); String choice = scanner.nextLine(); if (choice.equalsIgnoreCase("G")) { for (Courses c : courses) { if (c.getName().equalsIgnoreCase("Gamblers")) { printReport('G', c.getMaxTime()); } } } else if (choice.equalsIgnoreCase("J")) { for (Courses c : courses) { if (c.getName().equalsIgnoreCase("Jumpers")) { printReport('J', c.getMaxTime()); } } } else if (choice.equalsIgnoreCase("T")) { for (Courses c : courses) { if (c.getName().equalsIgnoreCase("Titling")) { printReport('T', c.getMaxTime()); } } } else { System.out.println("Invalid choice"); } } public void addDog() { System.out .println("Enter ID: (1 digit followed by a letter, followed by 3 digits)"); String id = scanner.nextLine(); if (id.length() != 5) { System.out.println("Invalid ID"); return; } else { /** * Checking if id is in proper format */ if (!(Character.isDigit(id.charAt(0)) && Character.isLetter(id.charAt(1)) && Character.isDigit(id.charAt(2)) && Character.isDigit(id.charAt(3)) && Character.isDigit(id.charAt(4)))) { System.out.println("Invalid ID"); return; } } System.out.println("Enter dog name: "); String name = scanner.nextLine(); try { System.out.println("Enter running time"); double runTime = Double.parseDouble(scanner.nextLine()); if (runTime <= 0) { System.out.println("Invalid run time"); return; } System.out.println("Enter penalty: "); double penalty = Double.parseDouble(scanner.nextLine()); if (penalty < 0) { System.out.println("Invalid penalty"); return; } System.out.println("Enter course code: "); String code = scanner.nextLine(); if (code.equalsIgnoreCase("G") || code.equalsIgnoreCase("J") || code.equalsIgnoreCase("T")) { Dogs d = new Dogs(id, name, runTime, penalty, code.charAt(0)); dogs.add(d); /** * Defining a printwriter object to append data to the file */ PrintWriter writer = new PrintWriter(new FileOutputStream(dogsFile,true)); writer.append(" " + id + " " + name + " " + runTime + " " + penalty + " " + code); writer.close(); System.out.println("Dog added"); } } catch (NumberFormatException e) { System.out.println("Invalid input"); } catch (Exception e) { System.out.println("Some error occured"); return; } }

}

//Competition.java

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Scanner;

import javax.swing.JOptionPane;

public class Competition { private ArrayList dogs; private ArrayList courses; private File dogsFile; private File coursesFile; private Scanner scanner; // Constructor ------------------------------------------------- public Competition() { dogsFile = new File("c://temp/dogs.txt"); coursesFile = new File("c://temp/courses.txt"); dogs = new ArrayList(); courses = new ArrayList(); scanner = new Scanner(System.in); try { loadData(); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * method to load data from datafiles */ public void loadData() throws FileNotFoundException { Scanner fileScanner = new Scanner(coursesFile); while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); /** * Splitting the line by white spaces */ String[] fields = line.split(" "); String name = fields[0].trim(); double maxTime = Double.parseDouble(fields[1].trim()); /** * creating a new Courses instance */ Courses c = new Courses(name, maxTime); /** * Adding to the arraylist */ courses.add(c); } fileScanner = new Scanner(dogsFile); while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); /** * Splitting the line by white spaces */ String[] fields = line.split(" "); String id = fields[0].trim(); String name = fields[1].trim(); double runningTime = Double.parseDouble(fields[2].trim()); double penalty = Double.parseDouble(fields[3].trim()); char code = fields[4].trim().charAt(0); /** * creating a new Dogs instance */ Dogs d = new Dogs(id, name, runningTime, penalty, code); /** * Adding to the arraylist */ dogs.add(d); } } /** * method to print report this will prompt the user to select a course, and * then will call printReport(coursecode, maxtime) method, which will * calculate and print the statistics */ /** * this method will be called by the printReport() method by supplying * course code and maxtime. this will calculate and print the report of the * specified course */ public void printReport(char courseCode, double maxTime) { String courseName = ""; if (courseCode == 'G') { courseName = "Gamblers"; } else if (courseCode == 'J') { courseName = "Jumpers"; } else if (courseCode == 'T') { courseName = "Titling"; } System.out.println("Report\t" + courseName + "\tCourse Time: " + maxTime); Date date = new Date(); System.out.println("Date: " + new SimpleDateFormat("MMMMM dd,yyyy").format(date)); // System.out.printf("%10s %15s %15s %10s %10s %15s", "ID", "Name", "Running Time", "Penalty", "Total", "Over/Under Time"); /** * two Dogs objects to represent winning dog and the dog with most * penalties */ Dogs winningDog = null; Dogs dogWithMostPenalty = null; for (Dogs d : dogs) { char c = d.getCourseCode(); /** * Considering only those dogs who participated in this course */ if (Character.toUpperCase(c) == courseCode) { /** * Calculating the total time */ double totalTime = d.getRunningTime() + d.getPenalty(); if (winningDog == null) { winningDog = d; } else { /** * checking if the total time is less than that of the * currently assumed winning dog's if yes, assign current * dog as the winning dog */ if (totalTime < (winningDog.getRunningTime() + winningDog.getPenalty())) { winningDog = d; } } if (dogWithMostPenalty == null && d.getPenalty() > 0) { dogWithMostPenalty = d; } else { if (d.getPenalty() > dogWithMostPenalty.getPenalty()) { dogWithMostPenalty = d; } } double over_underTime = totalTime - maxTime; String overUnderTimeString = ""; if (over_underTime > 0) { /** * appending the + sign to the overtime string */ overUnderTimeString = "+" + String.format("%.2f", over_underTime); } else { overUnderTimeString = String.format("%.2f", over_underTime); } /** * Displaying the stats */ System.out.printf(" %10s %15s %15.2f %10.2f %10.2f %15s", d.getId(), d.getName(), d.getRunningTime(), d.getPenalty(), totalTime, overUnderTimeString); } } System.out.println(); if (winningDog == null) { /** * No dogs */ System.out.println("No data"); } else { System.out.println("Winning Dog: " + winningDog.getName() + ", Time: " + (winningDog.getRunningTime() + winningDog.getPenalty())); } if (dogWithMostPenalty != null) { System.out.println("Dog with most penalty: " + dogWithMostPenalty.getName() + ", Penalty: " + dogWithMostPenalty.getPenalty()); } } /** * method to add a dog to the arraylist and to the datafile */ public void addDog() { System.out .println("Enter ID: (1 digit followed by a letter, followed by 3 digits)"); String id = scanner.nextLine(); if (id.length() != 5) { System.out.println("Invalid ID"); return; } else { /** * Checking if id is in proper format */ if (!(Character.isDigit(id.charAt(0)) && Character.isLetter(id.charAt(1)) && Character.isDigit(id.charAt(2)) && Character.isDigit(id.charAt(3)) && Character.isDigit(id.charAt(4)))) { System.out.println("Invalid ID"); return; } } System.out.println("Enter dog name: "); String name = scanner.nextLine(); try { System.out.println("Enter running time"); double runTime = Double.parseDouble(scanner.nextLine()); if (runTime <= 0) { System.out.println("Invalid run time"); return; } System.out.println("Enter penalty: "); double penalty = Double.parseDouble(scanner.nextLine()); if (penalty < 0) { System.out.println("Invalid penalty"); return; } System.out.println("Enter course code: "); String code = scanner.nextLine(); if (code.equalsIgnoreCase("G") || code.equalsIgnoreCase("J") || code.equalsIgnoreCase("T")) { Dogs d = new Dogs(id, name, runTime, penalty, code.charAt(0)); dogs.add(d); /** * Defining a printwriter object to append data to the file */ PrintWriter writer = new PrintWriter(new FileOutputStream(dogsFile,true)); writer.append(" " + id + " " + name + " " + runTime + " " + penalty + " " + code); writer.close(); System.out.println("Dog added"); } } catch (NumberFormatException e) { System.out.println("Invalid input"); } catch (Exception e) { System.out.println("Some error occured"); return; } } }

//Dogs.java

public class Dogs {

private String id;

private String name;

private double runningTime;

private double penalty;

private char courseCode;

/***

* Parameterized constructor to initialize all data fields

*/

public Dogs(String id, String name, double runningTime, double penalty,

char courseCode) {

this.id = id;

this.name = name;

this.runningTime = runningTime;

this.penalty = penalty;

this.courseCode = courseCode;

}

/**

* getters and setters

*/

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getRunningTime() {

return runningTime;

}

public void setRunningTime(double runningTime) {

this.runningTime = runningTime;

}

public double getPenalty() {

return penalty;

}

public void setPenalty(double penalty) {

this.penalty = penalty;

}

public char getCourseCode() {

return courseCode;

}

public void setCourseCode(char courseCode) {

this.courseCode = courseCode;

}

}

//Courses.java

public class Courses {

private String name;

private double maximumTime;

/***

* Parameterized constructor to initialize all data fields

*/

public Courses(String name, double maximumTime) {

this.name = name;

this.maximumTime = maximumTime;

}

/**

* getters and setters

*/

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getMaxTime() {

return maximumTime;

}

public void setMaximumTime(double maximumTime) {

this.maximumTime = maximumTime;

}

}

//AppDrive.java

import java.io.FileNotFoundException;

public class AppDriver {

public static void main(String[] args) {

try {

Menu m=new Menu();

m.loadData();

m.showMenu();

} catch (FileNotFoundException e) {

System.out.println("File not found");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions