samedi 28 mars 2015

Is this a correct way to share data member values within the class?



I am working with my getLuxuryTax() method in my class Boat. I want to calculate the price of the boat, but if its length is greater than or equal to 50 and its age is less then or equal to 10, then calculate the boat's price with luxury tax. So, is my getLuxuryTax() method good? The reason I ask is because it is not doing the way it suppose to do in the Java code. Here is what the code looks like for my class Boat:



public class Boat {

//Declare data members
private String make;
private String registeration;
private int year;
private int length;
private int beam;
private String fuel;
private double price;
private String picURL;
private int age;
private double priceLT;

//Define the methods
public String getMake()
{
return make;
}

public void setMake(String m)
{
make = m;
}

public String getRegi()
{
return registeration;
}

public void setRegi(String r)
{
registeration = r;
}

public int getYear()
{
return year;
}

public void setYear(int y)
{
year = y;
}

public int getLength()
{
return length;
}

public void setLength(int L)
{
length = L;
}

public int getBeam()
{
return beam;
}

public void setBeam(int b)
{
beam = b;
}

public String getFuel()
{
return fuel;
}

public void setFuel(String f)
{
fuel = f;
}

public double getPrice()
{
return price;
}

public void setPrice(double p)
{
price = p;
}

public String getPicURL()
{
return picURL;
}

public void setPicURL(String url)
{
picURL = url;
}

public int getAge()
{

int currentYear = 2015;

//Calculate the age of the boat
age = currentYear - year;

return age;
}

public double getLuxuryTax()
{
//Declare variables
double newPrice = 0;
double luxTax = .15;

//Calculate the price with luxury tax if the first condition
//is found to be True.
if (length >= 50 && age <= 10)
{
newPrice = price * luxTax;
priceLT = price + newPrice;
}
else
{
priceLT = 0;
}

return priceLT;
}

}


And, here is the code where I am calling the getLuxuryTax() method:



public void showBoat(View view)
{
//Declare references and variables
EditText et;
TextView tv;
ImageView im;
BoatList boat_list;
String boatRegist;
String pic_url;
String boatPrice;
boolean found;
int i;

//Set references
et = (EditText) findViewById(R.id.edit_boat_regist);

tv = (TextView) findViewById(R.id.text_main);

im = (ImageView) findViewById(R.id.image_area);

boat_list = BoatList.getInstance();

//Get user input
boatRegist = et.getText().toString();

//Search through the list and try to find the boat by registration
i = 0;
found = false;
while(!found && (i < boat_list.size()))
{
try
{
if(boat_list.get(i).getRegi().equals(boatRegist))
{
found = true;
}
else
{
i++;
}
}
catch(IndexOutOfBoundsException e)
{
Toast.makeText(ShowBoatActivity.this, "Error: Registration cannot be found!",
Toast.LENGTH_SHORT).show();
}
}

//If the registration exists, show the boat's details
if(found)
{
//If applicable, show the boat's detail with its price
//including luxury tax

if(boat_list.get(i).getLuxuryTax() > 0)
{
boatPrice = String.format("%.2f", boat_list.get(i).getLuxuryTax());

tv.setText("\n" + boat_list.get(i).getMake() + "\nRegistration #: " + boat_list.get(i).getRegi() +
"\n" + boat_list.get(i).getYear() + "\nLength: " + boat_list.get(i).getLength()
+ "\nBeam: " + boat_list.get(i).getBeam() + " inches" + "\nFuel: " + boat_list.get(i).getFuel() +
"\n$" + boatPrice + "\n" + boat_list.get(i).getAge() + " yrs" + "\n");

//If the boat's age is 0, then display it that it is new
if(boat_list.get(i).getAge() == 0)
{
tv.append("Brand new!");
}
}

//Format the price of the boat to have two decimal places
boatPrice = String.format("%.2f", boat_list.get(i).getPrice());

//Display the boat's full detail
tv.setText("\n" + boat_list.get(i).getMake() + "\nRegistration #: " + boat_list.get(i).getRegi() +
"\n" + boat_list.get(i).getYear() + "\nLength: " + boat_list.get(i).getLength()
+ "\nBeam: " + boat_list.get(i).getBeam() + "\nFuel: " + boat_list.get(i).getFuel() +
"\n$" + boatPrice + "\n" + boat_list.get(i).getAge() + " yrs" + "\n");

if(boat_list.get(i).getAge() == 0)
{
tv.append("Brand new!");
}


//Try to open the picture from the URL address
try
{
pic_url = boat_list.get(i).getPicURL();
ImageDownloader idl = new ImageDownloader();
idl.download(pic_url, im);
}
catch(IndexOutOfBoundsException e)
{
Toast.makeText(ShowBoatActivity.this, "Error: Cannot get the image or not found!",
Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(ShowBoatActivity.this, "Error: Registration # is invalid! ",
Toast.LENGTH_SHORT).show();
}

}//end of showBoat



Aucun commentaire:

Enregistrer un commentaire