I am posting what I did so far .. I do want to get the price for car from the ma
ID: 3622087 • Letter: I
Question
I am posting what I did so far .. I do want to get the price for car from the mashtable.
I do have a file car.txt and creat the class name car.java which is representing all of cars
Items available and When user input the name of car and then how to get the total price for
What user input the name of car? For example, if user input two times genesis and then total price
is 80000..
Please help me out.. thanks in advance..
-------------------------------------------------------------------------------------------------
Class name: UsedCarDealer.java
Method name: Main
Class name: Car.java
repersenting a all of cars items available
hashtable
class name: WhichCar.java
repersenting an indivial car
String name, double price
Class name: Receipt.java
represting a receip
---------------------------------------------------------------------------------------------------
import java.util.Hashtable;
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Which car do you want to buy? ");
String nameOfCar = keyboard.nextLine();
car h1 = new car();
System.out.println("The price for the car is " + h1.getCar());
}
}
----------------------------------------------------------------------------------------------
import java.util.Hashtable;
public class cas
{
private String nameOfCar;
public void setCar(String carName)
{
nameOfCar = carName;
}
public String getCar()
{
Hashtable hs = new Hashtable();
hs.put("genesis", new Double(40000));
hs.put("Elantra", new Double(20000));
Object obj = hs.get(nameOfCar);
System.out.println(obj);
return nameOfCar;
}
}
-----------------------------------------------------------------------------------------
car.txt
Genesis, 40000
Elentra, 20000
Sentra 21000
------------------------------------------------------------------------------------------------
Explanation / Answer
There are a few issues with this. For one, you don't seem to use your setCar method to let the program know what car it is. Another problem is that the program won't know how to print something of type Object, so you may want to cast it to something like a double.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.