Search code examples
javaarraysinheritanceuser-input

How to add user input into an array


This is the parent class

public class Holding {

private String holdingId, title;
private int defaultLoanFee, maxLoanPeriod, calculateLateFee;

public Holding(String holdingId, String title){

    this.holdingId = holdingId;
    this.title = title;
}

public String getId(){

    return this.holdingId;  

}

public String getTitle(){

    return this.title;  

}


public int getDefaultLoanFee(){

    return this.defaultLoanFee;

}
public int getMaxLoanPeriod(){

    return this.maxLoanPeriod;

}

public void print(){

    System.out.println("Title: " + getTitle());
    System.out.println("ID: " + getId());
    System.out.println("Loan Fee: " + getDefaultLoanFee());
    System.out.println("Max Loan Period: " + getMaxLoanPeriod());
}



}

This is the child class:

public class Book extends Holding{

private String holdingId, title;
private final int defaultLoanFee = 10;
private final int maxLoanPeriod = 28;

public Book(String holdingId, String title){
    super(holdingId, title);    
}

public String getHoldingId() {
    return holdingId;
}

public String getTitle(){
    return title;
}

public int getDefautLoanFee(){
    return defaultLoanFee;
}

public int getMaxLoanPeriod(){
    return maxLoanPeriod;
}

public void print(){
    System.out.println("Title: " + getTitle());
    System.out.println("ID: " + getHoldingId());
    System.out.println("Loan Fee: " + getDefaultLoanFee());
    System.out.println("Max Loan Period: " + getMaxLoanPeriod());
}

}

This is the menu:

public class LibraryMenu {

static Holding[]holding = new Holding[15];
static int hold = 0;
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    int options = keyboard.nextInt();
    do{
    }
    System.out.println();
    System.out.println("Library Management System");


    System.out.println("1. Add Holding");
    System.out.println("2. Remove Holding");


    switch(options){

    case 1:
        addHolding();
        break;

    default:
        System.out.println("Please enter the following options");
    }
}while(options == 13);
    System.out.println("Thank you");

}

public static void addHolding(){

    System.out.println("1. Book");
    System.out.println("2. Video");
    int input1 = input.nextInt();

    switch(input1){

    case 1:
        addBook();
        break;

    case 2:
        addVideo();
        break;

    default:
        System.out.println("Please select the following options");
        break;
    }
}

public static void addBook(){


    }   
}

All i want is when a user wants to add a book, the user will type in the title and id for it and it will save into the array, and when it is done, it will go back to the menu. I cant seem to do it with my knowledge at the moment.

i tried to do this in the addBook() method

holding[hold] = new Book();
holding[hold].setBook();

the setBook method would be in the Book class, to complete the setBook, i have to make set methods for all the get method. But when i do this, the constructor is undefined.

public void setBook(){

System.out.println("Title: " + setTitle());
System.out.println("ID: " + setHoldingId());
System.out.println("Loan fee: " + setDefaultLoanFee());
System.out.println("Max Loan Period: " + setMaxLoanPeriod());

}

If there is anything wrong with my code, please feel free to say it to me :D

Thanks.

edit: After the user adds a book or video, when the user wants to print all the holdings, it will show the title, id, loan fee and max loan period. How do i do that?

edit2: Clean up a few parts that wasn't necessary with my question


Solution

  • I would suggest you to use "List of Object" concept in handling it.

    1) You can define a List of Book object for example.

    List<Book> bookList = new ArrayList<Book>();
    

    2) Everytime when you hit AddBook() function, you can then do something like this.

    //Create a temporary object of Book to get the user input data.
    Book tempBook = new Book(holdingIDParam, titleParam);
    
    //Add them to the list
    bookList.Add(tempBook);
    

    3) You can then use that list to your likings, whether to store them into a .txt based database or to use them throughout the program. Usage example:

    bookList.get(theDesiredIndex).getBook() ...
    

    I am not entirely sure of what you want but after paying focus on your bold text,this is what I can understand for your case. Feel free to correct me on your requirements if it doesn't meet. Hope it helps :)