Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can I store user input in arraylist java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package library;

/**
 *
 * @author maryam
 */
import java.util.Scanner;
import java.util.ArrayList;


public class LibraryDriver {

    public static ArrayList<Student> studentList = new ArrayList<Student>();
    public static ArrayList<Faculty> FacultyList = new ArrayList<Faculty>();
    public static ArrayList<Book> bookList = new ArrayList<Book>();
    public static ArrayList<Book> borroeList = new ArrayList<Book>();

    public static void main(String[] args) {
        Scanner br = new Scanner(System.in);
        int reapeat = 13;
        while (reapeat > 0) {
            System.out.println("------------------------------------------------------");
            System.out.println("-------WELCOME TO OUR LIBRARY MANAGEMENT SYSTEM-------");
            System.out.println("------------------------------------------------------");
            System.out.println("----------Please choose one of below Options----------");
            System.out.println("1. Add Student");
            System.out.println("2. Add Faculty");
            System.out.println("3. Add Book");
            System.out.println("4. Display All Students");
            System.out.println("5. Display All Faculty");
            System.out.println("6. Display All Books");
            System.out.println("7. Search Student");
            System.out.println("8. Search Faculty");
            System.out.println("9. Search Book");
            System.out.println("10. Borow Book");
            System.out.println("11. Display All Borow Book");
            System.out.println("12. Return Book");
            System.out.println("13. Exit");
            System.out.print("your choice: ");
            int option = br.nextInt();
            System.out.println("------------------------------------------------------");
            switch (option) {
                case 1:
                    System.out.println("Enter Student Id");
                     int id = br.nextInt();
                    boolean chekId = checkStudentId(id);
                    if (chekId) {
                        System.out.println("StudentId Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Student FirstName");
                    String surname = br.nextLine();
                    System.out.println("Enter Student lastName");
                    String lastName = br.nextLine();
                    System.out.println("Enter Student Age");
                    int age = br.nextInt();
                    Student student = new Student(id, surname, lastName, age);
                    studentList.add(student);
                    break;
                case 2:
                    System.out.println("Enter Faculty Id");
                    int id1 = br.nextInt();
                    boolean chekId1 = checkFacultyId(id1);
                    if (chekId1) {
                        System.out.println("FacultyId Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Faculty FirstName");
                    String surname1 = br.nextLine();
                    System.out.println("Enter Faculty lastName");
                    String lastName1 = br.nextLine();
                    System.out.println("Enter Faculty Age");
                    int age1 = br.nextInt();
                    Faculty Faculty = new Faculty(id1, surname1, lastName1, age1);
                    FacultyList.add(Faculty);
                    break;
                case 3:
                    System.out.println("Enter Book Id :");
                    int bookId = br.nextInt();
                    boolean chekBookId = checkBookId(bookId);
                    if (chekBookId) {
                        System.out.println("Book Id Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Book tittle");
                    String tittle = br.nextLine();
                    System.out.println("Enter author FirstName");
                    String authorSurname = br.nextLine();
                    System.out.println("Enter author lastName");
                    String authorName = br.nextLine();
                    System.out.println("Enter Book Purchase Date");
                    String purchaseDate = br.nextLine();
                    System.out.println("Enter Book Availability :");
                    String status = br.nextLine();
                    Book b = new Book(bookId, tittle, authorSurname, authorName, purchaseDate, status) {
                    }; // to implement all abrstract method
                    bookList.add(b);
                    break;
                case 4:
                    displayAllStudent();
                    break;
                case 5:
                    displayAllFaculty();
                    break;
                case 6:
                    displayAllBook();
                    break;
                case 7:
                    System.out.println("Please Enter student Id, you want to Search");
                    int searchStudentId = br.nextInt();
                    searchStudent(searchStudentId);
                    break;
                case 8:
                    System.out.println("Please Enter Faculty Id, you want to Search");
                    int searchFacultyId = br.nextInt();
                    searchFaculty(searchFacultyId);
                    break;
                case 9:
                    System.out.println("Please Enter Book Id, you want to Search");
                    int searchBookId = br.nextInt();
                    searchBook(searchBookId);
                    break;
                case 10:
                    borrowBook();
                    break;
                case 11:
                    DisplayborrowBook();
                    break;
                case 12:
                    returnBook();
                    break;
                case 13:
                    reapeat = -13;
                    break;
            }
        }
    }

    public static void returnBook() {
        Scanner br = new Scanner(System.in);
        try {
            System.out.println("---------------------");
            System.out.println("Please enter Student/Faculty Id:");
            int id = br.nextInt();
            System.out.println("Please enter Book Id:");
            int bookId = br.nextInt();
            for (int i = 0; i < borroeList.size(); i++) {
                if (borroeList.get(i).getBookId() == bookId) {

                    borroeList.get(i).setStatus("AVAILABLE");
                    borroeList.remove(i);// to delete an element from an ArrayList
                    break;
                }
            }

        } catch (NumberFormatException e) {
            System.err.println("error. " + e);
        }
    }

    public static void DisplayborrowBook() {
        for (Book book : borroeList) {
            System.out.println("Book Id:  " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                    + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
        }
    }

    public static void borrowBook() {
        Scanner br = new Scanner(System.in);
        System.out.println("----List of all Available book as Below----");
        for (Book book : bookList) {
            if (book.getStatus().equalsIgnoreCase("AVAILABLE")) {
                System.out.println("Book Id: " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                        + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
            }
        }
        System.out.println("---------------------");
        System.out.println("Please enter Student/Faculty Id:");
        int id = br.nextInt();
        System.out.println("Please enter Book Id:");
        int bookId = br.nextInt();
        for (Book book : bookList) {
            if (book.getBookId() == bookId) {
                borroeList.add(book);
                book.setStatus("Not AVAILABLE");
            }
        }
    }

    public static void searchBook(int searchBookId) {
        boolean search = true;
        for (Book book : bookList) {
            if (book.getBookId() == searchBookId) {
                System.out.println("Book Id: " + book.getBookId() + " Tittle : " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                        + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchBookId + " Not exist");
        }
    }

    public static void searchStudent(int searchStudentId) {
        boolean search = true;
        for (Student st : studentList) {
            if (st.getStudentId() == searchStudentId) {
                System.out.println("Student Id: " + st.getStudentId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                        + " Age: " + st.getAge());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchStudentId + " Not exist");
        }
    }

    public static void displayAllBook() {
        for (Book book : bookList) {
            System.out.println("Book Id: " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                    + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
        }
    }

    public static void displayAllStudent() {
        for (Student st : studentList) {
            System.out.println("Student Id: " + st.getStudentId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                    + " Age: " + st.getAge());
        }
    }

   public static boolean checkBookId(int bookId) {
        for (Book book : bookList) {
            if (book.getBookId() == bookId) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkStudentId(int id) {
        for (Student st : studentList) {
            if (st.getStudentId() == id) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkFacultyId(int id) {
        for (Faculty st : FacultyList) {
            if (st.getFacultyId() == id) {
                return true;
            }
        }
        return false;
    }

    public static void displayAllFaculty() {
        for (Faculty st : FacultyList) {
            System.out.println("Faculty Id: " + st.getFacultyId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                    + " Age: " + st.getAge());
        }
    }

   public static void searchFaculty(int searchFacultyId) {
        boolean search = true;
        for (Faculty st : FacultyList) {
            if (st.getFacultyId() == searchFacultyId) {
                System.out.println("Faculty Id: " + st.getFacultyId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                        + " Age: " + st.getAge());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchFacultyId + " Not exist");
        }
    }
}


What I have tried:

i try but it does not work
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package library;

/**
 *
 * @author maryam
 */
import java.util.Scanner;
import java.util.ArrayList;


public class LibraryDriver {

    public static ArrayList<Student> studentList = new ArrayList<Student>();
    public static ArrayList<Faculty> FacultyList = new ArrayList<Faculty>();
    public static ArrayList<Book> bookList = new ArrayList<Book>();
    public static ArrayList<Book> borroeList = new ArrayList<Book>();
    add.studentList;

    public static void main(String[] args) {
        Scanner br = new Scanner(System.in);
        int reapeat = 13;
        while (reapeat > 0) {
            System.out.println("------------------------------------------------------");
            System.out.println("-------WELCOME TO OUR LIBRARY MANAGEMENT SYSTEM-------");
            System.out.println("------------------------------------------------------");
            System.out.println("----------Please choose one of below Options----------");
            System.out.println("1. Add Student");
            System.out.println("2. Add Faculty");
            System.out.println("3. Add Book");
            System.out.println("4. Display All Students");
            System.out.println("5. Display All Faculty");
            System.out.println("6. Display All Books");
            System.out.println("7. Search Student");
            System.out.println("8. Search Faculty");
            System.out.println("9. Search Book");
            System.out.println("10. Borow Book");
            System.out.println("11. Display All Borow Book");
            System.out.println("12. Return Book");
            System.out.println("13. Exit");
            System.out.print("your choice: ");
            int option = br.nextInt();
            System.out.println("------------------------------------------------------");
            switch (option) {
                case 1:
                    System.out.println("Enter Student Id");
                     int id = br.nextInt();
                    boolean chekId = checkStudentId(id);
                    if (chekId) {
                        System.out.println("StudentId Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Student FirstName");
                    String surname = br.nextLine();
                    System.out.println("Enter Student lastName");
                    String lastName = br.nextLine();
                    System.out.println("Enter Student Age");
                    int age = br.nextInt();
                    Student student = new Student(id, surname, lastName, age);
                    studentList.add(student);
                    break;
                case 2:
                    System.out.println("Enter Faculty Id");
                    int id1 = br.nextInt();
                    boolean chekId1 = checkFacultyId(id1);
                    if (chekId1) {
                        System.out.println("FacultyId Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Faculty FirstName");
                    String surname1 = br.nextLine();
                    System.out.println("Enter Faculty lastName");
                    String lastName1 = br.nextLine();
                    System.out.println("Enter Faculty Age");
                    int age1 = br.nextInt();
                    Faculty Faculty = new Faculty(id1, surname1, lastName1, age1);
                    FacultyList.add(Faculty);
                    break;
                case 3:
                    System.out.println("Enter Book Id :");
                    int bookId = br.nextInt();
                    boolean chekBookId = checkBookId(bookId);
                    if (chekBookId) {
                        System.out.println("Book Id Already Exist");
                        break;
                    }
                    br.nextLine();
                    System.out.println("Enter Book tittle");
                    String tittle = br.nextLine();
                    System.out.println("Enter author FirstName");
                    String authorSurname = br.nextLine();
                    System.out.println("Enter author lastName");
                    String authorName = br.nextLine();
                    System.out.println("Enter Book Purchase Date");
                    String purchaseDate = br.nextLine();
                    System.out.println("Enter Book Availability :");
                    String status = br.nextLine();
                    Book b = new Book(bookId, tittle, authorSurname, authorName, purchaseDate, status) {
                    }; // to implement all abrstract method
                    bookList.add(b);
                    break;
                case 4:
                    displayAllStudent();
                    break;
                case 5:
                    displayAllFaculty();
                    break;
                case 6:
                    displayAllBook();
                    break;
                case 7:
                    System.out.println("Please Enter student Id, you want to Search");
                    int searchStudentId = br.nextInt();
                    searchStudent(searchStudentId);
                    break;
                case 8:
                    System.out.println("Please Enter Faculty Id, you want to Search");
                    int searchFacultyId = br.nextInt();
                    searchFaculty(searchFacultyId);
                    break;
                case 9:
                    System.out.println("Please Enter Book Id, you want to Search");
                    int searchBookId = br.nextInt();
                    searchBook(searchBookId);
                    break;
                case 10:
                    borrowBook();
                    break;
                case 11:
                    DisplayborrowBook();
                    break;
                case 12:
                    returnBook();
                    break;
                case 13:
                    reapeat = -13;
                    break;
            }
        }
    }

    public static void returnBook() {
        Scanner br = new Scanner(System.in);
        try {
            System.out.println("---------------------");
            System.out.println("Please enter Student/Faculty Id:");
            int id = br.nextInt();
            System.out.println("Please enter Book Id:");
            int bookId = br.nextInt();
            for (int i = 0; i < borroeList.size(); i++) {
                if (borroeList.get(i).getBookId() == bookId) {

                    borroeList.get(i).setStatus("AVAILABLE");
                    borroeList.remove(i);// to delete an element from an ArrayList
                    break;
                }
            }

        } catch (NumberFormatException e) {
            System.err.println("error. " + e);
        }
    }

    public static void DisplayborrowBook() {
        for (Book book : borroeList) {
            System.out.println("Book Id:  " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                    + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
        }
    }

    public static void borrowBook() {
        Scanner br = new Scanner(System.in);
        System.out.println("----List of all Available book as Below----");
        for (Book book : bookList) {
            if (book.getStatus().equalsIgnoreCase("AVAILABLE")) {
                System.out.println("Book Id: " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                        + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
            }
        }
        System.out.println("---------------------");
        System.out.println("Please enter Student/Faculty Id:");
        int id = br.nextInt();
        System.out.println("Please enter Book Id:");
        int bookId = br.nextInt();
        for (Book book : bookList) {
            if (book.getBookId() == bookId) {
                borroeList.add(book);
                book.setStatus("Not AVAILABLE");
            }
        }
    }

    public static void searchBook(int searchBookId) {
        boolean search = true;
        for (Book book : bookList) {
            if (book.getBookId() == searchBookId) {
                System.out.println("Book Id: " + book.getBookId() + " Tittle : " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                        + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchBookId + " Not exist");
        }
    }

    public static void searchStudent(int searchStudentId) {
        boolean search = true;
        for (Student st : studentList) {
            if (st.getStudentId() == searchStudentId) {
                System.out.println("Student Id: " + st.getStudentId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                        + " Age: " + st.getAge());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchStudentId + " Not exist");
        }
    }

    public static void displayAllBook() {
        for (Book book : bookList) {
            System.out.println("Book Id: " + book.getBookId() + " Tittle: " + book.getTittle() + " Auhtor Surname: " + book.getAuthorSurname()
                    + " Author FirstName: " + book.getAuthorFirsName() + " Purchase Date: " + book.getPurchaseDate() + " staus: " + book.getStatus());
        }
    }

    public static void displayAllStudent() {
        for (Student st : studentList) {
            System.out.println("Student Id: " + st.getStudentId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                    + " Age: " + st.getAge());
        }
    }

   public static boolean checkBookId(int bookId) {
        for (Book book : bookList) {
            if (book.getBookId() == bookId) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkStudentId(int id) {
        for (Student st : studentList) {
            if (st.getStudentId() == id) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkFacultyId(int id) {
        for (Faculty st : FacultyList) {
            if (st.getFacultyId() == id) {
                return true;
            }
        }
        return false;
    }

    public static void displayAllFaculty() {
        for (Faculty st : FacultyList) {
            System.out.println("Faculty Id: " + st.getFacultyId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                    + " Age: " + st.getAge());
        }
    }

   public static void searchFaculty(int searchFacultyId) {
        boolean search = true;
        for (Faculty st : FacultyList) {
            if (st.getFacultyId() == searchFacultyId) {
                System.out.println("Faculty Id: " + st.getFacultyId() + " FirstName: " + st.getSurname() + " lastName: " + st.getLastName()
                        + " Age: " + st.getAge());
                search = false;
            }
        }
        if (search) {
            System.out.println(searchFacultyId + " Not exist");
        }
    }
}
Posted
Updated 27-Apr-21 21:52pm
Comments
OriginalGriff 28-Apr-21 1:56am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Java
Book b = new Book(bookId, tittle, authorSurname, authorName, purchaseDate, status) {
}; // to implement all abrstract method

What are those braces supposed to be for on this constructor call?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900