2

I'm writing code for an application that keeps track of a student’s food purchases at a campus cafeteria. There's two classes - Student, which holds overloaded constructors & appropriate getter & setter methods; and MealCard, which holds a class variable to track the number of meal cards issued, appropriate getter & setter methods, a purchaseItem() method, a purchasePoints() method & an overriddden toString() method. There's a Tester class also.

In my MealCard class, the methods are written but in the Tester when I call them, they don't work correctly. I want to get the itemValue from the user but how do I do this in the MealCard class?

Same goes for the purchasePoints method, how do I get the topUpValue from user in MealCard class?

Code so far is:

public class Student {

// Instance Variables
private String name;
private int age;
private String address;

// Default Constructor
public Student() {
    this("Not Given", 0, "Not Given");
}

// Parameterized constructor that takes in values
public Student(String name, int age, String address) {
    this.name = name;
    this.age = age;
    this.address = address;
}

// Getters and Setters
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

public String getAddress(){
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

// toString() to be overriden
@Override
public String toString() {
    return "Name: " + this.name + "\n" + "Age: " + this.age + "\n" + "Address: " + this.address;
}
}

`

public class MealCard extends Student {

static int numberOfMealCards;
private final static int DEFAULT_BALANCE = 1000;
private int itemValue;
private int topUpValue;
public int newBalance;

// Getters and Setters
public int getItemValue() {
    return itemValue;
}
public void setItemValue(int itemValue) {
    this.itemValue = itemValue;
}

public int getTopUpValue() {
    return topUpValue;
}
public void setTopUpValue(int topUpValue) {
    this.topUpValue = topUpValue;
}

// purchaseItem method for when students buy food
public int purchaseItem() {
    newBalance = DEFAULT_BALANCE - itemValue;
    return newBalance;
}

// purchasePoints method for students topping up their meal card balance
public int purchasePoints() {
    newBalance = DEFAULT_BALANCE + topUpValue;
    return newBalance;
}

// Overriden toString method
@Override
public String toString() {
    return super.toString() + "Meal Card Balance: " + this.newBalance + "\n" + "Number of Meal Cards: " + numberOfMealCards;
}

}

`

import java.util.Scanner;

public class TestMealCard {

public static void main(String[] args) {

    // Create instances of MealCard class
    MealCard student1 = new MealCard();
    MealCard student2 = new MealCard();

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Name: ");
    student1.setName(keyboard.nextLine()); 
    System.out.println("Age: ");
    student1.setAge(keyboard.nextInt()); 
    System.out.println("Address: ");
    student1.setAddress(keyboard.nextLine()); 
    System.out.println("Meal Card Balace: ");
    student1.newBalance = keyboard.nextInt();
    System.out.println("Number of Meal Cards Issued: ");
    student1.numberOfMealCards = keyboard.nextInt();

    System.out.println("Name: ");
    student2.setName(keyboard.nextLine()); 
    System.out.println("Age: ");
    student2.setAge(keyboard.nextInt()); 
    System.out.println("Address: ");
    student2.setAddress(keyboard.nextLine()); 
    System.out.println("Meal Card Balace: ");
    student2.newBalance = keyboard.nextInt();
    System.out.println("Number of Meal Cards Issued: ");
    student2.numberOfMealCards = keyboard.nextInt();

    // Call purchaseItem
    student1.purchaseItem();


    // Call purchasePoints
    student2.purchasePoints();

    // Call tString to output information to user
}
}
Laura Berry
  • 89
  • 2
  • 12
  • 1
    What isn't printing out completely can you give an example of output that is occurring and what you are expecting? – chrissukhram Mar 20 '15 at 03:11
  • Comment from Meguy 26: What do you meant by "they don't work correctly"? Is an error thrown? Or are the values being improperly set/got/manipulated. Your code will not work so long as the variables are unset, meaning that you have to call student1.setTopUpValue(int) before you call student1.getToUpValue, I would set those values, TopUp, mealCardNumber, etc, as 0 or another placeholder number, but thats just me. – xlm Mar 20 '15 at 03:22
  • Comment from Meguy 26: Are you actually unable to call the method, or, as aforementioned, do they throw an error? If you cannot call the method, then perhaps creating a constructor for the MealCard class, instead of using an inherited one, would work. As a last note, I would suggest not overriding toString(), its such a basic object method its better left untampered with, just implement your own getID, or getStringId or something. – xlm Mar 20 '15 at 03:23
  • @Meguy26 not true, it will work as the compiler will set defaults for the fields. See http://stackoverflow.com/questions/19131336/java-default-values-and-initialization – xlm Mar 20 '15 at 03:30
  • @Meguy26 it's actually strongly encouraged to override `toString` to something more meaningful. See Item 9 of Effective Java by Joshua Bloch . – xlm Mar 20 '15 at 03:33
  • @xlm Just curious, who are you talking with? I don't see any comment from Meguy26 here :). – iRuth Mar 20 '15 at 03:39
  • @iRuth he apologetically posted a comment as a answer because his rep is too low. I said I post his comment for him on condition he delete the non-answer he gave. I hope he sees what I had to say... :) – xlm Mar 20 '15 at 03:41

1 Answers1

0

In order to set the itemValue from the user you need to get get that input from the user using this setItemValue() method.

Ex.

System.out.print("Please enter the item value:   ");
student1.setItemValue(keyboard.nextInt());

or

int itemVal = 0;
System.out.print("Please enter the item value:   ");
itemVal = keyboard.nextInt();
student1.setItemValue(itemVal);

as for the other method call just call the setter for toUpValue.

Ex.

System.out.print("Please enter the item value:   ");
student1.setTopUpValue(keyboard.nextInt());

or

int toUpVal = 0;
System.out.print("Please enter the item value:   ");
itemVal = keyboard.nextInt();
student1.setTopUpValue(topUpVal);

Hope that helps =).