Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

using a boolean in creating a class

Posted on 2004-10-19
16
322 Views
Last Modified: 2010-03-31
I'm trying to create a class that stores a name, id number and the number for a library book borrowed by a student. I also want to give three methods to checkout a book and return a book.

The methods I'm supposed to be using resemble:

public boolean bkBorrow(long CN)

public boolean bkReturn(long CN)

I want them to change catNumber (defined as a long) to the CN of the book but if the student already has a book out (ie: catNumber != -1) it wont let them do it.

Similarly if the student tries to return a book that does not match the one they borrowed it would return an error.

I'm not sure how to work with booleans at all =(
0
Comment
Question by:Wraith10
  • 7
  • 7
  • 2
16 Comments
 

Author Comment

by:Wraith10
ID: 12353816
What I have so far:

public class LibraryAccount {
private String name;
private int studentId;
private long catNumber;

public LibraryAccount(string n, int id){
n=name;
id=123456789;
}
public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public String toString()
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12353859
try this:


public class test {
      public String name;
      public long id;
      public long catNumber;
      
      public test(String thisname,long thisid){
            name=thisname;
            id=thisid;
            catNumber=-1;
      }
      
      public boolean bkBorrow(long CN){
            if(catNumber!=-1){
                  System.out.println("Already borrowed book!");
                  return false;
            }else{
                  catNumber=CN;
                  System.out.println("borrowed book!");
                  return true;
            }                  
      }

      public boolean bkReturn(long CN){
            if(catNumber!=CN){
                  System.out.println("You do not have this book!");
                  return false;
            }else{
                  catNumber=-1;
                  System.out.println("returned book!");
                  return true;
            }
      }
      
      public static void main(String[] args){
            test student1=new test("a",1L);            
            //borrow
            boolean value=false;
            value=student1.bkBorrow(1L);
            System.out.println(value);
            //error here
            value=student1.bkBorrow(1L);
            System.out.println(value);
            value=student1.bkReturn(2L);
            System.out.println(value);
            //return
            value=student1.bkReturn(1L);            
            System.out.println(value);
      }
}
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12354187
Well,

I think that what fredwangus gave you is sufficient. But why do you have CN as long? I think that for the book, normally, the ID contains 0 in the front such as 000019232. I think that you should use String better.

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 16

Expert Comment

by:suprapto45
ID: 12354223
Hi,

Please tell me if you need some additional help. Good program fredwangus :).
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354382
:)
0
 

Author Comment

by:Wraith10
ID: 12354449
The output is formatted correctly except I do not think it is assigning the account names properly I get output like:

First LibraryAccount: LibraryAccount@f4a24a
when it should be
First LibraryAccount: Ryan Smith

I'm using a separate file to turn the data I need called SmLib.java

public class SmLib {
   public static void main(String[] args) {
      LibraryAccount ryan = new LibraryAccount("Ryan Smith", 94000);
      LibraryAccount cassie = new LibraryAccount("Cassie Cambell", 77000);

      System.out.println("\nFirst LibraryAccount: " + ryan);
      System.out.println("Second LibraryAccount: " + cassie);

      ryan.bookBorrow(111111111);
      System.out.println("\nFirst LibraryAccount after bookBorrow:\n" + ryan + "\n");

      ryan.bookReturn(111111111);
      System.out.println("First LibraryAccount after bookReturn:\n" + ryan);

      cassie.bookBorrow(444444444);
      System.out.println("\nSecond LibraryAccount after bookBorrow:\n" + cassie);

      if(cassie.bookBorrow(111111111)) System.out.println("\nCassie successfully borrowed a second book.");
      else System.out.println("\nCassie tried to borrow a second book. - DENIED");
      System.out.println("\nSecond LibraryAccount:\n" + cassie);

      if(cassie.bookReturn(111111111)) System.out.println("\nCassie successfully returned an unborrowed book.");
      else System.out.println("\nCassie tried to return a book she did not check out. - DENIED");
      System.out.println("\nSecond LibraryAccount:\n" + cassie + "\n");

   } // main method
} // SmLib
0
 

Author Comment

by:Wraith10
ID: 12354540
Bah there is also some error with my:

public String toString()
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}

It keeps telling me illegal start of type for the ip statement and missing method body for the toString part.
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354566
public String toString() {
String s;
if (catNumber = -1){
s=name + "has no book on loan."
else
s = name + "has catalogue number" + catNumber + "on loan."
}

guess u missed the {
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354577
    System.out.println("\nFirst LibraryAccount: " + ryan);
      System.out.println("Second LibraryAccount: " + cassie);

don't print the class Libraryaccount directly
use the member variables instead

eg, if the member is 'name'

use

System.out.println("\nFirst LibraryAccount: " + ryan.getName());
0
 

Author Comment

by:Wraith10
ID: 12354651
I missed a couple ; as well but once I put the bracket in the compiler actually found that.

I dont have a choice about the way the file is printed. That's a stock file I have to use as that is how my source code will be tested for correct output.

I now have:

public String toString(); {
  String s;
  if (catNumber = -1){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
}

I get a compile arrow pointing to "toString" saying missing method body or declare abstract and a second error on the if(catNumber) saying incompatible type found long required boolean
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12354674
public String toString() {
  String s;
  if (catNumber = -1){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
}

u added a ; after toString()
should remove it
0
 

Author Comment

by:Wraith10
ID: 12354801
Gah I feel like such an idiot.

I have that segment solved. In addition to removing the ; I need to add a return s; to the end.

The output for the code is now working nearly perfectly expect that it is now giving a value of null instead of the Library account names still AND it says they have no book on loan even when they should in fact have a book on loan.

For example I get an output of:

"null has no book on loan"

when it should be

"Ryan Smith has catalogue number 111111111 on loan"

I'm still not sure how to fix that. I'm going to post a full copy of my current code. If you see my comment after your ":)" post it has the output file that I am using (which I cannot modify).


public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(string n, int id){
   n=name;
   id=123456789;
   catNumber = -1;
}

public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (true){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


Currently it is ouputing:
First LibraryAccount: null has no book on loan
Second LibraryAccount: null has no book on loan

First LibraryAccount after bookBorrow:
null has no book on loan

It should output:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has catalogue number 111111111 on loan
0
 

Author Comment

by:Wraith10
ID: 12354850
Ignore my last post a friend of mine found the name problem. It should have been name = n not n = name.

However I still cannot get it to register that a book has been borrowed. It says they have no book on loan no matter what.

public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(string n, int id){
   name=n;
   id=123456789;
   catNumber = -1;
}

public string getName(){
return name;
}
public string getStudentID {
return StudentID + "";
}
public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (true){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


Currently it is ouputing:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has no book on loan

It should output:
First LibraryAccount: Ryan Smith has no book on loan
Second LibraryAccount: Cassie Cambell has no book on loan

First LibraryAccount after bookBorrow:
Ryan Smith has catalogue number 111111111 on loan
0
 
LVL 2

Accepted Solution

by:
fredwangus earned 500 total points
ID: 12354864
u made several small mistakes, try this and see whether the output is as expected:

public class LibraryAccount {
    private String name;
    private int studentID;
    private long catNumber;

public LibraryAccount(String n, int id){
   name=n;
   id=123456789;
   catNumber = -1;
}

public String getName(){
return name;
}
public String getStudentID() {
return studentID + "";
}

public double getCatNumber(){
return catNumber;
}

public boolean bookBorrow(long bookCN){
          if(catNumber!=-1){
               System.out.println(getName() + " tried to borrow a second book - DENIED");
               return false;
          }else{
               catNumber=bookCN;
               return true;
          }              
     }

     public boolean bookReturn(long bookCN){
          if(catNumber!= bookCN){
               System.out.println(getName() + " tried to return a book she did not check out. - DENIED");
               return false;
          }else{
               catNumber=-1;
               return true;
          }
     }


public String toString() {
  String s;
  if (catNumber==-1L){
  s=getName() + " has no book on loan.";
  }
else {
  s=getName() + " has catalogue number" + getCatNumber() + " on loan.";
  }
return s;
}


}


public class test {
        public static void main(String[] args) {
            LibraryAccount ryan = new LibraryAccount("Ryan Smith", 94000);
            LibraryAccount cassie = new LibraryAccount("Cassie Cambell", 77000);

            System.out.println("\nFirst LibraryAccount: " + ryan);
            System.out.println("Second LibraryAccount: " + cassie);

            ryan.bookBorrow(111111111L);
            System.out.println("\nFirst LibraryAccount after bookBorrow:\n" + ryan + "\n");

            ryan.bookReturn(111111111);
            System.out.println("First LibraryAccount after bookReturn:\n" + ryan);

            cassie.bookBorrow(444444444);
            System.out.println("\nSecond LibraryAccount after bookBorrow:\n" + cassie);

            if(cassie.bookBorrow(111111111)) System.out.println("\nCassie successfully borrowed a second book.");
            else System.out.println("\nCassie tried to borrow a second book. - DENIED");
            System.out.println("\nSecond LibraryAccount:\n" + cassie);

            if(cassie.bookReturn(111111111)) System.out.println("\nCassie successfully returned an unborrowed book.");
            else System.out.println("\nCassie tried to return a book she did not check out. - DENIED");
            System.out.println("\nSecond LibraryAccount:\n" + cassie + "\n");

         } // main method
}
0
 

Author Comment

by:Wraith10
ID: 12354970
Thanks for all your help (and patience). I just need to fine tune why I am getting a double output when she tries to borrow a second book and I should be all set.
0
 
LVL 2

Expert Comment

by:fredwangus
ID: 12355106
u r welcome :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
MySQL  on Tomcat 8 68
pagenation logic how it is working in my code 1 53
Java: anonymous class 4 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question