Solved

using a boolean in creating a class

Posted on 2004-10-19
16
319 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
IT Company 5 69
countAbc challenge 9 49
Java passing arguments (type error) 2 33
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now