I just wanted some advise as to what the best option is. Below is a class i am posting. You dont need to go through the class, all working fine. What i was wondering though is that i need to hardcode 20 Users and 20 LibraryItems. I actually like the neatness of my code at the moment, and i dont really want to clog up the page with loads of hardcoding. What would be the best way of making 20 of both items appear in my arrays upon startup of the program? I was thinking about working in my main, and from there calling up my load...File methods, loading up five items for each part of the method. Would this be a good option, or is there a better way?
cheers
CODE
import java.util.Scanner;
import java.io.*;
import java.util.*;
/**LibraryAdmin class, control center of library*/
public class LibraryAdmin{
Scanner input = new Scanner(System.in);
BufferedReader in;
Library lib;
/**Method which test the system*/
public void testSystem()
{
System.out.println("Please enter the library Name " );
String libraryName = input.next(); //current library name
lib = new Library(libraryName.toUpperCase()); //instantiating class Library with the current library name
int choice = 0; //initial choice value
do{ //do loop
choice = lib.textMenu(); //bring menu up after each choice completion
if(choice==1)
{
int number = lib.inputInt("Press 1 to add a Student, Press 2 to add a member of staff " );
lib.addUser(number); //calls addUser method passing it the type of user to add
}
if(choice==2)
{
int number = lib.inputInt("Press 1 to add a Book, Press 2 to add a DVD " );
lib.addItem(number); //calls addItem method passing it the type of item to add
}
if(choice==3)
{
lib.getBorrowers(); //calls getBorrowers method
}
if(choice==4)
{
lib.getItems(); //calls getItems method
}
if(choice==5)
{
lib.searchUsers(); //calls searchUsers method
}
if(choice==6)
{
lib.searchItems(); //calls searchItems method
}
if(choice==7)
{
int loan = lib.inputInt("Press 1 to loan a Book, Press 2 to loan a DVD " );
lib.loanItems(loan); //calls loanItems method passing it the loan type
}
if(choice==8)
{
lib.returnItem(); //calls the returnItem method
}
if(choice==9)
{
//int representing the type of load the user wishes to make
int load = lib.inputInt("Press 1 to load a Borrower, Press 2 to load an Item " );
if(load==1)
{
int borrower = lib.inputInt("Press 1 to load a Student file, Press 2 to load a Staff file" );
loadBorrowerFile(borrower); //calls loadBorrowerFile passing it the type of Borrower
}
else if(load==2)
{
int item = lib.inputInt("Press 1 to load a book file, Press 2 to load a dvd file" );
loadItemFile(item); //calls loadItemFile passing it the type of Item
}
}
}
while(choice != 10); //exit when choice == 10
}
/**loadBorrowerFile method*/
public void loadBorrowerFile(int choice)
{
String studentBorrower;
String staffBorrower;
if(choice==1)
{
try {
System.out.println("Please enter the name of the student file" );
String fileName = input.next();
in = new BufferedReader(new FileReader(fileName+".txt"));
studentBorrower = in.readLine();
StringTokenizer st = new StringTokenizer(studentBorrower, ","); // "," is the separator
String firstName = st.nextToken();
String lastName = st.nextToken();
String title = st.nextToken();
String dateOfBirth = st.nextToken();
String homeAddress = st.nextToken();
String phoneNumber = st.nextToken();
int barCode = Integer.parseInt(st.nextToken());
String studentId = st.nextToken();
StudentBorrower studentBor = new StudentBorrower(firstName, lastName, title, dateOfBirth,
homeAddress, phoneNumber, barCode, studentId);
lib.addUserFromFile(studentBor);
}
catch(IOException e){
System.out.println("Error loading file" + e);
}
}
if(choice==2)
{
try {
System.out.println("Please enter the name of the staff file" );
String fileName = input.next();
in = new BufferedReader(new FileReader(fileName+".txt"));
staffBorrower = in.readLine();
StringTokenizer st = new StringTokenizer(staffBorrower, ","); // "," is the separator
String firstName = st.nextToken();
String lastName = st.nextToken();
String title = st.nextToken();
String dateOfBirth = st.nextToken();
String homeAddress = st.nextToken();
String phoneNumber = st.nextToken();
int barCode = Integer.parseInt(st.nextToken());
int staffId = Integer.parseInt(st.nextToken());
StaffBorrower staffBor = new StaffBorrower(firstName, lastName, title, dateOfBirth,
homeAddress, phoneNumber, barCode, staffId);
lib.addStaffFromFile(staffBor);
}
catch(IOException e){
System.out.println("Error loading file" + e);
}
}
}
public void loadItemFile(int choice)
{
String bookItem;
String dvdItem;
if(choice==1)
{
try {
System.out.println("Please enter the name of the book file" );
String fileName = input.next();
in = new BufferedReader(new FileReader(fileName+".txt"));
bookItem = in.readLine();
StringTokenizer st = new StringTokenizer(bookItem, ","); // "," is the separator
String itemCode = st.nextToken();
String dueDate = st.nextToken();
String libraryName = st.nextToken();
String title = st.nextToken();
String bookTitle = st.nextToken();
String bookAuthor = st.nextToken();
String bookPublisher = st.nextToken();
int bookId = Integer.parseInt(st.nextToken());
Book book = new Book(itemCode, dueDate, lib.libraryName, title, bookTitle, bookAuthor,
bookPublisher, bookId);
lib.addBookFromFile(book);
}
catch(IOException e){
System.out.println("Error loading file" + e);
}
}
if(choice==2)
{
try {
System.out.println("Please enter the name of the book file" );
String fileName = input.next();
in = new BufferedReader(new FileReader(fileName+".txt"));
dvdItem = in.readLine();
StringTokenizer st = new StringTokenizer(dvdItem, ","); // "," is the separator
String itemCode = st.nextToken();
String dueDate = st.nextToken();
String libraryName = st.nextToken();
String title = st.nextToken();
String dvdTitle = st.nextToken();
String dvdDirector = st.nextToken();
double dvdLength = Double.parseDouble(st.nextToken());
int dvdId = Integer.parseInt(st.nextToken());
Dvd dvd = new Dvd(itemCode, dueDate, libraryName, title, dvdTitle, dvdDirector,
dvdLength, dvdId);
lib.addDvdFromFile(dvd);
}
catch(IOException e){
System.out.println("Error loading file" + e);
}
}
}
/**main method*/
public static void main(String[] args)
{
LibraryAdmin libAd = new LibraryAdmin(); //instantiates the class
libAd.testSystem(); //and executes the method testSystem
}
}