Created
May 4, 2020 22:18
-
-
Save nicooga/f6ccaef488c2c0c1665e072dc1a64654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class IntStorage { | |
RandomAccessFile file; | |
public IntStorage(String fileName) throws FileNotFoundException { | |
this.file = new RandomAccessFile(fileName, "rw"); | |
} | |
public void pushInt(int number) throws IOException { | |
this.doPushInt(this.file, number); | |
} | |
public void readAll() throws IOException { | |
this.doReadAll(this.file); | |
} | |
public void find(int number) throws IOException { | |
this.file.seek(0); | |
while (this.file.getFilePointer() < this.file.length()) { | |
if (this.file.readInt() == number) { | |
System.out.println(this.file.getFilePointer() / 4); | |
return; | |
} | |
} | |
} | |
public void average() throws IOException { | |
int count = 0; | |
int sum = 0; | |
this.file.seek(0); | |
while (this.file.getFilePointer() < this.file.length()) { | |
count += 1; | |
sum += this.file.readInt(); | |
} | |
System.out.println(sum / count); | |
} | |
public void sort() throws IOException { | |
// Collect numbers in vector | |
java.util.Vector<Integer> numbers = new java.util.Vector<Integer>(); | |
this.file.seek(0); | |
while (this.file.getFilePointer() < this.file.length()) { | |
numbers.addElement(this.file.readInt()); | |
} | |
// Do the actual sorting. | |
int size = numbers.size(); | |
for (int i = 0; i < size; i++) { | |
int min = i; | |
// Find smallest element in (numbers[i] ... numbers[size-1]) | |
// and swap it with numbers[i] | |
for (int j = i+1; j < size; j++) { | |
if (numbers.elementAt(j) < numbers.elementAt(min)) { | |
min = j; | |
} | |
} | |
java.util.Collections.swap(numbers, i, min); | |
} | |
// numbers should be sorted by now | |
for (int i = 0; i < size; i++) { | |
System.out.println(numbers.elementAt(i)); | |
} | |
} | |
public void export(String evensFileName, String oddsFileName) throws IOException { | |
RandomAccessFile evensFile = new RandomAccessFile(evensFileName, "rw"); | |
RandomAccessFile oddsFile = new RandomAccessFile(oddsFileName, "rw"); | |
// Overwrite files | |
evensFile.setLength(0); | |
oddsFile.setLength(0); | |
this.file.seek(0); | |
while (this.file.getFilePointer() < this.file.length()) { | |
int number = this.file.readInt(); | |
if (number % 2 == 0) { | |
this.doPushInt(evensFile, number); | |
} else { | |
this.doPushInt(oddsFile, number); | |
} | |
} | |
System.out.println("Even numbers exported to " + evensFileName); | |
this.doReadAll(evensFile); | |
System.out.println("========"); | |
System.out.println("Odd numbers exported to " + oddsFileName); | |
this.doReadAll(oddsFile); | |
System.out.println("========"); | |
} | |
public void doPushInt(RandomAccessFile file, int number) throws IOException { | |
file.seek(file.length()); | |
file.writeInt(number); | |
} | |
public void doReadAll(RandomAccessFile file) throws IOException { | |
file.seek(0); | |
while (file.getFilePointer() < file.length()) { | |
System.out.println(file.readInt()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Main { | |
static java.io.Console console = System.console(); | |
static IntStorage storage; | |
static { | |
try { | |
storage = new IntStorage("./storage.dat"); | |
} catch (FileNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static void main(String[] args) throws FileNotFoundException { | |
int option; | |
try { | |
while (true) { | |
System.out.println(""); | |
System.out.println("Choose an option:"); | |
System.out.println("1 - Push a new integer into storage "); | |
System.out.println("2 - Read all integers in storage "); | |
System.out.println("3 - Find an integer and print it's position"); | |
System.out.println("4 - Print average for all integers in storage"); | |
System.out.println("5 - Print sorted list of integers"); | |
System.out.println("6 - Export storage filtered by even or odd"); | |
System.out.println("0 - Exit"); | |
System.out.println(""); | |
option = Integer.parseInt(console.readLine()); | |
System.out.println(""); | |
System.out.println("========="); | |
System.out.println(""); | |
if (option == 0) { | |
return; | |
} else if (option > 0 && option <= 6) { | |
switch (option) { | |
case 1: { | |
int number = Integer.parseInt(console.readLine("What number: ")); | |
storage.pushInt(number); | |
break; | |
} | |
case 2: { | |
storage.readAll(); | |
break; | |
} | |
case 3: { | |
int number = Integer.parseInt(console.readLine("What number: ")); | |
storage.find(number); | |
break; | |
} | |
case 4: { | |
storage.average(); | |
break; | |
} | |
case 5: { | |
storage.sort(); | |
break; | |
} | |
case 6: { | |
storage.export("evens.dat", "odds.dat"); | |
break; | |
} | |
} | |
} else { | |
System.out.println("That's not a valid option"); | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment