import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;

import java.util.List;
import java.util.ArrayList;
import java.io.NotSerializableException;

@SuppressWarnings("unchecked")
public class Test
{
	private static final Logger LOGGER = LogManager.getLogger(Test.class);

	static final String FILE_PATH = "C:/Users/a/OneDrive/JAVA/20170927/write.txt";
	public static void main(String[] args)
	{
		String top1 = "下列做法哪些可以有效避免驾驶疲劳？";
		String[] op1 = {"A、用餐不宜过饱", "B、餐后适当休息后驾车", "C、保持良好的睡眠", "D、连续驾驶不超过4小时"};
		String an1 = "答案是:D";
		Question q1 = new Question(top1, op1, an1);

		String top2 = "车辆发生“水滑”时，以下做法正确的是什么？";
		String[] op2 = {"A、开远光灯", "B、不开启灯光", "C、开启示廓灯、后位灯", "D、开危险报警闪光灯"};
		String an2 = "答案：B";
		Question q2 = new Question(top2, op2, an2);

		String top3 = "夜间路边临时停车，以下做法错误的是什么？";
		String[] op3 = {"A、立刻猛踏制动踏板，降低车速", "B、不可急踩制动踏板", "C、不得迅速转向", "D、逐渐松抬加速踏板，让车速逐渐减缓"};
		String an3 = "答案：D";
		Question q3 = new Question(top3, op3, an3);

		String top4 = "机动车高速行驶中出现转向失控时，驾驶人要如何处置？";
		String[] op4 = {"A、抢挂低速挡", "B、合理使用行车制动和驻车制动，避免紧急制动", "C、紧急制动", "D、开启危险报警闪光灯"};
		String an4 = "答案：B";
		Question q4 = new Question(top4, op4, an4);

		String top5 = "如图所示，驾驶机动车遇到对向来车正在强行超车时，以下做法正确的是什么？";
		String[] op5 = {"A、向左打转向盘避让", "B、向右借用人行道避让", "C、迎着来车鸣喇叭将其逼回", "D、减速避让"};
		String an5 = "答案：D";
		Question q5 = new Question(top5, op5, an5);

		List<Question> list = new ArrayList<>();
		list.add(q1);
		list.add(q2);
		list.add(q3);
		list.add(q4);
		list.add(q5);

		for (Object obj : list)
		{
			writer(obj);
			LOGGER.info(reader().toString());
		}

		
	}

	public static void writer(Object obj)
	{
		
		
		ObjectOutputStream oos = null;
		try
		{
			
			oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
			oos.writeObject(obj);
			oos.flush();
		}
		catch (FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch (IOException e)
		{
			LOGGER.catching(e);
		}
		finally 
		{
			try
			{
				if (oos != null)
				{
					oos.close();
				}
			}
			catch (IOException e)
			{
				LOGGER.catching(e);
			}
		}
		
	}

	public static Object reader()
	{
		InputStream in = null;
		ObjectInputStream ois = null;
		Object obj = null;
		try
		{
			in = new FileInputStream(FILE_PATH);
			ois = new ObjectInputStream(in);

			obj = ois.readObject();


		}
		catch (FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch (ClassNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch (IOException e)
		{
			LOGGER.catching(e);
		}
		finally 
		{
			try
			{
				if (in != null)
				{
					in.close();
				}
			}
			catch (IOException e)
			{
				LOGGER.catching(e);
			}
			try
			{
				if (ois != null)
				{
					ois.close();
				}
			}
			catch (IOException e)
			{
				LOGGER.catching(e);
			}
		}
		return obj;
	}
	
}

class Question implements Serializable
{
	private String topic;

	private String[] options = new String[4];

	private String answer;

	public Question(){}

	public Question(String topic, String[] options, String answer)
	{
		this.topic = topic;
		this.options = options;
		this.answer = answer;
	}

	public String getTopic()
	{
		return this.topic;
	}

	public String[] getOptions()
	{
		return this.options;
	}

	public String getAnswer()
	{
		return this.answer;
	}

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append(this.topic).append("\n");
		for (String str : options)
		{
			sb.append(str).append("\n");
		}
		sb.append(this.answer).append("\n");
		return sb.toString();
	}
}