I am working on a homework assignment. The assignment is to calculate the
cost of a car based on options. I can get the cost to work, but I also
need to print out the price of the options - this I can't get to work.
I am working with two files
Car
and CarPurchase - code is pasted below from both. I have been working on
this for hours. I have tried putting it in my if statement
System.out.printf("The style price is 1000");
(this # will change for each option). I have tried also putting at
different places in each file. Nothing works. Please anyone help me!!!
Code is Car:
public class Car
{
public String style;
public float basePrice = 20000; // base price of carfloat basePrice =
20000; //
public float totalCost = basePrice;
public void setstyle(String styleprice)
{
style = styleprice;
}
public String getstyle()
{
if ( style == "1" )
{
totalCost = totalCost + 0;
}
if ( style == "2" )
{
totalCost = (totalCost + 800);
}
if (style == "3")
{
totalCost = (totalCost + 2000);
}
return style;
}
public void displayMessage()
{
System.out.printf("The style type is \n%s!\n", getstyle() );
if (getstyle() == "1" )
{
System.out.printf("The style price is 1000");
}
System.out.println("Total cost is: $" + totalCost );
}
}
Code in Car Purchase
import java.util.Scanner;
import java.io.*;
//import java.io.*;
public class CarPurchase
{
public static void main(String args[])throws IOException
{
Scanner input = new Scanner (System.in);
Car myCar = new Car();
System.out.printf("Initial style is: %s\n\n", myCar.getstyle() );
System.out.println("What style would you like (1=Hatchback, 2=Sedan,
3=Wagon )?");
BufferedReader stdin = new BufferedReader ( new InputStreamReader(
System.in ) );
String inData = stdin.readLine();
myCar.setstyle ( inData);
System.out.println();
myCar.displayMessage();
}
}
Hal Rosser - 28 May 2005 20:26 GMT
Does it compile ?
Try removing the 'f' from printf
System.out.print("hello");