got another problem with the library program i'm trying to code.
it seems like its going in to an infinite loop and i can't figure out for the life of me why. i've taken this on as a pet project to learn java and its doing my head in now lol.
hopefully somebody can help me out here...
here's the logic in my program:
database = arraylist = list of people books have been lent to
while (true)
1. get name if empty
2. if database is emtpty, add person to database and proceed to lend book
[iterate through list of people in the database]
3. check if person already in the database
4. if the name is equal to the name in database then lend book (at this stage assuming name's are unique)
4.1 check if another book needs to be lent to same person
4.2 if yes: checkout book
4.3 if no: set username to empty
5. if name is not in the database
5.1 add person
now after adding person it should go back to the start of the while loop to kick off the cycle again. which its not doing :s
code:
if (option.equals("2")){
ArrayList<Person> getPeople = ml.getPeople();
ArrayList<Book> getAvailableBooks = ml.getAvailableBooks();
boolean checkoutBook = true;
boolean contd = true;
Person p1 = new Person();
String username="";
while(contd){
//get name
if (username.equals("")){
System.out.println("Name of person you would like to lend the book to: ");
username=keyboard.nextLine();
}
if( getPeople.size() == 0){
System.out.println("Databse is empty. Will add to Database..");
p1.setName(username);
getPeople.add(p1);
System.out.println("Done.");
}
//iterate through list of available people
for(int i = 0; i < getPeople.size(); i++) {
Person aperson = getPeople.get(i);
//if there is a match
if (aperson.getName().equals(username)){
p1 = aperson;
//book(s) needs to be checked out
while (checkoutBook == true){
//print available books to lend
for (Book book : getAvailableBooks) {
System.out.println(book.title + " by "
+ book.author);
}//end-get available books
System.out.println("Which book would you like to check out?");
String bookName = keyboard.nextLine();
//check if book name entered is in the database
//if it is check it out - either successful or failed
for (Book book : getAvailableBooks) {
if (book.title.equalsIgnoreCase(bookName)){
boolean result =ml.checkOut(book, p1);
if(result==true){
System.out.println("Check out: successful.");
break;
}
else{
System.out.println("Check out failed: Book has already been checked out or "
+ p1.getName()
+ " has reached Maximum book limit");
break;
}
}//end-if: (book.title.equals("bookName"))
else if (!book.title.equalsIgnoreCase(bookName)){
System.out.println("Book doesn't exist");
}
}//end-for-get available books
System.out.print("Would you like to check out another book to "+ p1.getName() + " [y/n]?");
String choice = keyboard.nextLine();
if (choice.equals("y")){
checkoutBook = true;
}
else if (choice.equals("n")){
checkoutBook = false;
username="";
}
}//end-while-checkoutBook == true)
}//end-if-aperson.getName().equals(username)
//if person doesn't exist already, add them.
else if (!aperson.getName().equals(username)){
System.out.println("Person doesn't exist. Will add to Database..");
p1.setName(username);
getPeople.add(p1);
System.out.println("Done.");
}//end-!aperson.getName().equals(username)
System.out.println("exiting available people loop" + username);
}//end-available people
System.out.println("in while contd loop" + username);
}//end-while-contd
}//end-if-option
output:
[code]
What would you like to do?
1. Add Books
2. Lend a book to a person
3. Check which Books are available
4. Exit
Enter choice [1 | 2 | 3 | 4]: 1
Enter name of Book: War
Enter name of the Author for War:DD
Would you like to add another book? [y/n]
n
What would you like to do?
1. Add Books
2. Lend a book to a person
3. Check which Books are available
4. Exit
Enter choice [1 | 2 | 3 | 4]: 2
Name of person you would like to lend the book to:
Roby
Databse is empty. Will add to Database..
Done.
War by DD
Which book would you like to check out?
War
Check out: successful.
Would you like to check out another book to Roby [y/n]?n
Name of person you would like to lend the book to:
Sue
Person doesn't exist. Will add to Database..
Done
Would somebody able to tell me whats causing this?
thanks a lot!

New Topic/Question
Reply



MultiQuote




|