Hello, I need help for this JAVA homework: - Warmup Composition and File Handlin
ID: 3813966 • Letter: H
Question
Hello, I need help for this JAVA homework:
- Warmup Composition and File Handling
In this assignment, you’ll read in dates from a .txt file, load an array of type Date objects, sort the array and write the Date values out to another text file.
Design a program that will read in dates from a file. The filename will be user-entered. The dates read from the file will be in the following format: month/day/year where month and day are 1 or 2 digits and year is 4 digits (note: the file will also contain the /'s - e.g. 3/24/2017) Proper date format is guaranteed, so no error checking is required.
Store each date in a Date object created from a Date class that you create (no java API classes like Calendar may be used.) Your Date class should contain a Day object, a Month object, and a Year object. This means you will also need to create Day, Month, and Year classes.
The Day class should have the following behaviors:
• set the day (day number)
• get the day number
• return information on the day via toString (your toString should override the one inherited from Object)
• compare days for equality (by overriding the equals method inherited from Object)
• construct a day
The Month and Year classes should have similar methods to the Day class. You are welcome to provide other behaviors for each class, but make sure they make sense in the context of that class.
The Date class should the following behaviors:
• set the date
• get the date
• display information on the Date via toString (override the toString inherited from Object): In the following format: Month as a word, the day and the year. Example: September 27 2013
• If the toString includes the day of the week, the month of the year, the year, and the day number of the year (e.g. Thursday, September 27, 2017, day 270 of the year)
• compare dates for equality (by overriding the equals method inherited from Object)
• sort dates by year, then month, then day (overrides the compareTo method from the Comparable interface)
•An EVC (the parameters to the constructor should be three integers)
• A DVC (defaults to 1 1 1970 as its date)
Provide error checking to ensure any data assigned to any objects of the three classes falls in a reasonable range.
Create a tester file called DateTester that contains the main method as well as other methods to test the program as specified above. Provide other methods to perform specific tasks (display the menu, get input from user, etc.). The program flow should be implemented as follows:
• obtaining the input filename from the user
• read the file (use your FileUtil class to open the file)
• count the dates
• create an array of Date references that matches the count
• fill the array with the Date objects
• sort the array (use your SortSearchUtil class for sorting – type Comparable)
• display a menu that allows the following choices:
Print the array of dates
• To the screen
• To a file if the user wants. You will need to prompt the user for the file name.
2. Allow the user to search for a date – Date will be user entered
3. Allow the user to add a date – will need to make a new array and copy over the dates and then re-sort
4. Allow the user to delete a date (by value) – will need to make a new array and copy over the dates
5. Quit
This menu should be displayed repetitively until the user chooses to quit
To turn in :
Be sure to contains all source files (.java files) necessary to compile and run your program (including SortSearchUtil.java and FileUtil.java.)
Explanation / Answer
Unable to upload .java files, hence copy pasting the code for each file here:
DateTester.java -
package com.chegg;
import java.io.File;
import java.util.Scanner;
public class DateTester {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter filename");
String input = in.nextLine();
File file = new File(input);
FileUtil fileUtil = new FileUtil();
fileUtil.readFile(file);
while (true ) {
Scanner op = new Scanner(System.in);
System.out.println("Select an option:");
System.out.println("1: Print array of dates on screen");
System.out.println("2: Print array of dates to file");
System.out.println("3: Search for a date");
System.out.println("4: Add a date");
System.out.println("5: Delete a date");
System.out.println("6: Quit");
int option = Integer.parseInt(op.nextLine());
switch (option) {
case 1:
FileUtil f = new FileUtil();
f.printDates();
break;
case 2:
FileUtil f2 = new FileUtil();
f2.printToFile();
break;
case 3:
Scanner sc3 = new Scanner(System.in);
System.out.println("Enter date to be searched");
String date3 = sc3.nextLine();
FileUtil fileUtil3 = new FileUtil();
if(fileUtil3.searchDate(date3)) {
System.out.println("Found");
}
else {
System.out.println("Not found");
}
case 4:
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter date to be added");
String date1 = sc1.nextLine();
FileUtil fileUtil1 = new FileUtil();
fileUtil1.addDate(date1);
break;
case 5:
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter date to be removed");
String date2 = sc2.nextLine();
FileUtil fileUtil2 = new FileUtil();
fileUtil2.deleteDate(date2);
break;
case 6:
System.exit(0);
}
}
}
}
FileUtil.java -
package com.chegg;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class FileUtil {
private ArrayList<Date> dates = new ArrayList<>();
public void readFile (File file) {
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String s = scanner.next();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(s);
dates.add(date);
}
} catch (FileNotFoundException e) {
System.err.println("File not found");
} catch (ParseException pe) {
System.err.println("Parse Exception");
}
}
public void addDate (String s) {
try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(s);
dates.add(date);
} catch (ParseException pe) {
System.err.println("Parse Exception");
}
}
public void deleteDate(String s) {
try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(s);
dates.remove(date);
} catch (ParseException pe) {
System.err.println("Parse Exception");
}
}
public boolean searchDate(String s) {
try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(s);
return dates.contains(date);
} catch (ParseException pe) {
System.err.println("Parse Exception");
}
return false;
}
public void printDates () {
for (Date d : dates) {
System.out.println(d.toString());
}
}
public void printToFile() {
Scanner out = new Scanner(System.in);
System.out.println("Enter output filename");
String output = out.nextLine();
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(output), "utf-8"))) {
for (Date d : dates) {
writer.write(d.toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Date.java -
package com.chegg;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Date {
private Date date;
public void set (Date date) {
this.date = date;
}
private Date get () {
return this.date;
}
@Override
public String toString() {
int dayOfYear = LocalDate.now().get(ChronoField.DAY_OF_YEAR);
return this.date.get() + " " + String.valueOf(dayOfYear);
}
@Override
public boolean equals (Object obj) {
if(this == obj)
return true;
else if(obj instanceof Date)
return true;
return false;
}
}
Day.java -
package com.chegg;
public class Day {
private DayNumber dayNumber;
public enum DayNumber {
SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6),
SATURDAY(7);
private int value;
DayNumber (int value) {
this.value = value;
}
};
public void set (DayNumber dayNumber) {
this.dayNumber = dayNumber;
}
public DayNumber get () {
return this.dayNumber;
}
@Override
public boolean equals (Object obj) {
if(this == obj)
return true;
else if(obj instanceof Day)
return true;
return false;
}
}
Month.java -
package com.chegg;
public class Month {
private MonthNumber monthNumber;
public enum MonthNumber {
JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6),
JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10), NOVEMBER(11), DECEMBER(12);
private int value;
MonthNumber (int value) {
this.value = value;
}
};
public void set (MonthNumber monthNumber) {
this.monthNumber = monthNumber;
}
public MonthNumber get () {
return this.monthNumber;
}
@Override
public boolean equals (Object obj) {
if(this == obj)
return true;
else if(obj instanceof Month)
return true;
return false;
}
}
Year.java -
package com.chegg;
public class Year {
private Year year;
public void set (Year year) {
this.year = year;
}
public Year get () {
return this.year;
}
@Override
public boolean equals (Object obj) {
if(this == obj)
return true;
else if(obj instanceof Year)
return true;
return false;
}
}
SortSearchUtil.java -
package com.chegg;
import java.util.Date;
public class SortSearchUtil implements Comparable<SortSearchUtil> {
private Date dateTime;
private Date getDateTime() {
return dateTime;
}
public void setDateTime(Date datetime) {
this.dateTime = datetime;
}
@Override
public int compareTo(SortSearchUtil o) {
if (getDateTime() == null || o.getDateTime() == null) {
return 0;
}
return getDateTime().compareTo(o.getDateTime());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.