Skip to content

Instantly share code, notes, and snippets.

@themellowj
Created May 6, 2020 01:41
Show Gist options
  • Select an option

  • Save themellowj/58e91a7a4afa29297eba4da127b1b299 to your computer and use it in GitHub Desktop.

Select an option

Save themellowj/58e91a7a4afa29297eba4da127b1b299 to your computer and use it in GitHub Desktop.
package kebreau;
/**
* Name: Kei Kebreau
* Course: CIS217
* Lab: 3 - Wage Gamer Serializer
* Date: April 10, 2019
* Description: The Serializer is used for converting the objects into text
* representing the values in the objects and converting the text back to
* objects for use in a program. The GamerSerializer will be able to serialized
* and deserialize WageGamer objects and OpponentGamer objects. In addition, it
* should be a program that tests each class by instantiating the class and
* using the public methods.
*/
import McGriff.WageGamer;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class WageGamerSerializer {
// public static methods
/**
* void writeWageGamer(WageGamer writeMe, String filename)
* @param writeMe - a wageGamer to serialize
* @param filename - the file to serialize to
*/
public static void writeWageGamer(WageGamer writeMe, String filename) {
try {
PrintWriter outputWriter = new PrintWriter(new File(filename));
// Write winnings.
outputWriter.print("Winnings:");
outputWriter.println(writeMe.getWinnings());
// Write losses.
outputWriter.print("Losses:");
outputWriter.println(writeMe.getLosses());
// Write array of rounds.
outputWriter.print("Games:");
Integer[] rounds = writeMe.getRounds();
for (int i = 0; i < rounds.length; ++i) {
outputWriter.print(rounds[i]);
// Newline instead of comma after the last value.
if (i == rounds.length - 1) {
outputWriter.println();
} else {
outputWriter.print(",");
}
}
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* WageGamer readWageGamer(String filename)
* @param filename - the file to read from
*/
public static WageGamer readWageGamer(String filename) {
ArrayList<Integer> rounds = new ArrayList<>();
try {
Scanner s = new Scanner(new File(filename));
String[] line = null;
/* Lines are formatted like so...
[Name of data]:[data]
- Numeric data is simply written as a number.
- List data is written as one data point followed by another,
separated by a comma.
- Nested list data is written as one list followed by another,
each surrounded by braces and separated by a comma.
*/
while (s.hasNextLine()) {
line = s.nextLine().split(":");
if (line[0].equals("Games")) {
String[] rawRoundData = line[1].split(",");
for (String amountWon : rawRoundData) {
rounds.add(Integer.valueOf(amountWon));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return new WageGamer(rounds.toArray(new Integer[0]));
}
/**
* public static void main(String[] args)
* this program should test a WageGamer
* by creating one, testing all of it's methods, and
* serializing and deserializing the objects.
*/
public static void main(String[] args) {
WageGamer testGamer = new WageGamer();
for (int i = 0; i < 10; ++i) {
// Generate a random score between -10 and 10.
int randomScore = (int)(Math.random() * 20 - 10);
testGamer.playGame(randomScore);
}
writeWageGamer(testGamer, "testWageGamer.txt");
WageGamer copyCatGamer = readWageGamer("testWageGamer.txt");
writeWageGamer(copyCatGamer, "copyCatWageGamer.txt");
System.out.println("The files \"testWageGamer.txt\" and \"copyCatWageGamer.txt\" should be the same.");
System.out.println("If not, contact Kei Kebreau at [email protected].");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment