It is a java programming, the challenge is that, need to create two class public
ID: 3877879 • Letter: I
Question
It is a java programming, the challenge is that, need to create two class public class Dogs() {} and public class courses() {} and some driver class to test the code.
Several dogs have been entered in a competition. The dogs will run a selected course (either Jumpers, Gamblers, or Titling) and are timed. Any penalties are recorded in additional seconds and added to the dog's course time to get their total time.
A data file (dogs.txt) contains information about the dogs. Each dog record has these fields:
- id
- name
- running time in seconds
- dog’s total penalty in seconds
- course code
A data file (courses.txt) contains information about the courses. Each course record has these fields:
- name
- maximum time
Create an interactive, menu driven application that will do the following:
· allow the user to print a report of a specific course The report should consist of a list of the dogs that ran that particular course. The report should have appropriate headings (including the current date). Each detail line of the report should show the name of the dog, running time, penalty faults, total time and amount of total time over or under the course time. A summary of the winning dog and the dog with the most penalties should be printed at the bottom of the report.
allow the user to add a dog to the data file. Before the dog is added, the data entered by the user must be validated according to the following rules:
ID of dog: digit, letter, 3 digits
Run time: number > 0
Penalties: number > 0
Course: J,j,G,g,T,t
Details:
An example of a record of a dog in the dogs.dat file is:
e.g. 1H579 Bonzo 34.8 14 G
An example of a record of a course in the courses.dat file is:
e.g. Gamblers 53.4
An example of a report produced by the program:
e.g.
Report GAMBLERS – Course Time: 52.5 January 10, 2011
ID Name Running Penalty Total Over/
Time Time Under
1H579 Bonzo 34.8 14 48.8 - 3.7
2D123 Frank 33.4 25 58.4 + 6.9
etc.
Winning dog: Bonzo Time 48.8
etc.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//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 getMaximumTime() {
return maximumTime;
}
public void setMaximumTime(double maximumTime) {
this.maximumTime = maximumTime;
}
}
//Driver.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;
public class Driver {
static ArrayList<Dogs> dogs;
static ArrayList<Courses> courses;
static File dogsFile;
static File coursesFile;
static Scanner scanner;
public static void main(String[] args) {
dogsFile = new File("dogs.txt");
coursesFile = new File("courses.txt");
dogs = new ArrayList<Dogs>();
courses = new ArrayList<Courses>();
scanner = new Scanner(System.in);
try {
loadData();
showMenu();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
/**
* method to load data from datafiles
*/
static 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 display the menu and get user choice repeatedly until user
* quits
*/
static void showMenu() {
System.out.println("1. Print report");
System.out.println("2. Add a Dog");
System.out.println("3. Exit");
System.out.println("Enter a choice: ");
try {
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
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;
}
} catch (Exception e) {
System.out.println("Invalid choice, try again");
showMenu();
}
showMenu();
}
/**
* 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
*/
static 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.getMaximumTime());
}
}
} else if (choice.equalsIgnoreCase("J")) {
for (Courses c : courses) {
if (c.getName().equalsIgnoreCase("Jumpers")) {
printReport('J', c.getMaximumTime());
}
}
} else if (choice.equalsIgnoreCase("T")) {
for (Courses c : courses) {
if (c.getName().equalsIgnoreCase("Titling")) {
printReport('T', c.getMaximumTime());
}
}
} else {
System.out.println("Invalid choice");
}
}
/**
* 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
*/
static 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 " + courseName + " Course 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
*/
static 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.txt
1H579 Bonzo 34.8 14 G
8H270 Jimmy 44.8 3 G
9J500 Rocky 24.8 19 J
1X123 Christy 39.3 10 T
1X124 Tarzen 29.3 5 T
1X123 Jimbru 31.3 1 T
9I608 Rachel 28.8 10 J
5J588 Hachi 33.7 7 J
//courses.txt
Gamblers 53.4
Jumpers 59.2
Titling 50.2
//OUTPUT
1. Print report
2. Add a Dog
3. Exit
Enter a choice:
1
Choose the course:
Enter G for Gamblers, J for Jumpers, or T for Titling
G
Report Gamblers Course Time: 53.4
Date: January 23,2018
ID Name Running Time Penalty Total Over/Under Time
1H579 Bonzo 34.80 14.00 48.80 -4.60
8H270 Jimmy 44.80 3.00 47.80 -5.60
Winning Dog: Jimmy, Time: 47.8
Dog with most penalty: Bonzo, Penalty: 14.0
1. Print report
2. Add a Dog
3. Exit
Enter a choice:
2
Enter ID: (1 digit followed by a letter, followed by 3 digits)
2X994
Enter dog name:
Dogger
Enter running time
33.4
Enter penalty:
2
Enter course code:
T
Dog added
1. Print report
2. Add a Dog
3. Exit
Enter a choice:
1
Choose the course:
Enter G for Gamblers, J for Jumpers, or T for Titling
T
Report Titling Course Time: 50.2
Date: January 23,2018
ID Name Running Time Penalty Total Over/Under Time
1X123 Christy 39.30 10.00 49.30 -0.90
1X124 Tarzen 29.30 5.00 34.30 -15.90
1X123 Jimbru 31.30 1.00 32.30 -17.90
2X994 Dogger 33.40 2.00 35.40 -14.80
Winning Dog: Jimbru, Time: 32.3
Dog with most penalty: Christy, Penalty: 10.0
1. Print report
2. Add a Dog
3. Exit
Enter a choice:
3
Good bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.