Vehicle Data Inventory System - Java Project on Vehicle Inventory

Verified

Added on  2022/12/28

|18
|2151
|53
Project
AI Summary
This document details a Java project that implements a Vehicle Data Inventory System using a sorted linked list. The project utilizes Java generics, encapsulation, and abstraction to create a system that stores and manages vehicle data. The core of the system is a custom-built sorted linked list implemented using a Node class and the Vehicle class. The program takes user input for vehicle details (make, model, and miles per gallon), validates the data, and adds the vehicle objects to the sorted linked list. The list is sorted based on miles per gallon. The program then allows the user to specify an output file where the sorted vehicle data is written. The document includes the program's code (Node.java, Vehicle.java, LinkedList.java, MainClass.java), program execution screenshots, and a reflection section discussing the project's purpose, algorithm, inputs, outputs, limitations, and lessons learned. The reflection highlights the use of generics, linked lists, data validation, and the Comparable interface for sorting the linked list. The project provides an error-free mechanism for managing vehicle inventory.
Document Page
Running head: Vehicle Data Inventory System 1
Vehicle Data Inventory System
Name
Course
Professor
Date
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Vehicle Data Inventory System 2
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() {
Document Page
Vehicle Data Inventory System 3
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();
}
}
Document Page
Vehicle Data Inventory System 4
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();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Vehicle Data Inventory System 5
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;
Document Page
Vehicle Data Inventory System 6
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. */
Document Page
Vehicle Data Inventory System 7
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();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Vehicle Data Inventory System 8
}
}
}
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): ");
Document Page
Vehicle Data Inventory System 9
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<>();
Document Page
Vehicle Data Inventory System 10
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(
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Vehicle Data Inventory System 11
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();
}
}
Document Page
Vehicle Data Inventory System 12
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.
chevron_up_icon
1 out of 18
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]