I had a question about using "this" for classes with methods. Here is a snippet of code I wrote that is giving me an error. I am confused about how to use class non-static class methods while referencing the object using "this". Can someone please help me?
update, here is all my code
public class customer
{
public String name;
public String accountID;
public String address;
public String creditCardNumber;
public String password;
public book[] bookArray;
public int numBooks;
public String toString(){
return "Customer name: " + this.name + " Customer account ID " + this.accountID
+ " Customer address: " + this.address;
}
public String getName(){ //getter method for customer name
return this.name;
}
public String getAccountID(){ //getter method for cutomer accountID
return this.accountID;
}
public String getAddress(){ //getter method for customer address
return this.address;
}
public String getCreditCardNumber(){ //getter method for customer creditCardNumber
return this.creditCardNumber;
}
public String getPassword(){ //getter method for customer password
return this.password;
}
public void setPassword(String Password){ //setter method for customer password
Password = password;
}
public void setAddress(String Address){ //setter method for customer address
Address = address;
}
public void setCreditCardNumber(String CreditCardNumber){ //setter method for customer creditCardNumber
CreditCardNumber = creditCardNumber;
}
public customer(){ //constructor method for customer class
this.name = name;
this.address = address;
this.accountID = accountID;
bookArray = new book[500];
numBooks = 0;
}
public void addBook(book currentBook){ //addBook method, adds current book to bookArray and adds one to numBooks int
bookArray[numBooks] = currentBook;
numBooks++;
}
}
Basically I am trying to have the method take the current book object and pass it through the addBook method which is in the customer class which then adds that object to an array. What is the best way to do this?
public class book
{
public String idNumber;
public String title;
public String author;
public String publisher;
public String yearOfPublication;
public int inStock;
public int sold;
public int price;
public String toString(){
return "Book ID Number: " + this.IDNumber+ " Book Title " + this.bookTitle + " Book Author: " + this.bookAuthor //toString method that returns all info for book class
+ " Book Publisher: " + this.bookPublisher + " Year of Publication: " + this.yearOfPublication;
}
public String getIDNumber(){ //getter method for book idNumber
return this.idNumber;
}
public String getTitle(){ //getter method for book title
return this.title;
}
public String getAuthor(){ //getter method for book author
return this.author;
}
public String getPublisher(){ //getter method for book publisher
return this.publisher;
}
public String getYearOfPublication(){ //getter method for book yearOfPublication
return this.yearOfPublication;
}
public int getBooksInStock(){ //getter method for book inStock
return this.inStock;
}
public int getBooksSold(){ //getter method for book sold
return this.sold;
}
public int getBookPrice(){ //getter method for book price
return this.price;
}
public void bookSales(int numBooksRequested){ //method records sale of book to customer object
if(inStock == 0){ //checks if book is out if stock, if so prints error message
System.out.println("Error, " + book.getTitle() + " is currently out of stock.");
}
else if(numBooksRequested > inStock){ //checks if amount of book requsted is greater than whats in stock, if so prints error message
System.out.println("Error, you have requested more copies of " + this.book.getTitle() + " than is currently in stock");
}
else if(numBooksRequested < 0){ //checks if amount requested is less than 0, if so prints error message
System.out.println("Error, invalid amount");
}
else{
for(int i=0; i<numBooksRequested; i++){ //loops through until the total number of the book ordered is passed through the addBook method
customer.addBook(this.book); //if amount requested is valid, utilizes addBook method to add current book object
inStock = inStock - 1;
}
}
}
public book(String IDNumber, String Title, String Author, String Publisher, String YearOfPublication, int Price){
IDNumber = idNumber;
Title = title;
Author = author;
Publisher = publisher;
YearOfPublication = yearOfPublication;
Price = price;
}
public void reStockBook(int bookRestock){
if(bookRestock < 1){
System.out.println("Error, Invalid Number");
}
else{
inStock = inStock + bookRestock;
}
}
}