Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Delete method not deleting hard coded books

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delete method not deleting hard coded books

    deleted
    Last edited by Seph_Mon; April 7th, 2021 at 05:42 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Delete method not deleting hard coded books

    Do you have some print outs that show the problem? Please post them and add some comments where the problem is.

    Note: The code for ILibrary is missing

    Also posted here: https://coderanch.com/t/741159/java/...ing-hard-coded
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Delete method not deleting hard coded books

    deleted
    Last edited by Seph_Mon; April 7th, 2021 at 05:42 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Delete method not deleting hard coded books

    The code with my debug statements:
    /* https://www.javaprogrammingforums.com/whats-wrong-my-code/43659-delete-method-not-deleting-hard-coded-books.html#post171300
     
    Note: delete works / code adds three books every time
    */
     
    import java.util.Random;
    import java.util.*;
    import java.util.ArrayList;
    import java.util.Objects;
    import java.util.Random;
    import java.util.Scanner;
     
     
     
    public class MainLibrary1 {
     
        Library lib = new Library();
        public Scanner scan = new Scanner("4\n3\nNeverwhere\n4\n0\n"); //System.in);
     
     
        public static void main(String[] args) {
     
            MainLibrary1 main = new MainLibrary1();
            main.startPoint();
        }
     
        public void startPoint(){
            welcomeMessage();
            menuHeader();
            getUserInput();
        }
     
        private void welcomeMessage(){
            System.out.println("********************");
            System.out.println("*       Cork       *");
            System.out.println("*      Library     *");
            System.out.println("********************");
        }
     
        private void menuHeader(){
            System.out.println("********************");
            System.out.println("*       Menu       *");
            System.out.println("*     Options      *");
            System.out.println("********************");
        }
     
        private void menuOptions(){
            System.out.println("***********************");
            System.out.println("*1.) Add Book         *");
            System.out.println("*2.) Search for Book  *");
            System.out.println("*3.) Delete Book      *");
            System.out.println("*4.) Display all Books*");
            System.out.println("*0.) Exit Program     *");
            System.out.println("***********************");
        }
     
        private void getUserInput(){
            int choice;
            menuOptions();
            do{
                System.out.println(">");
                while(!scan.hasNext()){                         //???? nextInt
                    scan.next();
                    System.out.println("INTEGERS ONLY PLEASE");
                }
                choice = scan.nextInt();
                scan.nextLine();
                menuSelection(choice);
            }while(choice!= 0);
        }
     
        private void menuSelection(int choice){
            switch(choice){
                case 1: enterBookInfo();
                break;
     
                case 2:
                lib.bookInStock();
                inputSearch();
                break;
     
                case 3: inputBookToDelete();
                break;
     
                case 4:
                lib.bookInStock();       //???? keeps adding 3 books
                lib.printListOfBook();
                break;
     
                case 0:
    //            lib.exit();            //<<<<<<<< error: cannot find symbol  method exit()
                  System.exit(0); //<<<<<< ADDED
                break;
     
                default:
                    break;
            }
        }
     
        private void enterBookInfo(){
            while(lib.keepGoing){
     
                System.out.println("Please enter name of book: ");
                String bookName = scan.nextLine();
     
                if (bookName.equalsIgnoreCase("end"))
                {
                    break;
                }
     
                System.out.println("Please enter author: ");
                String authorName = scan.nextLine();
     
                System.out.println("Please enter ISBN Number: ");
                String isbnNumber = scan.nextLine();
     
                System.out.println("Please enter number of books in stock: ");
                int numberOfCopiesInStock = scan.nextInt();
                scan.nextLine();
     
                Book book = new Book(bookName, authorName, numberOfCopiesInStock, isbnNumber);
                lib.bookList.add(book);
            }
        }
     
        private void inputSearch(){
            System.out.println("\n**********Search for Book**********");
            System.out.println("Please enter BookName: ");
            lib.search = scan.nextLine();
            makeTheComputerSleep();
            lib.searchForBooks();
        }
     
        private void inputBookToDelete(){
     
            System.out.println("\n**********Delete Employee**********");
            System.out.println("Please enter book name: ");
            lib.deleteBookName = scan.nextLine();
            lib.deleteBooks();
        }
     
        public void makeTheComputerSleep(){
            int number = lib.random.nextInt(10)+1;
            System.out.println("Check system......please wait....");
            try{
                Thread.sleep(number * 1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    //>>>>>>>>>rb
     
    static interface ILibrary {
     
        int MAX_NUMBER_OF_COPIES = 3;
        int MIN_NUMBER_OF_COPIES = 0;
        int ISBN_MAX_NUMBER_OF_DIGITS = 10;
    }
     
     
    static class Book implements ILibrary {
     
        private String bookName;
        private String authorName;
        private int numberOfCopiesInStock;
        private String Isbn;
     
        public Book(String bookName,String authorName,int numberOfCopiesInStock,String Isbn){
            this.setBookName(bookName);
            this.setAuthorName(authorName);
            this.setNumberOfCopiesInStock(numberOfCopiesInStock);
            this.setIsbn(Isbn);
        }
     
        public void setBookName(String bookName)throws IllegalArgumentException{
            if(bookName.isEmpty()){
                throw new IllegalArgumentException("FIELD CANNOT BE EMPTY!");
            }else{
                this.bookName = bookName;
            }
        }
     
        public void setAuthorName(String authorName)throws IllegalArgumentException{
            if(authorName.isEmpty()){
                throw new IllegalArgumentException("FIELD CANNOT BE EMPTY");
            }else{
                this.authorName = authorName;
            }
        }
     
        public void setNumberOfCopiesInStock(int numberOfCopiesInStock)throws IllegalArgumentException{
            if(numberOfCopiesInStock < MIN_NUMBER_OF_COPIES || numberOfCopiesInStock > MAX_NUMBER_OF_COPIES){
                throw new IllegalArgumentException("INVALID NUMBER OF COPIES ENTERED INTO STOCK!");
            }else{
                this.numberOfCopiesInStock = numberOfCopiesInStock;
            }
        }
     
        public void setIsbn(String Isbn)throws IllegalArgumentException{
            if(Isbn.length() < ISBN_MAX_NUMBER_OF_DIGITS || Isbn.length() > ISBN_MAX_NUMBER_OF_DIGITS){
                throw new IllegalArgumentException("ISBN LENGTH ENTERED IS INVALID,TRY AGAIN");
            }else{
                this.Isbn = Isbn;
            }
        }
     
        public String getBookName(){
            return this.bookName;
        }
     
        public String getAuthorName(){
            return this.authorName;
        }
     
        public int getNumberOfCopiesInStock(){
            return this.numberOfCopiesInStock;
        }
     
        public String getIsbn(){
            return this.Isbn;
        }
     
        public String toString(){
            return("BookName: " + getBookName()
                    +"\n"
                    +"AuthorName: " + getAuthorName()
                    +"\n"
                    +"Number of Copies: " + getNumberOfCopiesInStock()
                    +"\n"
                    +"ISBN: " + getIsbn());
        }
     
     
     
     
     
    }
     
     
    static class Library {
     
        public String search;
        public String deleteBookName;
        public boolean keepGoing = true;
        public Random random = new Random();
        public ArrayList<Book> bookList = new ArrayList<Book>();
     
        public void bookInStock(){
            Book book1 = new Book("Neverwhere","Neil Gaimen",2,"A100023454");
            Book book2 = new Book("American Gods","Neil Gaimen",1,"A100023454");
            Book book3 = new Book("I.T","Stephen King",0,"A123432454");
            bookList.add(book1);
            bookList.add(book2);
            bookList.add(book3);
            System.out.println(">>>>>>>> END adding 3 books <<<<<<<<<");  //  Called more than one time!!!
        }
     
        public Boolean searchForBooks(){
            for(Book b : bookList){
     
                if(b.getBookName().equalsIgnoreCase(search)){
                    System.out.println(b);
                    return true;
                }
            }
            System.out.println("BOOK DOESN'T EXIST! ");
            return false;
        }
     
        public void printListOfBook(){
            for(Book b : bookList){
                System.out.println(b.toString());
                System.out.println();
            }
        }
     
        public void deleteBooks(){
            for(Book b : bookList){
                System.out.println("deleteBookName="+deleteBookName + "< vs >" + b.getBookName()+"<");
                if(deleteBookName.equalsIgnoreCase(b.getBookName())){
                    boolean results = bookList.remove(b);
                    System.out.println("results="+results + " for b=" + b); //results=true for b=BookName: Neverwhere
                    System.out.println("bL="+bookList + "\n<<<< end");
                    break;
                }
            }
        }
       }
     
    } // end class

    How are you trying to debug the code to find the problem?
    I suggest adding some print statements that show the values of variables as they are changed and used
    and to show the values and results in the delete method.
    The print out will help you find the problem.

    Note: Several of the methods are poorly named. Method names should be verbs describing what the method is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Delete method not deleting hard coded books

    Thread can closed,thanks

Similar Threads

  1. [SOLVED] Deleting an element out of a linked list with boolean method
    By arhzz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2020, 09:53 AM
  2. hard-coded text field looks different in LInux/Mac
    By iamhealed in forum AWT / Java Swing
    Replies: 1
    Last Post: September 9th, 2012, 09:45 AM
  3. help with delete method of linked list
    By sonicjr in forum Collections and Generics
    Replies: 1
    Last Post: February 18th, 2012, 09:21 PM
  4. undo delete method
    By Trunk Monkeey in forum Object Oriented Programming
    Replies: 7
    Last Post: February 7th, 2012, 03:49 PM
  5. please need help ... for the delete method in array
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2012, 03:22 PM