• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

using a boolean in creating a class

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
Wraith10
Asked:
Wraith10
  • 7
  • 7
  • 2
1 Solution
 
Wraith10Author Commented:
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
 
fredwangusCommented:
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
 
suprapto45Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
suprapto45Commented:
Hi,

Please tell me if you need some additional help. Good program fredwangus :).
0
 
fredwangusCommented:
:)
0
 
Wraith10Author Commented:
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
 
Wraith10Author Commented:
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
 
fredwangusCommented:
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
 
fredwangusCommented:
    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
 
Wraith10Author Commented:
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
 
fredwangusCommented:
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
 
Wraith10Author Commented:
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
 
Wraith10Author Commented:
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
 
fredwangusCommented:
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
 
Wraith10Author Commented:
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
 
fredwangusCommented:
u r welcome :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now