Directions from book - Serendipity Booksellers has a book club that awards point
ID: 672280 • Letter: D
Question
Directions from book - Serendipity Booksellers has a book club that awards points to its customers based on the number of books purchased each month. The points are awarded as follows:
If a customer purchases 0 books, he or she earns 0 points.
If a customer purchases 1 books, he or she earns 5 points.
If a customer purchases 2 books, he or she earns 15 points.
If a customer purchases 3 books, he or she earns 30 points.
If a customer purchases 4 or more books, he or she earns 60 points.
Write a program that asks the user to enter the number of books that he or she has purchased this month and then displays the number of points awarded.
Explanation / Answer
BooksDemoPoint.java
/*
BY CHEGG EXPERT
*/
package com.Books;
import java.util.Scanner;
/**
*
* @author Chegg subject expert
*/
public class BooksDemoPoints {
public static void main(String args[]) {
int points, totalpoints = 0;//initializing totalpoints=0
try{//errors amy occur in try block it will handled by catch block
System.out.println("No of Books Purchased in this month");
Scanner sc = new Scanner(System.in);//keyboard inputting thru scanner class
int noofbooks = sc.nextInt();
System.out.println("Total No of book purchased is" + noofbooks);
if (noofbooks == 0) {//if noofbook=0 points=0
points = 0;
System.out.println("Total No of Points awarded=" + points);
}
if (noofbooks == 1) {//if noofbooks=1 points=5
points = 5;
System.out.println("Total No of Points awarded=" + points);
}
if (noofbooks == 2) {//if noofbooks=2 points=5
points = 15;
System.out.println("Total No of Points awarded=" + points);
}
if (noofbooks == 3) {//if noofbooks=3 points=15
points = 30;
System.out.println("Total No of Points awarded=" + points);
}
if (noofbooks == 4) {//if noofbooks=4 points=60
points = 60;
System.out.println("Total No of Points awarded=" + points);
}
if (noofbooks > 4) {
totalpoints = (int) noofbooks / 4;
totalpoints = totalpoints * 60;
if (noofbooks % 4 == 3) {
totalpoints = totalpoints + 30;
}
if (noofbooks % 4 == 2) {
totalpoints = totalpoints + 15;
}
if (noofbooks % 4 == 1) {
totalpoints = totalpoints + 5;
}
if (noofbooks % 4 == 0) {
totalpoints = totalpoints + 0;
}
System.out.println("no of books purchased is" + noofbooks);
System.out.println("Total points awarded is" + totalpoints);
}
}
catch(NumberFormatException e)//handling exception
{
System.out.println("exception thrown is"+e.getMessage());
}
finally
{
//closeing files
}
}
}
output
testcase 1.
No of Books Purchased in this month
34
Total No of book purchased is34
no of books purchased is34
Total points awarded is495
BUILD SUCCESSFUL (total time: 4 seconds)
test case 2.
run:
No of Books Purchased in this month
345
Total No of book purchased is345
no of books purchased is345
Total points awarded is5165
BUILD SUCCESSFUL (total time: 5 seconds)
Test case 3.
run:
No of Books Purchased in this month
100
Total No of book purchased is100
no of books purchased is100
Total points awarded is1500
BUILD SUCCESSFUL (total time: 5 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.