Search code examples
javaarraysformatcurrency-formatting

currency format from a string java


I am taking a basic Java class and I am at the finishing part of my assignment. My only problem is figuring out my currency formats. I am not getting the right conversions and outputs. Per my assignment I have a two files a class and an executable file and I must display the price of books after they have been reduced by 40% and show the most expensive book after the discount. I am not sure how to go about figuring the currency formats for the proper output.


This is what my program is supposed to display.

Proper formats

This is what my program is displaying

Book title:Java Programming, author:Liang, pages:1320, price: $145.0

Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99

Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25

Book title:Born a Crime, author:Noah, pages:304, price: $17.33

Book title:null, author:null, pages:0, price: $0.0

Book title:null, author:null, pages:0, price: $0.0

Books after completing library and 40% discount
Book title:Java Programming, author:Liang, pages:1320, price: $58.0

Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $7.9959999999999996

Book title:The Hobbit, author:Tolkien, pages:320, price: $3.7

Book title:Born a Crime, author:Noah, pages:304, price: $6.9319999999999995

Book title:Dark Territory, author:Kaplan, pages:352, price: $4.496

Book title:Born to Run, author:Springsteen, pages:508, price: $4.868

Here is the most expensive book after discounts

Book title:Java Programming, author:Liang, pages:1320, price: $58.0

Here is my code: This is the class file

public class Book {
//instance data members 
private String title;
private String author;
private int pages;
private double price;
//public static int set to 0 
public static int numBooks = 0;

//parameterized constructor
Book(String title, String author, int pages, double price){
    this.title=title;
    this.author=author;
    this.pages=pages;
    this.price=price;
    numBooks++;
}

//default constructor
Book(){
    title = author = null;
    price = pages = 0;
    numBooks++;     
}

//getters and setters 
void setTitle(String title) {
    this.title=title;
}
void setAuthor(String author) {
    this.author=author;
}
void setPages(int pages) {
    this.pages=pages;
}
void setPrice(double price) {
    this.price=price;
}
String getTitle() {
    return title;
}
String getAuthor() {
    return author;
}
int getPages() {
    return pages;
}
double getPrice() {
    return price;
}
//toString method to return string showing state of a book instance
@Override
public String toString() {
    String str = "Book title:" + title + ", author:" + author +
            ", pages:" + pages + ", price: $" + price;
    return str;
}
}

This is the executable file

public class TestBook {

public static void main(String[] args) {
    // array to hold 6 book objects specify data in first 4
    Book[] bookArray=new Book[6];
    bookArray[0]=new Book("Java Programming","Liang",1320,145.00);
    bookArray[1]=new Book("Horton Hears a Who!","Dr.Seuss",72,19.99);
    bookArray[2]=new Book("The Hobbit","Tolkien",320,9.25);
    bookArray[3]=new Book("Born a Crime","Noah",304,17.33);
    bookArray[4]=new Book();
    bookArray[5]=new Book();
    for(Book b:bookArray)
    System.out.println(b.toString());
    System.out.println("\nBooks after completing library and 40% discount");
    finishArray(bookArray);
    Book expensive = reduceBooks(bookArray);
    for(Book b:bookArray)
        System.out.println(b.toString());
    System.out.println("\nHere is the most expensive book after discounts\n" + expensive.toString());
}
//void method, use setter method to fill in last 2 books in array
static void finishArray(Book[]array) {
    array[4].setTitle("Dark Territory");
    array[4].setAuthor("Kaplan");
    array[4].setPages(352);
    array[4].setPrice(11.24);

    array[5].setTitle("Born to Run");
    array[5].setAuthor("Springsteen");
    array[5].setPages(508);
    array[5].setPrice(12.17);
}
static Book reduceBooks(Book[]array) {
    Book mostExpensiveBook=null;
    //reduce price of every book by 40%
    for (int i=0; i<6; i++) {
        array[i].setPrice(array[i].getPrice()*0.40);
        if(mostExpensiveBook==null)
            mostExpensiveBook=array[i];
        else if(mostExpensiveBook.getPrice()<array[i].getPrice())
            mostExpensiveBook = array[i];
    }
    return mostExpensiveBook;
}

}


Solution

  • "A" simple solution, might be to make use a the NumberFormat class, for example...

    public static class Book {
        //instance data members 
    
        protected static final NumberFormat FORMATTER = NumberFormat.getCurrencyInstance();
        //...
    
        @Override
        public String toString() {
            String str = "Book title:" + title + ", author:" + author
                            + ", pages:" + pages + ", price: " + FORMATTER.format(price);
            return str;
        }
    }
    

    Which will output something like...

    Book title:Java Programming, author:Liang, pages:1320, price: $145.00
    Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99
    Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25
    Book title:Born a Crime, author:Noah, pages:304, price: $17.33
    Book title:null, author:null, pages:0, price: $0.00
    Book title:null, author:null, pages:0, price: $0.00
    
    Books after completing library and 40% discount
    Book title:Java Programming, author:Liang, pages:1320, price: $58.00
    Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $8.00
    Book title:The Hobbit, author:Tolkien, pages:320, price: $3.70
    Book title:Born a Crime, author:Noah, pages:304, price: $6.93
    Book title:Dark Territory, author:Kaplan, pages:352, price: $4.50
    Book title:Born to Run, author:Springsteen, pages:508, price: $4.87
    
    Here is the most expensive book after discounts
    Book title:Java Programming, author:Liang, pages:1320, price: $58.00
    

    You can configure the formatter to better meet your individual requirements, but it should give you a starting point

    A "alternative" solution might be to make use of String.format, for example...

    @Override
    public String toString() {
        String str = "Book title:" + title + ", author:" + author
                        + ", pages:" + pages + ", price: " + String.format("$%.2f", price);
        return str;
    }
    

    which will do a similar job (but NumberFormat will take into account the current users Locale when formatting the String)