Java OOP concept for library model -


i new java , need implementing oop rules projects similar library model (car rental model).

i have 2 classes:

  • public class book
  • public class customer

simply put, classes this:

public class customer {     public string fullname;     public string address;      /* constructor */     public customer(string fullname, string address) {         this.fullname = fullname;         this.address = address;     }} 

and

public class book {     private string bookname;     private int pagenumber;     public customer customer;   //i think needs fixed      /* constructor */     public book(string bookname, int pagenumber, customer customer) {         this.bookname = bookname;         this.pagenumber = pagenumber;         // ... there not sure how like: this.customer = customer ...      // there should constructor without customer parameter can deal cuz easy     }} 

each instance of customer class represents person borrowed book. struggling implement is: how pass instance of customer class parameter constructor of book class? this:

// constructor in class book: public book(string bookname, int pagenumber, customer customer) {     this.bookname = bookname;     this.pagenumber = pagenumber;     this.customer = customer;  //i think needs fixed } 

another question how create record of book boorowed sarah connor living in london? implementation correct book book1 = new book("the hobbit", 300, "sarah connor", "london")?

basic solution

basically, approach correct:

 public book(string bookname, int pagenumber, customer customer) {         this.bookname = bookname;         this.pagenumber = pagenumber;         this.customer = customer;  } 

however, comment shows, still lacking very basic understanding of java. let's recap tried:

book book1 = new book("harry potter", 500, "mike", "london"); 

why wouldn't work?
first, pass in 4 parameters instead of 3. let's assume typo , ignore "london". still, there issue: "mike" string, constructor expects customer. hence, code should this:

customer mike = new customer("mike malony", "n13bj london"); book book1 = new book("harry potter", 500, mike); 

since asked in comments, can done in 1 line well:

book book1 = new book("harry potter", 500, new customer("mike", "london")); 

further issues , advice

now issue seems solved, code still isn't very... versatile. reason model severely limited: can ever instantiate book if pass in customer. libraries know can have books not borrowed. also, whatever pagenumber seems represent (a bookmark, possibly?), assume untouched book doesn't need information. therefore suggest this:

public book(string bookname) {     this.bookname = bookname;     this.pagenumber = 0;      // 0    => no page bookmarked     this.customer = null;     // null => book available } 

now need way hand out book customer , check status. suggest:

public void lend(customer customer) {     this.customer = customer; }  public void receiveback() {     customer = null;     pagenumber = 0; }  public boolean isavailable() {     return (customer == null); } 

and bookmark page:

public setbookmark(int pagenumber) {      this.pagenumber = pagenumber; } 

of course, still far optimal. if customer wants borrow several books? should books hold reference customer? there several approaches this. 1 have dedicated class holds associations of customers books. use sort of collection in either book or customer. however, that's beyond scope of task, i'll leave @ above.



Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -