Question
java Program that creates a CSV file with a prompt to name it. Then Ask The user to input the name of employee, after an
java Program that creates a CSV file with a prompt to name it.
Then Ask The user to input the name of employee, after an employees name is provided ask the user to paste the shift data
it will appear 1 of the following formats Ethier
M1PGAXAVXP Duration: 34 minutes, 59 seconds Started: December 12, 2022 8:46 PM Ended: 15 days ago.
55WUY1CBJX Duration: 1 hour, 25 minutes, 22 seconds Started: December 11, 2022 3:06 PM Ended: 16 days ago
The format is dependent on whether the shift is over an hour, you may ask the user if the shift they are going to input is over or under an hour to make this eaiser
the data should be appended into a csv file to look like this
Employee Name | Shift ID | Hours | Minutes | seconds | Shift start date |
John Doe | M1PGAXAVXP | 0 | 34 | 59 | 12/12/2022 |
John Doe | 55WUY1CBJX | 1 | 25 | 22 | 12/11/2022 |
John Doe | Total | 2 | 0 | 21 |
Next the program should ask the user to enter a number of 1, 2, or 3
a 1 means they have more shifts to imput for this employee
a 2 means they have a new employee to enter shift data for this will tally up last employees total
a 3 means they are done entering data and to save and close the file.
Please debug
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CSVFile {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Prompt user to name the CSV file
System.out.print("Enter name for CSV file: ");
String fileName = br.readLine();
fileName = fileName + ".csv";
// Create CSV file with given name
File file = new File(fileName);
FileWriter outputFile = new FileWriter(file);
// Write header row to CSV file
outputFile.append("Employee Name");
outputFile.append(",");
outputFile.append("Shift ID");
outputFile.append(",");
outputFile.append("Hours");
outputFile.append(",");
outputFile.append("Minutes");
outputFile.append(",");
outputFile.append("Seconds");
outputFile.append(",");
outputFile.append("Shift start date");
outputFile.append(" ");
// Initialize variables to keep track of totals
int totalHours = 0;
int totalMinutes = 0;
int totalSeconds = 0;
// Continue to input shift data until user indicates they are done
int input = 1;
while (input == 1 || input == 2) {
// Ask for employee name
System.out.print("Enter employee name: ");
String employeeName = br.readLine();
// Input shift data
while (input == 1) {
// Ask if shift is over or under an hour
System.out.print("Is this shift over or under an hour? (Enter 'O' for over, 'U' for under): ");
String duration = br.readLine();
// Initialize variables for shift data
int hours = 0;
int minutes = 0;
int seconds = 0;
String startDate = "";
// Handle shift data based on duration
if (duration.equalsIgnoreCase("O")) {
// Ask for shift ID
System.out.print("Enter shift ID: ");
String shiftID = br.readLine();
// Parse hours, minutes, seconds, and start date from input
Scanner sc = new Scanner(System.in);
System.out.print("Enter shift data in the following format: Duration: HH hours, MM minutes, SS seconds Started: MMMM DD, YYYY HH:MM AM/PM Ended: NN days ago. ");
String shiftData = sc.nextLine();
String[] data = shiftData.split(" ");
hours = Integer.parseInt(data[1]);
minutes = Integer.parseInt(data[3]);
seconds = Integer.parseInt(data[5]);
startDate = data[7] + " " + data[8] + " " + data[9] + " " + data[10] + " " + data[11];
writer.append(',');
writer.append(shiftID);
writer.append(',');
writer.append(String.valueOf(hours));
writer.append(',');
writer.append(String.valueOf(minutes));
writer.append(',');
writer.append(String.valueOf(seconds));
writer.append(',');
writer.append(startDate);
writer.append(' ');
// Add shift hours, minutes, and seconds to total
totalHours += hours;
totalMinutes += minutes;
totalSeconds += seconds;
// Prompt user for next action
System.out.print("Enter 1 to add another shift, 2 to enter shifts for a new employee, or 3 to save and close the file: ");
int action = scan.nextInt();
scan.nextLine(); // consume newline character
if (action == 1) {
// Continue inputting shifts for current employee
continue;
} else if (action == 2) {
// Add total hours, minutes, and seconds for previous employee to CSV file
writer.append(employeeName);
writer.append(",Total,");
writer.append(String.valueOf(totalHours));
writer.append(',');
writer.append(String.valueOf(totalMinutes));
writer.append(',');
writer.append(String.valueOf(totalSeconds));
writer.append(',');
writer.append(' ');
// Reset total hours, minutes, and seconds
totalHours = 0;
totalMinutes = 0;
totalSeconds = 0;
// Prompt user to enter new employee name
System.out.print("Enter employee name: ");
employeeName = scan.nextLine();
} else if (action == 3) {
// Add total hours, minutes, and seconds for previous employee to CSV file
writer.append(employeeName);
writer.append(",Total,");
writer.append(String.valueOf(totalHours));
writer.append(',');
writer.append(String.valueOf(totalMinutes));
writer.append(',');
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started