Hey, I stumbled upon this forum on google and people seem to be very helpful, so I thought I'd post my problem (ethical or unethical?)
I've been going through my coursework and I just can't get my head around using Arrays in a class. I'll post the question followed by the code.
1. Create a new class called BookStore. Your shop will have two fields, an array storing a collection of books (Book[] anyname) and an integer for the number of books in the collection.
Add a constructor which initialises the field variables appropriately. (Here you should construct an empty array object, where you can determine the size).
The question after this is:
2. Write a method addBook that takes a Book object as parameter and puts it in the next free space in the collection. What should you do with the field variable for the number of existing books?
Well, I think I've done 1. fine, but I'm not sure about two, I just can't get my head around "Book" as an object. I understand the last part, I should reduce the number of existing books.
I'll post my code for the Bookstore class and the Book class.
Thanks.
I've been going through my coursework and I just can't get my head around using Arrays in a class. I'll post the question followed by the code.
1. Create a new class called BookStore. Your shop will have two fields, an array storing a collection of books (Book[] anyname) and an integer for the number of books in the collection.
Add a constructor which initialises the field variables appropriately. (Here you should construct an empty array object, where you can determine the size).
The question after this is:
2. Write a method addBook that takes a Book object as parameter and puts it in the next free space in the collection. What should you do with the field variable for the number of existing books?
Well, I think I've done 1. fine, but I'm not sure about two, I just can't get my head around "Book" as an object. I understand the last part, I should reduce the number of existing books.
I'll post my code for the Bookstore class and the Book class.
Code:
public class BookStore
{
Book[] stock;
int NoOfBooks;
public void initiliaseArray (int NoOfBooks)
{
NoOfBooks = NoOfBooks;
stock = new Book[NoOfBooks];
}
public void addBook (Book)
{
}
}
Code:
public class Book
{
private String author;
private String title;
private int year;
private double price;
private double pricespecial = 0;
public Book ()
{ }
public void getauthor(String authorhold)
{
author = authorhold;
}
public void gettitle(String titlehold)
{
title = titlehold;
}
public int getyear(int yearhold)
{
year = yearhold;
return year;
}
public double getprice(double pricehold)
{
price = pricehold;
return price;
}
public void reduceprice (double percentage)
{
pricespecial = Math.round(price * percentage);
}
public void lettercount ()
{
if (title == null)
return;
int lettercount = 0;
for (int i = 0; i < title.length(); i++)
{
if (Character.isLetter(title.charAt(i)))
lettercount++;
}
System.out.println("There are " + lettercount + " letters in the title");
}
public void bookcheck()
{
if (pricespecial > 0)
{
System.out.println("########################");
System.out.println("# Title: " + title);
System.out.println("# Author: " +author);
System.out.println("# Published: " + year);
System.out.println("# was £ " + price);
System.out.println("# now £ " + pricespecial);
System.out.println("########################");
}
else
{
System.out.println("########################");
System.out.println("# Title: " + title);
System.out.println("# Author: " +author);
System.out.println("# Published: " + year);
System.out.println("# £ " + price);
System.out.println("########################");
}
}
}
Thanks.