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-11 09:48:24 · 1 answers · asked by dingdong 1 in Computers & Internet Programming & Design

1 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

2007-08-11 11:19:14 · answer #1 · answered by McFate 7 · 0 0

fedest.com, questions and answers