Hello I am taking java class and I have this program as an assignment. I am trying to figure out who to set the value and return the calculation using Scanner class. I am a total beginner in java ( in any comp. language). if you can help me figure out the issue. I have look everywhere but I can't find the solution.
/**
* Write a description of class BookclubTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
public class BookclubTester {
public BookclubTester() {
String studentName;
}
public static void main (String [] args) {
int books;
int count = 0;
while (count < 5) {
Scanner input = new Scanner(System.in);
System.out.println("Please provided your name :" );
String studentName = input.nextLine();
Bookclub rewardsProgram = new Bookclub();
System.out.println("Please provide the numbers of books purchase for the month " );
books = input.nextInt();
rewardsProgram.setBooks(books);
System.out.println(rewardsProgram.getRewardPoints());
count++;
}
}
}
This is the class
/**
*
* @author
*/
public class Bookclub {
private String studentName;
private int books = 0;
private int rewardPoints;
public Bookclub () {
}
public int getBooks() {
return books;
}
public void setBooks(int books) {
this.books = books;
}
public int getCalculateRewardPoints (){
return rewardPoints;
}
public int getRewardPoints() {
return rewardPoints;
}
public void setCalculateRewardPoints (int rewardPoints) {
this.rewardPoints = rewardPoints;
if (books == 0) {
rewardPoints = 0;
System.out.println("Your points are:" + rewardPoints);
} else if (books == 1) {
rewardPoints = 5;
}else if (books == 2) {
rewardPoints = 15;//
}else if(books == 3) {
rewardPoints = 30;
}else if(books == 4) {
rewardPoints = 60;
}
}
public void setRewardPoints(int rewardPoints) {
this.rewardPoints = rewardPoints;
if(rewardPoints == 0) {
rewardPoints = 0;
}else if(rewardPoints ==5) {
System.out.println("Your points are : " + rewardPoints);
}else if(rewardPoints == 15) {
System.out.println("Your points are:" + rewardPoints);
}else if(rewardPoints == 30) {
System.out.println("Your points are:" + rewardPoints);
}else if(rewardPoints == 60) {
System.out.println("Your points are:" + rewardPoints);
}
}
}
You should address the following problems present in your code:
String studentName;
inside the constructor,BookclubTester(){}
is useless.studentName
nor set it into the object,rewardsProgram
.Scanner
outside thewhile
loop i.e.Scanner input = new Scanner(System.in);
should be outside the while loop. You can reuse the sameScanner
object instantiated outside thewhile
loop.Integer.parseInt(input.nextLine())
instead ofinput.nextInt()
to avoid problem described here.rewardPoints
into the object,rewardsProgram
. Ideally, the method calculating/setting the reward points should be called fromsetBooks
because the reward points are based on the number of books.if (books >= 4)
) for the reward points when the number of books is more than4
.Given below is the code incorporating these recommendations:
A sample run:
You can still think of many validations (e.g. check if the number of books is not negative) and I hope, these suggestions will help you design your program for those things.