-2

The Question of the assignment is:-

You have been requested to create an application for a bookshop that helps to keep track of books.

You are required to create the following classes for your application:

  1. The class to describe attributes or variable of book title and book price with correct data type.
  2. The class should store collection of books (using an array). This class should also keep track of the total number of books in the collection, the total cost of the books in the collection.
  3. [Bonus marks] The class should also display the most expensive book title and price as well as cheapest book title and price.

Requirements

You should implement arrays to store the book details. There is NO need to implement any databases or files.

All java source files must contain your name, student id, course, and date. Include this information within comments (in the header) in the source code.

Each of your classes should be commented appropriately and the code checked for

I have successfully stored books in array, and make it display. But I am having problem, with the price of the book. It outputs the book name, but does not output the price and the total price of the books purchased, please see my coding below..

import java.util.Scanner;

public class array_userinput {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int numOfBook;
        // prompts the user to enter the number of books he/she wants
        System.out.print("How many numbers of books u want: "); 

        double[] bookPrice = new double[numOfBook];
        int num = input.nextInt();
        // double price[];
        double totalCost = 0;
        String array[] = new String[num];

        System.out.print("Enter the " + num + " book names now: ");

        for (int i = 0; i < array.length; i++) {

            array[i] = input.next();

            System.out.println("These are the books you have entered: "
                    + array[i]);
            System.out.print("Please enter book price: ");
            bookPrice[i] = input.nextDouble();
            System.out.println(numOfBook);

            totalCost += bookPrice[i];
            double max = prices[i];
            int maxBook = 0;
            for (int i = 1; i < books.length; i++) {
                if (prices[i] > max) {
                    max = prices[i];
                    maxBook = i;
                }
            }

            System.out.print("Cheapest book: " + books[maxBook]);
            double min = prices[i];
            int minBook = 0;
            for (int i = 1; i < books.length; i++) {
                if (prices[i] < min) {
                    min = prices[i];
                    minBook = i;
                }
            }
            System.out.print("Cheapest book: " + books[minBook]);

        }
    }

}
2
  • 1
    possible duplicate of Dynamic object creation using input loops Commented Jun 6, 2012 at 15:37
  • You said that you stored books in an array, but I don't see any object of type Book anywhere in your code. Do you have one created? If so, please add it here. Commented Jun 6, 2012 at 15:39

2 Answers 2

4

This assignment strikes me as a code design exercise more than a "get the code working" exercise. So just making it work isn't going to cut it (if you want a good mark).

"The class to describe attributes or variable of book title and book price with correct data type". "The class should store collection of books (using an array)", you're not really doing that, you're just doing it all within the main function.

You need to use a little OO design to create some simple classes (e.g. Book should have title and price along with a constructor and getter methods). You can then store instances of them in your array and interrogate them. For your cheapest book problem, you could implement compareTo and use a for(Book book in books) construct.

Also consider functional decomposition of what you're trying to achieve in "main", keep your functions simple and easy to read.

Sign up to request clarification or add additional context in comments.

Comments

2

More work from this, right?

I made a mistake in my implementation: double max = prices[i]; shouldn't compile since i isn't initialized in this context. Try this:

double min = prices[0];
int minBook = 0;
for(int i = 1; i < books.length; i++) {
    if(prices[i] < min) {
        min = prices[i];
        minBook = i;
    }
}
System.out.print("Cheapest book: " + books[minBook]);

Jeff Watkins is also entirely correct in his response: in accordance with the homework assignment, you're going to need much better design. Follow his advice on the Book object; it's very good. With regard to breaking main into functions, you could write public Book minBook(Book[] books) and public Book maxBook(Book[] books) functions that would use aBook.compareTo(Book other) or something similar.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.