toliot toliot - 1 month ago 5
Java Question

Problems with setters in Java - Internet Bill Program

Hi I am having some problems assigning values in the object I am trying to make, it is calling the variable I set originally and not the one I set using my setter method, the odd thing is that it seems to pass correctly through the first scanner as my math method works as it should here is the code:

main

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

}


}

class

/*
* 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);

}
}


Please let me know how to use the scanner properly (I think that's what's causing the issue right now), thank you very much,

Answer

Your setters are incorrect.

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

Should be,

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