Question

this needs to be in Java:

Problem Description In this assignment, you will modify a solution to Assignment 1 with two modifications. You may start with use a comparator class which is passed into the sort method that is available on the ArrayList.

import java.util.Scanner;

public class Assign1{

public static void main(String[] args){

Scanner reader = new Scanner (System.in);

MyDate todayDate = new MyDate();

int choice = 0;

Library library = new Library();

while (choice != 6){

displayMainMenu();

if (reader.hasNextInt()){

choice = reader.nextInt();

switch(choice){

case 1:

library.inputResource(reader, todayDate);

break;

case 2:

System.out.println(library.resourcesOverDue(todayDate));

break;

case 3:

System.out.println(library.toString());

break;

case 4:

library.deleteResource(reader, todayDate);

break;

case 5:

System.out.println("Enter a new date for today's date");

todayDate.inputDate(reader);

break;

case 6:

System.out.println("");

break;

default:

System.out.println("Invalid entry, please try again");

break; }}

else{

System.out.println ("Invalid number input");

reader.next();

}}

reader.close();

}

private static void displayMainMenu(){

System.out.println();

System.out.println("Enter 1 to add to resources borrowed, ");

System.out.println("2 to display overdue items, ");

System.out.println("3 to display all resources borrowed, ");

System.out.println("4 to delete a resource, ");

System.out.println("5 to change today date");

System.out.print("6 to quit: ");

}}

import java.util.Scanner;

public class Book extends Resource {

private String author;

public String toString() {

return "author " + author + " " + super.toString();}

public Book(){

super();

author = "";

overdueCost = 2.0f;}

public boolean inputResource(Scanner scanner, MyDate date){

if(super.inputResource(scanner, date)){

System.out.print("Enter the author name (no spaces): ");

if(scanner.hasNext())

author = scanner.next();

dueDate = date.addDays(14);

if(author.trim().equals(""))

return false;

return true;}

return false;

}}

import java.util.Scanner;

public class DVD extends Resource{

private String type;

public DVD() {

super();

type = "";

overdueCost = 1.0f;

}

public String toString() {

return "type of DVD : " + type + " " + super.toString();

}

public boolean inputResource(Scanner scanner, MyDate date){

if(super.inputResource(scanner, date)){

System.out.print("Enter the type of DVD (no spaces): ");

if(scanner.hasNext())

type = scanner.next();

dueDate = date.addDays(3);

return true;}

return false;

}}

import java.util.Scanner;

import java.util.regex.Pattern;

public class Library{

private Resource[] resourcesBorrowed;

private int numResources;

private int max;

public Library(){

resourcesBorrowed = new Resource[10];

numResources = 0;

max = 10;}

public boolean inputResource (Scanner scanner, MyDate date){

if(numResources >= max)

return false;

System.out.println("Enter type of resource being borrowed - D for DVD, M for Magazine and B for book:");

if(scanner.hasNext(Pattern.compile("[DdMmBb]"))){

String resourceType = scanner.next();

if(resourceType.equalsIgnoreCase("d")){

resourcesBorrowed[numResources] = new DVD();}

else if(resourceType.equalsIgnoreCase("m")){

resourcesBorrowed[numResources] = new Magazine();}

else if(resourceType.equalsIgnoreCase("b")){

resourcesBorrowed[numResources] = new Book();}

if(resourcesBorrowed[numResources].inputResource(scanner, date)){

numResources++;

return true;}}

return false;}

public String toString(){

String returnString = "Items currently borrowed from library are:\n";

for(int i=0;i

returnString += (i+1)+": " + resourcesBorrowed[i].toString() + "\n";}

return returnString;}

public String resourcesOverDue (MyDate date){

String returnString = "Items currently borrowed from library that are overdue as of "+date.toString()+" are:";

for (int i=0;i

if(resourcesBorrowed[i].isOverDue(date))

returnString += resourcesBorrowed[i].toString()+"\n";

return returnString;}

public void deleteResource(Scanner scanner, MyDate date){

if(numResources == 0){

System.out.println("No resources to delete");

return;}

System.out.println("List of resources checked out in the library:");

System.out.println(toString());

int toDelete = 0;

do{

System.out.print("Which resource to delete: ");

if(scanner.hasNextInt())

toDelete = scanner.nextInt();

else{

System.out.println("Invalid number input");

scanner.next();}

if (toDelete>numResources || toDelete<1)

toDelete = 0;

}while(toDelete == 0);

if(resourcesBorrowed[toDelete-1].isOverDue(date))

resourcesBorrowed[toDelete-1].displayOverDue();

else{

for (int i=toDelete-1;i

resourcesBorrowed[i]=resourcesBorrowed[i+1];

numResources--;

}}}

import java.util.Formatter;

import java.util.Scanner;

public class MyDate implements Comparable{

private int day = 1;

private int month = 1;

private int year = 2019;

public MyDate() {}

public MyDate(MyDate newDate){

day = newDate.day;

month = newDate.month;

year = newDate.year;}

public String toString(){

return new String ("" + year + "/" + month + "/" + day);}

public boolean inputDate(Scanner in) {

month = 0;

day = 0;

year = 0;

do {

System.out.print ("Enter month - between 1 and 12: ");

if (in.hasNextInt())

this.month = in.nextInt();

else{

System.out.println ("Invalid month input");

in.next();}

} while (this.month <= 0 || this.month > 12);

do {

System.out.print ("Enter day - between 1 and 31: ");

if (in.hasNextInt())

this.day = in.nextInt();

else{

System.out.println ("Invalid day input");

in.next();}

} while (this.day <= 0 || this.day > 31 || (this.month == 2 && this.day > 29) || (this.day > 30 && (this.month == 9 ||this.month == 4 ||this.month == 6 ||this.month == 11) ) );

do {

System.out.print ("Enter year: ");

if (in.hasNextInt())

this.year = in.nextInt();

else {

System.out.println ("Invalid day input");

in.next();}

} while (this.year <= 0);

return true;}

public void addOne (){

this.day++;

if (this.day > 31 || (this.month == 2 && this.day > 29) || (this.day > 30 && (this.month == 9 ||this.month == 4 ||this.month == 6 ||this.month == 11) ) ){

this.day = 1;

this.month ++;

if (this.month > 12) {

this.month = 1;

this.year++;

}}}

public MyDate addDays(int daysToAdd){

MyDate returnDate = new MyDate(this);

for(int i=0;i

returnDate.addOne();

return returnDate;}

public boolean outputDate(Formatter writer){

try{

if(writer != null){

writer.format("%d\n",month);

writer.format("%d\n",day);

writer.format("%d\n",year);}}

catch(Exception e){

System.out.println("Error: "+e.getMessage());

return false;}

return true;}

public int compareTo(MyDate compDate){

int myValue = year * 365 + month * 30 + day;

int compValue = compDate.year * 365 + compDate.month * 30 + compDate.day;

return myValue - compValue;}

public boolean isEqual(MyDate date){

return (this.year == date.year &&

this.month == date.month &&

this.day == date.day);}

public boolean isGreaterThan(MyDate date){

return (compareTo(date)>0);

}}

import java.util.Scanner;

public class Resource {

protected String title;

protected String borrower;

protected MyDate dueDate;

protected float overdueCost;

public Resource(){

title = "";

borrower = "";

dueDate = new MyDate();

overdueCost = 0.0f;}

public boolean inputResource(Scanner scanner, MyDate date){

System.out.print("Enter title being borrowed: ");

if(scanner.hasNext())

title = scanner.next();

System.out.print("Enter borrower name (no spaces): ");

if(scanner.hasNext())

borrower = scanner.next();

if(title.trim().equals("") || borrower.trim().equals(""))

return false;

return true;}

public String toString(){

return borrower + " has "+ title + " due on " + dueDate.toString() + " and if late " + overdueCost;}

public boolean isOverDue (MyDate today){

return today.isGreaterThan(dueDate);}

public void displayOverDue(){

System.out.println("This resource is overdue - $"+overdueCost+" is owed by borrower "+borrower);}}

import java.util.Scanner;

public class Magazine extends Resource

private MyDate edition;

public Magazine(){

super();

edition = new MyDate();

overdueCost = 1.0f;}

public String toString() {

return "edition " + edition.toString() + " " + super.toString();}

public boolean inputResource(Scanner scanner, MyDate date){

if(super.inputResource(scanner, date)){

System.out.print("Enter the edition date: ");

edition.inputDate(scanner);

dueDate = date.addDays(7);

return true;}

return false;

}}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

import java.text.ParseException;
import java.util.Calendar;
import java.util.Scanner;

public class Driver {

    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        Calendar calendar = Calendar.getInstance();
        Library library = new Library();
        MyDate todayDate = new MyDate(calendar.get(Calendar.DATE), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.YEAR));
        int choice = 0;
        do {
            System.out.println("\nWelcome to the Library\n");
            System.out.println("\nPlease Enter \n");
            System.out.println("1 to add to resources borrowed,");
            System.out.println("2 to display overdue items,");
            System.out.println("3 to display all resources borrowed,");
            System.out.println("4 to delete a resource,");
            System.out.println("5 to change today date");
            System.out.println("6 to view a specific resource,");
            System.out.println("7 to read resources from a file,");
            System.out.println("8 to save the current resources to a file,");
            System.out.println("9 to quit:");

            if (input.hasNextInt()) {
                choice = input.nextInt();
            } else {
                System.out.println("Invalid entry – prompted to reenter choice");
                input.next();
            }

            if (choice == 1) {
                library.inputResource(input, todayDate);
            } else if (choice == 2) {
                library.resourcesOverDue(todayDate);
            } else if (choice == 3) {
                library.toString();
            } else if (choice == 4) {
                library.deleteResource(input, todayDate);
            } else if (choice == 5) {
                todayDate = new MyDate();
                System.out.println("Enter a new date for today's date: ");
                todayDate.inputDate(input, true);
            } else if (choice == 6) {
                library.viewResource(input);
            } else if (choice == 7) {
                library.readResourceFile(input, todayDate);
            } else if (choice == 8) {
                library.saveResourceFile(input);
            } else if (choice == 9) {
                System.out.println("goodbye");
            }
        } while (choice != 9);

    }

}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Formatter;
import java.util.Scanner;

public class Library {

    private ArrayList<Resource> resourcesBorrowed;

    public Library() {
        resourcesBorrowed = new ArrayList<>();
    }

    public void inputResource(Scanner in, MyDate myDate) throws ParseException {
        String resouceType = "";
        boolean isValidResourceType = false;
        do {
            System.out.println("Enter type of resource being borrowed - D for DVD, M for Magazine and B for book: ");
            resouceType = in.next();
            Resource resource = null;
            if (resouceType.equalsIgnoreCase("B")) {
                resource = new Book();
                isValidResourceType = true;
            } else if (resouceType.equalsIgnoreCase("D")) {
                resource = new DVD();
                isValidResourceType = true;
            } else if (resouceType.equalsIgnoreCase("M")) {
                resource = new Magazine();
                isValidResourceType = true;
            }
            if (isValidResourceType) {
                resource.inputResource(in, myDate, true);
                int index = findIndexToInsert(resource);
                resourcesBorrowed.add(index, resource);
            }
        } while (!isValidResourceType);
    }

    public int findIndexToInsert(Resource resource) {
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            Resource currentResource = resourcesBorrowed.get(i);
            if (currentResource.title.compareTo(resource.title) > 0) {
                return i;
            }
        }
        return resourcesBorrowed.size();
    }

    public String toString() {
        System.out.println("Items currently borrowed from library are:");
        if (resourcesBorrowed.size() == 0) {
            System.out.println("No entries underneath");
            return "";
        }
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            System.out.println((i + 1) + ": " + resourcesBorrowed.get(i).toString());
        }
        return "";
    }

    public String resourcesOverDue(MyDate today) throws ParseException {
        System.out.println("Items currently borrowed from library that are overdue as of " + today + " are:");
        if (resourcesBorrowed.size() == 0) {
            System.out.println("No entries underneath");
            return "";
        }
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            Resource resource = resourcesBorrowed.get(i);
            if (resource.isOverDue(today)) {
                System.out.println((i + 1) + ": " + resource.toString());
            }
        }
        return "";
    }

    public void deleteResource(Scanner in, MyDate todayDate) throws ParseException {
        int resourceNum = 0;
        if (resourcesBorrowed.isEmpty()) {
            System.out.println("No resources to delete");
            return;
        }
        System.out.println("List of resources checked out in the library: ");
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            System.out.println("[" + (i + 1) + "]: " + resourcesBorrowed.get(i).toString());
        }

        do {
            System.out.println("Which resource to delete: ");
            if (in.hasNextInt()) {
                resourceNum = in.nextInt();
            } else {
                System.out.println("Invalid resource number. Prompted to re-enter");
            }
        } while (resourceNum <= 0 || resourceNum > resourcesBorrowed.size());

        //check if overdue present or not
        Resource resource = resourcesBorrowed.get(resourceNum - 1);
        if (resource.isOverDue(todayDate)) {
            System.out.println("This resource is overdue - $" + resource.overdueCost + " is owed by borrower. Item is not deleted.");
        }
        resourcesBorrowed.remove(resourceNum - 1);

        System.out.println("Item is deleted.");
    }


    public void viewResource(Scanner input) {
        if (resourcesBorrowed.isEmpty()) {
            System.out.println("No resources to view");
            return;
        }
        System.out.println("Enter the title to search for: ");
        String resouceTitle = input.next();
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            if (resourcesBorrowed.get(i).isSameTitle(resouceTitle)) {
                System.out.println(resourcesBorrowed.get(i).toString());
                return;
            }
        }
        System.out.println("Resource with this title is not found");

    }

    public void readResourceFile(Scanner input, MyDate myDate) throws ParseException {
        String resouceType = "";
        boolean isValidResourceType = false;
        System.out.println("Enter name of file to process: ");
        String fName = input.next();
        try {
            File file = new File(fName);
            if (file.exists()) {
                input = new Scanner(file);
                while (input.hasNext()) {
                    resouceType = input.next();
                    Resource resource = null;
                    if (resouceType.equalsIgnoreCase("B")) {
                        resource = new Book();
                        isValidResourceType = true;
                    } else if (resouceType.equalsIgnoreCase("D")) {
                        resource = new DVD();
                        isValidResourceType = true;
                    } else if (resouceType.equalsIgnoreCase("M")) {
                        resource = new Magazine();
                        isValidResourceType = true;
                    }
                    if (isValidResourceType) {
                        resource.inputResource(input, myDate, false);
                        int index = findIndexToInsert(resource);
                        resourcesBorrowed.add(index, resource);
                    }
                }
            } else {
                System.out.println("File does not exist");
            }

        } catch (IOException e) {
            System.out.println("Could not open file..." + fName + "exiting");
        }
    }

    void saveResourceFile(Scanner input) {
        String str = "";
        for (int i = 0; i < resourcesBorrowed.size(); i++) {
            str += resourcesBorrowed.get(i).saveToString() + "\n";
        }

        try {
            System.out.println("Enter name of file to write to: ");
            Formatter x = new Formatter(input.next());
            x.format("%s", str);
            x.close();
        } catch (Exception e) {
            System.out.println("you have an error");
        }

    }

}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Book extends Resource {

    protected String author;

    public Book() {

    }

    @Override
    public boolean inputResource(Scanner in, MyDate myDate, boolean enableLog) throws ParseException {
        MyDate dueMydate;
        super.inputResource(in, myDate, enableLog);
        if (enableLog) {
            Date date = new SimpleDateFormat("yyyy/MM/dd").parse(myDate.toString());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 14);
            dueMydate = new MyDate(calendar.get(Calendar.DATE), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.YEAR));
        } else {
            dueMydate = new MyDate();
            dueMydate.inputDate(in, enableLog);

            if (in.hasNextFloat()) {
                this.overdueCost = in.nextFloat();
            }
        }
        this.dueDate = dueMydate;

        do {
            if (enableLog) {
                System.out.print("Enter the author name (no spaces): ");
            }
            this.author = in.next();
        } while (this.author == null || this.author.trim().equals(""));

        return true;
    }

    @Override
    public String toString() {
        return "author " + author + " " + super.toString();
    }

    @Override
    public String saveToString() {
        return "b " + super.saveToString() + author;
    }

}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class DVD extends Resource {

    private String type;

    public DVD() {

    }

    @Override
    public boolean inputResource(Scanner in, MyDate myDate, boolean enableLog) throws ParseException {
        MyDate dueMydate = null;
        super.inputResource(in, myDate, enableLog);
        if (enableLog) {
            Date date = new SimpleDateFormat("yyyy/MM/dd").parse(myDate.toString());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 14);
            dueMydate = new MyDate(calendar.get(Calendar.DATE), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.YEAR));
        } else {
            dueMydate = new MyDate();
            dueMydate.inputDate(in, enableLog);

            if (in.hasNextFloat()) {
                this.overdueCost = in.nextFloat();
            }
        }
        this.dueDate = dueMydate;

        do {
            if (enableLog) {
                System.out.print("Enter the type of DVD (no spaces): ");
            }
            this.type = in.next();
        } while (this.type == null || this.type.trim().equals(""));

        return true;
    }

    @Override
    public String toString() {
        return "type of DVD : " + type + " " + super.toString();
    }


    @Override
    public String saveToString() {
        return "d " +super.saveToString() + type;
    }

}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Magazine extends Resource {

    protected MyDate edition;

    public Magazine() {
        edition = new MyDate();
    }

    @Override
    public boolean inputResource(Scanner in, MyDate myDate, boolean enableLog) throws ParseException {

        MyDate dueMydate;
        super.inputResource(in, myDate, enableLog);
        if (enableLog) {
            Date date = new SimpleDateFormat("yyyy/MM/dd").parse(myDate.toString());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 14);
            dueMydate = new MyDate(calendar.get(Calendar.DATE), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.YEAR));
        } else {
            dueMydate = new MyDate();
            dueMydate.inputDate(in, enableLog);


            if (in.hasNextFloat()) {
                this.overdueCost = in.nextFloat();
            }
        }
        this.dueDate = dueMydate;
        if (enableLog) {
            System.out.print("Enter the edition date: ");
        }
        edition.inputDate(in, enableLog);

        this.overdueCost = 1;
        return true;
    }

    @Override
    public String toString() {
        return "edition " + edition + " " + super.toString();
    }

    @Override
    public String saveToString() {
        return "m " + super.saveToString() + edition.saveToString();
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.util.Scanner;

public class Resource {

    protected String title;
    protected String borrower;
    protected MyDate dueDate;
    protected float overdueCost;

    public Resource() {

    }

    public boolean inputResource(Scanner in, MyDate myDate, boolean enableLog) throws ParseException {
        do {
            if (enableLog) {
                System.out.print("Enter title being borrowed: ");
            }
            this.title = in.next();
        } while (this.title == null || this.title.trim().equals(""));


        do {
            if (enableLog) {
                System.out.print("Enter borrower name (no spaces):");
            }
            this.borrower = in.next();
        } while (this.borrower == null || this.borrower.trim().equals(""));

        this.dueDate = myDate;
        return true;
    }

    @Override
    public String toString() {
        return borrower + " has " + title + " due on " + dueDate + " and if late " + overdueCost;
    }
    public String saveToString() {
        return borrower + " " + title + " " + dueDate.saveToString() + " " + overdueCost + " ";
    }

    public boolean isOverDue(MyDate today) throws ParseException {
        return dueDate.isGreaterThan(today);
    }

    public void displayOverDue() {
        System.out.println("Borrower name:" + this.borrower + " and Due Cost: " + overdueCost);
    }

    public boolean isSameTitle(String resouceTitle) {
        return title.equalsIgnoreCase(resouceTitle);
    }

}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MyDate {

    private int day = 1;
    private int month = 1;
    private int year = 2018;

    public MyDate() {
    }

    public MyDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    @Override
    public String toString() {
        return new String("" + year + "/" + month + "/" + day);
    }

    public String saveToString() {
        return month + " " + day + " " + year;
    }

    public boolean inputDate(Scanner in, boolean enableLog) {
        month = 0;
        day = 0;
        year = 0;
        do {
            if (enableLog) {
                System.out.print("Enter month - between 1 and 12: ");
            }
            if (in.hasNextInt()) {
                this.month = in.nextInt();
            } else {
                if (enableLog) {
                    System.out.println("Invalid month input");
                }
                in.next();
            }
        } while (this.month <= 0 || this.month > 12);

        do {
            if (enableLog) {
                System.out.print("Enter day - between 1 and 31: ");
            }
            if (in.hasNextInt()) {
                this.day = in.nextInt();
            } else {
                if (enableLog) {
                    System.out.println("Invalid day input");
                }
                in.next();
            }
        } while (this.day <= 0 || this.day > 31 || (this.month == 2 && this.day > 29) || (this.day > 30 && (this.month == 9 || this.month == 4 || this.month == 6 || this.month == 11)));

        do {
            if (enableLog) {
                System.out.print("Enter year: ");
            }
            if (in.hasNextInt()) {
                this.year = in.nextInt();
            } else {
                if (enableLog) {
                    System.out.println("Invalid day input");
                    in.next();

                }
            }
        } while (this.year <= 0);

        return true;
    }

    public void addOne() {

        if (day < 29 && month == 2) {
            day = day + 1;
        } else {
            if (day == 29 && month == 2) {
                day = 1;
                month = month + 1;
                return;
            }

        }

        if (day < 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)) {

            day = day + 1;

        } else {
            if (day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)) {
                day = 1;
                month = month + 1;
                return;
            }
        }

        if (day < 31 && month == 12) {
            day = day + 1;
        } else {
            if (day == 31 && month == 12) {
                day = 1;
                month = 1;
                year = year + 1;
                return;
            }
        }// end for dec

        if (day < 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
            day = day + 1;
        } else {
            if (day == 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
                day = 1;
                month = month + 1;
                return;
            }
        }

    }

    public boolean isEqual(MyDate myDate) throws ParseException {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
        Date date_1 = formater.parse(myDate.toString());
        Date date_2 = formater.parse(this.toString());

        if (date_1.equals(date_2)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isGreaterThan(MyDate myDate) throws ParseException {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
        Date date_1 = formater.parse(myDate.toString());
        Date date_2 = formater.parse(this.toString());

        if (date_1.after(date_2)) {
            return true;
        } else {
            return false;
        }
    }

}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


LibraryProgram ldeaProjects/LibraryProgram] - ./src/Driver.java (LibraryProgram] src Driver LibraryProgram Main Book java Mag

Add a comment
Know the answer?
Add Answer to:
this needs to be in Java: use a comparator class which is passed into the sort...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding...

    Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • My values are not storing into my object and I keep returning zero. please explain my...

    My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0;    public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); }          public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...

    Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }    public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT