Created
January 21, 2016 20:11
-
-
Save ZhdanRuslan/4e8eb4b3089ee41306ce to your computer and use it in GitHub Desktop.
Level 20, Lesson 02, Task 01
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
package com.javarush.test.level20.lesson02.task01; | |
public class Asset { | |
public Asset(String name) { | |
this.name = name; | |
} | |
private String name; | |
private double price; | |
public String getName() { | |
return name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Asset {" +" name= '" + name + '\'' + ", price= " + price + '}'; | |
} | |
} |
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
package com.javarush.test.level20.lesson02.task01; | |
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) | |
{ | |
try | |
{ | |
final String hardCodeFName = "C:\\f1.txt"; | |
OutputStream outputStream = new FileOutputStream(hardCodeFName); | |
InputStream inputStream = new FileInputStream(hardCodeFName); | |
Human ivanov = new Human("Ivanov", new Asset("home"), new Asset("car")); | |
ivanov.save(outputStream); | |
outputStream.flush(); | |
Human somePerson = new Human(); | |
somePerson.load(inputStream); | |
System.out.println(ivanov); | |
System.out.println(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) | |
{ | |
this.name = name; | |
if (assets != null) | |
{ | |
this.assets.addAll(Arrays.asList(assets)); | |
} | |
} | |
public void save(OutputStream outputStream) throws Exception | |
{ | |
PrintWriter writeFile = new PrintWriter(outputStream); | |
String isNamePresent = (name != null) ? "yes" : "no"; | |
writeFile.println(isNamePresent); | |
if (isNamePresent.equals("yes")) | |
{ | |
writeFile.println(name); | |
if (assets.size() > 0) | |
{ | |
for (Asset a : assets) | |
writeFile.write(a.getName() + "\r\n"); | |
} | |
} | |
writeFile.close(); | |
} | |
public void load(InputStream inputStream) throws Exception | |
{ | |
BufferedReader readFile = new BufferedReader(new InputStreamReader(inputStream)); | |
String hasName = readFile.readLine(); | |
if (hasName.equals("yes")) | |
{ | |
this.name = readFile.readLine(); | |
String temp; | |
while ((temp = readFile.readLine()) != null) | |
assets.add(new Asset(temp)); | |
} | |
readFile.close(); | |
} | |
@Override | |
public String toString() | |
{ | |
return "Human {" + "name = '" + name + '\'' + ", assets = " + assets + '}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment