Your college book store needs your help in estimating its business for next year
ID: 3776723 • Letter: Y
Question
Your college book store needs your help in estimating its business for next year. Experience has shown that sales depend greatly on whether a book is required for a course or just optional, and whether or not it has been used in the class before. A new, required textbook will sell to 90% of prospective enrollment, but if it has been used in the class before, only 65% will buy. Similarly, 40% of prospective enrollment will buy a new, optional textbook, but if it has been used in the class before only 20% will buy. (Note that "used" here does not mean second-hand books.)
Write a program that accepts as input a series of books (until the user enters a sentinel).
For each book ask for:
a code for the book,
the single copy cost for the book,
the current number of books on hand,
the prospective class enrollment,
and data that indicates if the book is required/optional, new/used in past.
As output, show all the input information in a nicely formatted screen along with how many books must be ordered (if any, note that only new books are ordered), the total cost of each order.
Then, after all input is complete, show the total cost of all book orders, and the expected profit if the store pays 80% of list price. Process one book at a time and show the output screen for that book.
Then, when the user has finished entering all the data, your program should output the total and profit values.
Before you start writing code, take some time to think about design of this program. Remember you need to include a BookStore class and it should be decomposed into a set of functions that perform only one task. The main function should be used to call the many functions of the Bookstore.
Here is sample output:
Please enter the book code: 1221
single copy price: 69.95
number on hand: 30
prospective enrollment: 150
1 for reqd/0 for optional: 1
1 for new/0 for used: 0
Book: 1221
Price: $69.95
Inventory: 30
Enrollment: 150
This book is required and used.
Need to order: 67
Total Cost: $4686.65
Enter 1 to do another book, 0 to stop. 0
Total for all orders: $4686.65
Profit: $937.33
Explanation / Answer
import java.util.*;
import java.text.DecimalFormat;
public class BookStore
{
public static void main(String args[])
{
int code, hand, enroll, opt1, opt2, need, choice, i = 1;
double price, order, profit, total = 0;
String op1, op2;
// loop will continue until the value of choice or i is 1
while(i == 1)
{
System.out.println("Please enter the book code: ");
code = scanInput();
System.out.println("single copy price: ");
Scanner scan2 = new Scanner(System.in);
price = scan2.nextDouble();
System.out.println("number on hand: ");
hand = scanInput();
System.out.println("prospective enrollment:");
enroll = scanInput();
System.out.println("1 for reqd/0 for optional:");
opt1 = scanInput();
System.out.println("1 for new/0 for used:");
opt2 = scanInput();
System.out.println("Book: " + code);
System.out.println("Price: $" + price);
System.out.println("Inventory: " + hand);
System.out.println("Enrollment: " + enroll);
op1 = (opt1 == 1) ? "required" : "optional"; // selecting option between required and optional book
op2 = (opt2 == 1) ? "new" : "used"; // selecting option between new and used book
System.out.println("This book is " + op1 + " and " + op2 + ".");
// calculation of books order
if(opt1 == 1)
order = (opt2 == 1) ? 0.9 : 0.65;
else
order = (opt2 == 1) ? 0.4 : 0.2;
// calculation of books needed
need = ((int) (enroll * order)) - hand;
System.out.println("Need to order: " + need);
// calculating total cost
total += need * price;
System.out.println("Total Cost: $" + numFormat(total));
System.out.println("Enter 1 to do another book, 0 to stop. ");
choice = scanInput();
i = choice;
}
System.out.println("Total for all orders: $" + numFormat(total));
//calculating profit
profit = total * 0.2;
System.out.println("Profit: $" + numFormat(profit));
}
// scanning every input
public static int scanInput()
{
Scanner scan = new Scanner(System.in);
return scan.nextInt();
}
// formating number upto 2 decimal places.
public static double numFormat(double num)
{
DecimalFormat df = new DecimalFormat("#.##");
return Double.parseDouble(df.format(num));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.