So i have this code that i am using arrays of [60] and how if i add an object can i edit it afterwards in java?

StackOverflow https://stackoverflow.com/questions/23446074

  •  14-07-2023
  •  | 
  •  

سؤال

In my program, after I add a book I can see it when I press the (4) command in the menu.

How will I be able to edit it using the (2) command in the menu?

import java.lang.Object.*;
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;

public class library {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {    
        Management manager = new Management();
        Scanner input = new Scanner( System.in );
        System.out.println("\n\t\t^*^*^*^*^*^*^* LIBRARY  MANAGEMENT ^*^*^*^*^*^*^");

        while(true){
            System.out.println("------------------MENU----------" +
                    "---------------------");
            System.out.print("\nENTER UR CHOICE\n\t1:Add a new Book\n\t2:Edit Book Infos\n\t3:Search a Book (with ISBN)\n\t4:Show all the Books\n\t5:Delete a Book (with ISBN)\n\t6:Exit \n   :");
            int selection;
            selection=input.nextInt();
            if (selection == 1)
            {
                Book book = new Book();
                System.out.println("Adding a new book ");
                String empty = input.nextLine();
                System.out.println("name of book:");
                book.name = input.nextLine();
                System.out.println("Author:");
                book.author = input.nextLine();
                System.out.println("ISBN:");
                book.isbn = input.nextLine();
                System.out.println("Number of copies:");
                book.number = input.nextInt();
                System.out.println("");
                manager.AddBook(book);

            }
            else
            $$$$$$$$        **if (selection == 2)
            {
                System.out.println("Editing a Book ");
                String empty = input.nextLine();**$$$$$$$$$$
            }
            else
            if (selection == 3)
            {
                System.out.println("Searching a  Book ");
                String empty = input.nextLine();
            }
            else
            if (selection == 4)
            {
                System.out.println("You Choose to  view all       the Books  ");
                manager.PrintAllBooks();
            }
            else
            if (selection == 5)
            {
                System.out.println("You Choose to Delete a Book ");
                String empty = input.nextLine();
            }
            else
            if(selection == 6)
            {
                System.out.println("Library System Terminated!!! ");
                String empty = input.nextLine();
                System.exit(0);
            }
            else
            {
                System.out.println("Wrong Choice");
            }

        }
    }

Here's the Book class:

public class Book {
    public String isbn;
    public String name;
    public String author;
    public int number;
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    String bookinfo = name + "    ," + author + "    ," + isbn;
}

Here's the Management class:

public class Management  {
    private  Book[] books =new Book [60];
    int current = 0;
    public void AddBook(Book book)
    {
        books[current]=book;
        current++;
    }
    public void PrintAllBooks()
    {
        for (int i=0;i<current;i++)
        {
            Book b = books[i];
            System.out.println(b.name);
        }
    }
    public void Editbooks()
    {
        for (int i=0;i<current;i++)
        {
            Book b = books[i];
        }
    }
}
هل كانت مفيدة؟

المحلول

You will have to know the array index of the book you want to edit. Or some how you will have to have the reference to book object that you want to edit.

Once you have the index that you want to edit. You can do something like this.

Replace this by

$$$$$$$$        **if (selection == 2)
            {
                System.out.println("Editing a Book ");
                String empty = input.nextLine();**$$$$$$$$$$
            }

This

if (selection == 2) {
            System.out.println("Editing a Book ");
            System.out.println("Please enter title of book to edit:");
            String title = input.next();

            Book editingBook = findBookByTitle(title);
            if(editingBook == null) {
                System.out.println("Sorry no book found with title name = " + title);
            } else {
                // Now ask user for new price etc what ever you want to edit.
                System.out.println("Please enter new price:");
                int newPrice = input.nextInt();
                editingBook.setPrice(newPrice);
                // etc. other editing to book.
            }
        }

In Addition you will have to add a method in your manager class. called findBookByTitle

/**
     * This function takes a book title and search in the book array.
     * 
     * @param title
     * @return returns null if title is not found.
     */
    public Book findBookByTitle(String title) {
        Book searchBook = null;
        for (Book book : books) {
            if(book.getTitle().equalsIgnoreCase(title)) {
                searchBook = book;
                break;
            }
        }
        return searchBook;
    }

Hope this helps :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top