Reputation: 89
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.
How do I override the toString method in MealCard from Student? It won't accept name, age or address - and I know they're private.
And in the tester class, how do I implement toString() to show the user information?
My 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 {
private 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 "Name: " + getName + "\n" + "Age: " + getAge + "\n" + "Address: " + getAddress +
"\n" + "Meal Card Balance: " + newBalance + "\n" + "Number of Meal Cards: " + numberOfMealCards;
}
}
Upvotes: 1
Views: 2663
Reputation: 8323
I figure it might be easier to show this in an answer, rather than in extensive commenting.
Providing an implementation of toString
in a class boils down to providing a String
representation of that class, nothing more. What you're trying to do is provide string information for an unrelated class (Student
), of which MealCard
knows nothing (and shouldn't) about.
How you accomplish what you want is probably to have Student
update to hold a reference to a MealCard
(read: each student has a MealCard
), and in your Student
implementation of toString
, use your meal card reference to get toString
data from the meal card class.
Ex:
public class MealCard {
//... skipped over for simplicity
@Override
public String toString() {
return "Item Value = " + this.getItemValue() + ", TopUpValue = " + this.getTopUpValue() + " ... "; // and so on...
}
}
Then in Student
, have a reference to MealCard
and use that in toString
public class Student {
// other student data
MealCard mealCard; // set in constructor
// ... other methods
@Override
public String toString() {
return " ... other student data... MealCard = " + getMealCard().toString(); // use the mealcard reference to get string info
}
}
Make sense?
Upvotes: 1