从不同类调用方法| Java

从不同类调用方法| Java,java,inheritance,methods,user-input,tostring,Java,Inheritance,Methods,User Input,Tostring,我正在为一个应用程序编写代码,该应用程序跟踪学生在校园自助餐厅购买的食物。有两个类-Student,它包含重载构造函数和适当的getter&setter方法;和MealCard,它保存一个类变量来跟踪发放的餐卡数量、适当的getter和setter方法、purchaseItem()方法、purchasePoints()方法和重写的toString()方法。还有一个测试仪类 在我的MealCard类中,方法是编写的,但是在Tester中调用它们时,它们不能正常工作。我想从用户那里获取itemVal

我正在为一个应用程序编写代码,该应用程序跟踪学生在校园自助餐厅购买的食物。有两个类-Student,它包含重载构造函数和适当的getter&setter方法;和MealCard,它保存一个类变量来跟踪发放的餐卡数量、适当的getter和setter方法、purchaseItem()方法、purchasePoints()方法和重写的toString()方法。还有一个测试仪

在我的MealCard类中,方法是编写的,但是在Tester中调用它们时,它们不能正常工作。我想从用户那里获取itemValue,但是在MealCard类中如何实现这一点

purchasePoints方法也是如此,如何从MealCard类中的用户处获取topUpValue

迄今为止的代码是:

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;
}
}
`

`


为了从用户处设置itemValue,您需要使用此setItemValue()方法从用户处获取输入

至于另一个方法调用,只需为toUpValue调用setter


希望这有帮助。

什么东西没有完全打印出来?你能举一个例子说明正在发生的输出和你的期望吗?Meguy 26的评论:“它们不能正常工作”是什么意思?是否抛出错误?或者这些值设置/获取/操作不当。只要变量未设置,代码就无法工作,这意味着您必须先调用student1.setTopUpValue(int),然后再调用student1.gettopupValue,我会将这些值、TopUp、mealCardNumber等设置为0或另一个占位符号,但这只是我的问题。Meguy的评论26:您实际上无法调用该方法,还是,如上所述,他们会抛出错误吗?如果您无法调用该方法,那么也许可以为MealCard类创建一个构造函数,而不是使用继承的构造函数。最后一点,我建议不要重写toString(),这是一种基本的对象方法,最好不要使用它,只需实现您自己的getID或getStringId之类的东西。@Meguy26不是真的,它会工作,因为编译器会为字段设置默认值。请参见@Meguy26,实际上强烈建议您将
toString
改写为更有意义的内容。见Joshua Bloch的《有效Java》第9项。
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
}
}
System.out.print("Please enter the item value:   ");
student1.setItemValue(keyboard.nextInt());
int itemVal = 0;
System.out.print("Please enter the item value:   ");
itemVal = keyboard.nextInt();
student1.setItemValue(itemVal);
System.out.print("Please enter the item value:   ");
student1.setTopUpValue(keyboard.nextInt());
int toUpVal = 0;
System.out.print("Please enter the item value:   ");
itemVal = keyboard.nextInt();
student1.setTopUpValue(topUpVal);