(Java) 如何让数据库删除存储的值,直到该值存储超过 5 次?
(Java) How do I make a database delete a stored value until the value has been stored more than 5 times?
我有点想以某种方式进行非常基本的内存复制。
基本上,我希望我的程序接受用户输入并在大约 18 秒后忘记它(将其从数据库中删除),但如果输入重复 5 次或更多次则记住它(将其永久存储在数据库中)。
这是我目前的代码(顺便说一句,这是一个 JavaFX 程序):
TextField userText = new TextField();
Timeline time;
String message;
message = userText.getText();
ArrayList<String> memory = new ArrayList<>();
if(message.contains(message) && message.contains(" ") && !memory.contains(message)){
String[] splitMessage = message.split(" ");/*To split the sentence*/
for(int i = 0; i<splitMessage.length; i++)
memory.add(splitMessage[i]); /*To add the individual words of a sentence*/
memory.add(message); /*To add the sentence itself*/
time = new Timeline(new KeyFrame(Duration.millis(18000),
ae -> memory.remove(message)));
time.play();
}
到目前为止我已经有了这个,它可以存储数据 18 秒。但我想要它,以便在程序尝试连续或随机顺序存储同一消息超过 5 次后,数据将永久存储到 "memory" 中。
有办法吗?
希望这是有道理的。我是出了名的问题措辞不当哈哈。
提前致谢:)。
这是我尝试提供一个额外的复杂层来满足您的需求(如果我理解正确的话)。
我会替换:
ArrayList<String> memory = new ArrayList<>();
和
MemorySpace memorySpace = new MemorySpace();
我看到的唯一问题是正确解释:
!memory.contains(message);
可能是
memorySpace.isMemoryPermanent(message);
或
memorySpace.isMemoryActive(message);
希望 API 足够清楚,让您理解我的意图,以及它对您的情况有何帮助。我的理解是,一个词第一次就记住了,一句话却要记住五次才能永久。
public class MemorySpace {
private final Map<String, Memory> memorySpace;
public MemorySpace() {
this.memorySpace = new HashMap<>();
}
public void addWord(String word) {
Memory m = this.memorySpace.get(word);
if (m == null) {
this.memorySpace.put(word, new Memory(true, word))
}
}
public void addSentence(String sentence) {
Memory m = this.memorySpace.get(sentence);
if (m == null) {
this.memorySpace.put(sentence, new Memory(false, sentence))
}
else {
m.increaseSeenCount();
}
}
public boolean isMemoryPermanent(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryPermanent();
}
return false;
}
public boolean isMemoryActive(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryActive();
}
return false;
}
private class Memory {
private final boolean isWordMemory;
private final String wordOrSentence;
private int seenCount;
private long lastSeenAtMilliseconds;
Memory(boolean isWordMemory, String newWordOrSentence) {
this.isWordMemory = isWordMemory;
this.wordOrSentence = newWordOrSentence;
this.seenCount = 1;
this.lastSeenAtMilliseconds = System.currentTimeMillis();
}
boolean isWordMemory() {
return this.isWordMemory;
}
void increaseSeenCount() {
if (!this.isWordMemory) {
if (this.seenCount < 5) { // Stop overflow
this.seenCount++;
}
this.lastSeenAtMilliseconds = System.milliseconds();
}
}
void isMemoryPermanent() {
return this.isWordMemory || this.seenCount >= 5;
}
void isMemoryActive() {
return this.isWordMemory || this.isMemoryPermanent() || (System.currentTimeMillis() - this.lastSeenAtMilliseconds) < (18 * 1000);
}
}
}
我有点想以某种方式进行非常基本的内存复制。 基本上,我希望我的程序接受用户输入并在大约 18 秒后忘记它(将其从数据库中删除),但如果输入重复 5 次或更多次则记住它(将其永久存储在数据库中)。
这是我目前的代码(顺便说一句,这是一个 JavaFX 程序):
TextField userText = new TextField();
Timeline time;
String message;
message = userText.getText();
ArrayList<String> memory = new ArrayList<>();
if(message.contains(message) && message.contains(" ") && !memory.contains(message)){
String[] splitMessage = message.split(" ");/*To split the sentence*/
for(int i = 0; i<splitMessage.length; i++)
memory.add(splitMessage[i]); /*To add the individual words of a sentence*/
memory.add(message); /*To add the sentence itself*/
time = new Timeline(new KeyFrame(Duration.millis(18000),
ae -> memory.remove(message)));
time.play();
}
到目前为止我已经有了这个,它可以存储数据 18 秒。但我想要它,以便在程序尝试连续或随机顺序存储同一消息超过 5 次后,数据将永久存储到 "memory" 中。 有办法吗?
希望这是有道理的。我是出了名的问题措辞不当哈哈。
提前致谢:)。
这是我尝试提供一个额外的复杂层来满足您的需求(如果我理解正确的话)。
我会替换:
ArrayList<String> memory = new ArrayList<>();
和
MemorySpace memorySpace = new MemorySpace();
我看到的唯一问题是正确解释:
!memory.contains(message);
可能是
memorySpace.isMemoryPermanent(message);
或
memorySpace.isMemoryActive(message);
希望 API 足够清楚,让您理解我的意图,以及它对您的情况有何帮助。我的理解是,一个词第一次就记住了,一句话却要记住五次才能永久。
public class MemorySpace {
private final Map<String, Memory> memorySpace;
public MemorySpace() {
this.memorySpace = new HashMap<>();
}
public void addWord(String word) {
Memory m = this.memorySpace.get(word);
if (m == null) {
this.memorySpace.put(word, new Memory(true, word))
}
}
public void addSentence(String sentence) {
Memory m = this.memorySpace.get(sentence);
if (m == null) {
this.memorySpace.put(sentence, new Memory(false, sentence))
}
else {
m.increaseSeenCount();
}
}
public boolean isMemoryPermanent(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryPermanent();
}
return false;
}
public boolean isMemoryActive(String workOrSentence) {
Memory m = this.memorySpace.get(wordOrSentence)
if (m != null) {
return m.isMemoryActive();
}
return false;
}
private class Memory {
private final boolean isWordMemory;
private final String wordOrSentence;
private int seenCount;
private long lastSeenAtMilliseconds;
Memory(boolean isWordMemory, String newWordOrSentence) {
this.isWordMemory = isWordMemory;
this.wordOrSentence = newWordOrSentence;
this.seenCount = 1;
this.lastSeenAtMilliseconds = System.currentTimeMillis();
}
boolean isWordMemory() {
return this.isWordMemory;
}
void increaseSeenCount() {
if (!this.isWordMemory) {
if (this.seenCount < 5) { // Stop overflow
this.seenCount++;
}
this.lastSeenAtMilliseconds = System.milliseconds();
}
}
void isMemoryPermanent() {
return this.isWordMemory || this.seenCount >= 5;
}
void isMemoryActive() {
return this.isWordMemory || this.isMemoryPermanent() || (System.currentTimeMillis() - this.lastSeenAtMilliseconds) < (18 * 1000);
}
}
}