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 12 of 12

Thread: Java adding borrow and return books in library

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java adding borrow and return books in library

    Hello can you help me with adding the borrow-return system to my library, first class is book class with basic attributes, second class is a library with an array of books and third is main with 0 to end the program, 1 adds book, 2 print list of all added books. Whats the best way to add borrow and return system here?


    **UPDATED**

    public class Kniha {
     
            private String Nazov, Autor, Zaner;
    	private int ISBN, pocetStran;
            boolean pozicana;
     
    	public Kniha(int ISBN, String Nazov, String Autor, int pocetStran, String Zaner, boolean pozicana){
    		this.ISBN = ISBN;
    		this.Nazov = Nazov;
    		this.Autor = Autor;
    		this.pocetStran = pocetStran;
                    this.Zaner = Zaner;
                    this.pozicana = pozicana;
    	}
     
     
            public String vratNazov()
                {
                    return Nazov;
                }
     
            public boolean jePozicana()
                {
                    return pozicana;
                }
     
    	public String toString(){
    		return "************************" + "\nISBN: " + ISBN + "\nNazov: " + Nazov + "\nAutor: " + Autor + "\nZaner: " + Zaner + "\nPocet Stran: " + pocetStran + "\nJe pozicana: " + pozicana + "\n";
    	}
     
    }



     
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
     
    public class Kniznica{
     
    	private List<Kniha> kniznicka;
            private ArrayList<Historia> historia = new ArrayList<Historia>();
     
    	public Kniznica(){
    		kniznicka = new ArrayList<Kniha>();
     
    	}
     
    	public void pridajKnihu(Kniha kniha){
    		kniznicka.add(kniha);
    	}
     
            public void pridajHistoriu(Historia historicka){
                   historia.add(historicka);
            }
     
     
     
            public void pozicajKnihu(String Nazov){
     
                int found = 0;
                for (Kniha b : kniznicka) 
                {
                     if (b.vratNazov().equals(Nazov)) 
                 {
                        if (found == 0) 
                        {
                            found = 1;
                        }
                            if (!b.jePozicana()) 
                        {
                            b.pozicana=true;
                            found = 2;
                            break;
                        }
                 }
                }  
     
                if (found == 0) {
                    System.out.println("Tato kniha sa u nas nenachadza");
                    } else if (found == 1) {
                System.out.println("Tato kniha je uz pozicana");
                    } else if (found == 2) {
                System.out.println("Knihu s nazvom " + Nazov + " ste uspesne pozicali");
                    }
                }
     
     
            public void vratKnihu(String Nazov){
                int found = 0;
                for (Kniha b : kniznicka) {
                     if (b.vratNazov().equals(Nazov) && b.pozicana==true) {
                         {
                            b.pozicana=false;
                            found = 1;
                        }
                    }
                }  
     
                if (found == 0) {
                    System.out.println("Tato kniha k nam nepatri");
                    } else if (found == 1) {
                System.out.println("Kniha " + Nazov + " bola vratena");
                    }
            }
     
     
    	@Override
    	public String toString() {
    		String total = "\n";
    		Iterator<Kniha> i = kniznicka.iterator();
    		while(i.hasNext()){
    			Kniha b = (Kniha) i.next();
    			total = total + b.toString();
    		}
    		return total;
    	}
     
     
    	public String totoString() {
    		String total = "\n";
    		Iterator<Historia> i = historia.iterator();
    		while(i.hasNext()){
    			Historia b = (Historia) i.next();
    			total = total + b.toString();
    		}
    		return total;
    	}
    }


    import java.util.Scanner;
     
    public class Aplikacia {
     
    	static Kniznica ZMK = new Kniznica();
    	static Scanner in = new Scanner(System.in);
    	static Boolean beh = true;
     
    	public static void main(String[] args) {
                System.out.println("**********Víta Vás Žilinská Mestská Knižnica**********");
    		while (beh) {
    			System.out.println( "\n\nVyberte si z možností?: "
                                              + "\nZadaj 0 pre opustenie kniznice"
    					  + "\nZadaj 1 na pridanie knihy"
                                              + "\nZadaj 2 pre vypisanie zoznamu knih"
                                              + "\nZadaj 3 pre požicanie knihy"
                                              + "\nZadaj 4 pre vratenie knihy"
                                              + "\nZadaj 5 pre historiu pozicanych a vratenych knih\n");
     
    			int pom = in.nextInt();
    			switch (pom) {
     
    			case 0:
                                    System.exit(0);
                                    break;	
    			case 1:
    				pridajKnihu();
    				break;
                            case 2:       
                                    System.out.println(ZMK.toString());
    				break;
                            case 3:
                                    pozicajKnihu();
    				break;
                            case 4:
                                    vratKnihu();
                                    break;
                            case 5:
                                    System.out.println(ZMK.totoString());
                                    break;
     
    			}
    		}
    		System.exit(0);
    	}
     
    	private static void pridajKnihu() {
     
     
    		int ISBN, pocetStran;
    		String Nazov, Autor, Zaner;                
                    boolean pozicana = false;
     
     
                    System.out.println("\nZadaj ISBN: ");
    		ISBN = in.nextInt();
     
    		System.out.println("\nZadaj Nazov: ");
    		Nazov = in.next();
     
    		System.out.println("\nZadaj Autora: ");
    		Autor = in.next();
     
                    System.out.println("\nEnter Zaner: ");
    		Zaner = in.next();
     
    		System.out.println("\nZadaj pocet stran: ");
    		pocetStran = in.nextInt();
     
    		Kniha b = new Kniha(ISBN, Nazov, Autor, pocetStran, Zaner, pozicana);
    		ZMK.pridajKnihu(b);             
                    System.out.println("\n\n\nKniha bola uložená!");
    	}
     
            private static void pozicajKnihu(){
     
                 String Nazov;
                 String akcia = "požičaná";
     
                 System.out.println("\nZadaj nazov knihy ktoru si chceš požičat: ");
    	     Nazov = in.next();
                 ZMK.pozicajKnihu(Nazov);
                 Historia b = new Historia(Nazov, akcia);
                 ZMK.pridajHistoriu(b);               
            }
     
           private static void vratKnihu(){
     
                 String Nazov;
                 String akcia = "vratená";
     
                 System.out.println("\nZadaj nazov knihy ktoru chces vratit: ");
    	     Nazov = in.next();
                 ZMK.vratKnihu(Nazov); 
                 Historia b = new Historia(Nazov, akcia);
                 ZMK.pridajHistoriu(b);   
            }
     
    }





    import java.util.Date;
     
     
    public class Historia {
     
     
     
        private String Nazov, akcia;
     
     
        public Historia(String Nazov, String akcia){
    		this.Nazov = Nazov;
                    this.akcia = akcia;
    	}
     
        @Override
                public String toString(){
    		return "************************" + "\nNazov: " + Nazov + "\nakcia: " + akcia + "\n";
            }
     
    }
    Last edited by Pentrax; December 20th, 2017 at 03:52 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: Java adding borrow and return books in library

    adding the borrow-return system to my library,
    What is the borrow-return system supposed to do? Can you make a list of what the code needs to do?
    When you have the list then pick a item from the list and work on how to code it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Pentrax (December 20th, 2017)

  4. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java adding borrow and return books in library

    thank you Norm for quick guidance

    1. borrow-return book system should be really easy, for example case 3(borrow) will print all the books(unborrowed) in container based on ISBN i will be able to choose which book i want and then borrow it, book status will switch into borrowed. And case 4 would be (return) it will print all the borrowed books and i will be able to return it by ISBN and status switch back to unborrowed.

    2. history of transactions case 5 would be just printing all books which were borrowed or returned

  5. #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: Java adding borrow and return books in library

    Ok, pick one of the requirements and describe in more detail what the program would need to do?
    What data structure would be required to hold book info
    What method(s) would be required to support that requirement.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java adding borrow and return books in library

    ok, so i pretty much have easy implementation of borrowed function in my library. I added boolean attribut and method isBorrowed() to my book class. I will do the oposite with return function, so i consider it done, but i really need some help with history of my library, where i will store information, so when i pres button (show history), it will show full list of books and stored times of when book atributes where changed from true to false. I have other thought in my mind and that is when i will be borrowing/returning i will add something like history ticket and i will save that to some string or something, is it good idea? Thank you for your help.


    *UPDATED* also i updated my first post with new code.

    Can you help me with that history ? how can i do it ?
    Last edited by Pentrax; December 20th, 2017 at 11:39 AM.

  7. #6
    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: Java adding borrow and return books in library

    (show history), it will show full list of books and stored times of when book atributes where changed from true to false.
    Will all the books in the library be shown in the list or only those that have been checked-out and returned?
    Will the list only need to show the last transaction or do you need a history of all the check-outs and returns?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java adding borrow and return books in library

    hm, when i pick case 5 to show history of my library it will have stored information off all the check-outs and returns, in history only books that were borrowed/returned will be displayed.


    ****UPDATE***

    so now i have everything but that history
    Last edited by Pentrax; December 20th, 2017 at 12:41 PM.

  9. #8
    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: Java adding borrow and return books in library

    in history only books that were borrowed/returned will be displayed.
    If there were a new class to save a reference to the book and date and type of transaction: borrow or return
    Instances of that class could be save in a List so that all the times any books were borrowed or returned would be there.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java adding borrow and return books in library

    ok thanks man its working fine, but without date, i dont know hot to implement date how can i add date to the definition on my History class and constructor and than use it with this
    Historia b = new Historia(Nazov, akcia);
    ZMK.pridajHistoriu(b);

  11. #10
    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: Java adding borrow and return books in library

    What data do you want to save for the date? For example the system's current time, a String entered by the user or what???
    When you decide, then you will know what kind of field is needed and how to get the value.
    If the current system time is used, the history class can get it directly in its constructor.
    If it comes from somewhere else, it's value will need to be passed to the constructor as an argument.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java adding borrow and return books in library

    i want current time when the one instance of hisotry is created, how will i construct it in constructor ?

  13. #12
    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: Java adding borrow and return books in library

    Get the current time with the System class's method and save it in a field in the history class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: April 21st, 2013, 07:12 PM
  2. Replies: 4
    Last Post: April 21st, 2013, 04:20 PM
  3. JAVA books- help
    By the_marija in forum Java Theory & Questions
    Replies: 2
    Last Post: September 19th, 2010, 05:59 AM
  4. java books
    By bbr201 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 20th, 2010, 09:51 AM