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

What is the code in Java? Use books as an object. One of the main purposes of th

ID: 3876555 • Letter: W

Question

What is the code in Java? Use books as an object.

One of the main purposes of this assignment is to review and refresh many of the concepts covered in CSIS-1400 It also is an opportunity to demonstrate that you understood how to use doc comments and how to create jar files. Learning Objectives: Design and implement a class access class members of another class use an ArrayList use a static field "read user input from keyboard " provide user choices with a menu use repetition statements use selection statements override toString create a jar file that includes the Java source code "use doc comments to provide useful documentation for your code Description: In this assignment l encourage you to work with a partner so you can help each other refresh concepts that have beern covered in CSIS-1400. It is important to be responsive and that each partner contributes his/her fair share. If you have concerns about your partner bring it up as early as possible. First try to resolve the issue with your partner. If that is difficult talk to your instructure Each code file should include a comment on top that lists both students and the name of the assignment. Together write a program that keeps track of a list of items and that provides the user with a menu that allows the user to add, remove, list items, etc. For more details read the instructions belovw Instructions: Decide which items you would like to store in the list. (e.g. books, bikes, gemstones, etc.) It can by any thing but not cars (I used that for the example) and not people Create a class that represents the item you chose Here are some requirements your class needs to fulfill It can not be cars or people (see above) The class needs to have at least 3 attributes you are keeping track of (e.g. year, make, model) It needs to have two additional fields: o a unique id that cannot be changed once created (like a primary key in a database) o a static count that is used to initialize the id with a unique number Notice: At this point, we have a total of at least 5 attributes It needs a parameterized constructor It allows the user to provide values for all the attributes you are keeping track of but not for the id nor for the count. (in our case that would be 3 parameters: year, make, and model) The constructor creates a unique id e.g. an 8 digit number for each item based on the static field count. (e.g. 12345678+count++; In this example the smallest id would be 12345678) It needs a getter for each of the fields except for the static count (in our case that would be 4 getters) The static count should not be exposed to another class. It is only used to initialize the unique id in the

Explanation / Answer

Here is the full production level code meeting all your requirements. It has a Book class to uniquely represent the data structure for a book. It has Main class which handles all the functions and the required Input/Output. The code is self explanatory and has relevant comments for you to understand it.

import java.util.ArrayList;

import java.util.Scanner;

// data structure for storing a book object

class Book{

// static counter to assign unique id's

private static long count = 12345678l;

// unique ID for a book

private final long id;

// other attributes related to book

private String name;

private String author;

private String publication;

// parameterized constructor

public Book(String name, String author, String publication){

id = count;

count++;

this.name = name;

this.author = author;

this.publication = publication;

}

// getter methods for attributes

public String getName(){

return name;

}

public String getAuthor(){

return author;

}

public String getPublication(){

return publication;

}

public long getUniqueID(){

return id;

}

@Override

public String toString(){

String result = name + " by " + author + ", published by " + publication;

return result;

}

}

class Main{

// lists for books in current list

private ArrayList<Book> books;

// list for deleted books

private ArrayList<Book> deletedBooks;

public Main(){

books = new ArrayList<>();

deletedBooks = new ArrayList<>();

}

// method to initialize the list with 4 books

private void initialize(){

addBook(new Book("Introduction to Algorithms","C.L.R.S.","MIT Press"));

addBook(new Book("Operating System Concepts", "Silberchatz and Galvin", "Wiley"));

addBook(new Book("OCA/OCP Java SE 8", "Kathy Sierra", "Oracle Press"));

addBook(new Book("Database Systems","Garcia Molina","Pearson"));

}

// helper function to add book to list

private void addBook(Book book){

books.add(book);

}

// helper function to search for a book

private int indexOf(long id, boolean isDeleted){

ArrayList<Book> temp = (isDeleted)?deletedBooks:books;

int index;

for (index = 0; index < temp.size(); index++){

if(temp.get(index).getUniqueID() == id)

return index;

}

return -1;

}

// find book according to id

private void find(long id){

Book book;

int index = indexOf(id, false);

if (index == -1){

// check if book was deleted before

index = indexOf(id, true);

if (index == -1)

System.out.println("The id: " + id + " could not be found.");

else

System.out.println(deletedBooks.get(index) + " has been deleted.");

}

else{

book = books.get(index);

System.out.println(book+" id: "+id);

}

}

// delete a book with unique id

private void delete(long id){

Book book;

int index = indexOf(id, false);

if (index == -1){

// check if book was deleted before

index = indexOf(id, true);

if (index == -1)

System.out.println("The id: " + id + " could not be found.");

else

System.out.println(deletedBooks.get(index) + " has already been deleted.");

}

else{

book = books.get(index);

books.remove(index);

deletedBooks.add(book);

System.out.println(book+" has been deleted.");

}

}

// function to display all books

private void displayAll(){

for(Book book : books){

System.out.println(book+" id:"+book.getUniqueID());

}

System.out.println();

}

// function to display the menu to user

private static void displayMenu(){

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

System.out.println("2. Add a book");

System.out.println("3. Find a book");

System.out.println("4. Delete a book");

System.out.println("5. Number of books in list");

System.out.println("6. Exit");

System.out.print("Enter your selection: ");

}

public static void main(String[] args){

Scanner keyboard = new Scanner(System.in);

Main main = new Main();

main.initialize();

displayMenu();

int userChoice = Integer.parseInt(keyboard.nextLine());

System.out.println();

while(userChoice != 6){

switch(userChoice){

case 1:

main.displayAll();

break;

case 2:

System.out.print("Name of book: ");

String name = keyboard.nextLine();

System.out.print("Author of book: ");

String author = keyboard.nextLine();

System.out.print("Publication of book: ");

String publication = keyboard.nextLine();

Book newBook = new Book(name, author, publication);

main.addBook(newBook);

break;

case 3:

System.out.print("id: ");

long id = Long.parseLong(keyboard.nextLine());

main.find(id);

break;

case 4:

System.out.print("id: ");

long uniqueID = Long.parseLong(keyboard.nextLine());

main.delete(uniqueID);

break;

case 5:

System.out.println("Number of books : "+main.books.size());

break;

default:

System.out.println("Enter a number between 0-6");

}

System.out.println();

displayMenu();

userChoice = Integer.parseInt(keyboard.nextLine());

System.out.println();

}

System.out.println("Good Bye!");

keyboard.close();

}

}


Hope it helps!! :)

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