Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a java program here, but I need a .txt file that can be used (imported to java) in this program. Can you please

I have a java program here, but I need a .txt file that can be used (imported to java) in this program. Can you please create a text file that can fit into this program?

Program Summary

Develop java program to create a Library Collection Management System, which will be used to manage a librarys collection of materials (books, magazines, and videos), as well as patrons and the materials they have checked out.

File Input

This program should also be able to read in a file of entries where each line will contain data for an individual patron.

This program should also be able to read in a file of entries where each line will contain data for a separate book, magazine, or video, along with information indicating whether the book is checked out, which patron checked out the book, and the due date.

Required Menu System Options

The program should have a robust menu system that offers the user choices like displaying all the materials in the library, sorting by author/title/type, searching for specific materials by author/title/type, and adding new entries that will be saved when the program ends.

(1) Manage Materials:

1. Display all materials: Sorted by author, Sorted by title, Sorted by type

2. Search for materials: Search by author, Search by title, Search by type

3. Checkout a material

4. Return a checked out material

5. Display all overdue materials

6. Add a new material

7. Remove a material

(2) Manage Patrons:

1. Display all patrons

2. Display all patrons with overdue materials

3. Add a new patron

4. Remove a patron

(3) Load/Save Data:

1. Load Data from File

2. Save Data to File

(4) Quit Program

**JAVA PROGRAM**

Please upvote if you find the answer satisfactory :)

Please comment in case you run into any errors.

The package for these classes is LibraryManagement.

You need a file called myObjects.txt in the package, and you should change the filePath variable in LoadData class - to where you are keeping the myObject.txt file, otherwise, the code will throw a FileNotFoundException.

MENU CLASS

package LibraryManagement;

import java.util.Scanner;

public class Menu {

public static Scanner sc;

public static void printMainMenu(){

System.out.println("(1) Manage Materials");

System.out.println("(2) Manage Patrons");

System.out.println("(3) Load/Save Data");

System.out.println("(4) Quit Program");

}

public static void manageMaterialsMenu(){

System.out.println("1. Display all materials: Sorted by author");

System.out.println("2. Display all materials: Sorted by title");

System.out.println("3. Display all materials: Sorted by type");

System.out.println("4. Search by author");

System.out.println("5. Search by title");

System.out.println("6. Search by type");

System.out.println("7. Checkout a material");

System.out.println("8. Return a checked out material");

System.out.println("9. Display all overdue materials");

System.out.println("10. Add a new Material");

System.out.println("11. Remove a Material");

System.out.println("12. Go Back");

}

public static void managePatronsMenu(){

System.out.println("1. Display all patrons");

System.out.println("2. Display all patrons with overdue materials");

System.out.println("3. Add a new patron");

System.out.println("4. Remove a patron");

System.out.println("5. Go Back");

}

public static void loadSaveDataMenu(){

System.out.println("1. Load Data from file");

System.out.println("2. Save Data to file");

System.out.println("3. Go Back");

}

public static void main(String[] args) throws Exception {

sc = new Scanner(System.in);

printMainMenu();

int option = sc.nextInt();

int option2=0;

while (true) {

switch (option) {

case 0:{

printMainMenu();

option = sc.nextInt();

break;

}

case 1: {

manageMaterialsMenu();

option2 = sc.nextInt();

option=callSubOption(1,option2);

break;

}

case 2: {

managePatronsMenu();

option2 = sc.nextInt();

option=callSubOption(2,option2);

break;

}

case 3: {

loadSaveDataMenu();

option2 = sc.nextInt();

option=callSubOption(3,option2);

break;

}

case 4: {

System.exit(0);

break;

}

default: System.out.println("Wrong option");

}

}

}

private static int callSubOption(int option1, int option2) throws Exception {

switch(option1){

case 1:{

switch(option2){

case 1:{

//display all sorted by author

Materials.displayAllByAuthor();

break;

}

case 2:{

//display all sorted by title

Materials.displayAllByTitle();

break;

}

case 3:{

//display all sorted by type

Materials.displayAllByType();

break;

}

case 4:{

//search author

String input=sc.nextLine();

Materials.searchByAuthor(input);

break;

}

case 5:{

//search title

String input=sc.nextLine();

Materials.searchByTitle(input);

break;

}

case 6:{

//search type

String input=sc.nextLine();

Materials.searchByType(input);

break;

}

case 7:{

//checkout

Materials.checkOut();

break;

}

case 8:{

//return checkout

Materials.returnCheckedOut();

break;

}

case 9:{

//display over due material

LoadData.displayOverDueList();

break;

}

case 10:{

//add material

Materials.addMaterial();

break;

}

case 11:{

//remove material

Materials.removeMaterial();

break;

}

case 12:{

return 0;

}

default: return option1;

}

}

case 2:{

switch(option2){

case 1:{

//display all patrons

LoadData.displayPatronList();

break;

}

case 2:{

//display patrons with overdue

LoadData.displayPatronOverDueList();

break;

}

case 3:{

//add patron

Patron.addPatron();

break;

}

case 4:{

//remove patron

Patron.removePatron(0);

break;

}

case 5:{

//back

return 0;

}

}

break;

}

case 3:{

switch(option2){

case 1:{

LoadData.loadData();

break;

}

case 2:{

LoadData.writeData();

break;

}

case 3:{

//back

return 0;

}

}

break;

}

}

return 0;

}

}

LOADDATA CLASS

package LibraryManagement;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

public class LoadData {

public static ArrayList list=new ArrayList(0);

public static ArrayList patronList=new ArrayList(0);

public static String filePath="src"+File.separator+"LibraryManagement"+File.separator+"myObjects.txt";

public static void displayList(){

for(Materials m: list)

{

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

System.out.println("_______________");

}

if(list.size()==0)

{

System.out.println("No materials");

}

}

public static void displayPatronList(){

for(Patron m: patronList)

{

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

System.out.println("________________");

}

if(patronList.size()==0)

{

System.out.println("No patrons");

}

}

public static void displayPatronOverDueList(){

boolean found=false;

for(Materials m: list)

{

if(m.isOverDue==true)

{

found=true;

m.getCheckoutBy().toString();

System.out.println("_______________");

}

}

if(found==false){

System.out.println("No over dues");

}

}

public static void displayOverDueList(){

boolean found=false;

for(Materials m: list)

{

if(m.isOverDue==true)

{

found=true;

m.toString();

System.out.println("_______________");

}

}

if(found==false){

System.out.println("No over dues");

}

}

public static void loadData() throws Exception{

System.out.println("Load data::");

FileInputStream fi = new FileInputStream(new File(filePath));

ObjectInputStream oi=null;

try {

oi = new ObjectInputStream(fi);

while (true) {

Materials m = (Materials) oi.readObject();

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

System.out.println("_______________");

list.add(m);

if(m.getCheckoutBy().getpId()!=0 && Patron.searchForPatron(m.getCheckoutBy())==null){

patronList.add(m.getCheckoutBy());

}

}

} catch (EOFException e) {

System.out.println("___End of File___");

} finally {

if(oi!=null)

oi.close();

fi.close();

}

}

public static void writeData() throws Exception{

System.out.println("Write data::");

FileOutputStream f=null;

ObjectOutputStream o=null;

try{

f = new FileOutputStream(new File(filePath));

o = new ObjectOutputStream(f);

Materials obj=new Materials("a","t","magazine",new Patron(123, "p1"),false);

list.add(obj);

//o.writeObject(obj);

for(Materials m: list)

{

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

System.out.println("_______________");

o.writeObject(m);

}

}catch (Exception e) {

e.printStackTrace();

}finally {

try{

o.close();

f.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

}

PATRON CLASS

package LibraryManagement;

import java.io.Serializable;

import java.util.Iterator;

public class Patron implements Serializable {

/**

*

*/

private static final long serialVersionUID = -618919888987997367L;

public Patron(){

this.pId = 0;

this.name = "";

}

public Patron(int pId, String name) {

super();

this.pId = pId;

this.name = name;

}

public int getpId() {

return pId;

}

public void setpId(int pId) {

this.pId = pId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

int pId;

String name;

@Override

public String toString() {

return "Patron [ID=" + pId + ", Name=" + name + "]";

}

public static long getSerialversionuid() {

return serialVersionUID;

}

public static Patron searchForPatron(Patron t) {

System.out.println("Searching for Patron::");

for(Patron p: LoadData.patronList)

{

if(p.getName().equalsIgnoreCase(t.getName())

&& p.getpId()==t.getpId() )

{

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

System.out.println("_______________");

return p;

}

}

System.out.println("Not found");

return null;

}

public static Patron searchForPatronById(int t) {

System.out.println("Searching for Patron by Id::");

for(Patron p: LoadData.patronList)

{

if( p.getpId()==t )

{

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

System.out.println("_______________");

return p;

}

}

System.out.println("Not found");

return null;

}

public static Patron removePatron(int id) {

Patron p=null;

if(id==0){

System.out.println("Enter id:");

int pid=Menu.sc.nextInt();

p=searchForPatronById(id);

if(p==null)

{

System.out.println("Patron not found");

return null;

}

}

System.out.println("Removing Patron::");

Iterator itr=(Iterator) LoadData.patronList.iterator();

while(itr.hasNext()){

Patron elem = (Patron)itr.next();

if (elem.getpId()==id){

itr.remove();

return elem;

}

}

return null;

}

public static void addPatron() {

System.out.println("Enter name:");

String name=Menu.sc.nextLine();

Menu.sc.nextLine();

System.out.println("Enter pid:");

int pid=Menu.sc.nextInt();

Patron p=new Patron(pid,name);

if(searchForPatron(p)==null)

{

LoadData.patronList.add(p);

System.out.println("Added");

}

else

System.out.println("Patron already exists");

}

}

MATERIALS CLASS

package LibraryManagement;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

public class Materials implements Serializable {

//default serialVersion id

private static final long serialVersionUID = 1L;

String author;

String title;

String type;

boolean isOverDue;

Patron checkoutBy;

public String getAuthor() {

return author;

}

public Materials(String author, String title, String type, Patron checkoutBy,boolean isOverDue) {

super();

this.author = author;

this.title = title;

this.type = type;

this.checkoutBy = checkoutBy;

this.isOverDue=isOverDue;

}

public void setAuthor(String author) {

this.author = author;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public Patron getCheckoutBy() {

return checkoutBy;

}

public void setCheckoutBy(Patron checkoutBy) {

this.checkoutBy = checkoutBy;

}

public boolean isCheckedOut() {

return !(checkoutBy.getpId()==0);

}

@Override

public String toString() {

return "Author:" + author + " Title: " + title + " Type: " + type+ " Checked out by: " +((checkoutBy.getpId()==0)?"Noone":checkoutBy.toString());

}

public static void displayAllByAuthor(){

Collections.sort(LoadData.list,new sortByAuthor());

LoadData.displayList();

}

public static void displayAllByTitle(){

Collections.sort(LoadData.list,new sortByTitle());

LoadData.displayList();

}

public static void displayAllByType(){

Collections.sort(LoadData.list,new sortByType());

LoadData.displayList();

}

public static void displayAllByPatron(Patron p,int i){

}

public static void searchByAuthor(String a) {

System.out.println("Searching by Author::");

boolean flag=false;

for(Materials m: LoadData.list)

{

if(m.getAuthor().equalsIgnoreCase(a))

{

flag=true;

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

System.out.println("_______________");

}

}

if(flag==false){

System.out.println("Not found");

}

}

public static void searchByType(String t) {

System.out.println("Searching by Type::");

boolean flag=false;

for(Materials m: LoadData.list)

{

if(m.getType().equalsIgnoreCase(t))

{

flag=true;

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

System.out.println("_______________");

}

}

if(flag==false){

System.out.println("Not found");

}

}

public static void searchByTitle(String t) {

System.out.println("Searching by Title::");

boolean flag=false;

for(Materials m: LoadData.list)

{

if(m.getTitle().equalsIgnoreCase(t))

{

flag=true;

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

System.out.println("_______________");

}

}

if(flag==false){

System.out.println("Not found");

}

}

public static Materials searchForMaterial(Materials t) {

System.out.println("Searching by Title::");

for(Materials m: LoadData.list)

{

if(m.getTitle().equalsIgnoreCase(t.getTitle())

&& m.getType().equalsIgnoreCase(t.getType())

&& m.getAuthor().equalsIgnoreCase(t.getAuthor()))

{

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

System.out.println("_______________");

return m;

}

}

System.out.println("Not found");

return null;

}

public static Materials updateCheckedOutBy(Materials t,Patron p) {

System.out.println("Searching by Title::");

for(Materials m: LoadData.list)

{

if(m.getTitle().equalsIgnoreCase(t.getTitle())

&& m.getType().equalsIgnoreCase(t.getType())

&& m.getAuthor().equalsIgnoreCase(t.getAuthor()))

{

m.setOverDue(t.isOverDue());

m.setCheckoutBy(p);

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

System.out.println("_______________");

return m;

}

}

System.out.println("Not found");

return null;

}

public static class sortByAuthor implements Comparator

{

public int compare(Materials a, Materials b)

{

return a.author.compareTo(b.author);

}

}

public static class sortByTitle implements Comparator

{

public int compare(Materials a, Materials b)

{

return a.title.compareTo(b.title);

}

}

public static class sortByType implements Comparator

{

public int compare(Materials a, Materials b)

{

return a.type.compareTo(b.type);

}

}

public static void addMaterial() {

Menu.sc.nextLine();

System.out.println("Enter author:");

String auth=Menu.sc.nextLine();

System.out.println("Enter title:");

String title=Menu.sc.nextLine();

System.out.println("Enter type:");

String type=Menu.sc.nextLine();

Materials m=new Materials(auth,title,type,new Patron(),false);

if(searchForMaterial(m)==null)

{

LoadData.list.add(m);

System.out.println("Added");

}

else

System.out.println("Material already exists");

}

public static void checkOut() {

System.out.println("Enter author:");

String auth=Menu.sc.nextLine();

Menu.sc.nextLine();

System.out.println("Enter title:");

String title=Menu.sc.nextLine();

System.out.println("Enter type:");

String type=Menu.sc.nextLine();

Materials m=new Materials(auth,title,type,new Patron(),false);

Materials search=searchForMaterial(m);

if(search!=null)

{

System.out.println("Enter Patron id:");

int id=Menu.sc.nextInt();

System.out.println("Enter Name:");

String name=Menu.sc.nextLine();

//System.out.println("Enter isOverdue:");

boolean isOverdue=false;

search.isOverDue=isOverdue;

Patron p=new Patron(id,name);

Patron searchP=Patron.searchForPatron(p);

if(searchP!=null){

search.setCheckoutBy(searchP);

updateCheckedOutBy(search,searchP);

}

else

{

System.out.println("Patron not found");

}

}

else

System.out.println("Material already exists");

}

public boolean isOverDue() {

return isOverDue;

}

public void setOverDue(boolean isOverDue) {

this.isOverDue = isOverDue;

}

public static Materials removeMaterial() {

Menu.sc.nextLine();

System.out.println("Enter author:");

String auth=Menu.sc.nextLine();

System.out.println("Enter title:");

String title=Menu.sc.nextLine();

System.out.println("Enter type:");

String type=Menu.sc.nextLine();

Materials m=new Materials(auth,title,type,new Patron(),false);

Materials searchM=searchForMaterial(m);

Patron p=null;

if(searchM.getCheckoutBy().pId==0){

System.out.println("Removing Material::");

Iterator itr=(Iterator) LoadData.list.iterator();

while(itr.hasNext()){

Materials elem = (Materials)itr.next();

itr.remove();

return elem;

}

}

else{

System.out.println("Material has been checked out by:"+searchM.getCheckoutBy().toString());

if(searchM.isOverDue==true)

{

System.out.println("It is also overdue");

}

}

return null;

}

public static void returnCheckedOut() {

System.out.println("Enter Patron id:");

int id=Menu.sc.nextInt();

System.out.println("Enter Name:");

String name=Menu.sc.nextLine();

Patron p=new Patron(id,name);

Patron searchP=Patron.searchForPatron(p);

if(searchP==null){

System.out.println("Patron not found");

return;

}

else{

//displayAllByPatron(searchP);

boolean found=false;

int count=1;

ArrayList checkoutMaterials=new ArrayList();

for(Materials m: LoadData.list)

{

if(m.getCheckoutBy().getpId()==p.getpId())

{

found=true;

System.out.println(count++ +". "+m.toString());

System.out.println("_______________");

checkoutMaterials.add(m);

}

}

if(found==false){

System.out.println("No checkouts");

return;

}

else{

int tocheckout=Menu.sc.nextInt();

LoadData.list.get(tocheckout-1).getCheckoutBy().setName("");

LoadData.list.get(tocheckout-1).getCheckoutBy().setpId(0);

LoadData.list.get(tocheckout-1).setOverDue(false);

//search.isOverDue=isOverdue;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions

Question

LO 211 What is thinking?

Answered: 1 week ago