This program implements a Vehicle Inventory using Linked Lists. It covers the understanding of Java generics, encapsulation, and abstraction and data structures. The program uses a sorted linked list to store and sort vehicle objects based on their miles per gallon value.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Running head: Vehicle Data Inventory System1 Vehicle Data Inventory System Name Course Professor Date
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Vehicle Data Inventory System2 Vehicle Data Linked List Their Vehicle Data Linked List program uses generics and data structure implementation for a linked list to implement a Vehicle Inventory using Linked Lists. This topic covers the understanding of Java generics, encapsulation, and abstraction and data structures. They have implemented their own sorted linked list in this application. They have used a Node class which will store a single node of the linked list. This node class will store an address to the next node and an Object element of type vehicle. A linked list will be formed as they will insert various vehicle objects in the list. Completed Program Node.java public class Node<E> { private E data; private Node<E> next; public E getData() { return data; } public Node<E> getNext() {
Vehicle Data Inventory System3 return next; } public Node(E e) { super(); this.data = e; } public void setData(E data) { this.data = data; } public void setNext(Node<E> next) { this.next = next; } public String toString() { return this.data.toString(); } }
Vehicle Data Inventory System4 Vehicle.java public class Vehicle implements Comparable<Vehicle>{ private String make; private String model; private double miles_per_gallon; public String getMake() { return make; } public String getModel() { return model; } public double getMiles_per_gallon() { return miles_per_gallon; } public Vehicle(String make, String model, double miles_per_gallon) { super();
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Vehicle Data Inventory System5 this.make = make; this.model = model; this.miles_per_gallon = miles_per_gallon; } @Override public int compareTo(Vehicle o) { return (int) (this.miles_per_gallon-o.miles_per_gallon); } @Override public String toString() { return make + " (" + model + ") : " + miles_per_gallon; } } LinkedList.java import java.io.PrintStream;
Vehicle Data Inventory System6 public class LinkedList<E extends Comparable<E>> { private Node<E> head; public void add(E e) { Node<E> current; Node<E> newNode=new Node<E>(e); /* Special case for head node */ if (head == null || head.getData().compareTo(newNode.getData())>=0) { newNode.setNext(head); head = newNode; } else { /* Locate the node before point of insertion. */
Vehicle Data Inventory System7 current = head; while (current.getNext() != null && current.getNext().getData().compareTo(newNode.getData()) < 0) current = current.getNext(); newNode.setNext(current.getNext()); current.setNext(newNode); } } public void print(PrintStream out) { Node<E> current=head; while(current!=null) { out.append(current.getData().toString()+System.lineSeparator()); current=current.getNext();
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Vehicle Data Inventory System8 } } } MainClass.java import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Scanner; public class MainClass { private static double readMiles(Scanner sc) { try { System.out.print("Vehicle miles per gallon (Greater than 0): ");
Vehicle Data Inventory System9 double value=Double.parseDouble(sc.nextLine()); if(value >0) return value; else { System.out.println("Please enter valid value for Miles Per Gallon."); return readMiles(sc); } } catch(Exception e) { System.out.println("Please enter valid value for Miles Per Gallon."); return readMiles(sc); } } public static void main(String[] args) { LinkedList<Vehicle> vehicles =new LinkedList<>();
Vehicle Data Inventory System10 Scanner sc=new Scanner(System.in); while(true) { String make, model; System.out.print("\nVehicle make(-1 to exit): "); make=sc.nextLine(); if(make.equals("-1")) break; System.out.print("Vehicle model: "); model=sc.nextLine(); double miles_per_gallon=readMiles(sc); Vehicle vehicle=new Vehicle(make, model, miles_per_gallon); vehicles.add(vehicle); } System.out.print("Please Enter the name of output file: "); String fileName=sc.nextLine(); try { PrintStream outStream = new PrintStream(
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Vehicle Data Inventory System11 new FileOutputStream(fileName, false)); vehicles.print(outStream); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Data successfully saved to "+fileName); sc.close(); } }
Vehicle Data Inventory System12 Program Execution Screenshots Screenshot 1: This screenshot shows the program output when the program is executed. The user is allowed to enter any number of data till the user enters -1 as the make of the vehicle. Then, the program will ask for the output file name and will write the output to that file. Screenshot 2: This screenshot shows the output file generated by the program. They can clearly see that all the data is sorted by the miles per gallon value of the vehicle.
Vehicle Data Inventory System13
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Vehicle Data Inventory System14 Reflection Questions Project Purpose:To create various java programs using own implementation of the Linked list, and other java provided facilities Exception handling, GUI in Java. The main purpose of this program is to implement a sorted linked list. The list that they are creating is sorted by the miles per gallon value of the vehicle. Algorithm Used:They have used their own implementation of a linked list in this program. They have a generic class node which can store any type of data and a pointer to the next data. Here, they are storing the vehicle object in the data part of the node. Then, there is a LinkedList class which is their own implementation of the linked list. They are using a sorted linked list and their list will be sorted by the miles per gallon value of the vehicle, so while inserting a data they will search for a correct place to insert the new node and then insert it in such a way that their list will always remain sorted. When their program will start, it will take continuous input from the user for the vehicle data and keep on adding this data to the linked list. Data validation is performed for all the user entered data and the program will stop taking further inputs, only when the user will enter -1 as the make of the vehicle. After adding all the data to the linked list, the program will ask the user for a file name where it will output all the data. This data is then printed to the user entered file name. Program Inputs:The program requires continuous input from the user to enter the vehicle details including Vehicle make, vehicle model and vehicle miles_per_gallon value. The program will stop taking new vehicle data from the user only when the user will enter -1 as the vehicle make. The user will have to enter a valid double value for miles per gallon of a vehicle and this
Vehicle Data Inventory System15 value must be a positive value greater than 0. After entering all the vehicle input data, the program will ask the user for an output file name and print all the output data to that file. Program Outputs:The program outputs all the vehicle data entered by the user into the output file. This file is a simple text file and all the data is sorted by the miles per gallon value of the vehicle. If the output file already exists with the provided name, then the file is overwritten with the new data. Program Limitations:Currently, this program is only capable of outputting the data to a text file. They can further extend the program to load the data from this text file back into the program when the program is loaded. In this way, the text file will act as a database to their program and the data can be persisted and loaded between different program runs. They can also add a graphical user interface to the program which will make it more user- friendly. Program Errors:No errors were found while testing the program as they have already implemented the error handling mechanism.
Vehicle Data Inventory System16 Portfolio Project: Lessons Learned Reflection They have learnt various lessons while working on the assignments. They have learnt about the use of generics, linked lists and data validation of data. The most difficult part of this assignment was to make their own implementation of sorted linked lists. But after spending some time on it, they were able to implement their own sorted linked list which will be always sorted by the miles per gallon value of the vehicle. They have also learnt about the comparable interface provided by java. Their vehicle class is implementing a comparable interface which is further helping their in sorting the linked list according to the vehicle miles per gallon value..Apart from the above-mentioned things, they have learnt about Java generics. Both Node and LinkedList classes are generic classes and can be used with any type of object type. They have also learnt about the use of PrintStream for printing the output to a text file. All the output is directly getting written to the output file and anyone can open that file directly to go through the data created by the program.For printing the vehicle object, they are overriding the toString method of the object class. This method is being overridden in the Vehicle class so that all the data is getting printed in a formatted way. They have also learnt about error handling in Java by making the use of a try-catch block. They are using error handling while validating the user input for the miles per gallon value of the vehicle. They are parsing the string to a double value and the application will throw an error if the user will enter any invalid double value.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Vehicle Data Inventory System17 Conclusion In conclusion, They have learned about how to implement their own linked list in java while working on this progra,. They have utilized the exception handling, printstream and generics provided by java. They are also validating the miles_per_gallon value entered by the user while entering the vehicle data. Their application will continue to ask for the miles per gallon value until the user enters a valid value.Using this application, any user can effectively and efficiently manage the vehicle data and get a sorted output in the text file. Thus, this application provides an error-free mechanism for handling the vehicle inventory for any user.
Vehicle Data Inventory System18 Bibliography Exception Handling in Java | Java Exceptions - javatpoint. (2019). Retrieved from https://www.javatpoint.com/exception-handling-in-java Insert given node into the correct sorted position in the given sorted linked list. (2019). Retrieved fromhttps://www.techiedelight.com/sorted-insert-in-linked-list/ Java Comparable interface example - HowToDoInJava. (2019). Retrieved from https://howtodoinjava.com/java/collections/java-comparable-interface/ Java Exceptions (Try...Catch). (2019). Retrieved from https://www.w3schools.com/java/java_try_catch.asp Java Generics. (2019). Retrieved fromhttps://www.tutorialspoint.com/java/java_generics.htm Java LinkedList Tutorial with Examples. (2019). Retrieved from https://www.callicoder.com/java-linkedlist/ Java toString() Method | Baeldung. (2019). Retrieved fromhttps://www.baeldung.com/java- tostring PrintStream (Java Platform SE 7 ). (2019). Retrieved from https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html