如何让踢脚员在下面的三个加油站之间进行转运?

时间:2015-07-15 22:15:17

标签: java inheritance methods polymorphism instance

给定Kickback类,以及作为Kickback子类的Store子类的三个子类。理解为此类中的Kicker成员持有被封装的Kickback点,以返回到Person类中的虚拟对象。如何让Kickback子类中的踢脚员可以在三个加油站之间转移。所以换句话说,如何设置Kickback中的踢球者和人物中的封装成员点在神圣访问变异后保持85点,在76次访问后获得125点后保持210点?以下代码是Kickback类,Person对象和上述加油站。任何诚实的帮助表示赞赏。如果它仍然可能是一个很大的代码转储,我道歉;我想尽可能地直截了当。

回扣

import java.util.*;

public class Kickback{

    public static final int START = 0;
    public static final int REWARD = 50;

    protected Person visitor;
    protected int purchase, kicker;
    protected String name;

    public Kickback(){
    visitor = new Person(purchase,kicker);
    name = "Darin Douglas";
    }

    public double get_points(){
      return kicker += purchase/REWARD;
    }

    public boolean redeem_points(){
    if(kicker < purchase){
         purchase -= kicker;
         zeroKB();
         return true;
    }
    kicker -= purchase;
    return false;
    }
    public double zeroKB(){
    return kicker = START;
    }
}

public class Divine extends Store{
    protected int puncher, drinks;
    protected String freebie;
    public static final double COST = 99;
    public static final int COFFEES = 5;
    public static final String FREEBIE = "Congratulations, you have earned a free coffee!";
    public Divine(){
    super();
    puncher = COFFEES;
    freebie = FREEBIE;
    }
    public void init(){
    drink();
    purchase += drinks*COST;
    System.out.println("Just note:\n You have " +drinks+ " and your purchase so far is "+purchase);
    super.init();
    System.out.println(toString());
    }
     public void drink(){
    System.out.print("How many coffees?\n99 cents each\n");
    drinks = scan.nextInt();
    if(puncher == super.START)
        free_coffee();
    puncher -= drinks;
    }
    private void free_coffee(){
    System.out.println(freebie);
        reset();
    }
    public String toString(){
      return name + ",\nYou have " + kicker + " points.\nAnd have " +puncher+ " coffees remaining.";
    }
    public int reset(){
       return puncher = COFFEES;
    }
}

SeventySix:

import java.util.*;
public class SeventySix extends Store{
    public SeventySix(){
    super();
    }
    public void nav(){
    super.init();
    System.out.print(super.toString());
    }
}

Jmart:

import java.util.*;
import java.io.*;
public class Jmart extends Store{
    public static final int PERCENT = 100;
    public Jmart(){
    super();
    }
    public void init(){
    System.out.print("Enter in an amount: ");
    purchase = scan.nextInt();
        get_points();
    super.spend_money();
    System.out.println(super.toString());
    }
    public double get_points(){
        return kicker += purchase/PERCENT;
    }
}

假人:

import java.util.*;
public class Person{
    private int cash, debit, amount, points;  
    public static final int CASH = 1000;
    public static final int CREDIT = 3000;

  public Person(int purchase, int kicker){
    cash = CASH;
    debit = CREDIT;
    amount = purchase;
    points = kicker;
    }
    public boolean use_cash(){
    if(cash < amount){
        amount -=  cash;
        System.out.println("We got " + cash + "cash.\nYour remaining balance is " + amount + "\nPlease try another method of payment.");
        reset_cash();
        return true;
}
        cash -= amount;
    return false;
    }
    public boolean use_card(){
    if(debit < amount){
        System.out.println("Card Declined.\nPlease try another method of payment.");
        return true;
    }
        debit -= amount;
    return false;
    }
    public double reset_cash(){
    return cash = Kickback.START;
    }
}

1 个答案:

答案 0 :(得分:1)

因此,由于您已经发布了整个代码并且代码很长并且需要一段时间才能解密和调试,我将帮助您入门。首先,我知道您创建的Kickback课程的目的是为Kickback卡片建模以存储奖励积分。因此,通过创建一个类,您已经创建了一个用户定义的类型。在您的方法“get_points”中,您将“Double Kickback”作为参数传递。参数和变量由类型和对应于值的标识符表示。该参数将是“Kickback myCard”或类似的东西,但在这种情况下,由于您试图修改Kickback卡的值,并且您在Kickback类中,因此您不需要Kickback类型的参数。您只需要直接修改名为cardValue的类的私有成员,该成员可能具有double类型。 Java不支持运算符重载。你不能只做Kickback ++。因此,当您声明私有成员时,声明一个名为myCardValue的属性:

    double myCardValue;

因此,而不是做

    Kickback += purchase*REWARD; 

您需要修改myCardValue成员:

    myCardValue += purchase*REWARD. 

注意:扫描仪不应该是Kickback的成员,也不应该是这些常量。这些常量应该只在程序中的类之外声明和初始化。他们是公开的,所以所有班级都可以访问。

鉴于您希望在其他类中使用此成员,您必须提供与Kickback类的myCardValue成员交互的getter和setter方法(a.k.a访问器和mutator方法)。实施例

    public static double getKickBackCardVal(){
        return myCardValue;
    }

请记住,Kickback是一个类型/对象。在Person类中,Person应该具有一个属性,该属性是Kickback类的实例的声明:

    Kickback myCard;

因为我假设你的世界,你想要假人有一张Kickback卡。希望这有所帮助。我为这么长而复杂的解释道歉,但你发布了整个代码来调试和判断代码,你已经表明你需要更好地理解类和对象,如何创建和交互它们。