Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java assignment. This is a personal lending library assignment so I have a Media

ID: 3730688 • Letter: J

Question

Java assignment. This is a personal lending library assignment so I have a MediaItem class and a Library class along with the main method/program. I need to have it display an error message when someone makes a choice greater than 5. My code does say Goodbye when 5 is selected but does not close after any length of time. Also, the error message "I'm sorry, I couldn't find (title) in the Library" should display after entering a title that doesn't exist. Currently the code displays it after allowing the user to enter the person loaned to and date loaned prompts.

Here is my Media item code.

public class MediaItem {

String title;

String format;

boolean onLoan;

String loanedTo;

String dateLoaned;

MediaItem(){ //default constructor

title = null;

format = null;

onLoan = false;

loanedTo = null;

dateLoaned = null;

}

MediaItem(String title, String format){ //constructor

onLoan = false;

this.title = title;

this.format = format;

}

public String getTitle() { //getters and setters are here

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getFormat() {

return format;

}

public void setFormat(String format) {

this.format = format;

}

public boolean isOnLoan() {

return onLoan;

}

public void setOnLoan(boolean onLoan) {

this.onLoan = onLoan;

}

public String getLoanedTo() {

return loanedTo;

}

public void setLoanedTo(String loanedTo) {

this.loanedTo = loanedTo;

}

public String getDateLoaned() {

return dateLoaned;

}

public void setDateLoaned(String dateLoaned) {

this.dateLoaned = dateLoaned;

}

void markOnLoan(String name, String date){ //methods

if(onLoan == true)

System.out.println(this.title + " is already loaned out");

else {

onLoan = true;

loanedTo = name;

dateLoaned = date;

}

}

void markReturned(){

if(onLoan == false)

System.out.println(this.title + " is not currently loaned out");

onLoan = false;

}

}

Here is my Library code.

import java.util.Scanner;

public class Library {

static Scanner in = new Scanner(System.in); //instantiation

MediaItem t = new MediaItem();

MediaItem[] items = new MediaItem[100];

String[] str = new String[100];

int numberOfItems = 0; //fields

int check = 0;

int called = 0;

int displayMenu(){ //methods

System.out.println("1. Add new item 2. Mark an item as on loan 3. List all items 4. Mark an item as returned 5. Quit");

System.out.print("What would you like to do? ");

int a = in.nextInt();

return a;

}

void addNewItem(String title, String format){

MediaItem item = new MediaItem(title, format);

items[numberOfItems] = item;

numberOfItems++;

}

void markItemOnLoan(String title, String name, String date){

for(int b = 0; b < numberOfItems; b++){

if(title.equals(items[b].title)){

items[b].markOnLoan(name, date);

called = 1;

}

}

if(called == 0)

System.out.println(title + " doesn't exist");  

called = 0;

}

void listAllItems(){

for(int c = 0; c < numberOfItems; c++){

if (items[c].onLoan)

str[c] = " " + items[c].title + " " + items[c].format + " loaned to " + items[c].loanedTo + " on " + items[c].dateLoaned;

else

str[c] = " " + items[c].title + " " + items[c].format;

System.out.println(str[c] + " ");

}

}

void markItemReturned(String title){

for(int b = 0; b < numberOfItems; b++){

if(title.equals(items[b].title)){

items[b].markReturned();

check = 1;

}   

}

if(check == 0)

System.out.println("Sorry, I couldn't find " + title + " in the library.");

check = 0;

}

}  

Here is my main code

import java.util.Scanner;

public class MidtermProject {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Library track = new Library();

MediaItem obj = new MediaItem();

int choice = 0;

while (choice != 5){

choice = track.displayMenu();

switch(choice){

case 1: System.out.print("What is the title? ");

obj.title = in.nextLine();

System.out.print(" To enter the format correctly; for movies, use DVD, VHS or Blue-Ray. For games, use the platform the game runs on. For example, Windows, Mac, XBox, etc.");

System.out.print(" What is the format? ");

obj.format = in.nextLine();

track.addNewItem(obj.title, obj.format);

break;

case 2: System.out.print("Which title? ");

obj.title = in.nextLine();

System.out.print("Who are you loaning it to? ");

obj.loanedTo = in.nextLine();

System.out.print("When did you loan the item? ");

obj.dateLoaned = in.nextLine();

track.markItemOnLoan(obj.title, obj.loanedTo, obj.dateLoaned);

break;

case 3: track.listAllItems();

break;

case 4: System.out.println("Which title? ");

obj.title = in.nextLine();

track.markItemReturned(obj.title);

break;

case 5: System.out.println("Goodbye");

}

}

in.close();

}

}

Explanation / Answer

Hi, I have added required message.

Please let me know in case of ayn doubt.

import java.util.Scanner;

public class MidtermProject {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       Library track = new Library();

       MediaItem obj = new MediaItem();

       int choice = 0;

       while (choice != 5){

           choice = track.displayMenu();

           switch(choice){

           case 1: System.out.print("What is the title? ");

           obj.title = in.nextLine();

           System.out.print(" To enter the format correctly; for movies, use DVD, VHS or Blue-Ray. For games, use the platform the game runs on. For example, Windows, Mac, XBox, etc.");

           System.out.print(" What is the format? ");

           obj.format = in.nextLine();

           track.addNewItem(obj.title, obj.format);

           break;

           case 2: System.out.print("Which title? ");

           obj.title = in.nextLine();

           System.out.print("Who are you loaning it to? ");

           obj.loanedTo = in.nextLine();

           System.out.print("When did you loan the item? ");

           obj.dateLoaned = in.nextLine();

           track.markItemOnLoan(obj.title, obj.loanedTo, obj.dateLoaned);

           break;

           case 3: track.listAllItems();

           break;

           case 4: System.out.println("Which title? ");

           obj.title = in.nextLine();

           track.markItemReturned(obj.title);

           break;

           case 5: System.out.println("Goodbye");

               break;

           default:

               System.out.println("invalid option");

           }

       }

       in.close();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote