Chuyện là em tìm hiểu và học java khoảng một tuần rồi.
Khi em code đến đây thì vấn đề mới xuất hiện, em không biết là do lỗi logic hay là do cơ chế của stack,nên em mạo muội mong mấy tiền bối hỗ trợ em.
Lần đầu em đăng topic nên có lỗi lầm mọi người bỏ qua cho em.
Code
Summary
public interface ILog {
}
public class MTLog implements ILog {
MTLog() {
}
public String toString() {
return " ";
}
}
public class ConsLog implements ILog {
private Log first;
private ILog rest;
public ConsLog(Log nextLog, ILog oldrest) {
first = nextLog;
rest = oldrest;
};
public void add(Log nextLog) {
rest = new ConsLog(nextLog, rest);
return;
};
public String toString() {
return this.first + "\n" + this.rest;
}
}
public class Log {
private Date daytime;
private double distance;
private int duration; // with duration is minute
private String comment;
public Log(Date daytime, double distance, int duration, String comment) {
this.daytime = daytime;
this.distance = distance;
this.duration = duration;
this.comment = comment;
}
public String toString() {
return "day:" + this.daytime.getDay() + "\nmonth: " + this.daytime.getMonth() + "\nyear: "
+ this.daytime.getYear() + "\ndistance: " + this.distance + " mile" + "\nin: " + this.duration
+ " minute" + "\nI feel: " + this.comment + "\n-------------------\n";
}
}
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class LogTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
Log day1 = new Log(new Date(1, 10, 2016), 3.0, 12, "good");
Log day2 = new Log(new Date(2, 10, 2016), 3.1, 19, "not good");
Log day3 = new Log(new Date(3, 10, 2016), 3.2, 9, "coll");
Log day4 = new Log(new Date(4, 10, 2016), 3.3, 8, "very cool");
Log day5 = new Log(new Date(5, 10, 2016), 3.4, 7, "like superman");
Log day6 = new Log(new Date(6, 10, 2016), 3.5, 5, "like god");
ConsLog dayList = new ConsLog(day1, new MTLog());
dayList.add(day2);
dayList.add(day3);
dayList.add(day4);
dayList.add(day5);
dayList.add(day6);
System.out.println(dayList.toString());
}
}
Output