English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A local bookstore has a markup of 10% on each book. Write a program that takes the sales price of a book as input and displays the following outputs:

the markup of the book just sold

the wholesale amounts(to go to the publisher of the book just sold)

the total sales price of all the books sold

the total markup amount of all the books sold



Use a while loop that continues to prompt the user for the price of a book. the prompt should end when the user enters a negative number for the price.

2007-08-12 12:31:58 · 2 answers · asked by dingdong 1 in Computers & Internet Programming & Design

2 answers

public class TestClass {
public static void main(String[] args) {
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
long totalWholesale = 0;
long totalMarkup = 0;
while (true) {
System.out.print("Price: ");
String s = br.readLine();
if (s == null) return;

double inputPrice = Double. parseDouble(s);
if (inputPrice < 0) return;

// From here, calculate in pennies
long price = (int) (100.0 * inputPrice + 0.5);

long markup = (int) (((double) price) / 11.0 + 0.5);
System.out.println( "Markup on this book: " + penniesToCurrency( markup));

long wholesale = price - markup;
System.out.println( "Wholesale on this book: " + penniesToCurrency( wholesale));

totalWholesale += wholesale;
totalMarkup += markup;

System.out.println( "Total markup so far: " + penniesToCurrency( totalMarkup));
System.out.println( "Total wholesale so far: " + penniesToCurrency( totalWholesale));
} }

static final DecimalFormat MONEYFMT = new DecimalFormat( "0.00");
static String penniesToCurrency( long penniesValue) {
return MONEYFMT.format( ((double) penniesValue) / 100.0);
} }

======================
Sample input and output:

Price: 110.00
Markup on this book: 10.00
Wholesale on this book: 100.00
Total markup so far: 10.00
Total wholesale so far: 100.00

Price: 55.00
Markup on this book: 5.00
Wholesale on this book: 50.00
Total markup so far: 15.00
Total wholesale so far: 150.00

Price: -10
[program exits]

2007-08-12 14:15:59 · answer #1 · answered by McFate 7 · 0 3

Just think about it for a while.
How are you going to design your loop? Should you go for a for loop or a while loop? You're obviously going to have to stop when the number is < 0.

Take one step at a time. Your first going to get the input from the user. How would you do that? Are you going to allow strings and numbers?

Once you have that value set to some sort of variable... how are you going to display the markup? The markup is going to be 10% of each book. So you're going to need to take 10% of each book and add them together. What would be the easiest way to do this?

You're going to need to get the wholesale amounts. So you're going to have to keep a running total of each book (without markup).

How are you going to keep track of the total sales price of books sold? You're obviously going to need another variable for that.

How are you going to keep track of the total markup amount? This would probably necessitate another variable.

Start with this and see if you can figure it out yourself...

Good Luck :) Java is a great language.

2007-08-12 20:23:58 · answer #2 · answered by marleymang 2 · 1 0

fedest.com, questions and answers