0

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);
            }  
        }
}
kaviranga
  • 486
  • 2
  • 8
  • 20
  • `getCalculateRewardPoints` and `getRewardPoints` have the same method body. That's weird, don't you think? – MC Emperor Oct 18 '20 at 18:46
  • yes I got the answer. .. I did it a little different than what it was suggested, but I was point to the right direction... you guys are amazing. – Carlos Caceres Oct 18 '20 at 18:50
  • MC ... I notice that after you mention it ... I got rid of most of my code and it is working now ... thanks for looking over my code. – Carlos Caceres Oct 18 '20 at 18:52

3 Answers3

0

There is a little bit of confusion in your code, but for beginners it's normal :)

First of all, in your main class you are printing rewardsProgram.getRewardPoints() that just simply returns rewardPoints which is null!

When you set your books inside setBooks method you have to call a second method to calculate your rewardsPoints, in your case it's the setRewardsPoints. Inside this method you have to use your variable books to get your points, you can use something like:

switch(books){
  case 0:
   rewardsPoints=0; break;
  case 1:
   rewardsPoints=5; break;
  ...
}

This is how you should calculate your points. To print rewardsPoints, simply do:

System.out.println(rewardsProgram.getRewardPoints());

You also need to clear a little bit your class, some methods are useless! This is a simple way to do what you need.

Stefano Leone
  • 236
  • 2
  • 10
0

You should address the following problems present in your code:

  1. The declaration String studentName; inside the constructor, BookclubTester(){} is useless.
  2. You have neither defined the setter and getter methods for studentName nor set it into the object, rewardsProgram.
  3. Instantiate Scanner outside the while loop i.e. Scanner input = new Scanner(System.in); should be outside the while loop. You can reuse the same Scanner object instantiated outside the while loop.
  4. I also suggest you use Integer.parseInt(input.nextLine()) instead of input.nextInt() to avoid problem described here.
  5. You have not set rewardPoints into the object, rewardsProgram. Ideally, the method calculating/setting the reward points should be called from setBooks because the reward points are based on the number of books.
  6. There should be a condition (e.g. if (books >= 4)) for the reward points when the number of books is more than 4.

Given below is the code incorporating these recommendations:

import java.util.Scanner;

class Bookclub {

    private String studentName;
    private int books = 0;
    private int rewardPoints;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getBooks() {
        return books;
    }

    public void setBooks(int books) {
        this.books = books;
        setRewardPoints(books);
    }

    public int getCalculateRewardPoints() {
        return rewardPoints;
    }

    public int getRewardPoints() {
        return rewardPoints;
    }

    public void setRewardPoints(int books) {
        if (books == 0) {
            rewardPoints = 0;
        } 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 class Main {

    public static void main(String[] args) {
        int books;
        int count = 0;
        Scanner input = new Scanner(System.in);

        while (count < 5) {
            Bookclub rewardsProgram = new Bookclub();

            System.out.print("Please provided your name: ");
            String studentName = input.nextLine();
            rewardsProgram.setStudentName(studentName);

            System.out.print("Please provide the numbers of books purchase for the  month: ");
            books = Integer.parseInt(input.nextLine());
            rewardsProgram.setBooks(books);

            System.out.println("Reward points: " + rewardsProgram.getRewardPoints());
            count++;
        }
    }
}

A sample run:

Please provided your name: Test1
Please provide the numbers of books purchase for the  month: 2
Reward points: 15
Please provided your name: Test2
Please provide the numbers of books purchase for the  month: 1
Reward points: 5
Please provided your name: Test3
Please provide the numbers of books purchase for the  month: 7
Reward points: 60
Please provided your name: Test4
Please provide the numbers of books purchase for the  month: 0
Reward points: 0
Please provided your name: Test5
Please provide the numbers of books purchase for the  month: 4
Reward points: 60

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.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0
public class BookClubMember {

    private final String name;
    private int books;
    private int rewardPoints;

    public BookClubMember(String name, int numberOfBooks) {
            this.name = name;
            setBooks(numberOfBooks);
    }

    public BookClubMember(String name) {
            this(name, 0);

    }

    public final String getName() {
            return name;
    }

    public final int getBooks() {
            return books;
    }

    public final void setBooks(int numberOfBooks) {
            int oldValue = books;
            if (numberOfBooks <= 0) {
                books = 0;
            } else {
                books = numberOfBooks;
            }
            if (oldValue != books) {
                // update your points if they have changed.
                rewardPoints = calculateRewardPoints(books);
            }
    }

        
    public final int getRewardPoints() {
            return rewardPoints;
    }

        /**
         * This can be overridden in the future if you ever wanted to extend
         * this class and make a "Platinum Member"... etc where they get more points
         * per book, or they are on a linear scale, etc etc.
         * 
         *
         * @param bookCount
         * @return
         */
        protected int calculateRewardPoints(int bookCount) {
            switch (bookCount) {
                case 0:
                    return 0;
                case 1:
                    return 5;
                case 2:
                    return 15;
                case 3:
                    return 30;
                default:
                    return 60;
            }
        }
    }

And your main method

 public static void main(String[] args) {
    
            // so you need to work in a scanner object here to collect a name, and number of books.
            // once you have both you can create a new BookClubMember object.
            // I'm cheating and manually putting them in :)
            BookClubMember member = new BookClubMember("Johnny", 5);
            System.out.println(member.getName() + " has " + member.getRewardPoints() + " points " + " for " + member.getBooks() + " books");
        }

Output:

Johnny has 60 points for 5 books