Java中的setter问题 - Internet Bill Program

时间:2016-10-21 18:42:18

标签: java io setter getter

您好我在尝试制作的对象中分配值时遇到了一些问题,它调用的是我最初设置的变量而不是我使用setter方法设置的变量,奇怪的是它似乎正确地通过了第一个扫描仪作为我的数学方法的工作原理应该是代码:

package a2.internetbill;
import java.util.Scanner;

public class A2InternetBill {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String type = "A";
    int nUsage = 0;
    String nAccountName = "Bob";
    // inititates objects
    Packages newPackageA = new Packages(type, nUsage, nAccountName, 0.0);

    Scanner in = new Scanner(System.in);

    //
    System.out.println("Please enter a package type(Must be A, B, C or D):\n\r");
    type = in.nextLine();
    newPackageA.setPackageType(type);

    System.out.println("Please Enter Your Name:\n\r ");

    newPackageA.setAccountName(in.nextLine());

    System.out.println("Please enter how much GB of Data you used: \n\r");

    newPackageA.setUsage(in.nextInt());


    String outPut;
    // do maths and prints formatted toString method
    newPackageA.moneyMath(type, nUsage, nUsage);
    outPut = newPackageA.toString();
    System.out.println(outPut);
    System.out.println(newPackageA.getAccountName() + newPackageA.getUsage() + newPackageA.getPackageType());
    // TODO code application logic here
        // instance variables

}

}

    /*
 * Makes internet package for specific user
 */
package a2.internetbill;

/**
 * 4 packages are:
 * PACKAGE A (IGNITE_60): 200GB usage for $49.99 per month. 
 * Extra usage is $3.00 per GB.
 * 
    PACKAGE B (IGNITE_30): 125GB usage for $39.99 per month. 
    * Extra usage is $1.50 per GB.
    PACKAGE C (INTERNET_5): 25GB usage for $24.99 per month. 
    * Extra usage is $5 per GB.

    PACKAGE D (UNLIMITED_USAGE): For $69.99 per month 
    * unlimited access is provided.

 * @author sav
 */


    public class Packages {
    private String packageType;
    private int usage;
    private String accountName;
    public double runningTotal;


    //initiates a Packages object
    public Packages(String type, int nUsage, String nAccountName, double nRunningTotal){
        packageType = type;
        usage = nUsage;
        accountName = nAccountName;
        runningTotal = nRunningTotal;

    }


    // getters
    public String getPackageType(){
        return packageType;
    }

    public int getUsage(){
        return usage;
    }

    public String getAccountName(){
        return accountName;
    }

    // setters     
    public void setPackageType(String type){
        type = packageType;
    }
    public void setUsage(int nUsage){
        nUsage = usage;

    }
    public void setAccountName(String nAccountName){
        nAccountName = accountName;
    }     

    public void moneyMath(String type, int nUsage, double nRunningTotal){
        if (type.equals("A")){
            if (nUsage>=200){
                nRunningTotal = ((nUsage - 200) * 3) + 49.99;
                type = "Ignite_60";
                packageType = type;

            }
            else{
                nRunningTotal = 49.99;
                type = "Ignite_60";
                packageType = type;
            }
        }
        else if (type.equals("B")){
            if (nUsage>=125){
                nRunningTotal = ((nUsage - 125) * 1.5) + 39.99;
                type = "Ignite_30";
                packageType = type;
            }
            else{
                nRunningTotal = 39.99;
                type = "Ignite_30";
                packageType = type;
            }
        }
        else if (type.equals("C")){
            if (nUsage>=25){
                nRunningTotal = ((nUsage - 25) * 5) + 24.99;
                type = "Internet_5";
                packageType = type;
            }
            else{
                nRunningTotal = 24.99;
                type = "Internet_5";
                packageType = type;
            }
        }
        else if (type.equals("D")){
            nRunningTotal = 69.99;
            type = "Unlimited_Usage";
            packageType = type;
        }
        else{
            System.out.println("Something something something");
        }



    }

    // 

    /**
     * Modified toString for specified outputs(formatted as assigned)
     * @return
     */
    @Override
    public String toString(){
        return ("Dear, \n\r" + accountName + " \n\r You have selected the : " + packageType + " package, and your current amount due is: $" + runningTotal);

    }
}

请让我知道如何正确使用扫描仪(我认为这是导致问题的原因),非常感谢,

1 个答案:

答案 0 :(得分:3)

你的二传手不正确。

public void setPackageType(String type){
    type = packageType;
}
public void setUsage(int nUsage){
    nUsage = usage;
}
public void setAccountName(String nAccountName){
    nAccountName = accountName;
}   

应该是,

public void setAccountName(String nAccountName){
    accountName = nAccountName;
}   
public void setPackageType(String type){
    packageType = type;
}
public void setUsage(int nUsage){
    usage = nUsage;
}