Created
December 24, 2016 09:08
-
-
Save alekseytimoshchenko/6fb492f09ce659028e8c45d2e47e6d8a 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.*; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/* Читаем и пишем в файл: Human | |
Реализуйте логику записи в файл и чтения из файла для класса Human | |
Поле name в классе Human не может быть пустым | |
В файле your_file_name.tmp может быть несколько объектов Human | |
Метод main реализован только для вас и не участвует в тестировании | |
*/ | |
public class Solution { | |
public static void main(String[] args) { | |
//you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream | |
// according to your real file location | |
//вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream | |
// в соответствии с путем к вашему реальному файлу | |
try { | |
File your_file_name = File.createTempFile("your_file_name", null); | |
OutputStream outputStream = new FileOutputStream(your_file_name); | |
InputStream inputStream = new FileInputStream(your_file_name); | |
Human ivanov = new Human("Ivanov", new Asset("home"), new Asset("car")); | |
ivanov.save(outputStream); | |
outputStream.flush(); | |
Human somePerson = new Human(); | |
somePerson.load(inputStream); | |
//check here that ivanov equals to somePerson - проверьте тут, что ivanov и somePerson равны | |
if (ivanov.equals(somePerson)) { | |
} | |
inputStream.close(); | |
} catch (IOException e) { | |
//e.printStackTrace(); | |
System.out.println("Oops, something wrong with my file"); | |
} catch (Exception e) { | |
//e.printStackTrace(); | |
System.out.println("Oops, something wrong with save/load method"); | |
} | |
} | |
public static class Human { | |
public String name; | |
public List<Asset> assets = new ArrayList<>(); | |
public Human() { | |
} | |
public Human(String name, Asset... assets) { | |
if (name.equals("")) { | |
this.name = "default"; | |
} else { | |
this.name = name; | |
} | |
if (assets != null) { | |
this.assets.addAll(Arrays.asList(assets)); | |
} | |
} | |
public void save(OutputStream outputStream) throws Exception { | |
//implement this method - реализуйте этот метод | |
PrintWriter pw = new PrintWriter(outputStream); | |
pw.println(name); | |
for (Asset asset : assets) { | |
pw.println(asset.getName()); | |
pw.println(String.valueOf(asset.getPrice())); | |
} | |
pw.flush(); | |
pw.close(); | |
} | |
public void load(InputStream inputStream) throws Exception { | |
//implement this method - реализуйте этот метод | |
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); | |
String userName = br.readLine(); | |
List<Asset> assets = new ArrayList<>(); | |
while (br.ready()) { | |
String assetName = br.readLine(); | |
double assetPrice = Double.valueOf(br.readLine()); | |
Asset asset = new Asset(assetName); | |
asset.setPrice(assetPrice); | |
assets.add(asset); | |
} | |
this.name = userName; | |
this.assets = assets; | |
br.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment