Answer the question
In order to leave comments, you need to log in
Why is the output not what you need (Input data output)?
A decorator implementation has been given. The task was to write an adapter. It seems like he wrote. But when you use it, it doesn't output what you want. Can you tell me where the implementation errors are (although usage errors are also possible)? How to write all the same correctly this adapter?
Result of program execution: Person(, 0) // А должно быть Person(Alex, 28)
Full code:
import java.io.*;
class Point {
private final int x;
private final int y;
public Point(int x, int y) {
if (x < 0 || 15 < x) {
throw new IllegalArgumentException();
}
if (y < 0 || 15 < y) {
throw new IllegalArgumentException();
}
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person(" + name + ", " + age + ")";
}
}
interface EntityInput {
public Person readPerson() throws IOException;
public Point readPoint() throws IOException;
}
interface EntityOutput {
public void writePerson(Person person) throws IOException;
public void writePoint(Point point) throws IOException;
}
class EntityOutputStream implements EntityOutput {
private final DataOutput out;
public EntityOutputStream(OutputStream out) {
this.out = new DataOutputStream(out);
}
@Override
public void writePerson(Person person) throws IOException {
out.writeInt(person.getAge());
if (person.getName() == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(person.getName());
}
}
@Override
public void writePoint(Point point) throws IOException {
int value = point.getX() << 4 | point.getY();
out.writeByte(value);
}
}
class EntityInputStream implements EntityInput {
private final DataInput src;
public EntityInputStream(InputStream src) {
this.src = new DataInputStream(src);
}
@Override
public Person readPerson() throws IOException {
return new Person(src.readUTF(), src.readByte());
}
@Override
public Point readPoint() throws IOException {
return new Point(src.readByte(), src.readByte());
}
}
public class App {
public static void main(String[] args) throws IOException {
Person person = new Person("Alex", 28);
EntityOutputStream out = new EntityOutputStream(new FileOutputStream("c://file.txt"));
EntityInput in = new EntityInputStream(new FileInputStream("c://file.txt"));
out.writePerson(person);
System.out.println(in.readPerson());
}
}
Answer the question
In order to leave comments, you need to log in
write int, bool, str
read str, byte
we need something like this
@Override
public Person readPerson() throws IOException {
int age = src.readInt();
boolean notNull = src.readBoolean();
String name = null;
if (notNull) name = src.readUTF();
return new Person(name, age);
}
How to be?
get is not part of REQUEST_URI
, but it is easily caught through a Query string, I think it will be easier for you to do this here
https://www.donatstudios.com/RewriteRule_Generator
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question