In: Computer Science
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of a Car, “Total_Price” will be calculate, and then the object must be saved in an array of objects. 4. Write each object created to a seperate file, for example: "Honda.txt", “KIA.text” and “BMW.text” for the following sample input file. 5. Write a method named showCar(double eNumber),takes the Engine Number as parameter, and displays all the information of the particular car including Total Price. Sample Input File: Honda 33244231 80 5 KIA 5544546 60 4 BMW 9846343 110 3.5
import java.io.*; class Car { String Vehicle_Name; String Engine_Number; double Vehicle_Price; double Profit; double Total_Price; Car(String Vehicle_Name, String Engine_Number, double Vehicle_Price, double profit) { this.Vehicle_Name = Vehicle_Name; this.Engine_Number = Engine_Number; this.Vehicle_Price = Vehicle_Price; this.Profit = Profit; this.Total_Price = Vehicle_Price + Vehicle_Price* Profit/100; } } class TestCar { public static void main(String args[]) throws Exception { File file = new File("/home/keerthi/Desktop/sample.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String st; int i = 0; Car cars[] = new Car[10]; File file1 = new File("/home/keerthi/Desktop/Honda.txt"); File file2 = new File("/home/keerthi/Desktop/KIA.txt"); File file3 = new File("/home/keerthi/Desktop/BMW.txt"); while ((st = br.readLine()) != null) { String[] arrOfStr = st.split(" ", 4); double price = Double.valueOf(arrOfStr[2]); double profit = Double.valueOf(arrOfStr[3]); cars[i] = new Car(arrOfStr[0],arrOfStr[1],price,profit); i++; if(arrOfStr[0] == "Honda") { FileWriter fw = new FileWriter(file1); fw.write(st); } else if(arrOfStr[0] == "KIA") { FileWriter fw=new FileWriter(file2); fw.write(st); } else if(arrOfStr[0] == "BMW") { FileWriter fw=new FileWriter(file3); fw.write(st); } } carShow(5544546,cars); } public static void carShow(double Enumber,Car cars[]) { for(int i = 0;i < cars.length;i++) { if(Enumber == Double.valueOf(cars[i].Engine_Number)) { System.out.println(cars[i].Vehicle_Name); System.out.println(cars[i].Engine_Number); System.out.println(cars[i].Vehicle_Price); System.out.println(cars[i].Profit); System.out.println(cars[i].Total_Price); } } } }
If you have any doubts please comment and please don't dislike.