Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,275 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,166 people online right now. Registration is fast and FREE... Join Now!




Just some advise needed

 
Reply to this topicStart new topic

Just some advise needed

nick2price
8 Jan, 2009 - 06:13 PM
Post #1

D.I.C Addict
****

Joined: 23 Nov, 2007
Posts: 963



Thanked: 83 times
My Contributions
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
    }
}


User is online!Profile CardPM
+Quote Post


BigAnt
RE: Just Some Advise Needed
8 Jan, 2009 - 06:26 PM
Post #2

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
My Contributions
If you are testing then there is usually going to be hard coding the data, as you want specific test cases coded that you know what are supposed to be returned. Now people may put the test data in text files and try to load it in s tests, but then you may have errors reading in the data from the text, and the program you think is broken really isn't and it is the reading in of the test data.

The point is, when you are testing there is usually hard coding of the test cases done.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Just Some Advise Needed
8 Jan, 2009 - 06:27 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
Not much to say...

A switch statement will look a lot nicier
Instead of calling loadBorrowerFile() with choice and have the method react according to the choice you can have multiple method for every choice so you'll avoid to check choice value agaib
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Just Some Advise Needed
8 Jan, 2009 - 06:56 PM
Post #4

D.I.C Addict
****

Joined: 23 Nov, 2007
Posts: 963



Thanked: 83 times
My Contributions
Suppose a switch would be nicer. I am one of these people who has become so overtaught on if statements, that switch makes me uncomfortable, but the aesthetics of my code is my issue at the moment, so i will use switch.

As for hardcoding the test data, would there be any difference in performance by hardcoding it straight into my main, or doing it in a method which is called by my main? Shows how hard my coursework was....concentrating on aesthetics and performance.
User is online!Profile CardPM
+Quote Post

BigAnt
RE: Just Some Advise Needed
8 Jan, 2009 - 07:15 PM
Post #5

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
My Contributions
QUOTE
As for hardcoding the test data, would there be any difference in performance by hardcoding it straight into my main, or doing it in a method which is called by my main? Shows how hard my coursework was....concentrating on aesthetics and performance.


Basically when testing the tests are usually divided by case, where a method contains a particular test case, another method another, and so on.
As for performance there will not be a performance deficit if you put the test codes in seperate methods and call it from main, as this is the general practice. it is better to divide the code up and have each method do a specific test then to lump the code all into main.

This is the way that JUnit does it, have test____ methods, and then testSuites to run groups of tests at once.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 01:43PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month