Logic or Stack? (JAVA)

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

Summary

day:1
month: 10
year: 2016
distance: 3.0 mile
in: 12 minute
I feel: good

day:6
month: 10
year: 2016
distance: 3.5 mile
in: 5 minute
I feel: like god

day:5
month: 10
year: 2016
distance: 3.4 mile
in: 7 minute
I feel: like superman

day:4
month: 10
year: 2016
distance: 3.3 mile
in: 8 minute
I feel: very cool

day:3
month: 10
year: 2016
distance: 3.2 mile
in: 9 minute
I feel: coll

day:2
month: 10
year: 2016
distance: 3.1 mile
in: 19 minute
I feel: not good

1 Like

vấn đề là ở chỗ này đây. Với cách thiết kế này thì đây là một Stack, kết quả trả ra của nó là LIFO (Last In - First Out), nhưng lại mong muốn có kết quả trả ra là FIFO(First In - First Out) như Queue.
Còn tại sao nó lại trả ngày thứ 1 trước tiên, đó là do cách thiết kế “khá đặc biệt” của em, vừa cho lớp dữ liệu lưu trữ thông tin, vừa cho nó quản lý. Thì khi thằng ngày 1 nhận vào nó giữ trong lớp ConsoleLog đầu tiên, những thằng sau thì add tiếp như một Stack.
Cách giải quyết là thiết kế lại lớp Console chỉ nhận lớp chứa thông tin, add thêm hay lấy ra, chứ bản thân nó không được chưa dữ liệu.

2 Likes

Cám ơn anh nhiều ạ, vò đầu bức tóc cả ngày mà cũng không phân biệt nổi là do Logic hay do Stack :((,
giờ mới hiểu là do cả cách thiết kế :frowning:

nếu muốn ra theo thứ tự add
thì bạn cũng phải thay đổi lại cái first khi add vào cái mới cho cái đó làm first
thí dụ ở phương thức add

add(Log next) {
    Log temp = first;
    first = next;
    rest = new ConsLog(temp,rest);
    temp = null;
}

hope this help :smile:
nếu như vậy nó sẽ ra theo quy tắc FILO

Cho em hỏi ILogInterface-Log
ConsLogConsole-Log
Còn MTLog là gì vậy ?

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?