Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save enseitankad0/ce1471b67069f91ece848b6a97d10222 to your computer and use it in GitHub Desktop.
Save enseitankad0/ce1471b67069f91ece848b6a97d10222 to your computer and use it in GitHub Desktop.
The purpose of this program is to generate a number of random cars and to store them in the data structure. Then the data is saved to the format .dat and .txt and then read by the program.
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_9" project-jdk-name="9.0" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/205_extraChallangeSystemOutpuy.iml" filepath="$PROJECT_DIR$/205_extraChallangeSystemOutpuy.iml" />
</modules>
</component>
</project>
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
��srcom.enseitankado.CaruU �h|�InumberLcolortLjava/lang/String;Lmodelq~xptNavytOpelsq~tViolettSaabsq~tOrangetPeugeotsq~tBluetNissansq~tYellowtFordsq~q~q~sq~q~q~sq~q~q~sq~q~ q~
sq~ tGreentToyotasq~
q~q~sq~ q~q~sq~ q~ q~
sq~
q~q~sq~q~q~sq~q~ q~
0: Opel: Navy
1: Saab: Violet
2: Peugeot: Orange
3: Nissan: Blue
4: Ford: Yellow
5: Ford: Yellow
6: Ford: Yellow
7: Opel: Navy
8: Nissan: Blue
9: Toyota: Green
10: Opel: Navy
11: Ford: Yellow
12: Nissan: Blue
13: Opel: Navy
14: Toyota: Green
15: Peugeot: Orange
package com.enseitankado;
import java.io.Serializable;
public class Car implements Serializable{
private int number;
private String model;
private String color;
public Car(int number, String model, String color) {
this.number = number;
this.model = model;
this.color = color;
}
public int getNumber() {
return number;
}
public String getModel() {
return model;
}
public String getColor() {
return color;
}
@Override
public String toString() {
return number +": "+ model +": "+ color ;
}
}
package com.enseitankado;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Garage implements Map<Integer, Car> {
HashMap<Integer, Car> garage = new HashMap<>();
public String printGarage(){
for(Car car : garage.values()){
return car.toString();
}
return null;
}
@Override
public int size() {
return garage.size();
}
@Override
public boolean isEmpty() {
return garage.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return garage.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return garage.containsValue(value);
}
@Override
public Car get(Object key) {
return garage.get(key);
}
@Override
public Car put(Integer key, Car value) {
return garage.put(key,value);
}
@Override
public Car remove(Object key) {
return garage.remove(key);
}
@Override
public void putAll(Map<? extends Integer, ? extends Car> m) {
}
@Override
public void clear() {
garage.clear();
}
@Override
public Set<Integer> keySet() {
return garage.keySet();
}
@Override
public Collection<Car> values() {
return garage.values();
}
@Override
public Set<Entry<Integer, Car>> entrySet() {
return null;
}
}
package com.enseitankado;
import java.io.*;
import java.util.Random;
public class Main {
public static void main(String[] args) throws IOException {
// The purpose of this program is to generate a number of random cars and to store them in the data structure.
// Then the data is saved to the format .dat and .txt and then read by the program.
Garage garage = new Garage();
Garage newGarage = new Garage();
String[] cars = {"Fiat", "Peugeot", "Ford", "Toyota", "Nissan", "Opel", "Saab"};
String[] colors = {"Red", "Orange", "Yellow", "Green", "Blue", "Navy", "Violet"};
int randomNumber;
System.out.println("\n\n\t CREATING NEW RANDOM CARS");
for (int i = 0; i < 16; i++) {
Random random = new Random();
randomNumber = random.nextInt(cars.length);
Integer k = Integer.valueOf(i);
System.out.println(i + " - " + cars[randomNumber] +" - "+ colors[randomNumber]);
garage.put(i,new Car(k,cars[randomNumber],colors[randomNumber]));
}
// 1. WRITE TO DAT FILE
try (ObjectOutputStream locFile = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("garage.dat")))) {
for(Car car : garage.values()) {
locFile.writeObject(car);
}
}
// 2. READ FROM DAT FILE
try(ObjectInputStream locFile = new ObjectInputStream(new BufferedInputStream(new FileInputStream("garage.dat")))) {
System.out.println("\n\n\t SAVING DATA IN garage.dat");
boolean eof = false;
while(!eof) {
try {
Car car = (Car) locFile.readObject();
newGarage.put(car.getNumber(),car);
System.out.println(car.toString());
} catch(EOFException e) {
eof = true;
}
}
} catch(IOException io) {
System.out.println("IO Exception" + io.getMessage());
} catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException " + e.getMessage());
}
System.out.println("\n\n\t CONFIRMATION OF SUCCESFULL DATA IMPORT");
for(Car car : newGarage.values()){
System.out.println(car);
}
// 3. WRITE TO TXT FILE
try (BufferedWriter locFile = new BufferedWriter(new FileWriter("garageTXT.txt"))){
System.out.println("\n\n\t WRITING DATA TO garageTXT.txt file");
for (Car car : garage.values()) {
locFile.write(car.toString()+ "\n");
System.out.println(car.toString());
}
}
// 4. READ FROM TXT FILE
try (BufferedReader dirFile = new BufferedReader(new FileReader("garageTXT.txt"))) {
System.out.println("\n\n\t READING DATA FROM garageTXT.txt FILE");
String input;
System.out.println("Importing from file");
while((input = dirFile.readLine()) != null) {
String[] data = input.split(":");
int loc = Integer.parseInt(data[0]);
String model = data[1];
String color = data[2];
System.out.println(loc + ":" + model + ":" + color);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment