Skip to content

Commit

Permalink
only quiz and update must be done
Browse files Browse the repository at this point in the history
  • Loading branch information
felixnguyen678 committed Apr 18, 2021
1 parent 09ca653 commit a4d236d
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 144 deletions.
26 changes: 22 additions & 4 deletions src/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Model.*;
import Model.*;
import View.*;
import Controller.*;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -8,15 +11,30 @@

public class App {
public static void start() throws IOException {
// repository
Repository repository = Repository.getInstance();
repository.reset("slang.txt");
System.out.println(repository.randomSlang().getSlang());

System.out.println(repository.findBySlang(new Slang("BHD")).getMeanings());;
HomeView homeView = new HomeView();


}
public static void main(String[] args) throws IOException {

try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}


//JFrame.setDefaultLookAndFeelDecorated(true);
start();

}
Expand Down
99 changes: 99 additions & 0 deletions src/Controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package Controller;

import Model.*;
import View.*;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;

public class Controller {
//singleton

private static Controller instance = new Controller();
private Controller(){}

public static Controller getInstance() {
return instance;
}



public void searchBySlang(String _slang){
ArrayList<Slang> slangs = new ArrayList<Slang>();
Slang req = new Slang(_slang);
Slang res = Repository.getInstance().findBySlang(req);
if(res == null){
JOptionPane.showMessageDialog(null, "We do not have any slang like this","Search warning", JOptionPane.WARNING_MESSAGE);
req.addMeaning("This slang doesn't exist");
Repository.getInstance().addSlangHistory(req);
slangs.add(req);
return;
}
else{
Repository.getInstance().addSlangHistory(res);
slangs.add(res);
}
ResultsView resultsView = new ResultsView(slangs);

}
public void searchByKeyword(String _keyword){
Keyword keyword = Repository.getInstance().findByKeyword(
new Keyword(_keyword)
);
if(keyword == null){
JOptionPane.showMessageDialog(null, "We do not have any slang like this","Search warning", JOptionPane.WARNING_MESSAGE);
return;
}
ResultsView resultsView = new ResultsView(keyword.getSlangs());

}
public ArrayList<Slang> getSlangHistory(){
return Repository.getInstance().getSlangHistory();
}
public void addSlang(String _slang, String _meaning){
Slang slang = new Slang(_slang, _meaning);
Slang check = Repository.getInstance()
.findBySlang(slang);
if(check == null){
Repository.getInstance().addSlang(slang);
JOptionPane.showMessageDialog(null,"Successfully Added !!!");
}
else{
String[] options = {"Overwrite", "Dupplicate"};
int i = JOptionPane.showOptionDialog(null, "Overwrite or duplicate", "Select an Option",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if(i == 0){
Repository.getInstance().overwriteSlang(slang);
JOptionPane.showMessageDialog(null,"Successfully Overwrited !!!");
}
if(i == 1){
Repository.getInstance().duplicateSlang(slang);
JOptionPane.showMessageDialog(null,"Successfully Duplicated !!!");
}
}
}
public void deleteSlang(String _slang){
Slang slang = new Slang(_slang);
if(Repository.getInstance().findBySlang(slang) == null){
JOptionPane.showConfirmDialog(null, "We do not have any slang like this");

}else{
Repository.getInstance().deleteSlang(slang);
JOptionPane.showMessageDialog(null, "Successfully Deleted !!!");
}
}
public void resetDictionary(){
Repository.getInstance().reset("slang.txt");
JOptionPane.showMessageDialog(null, "Successfully reseted !!!");

}
public ArrayList<Slang> randomSlang(){
ArrayList<Slang> slangs = new ArrayList<Slang>();
slangs.add(Repository.getInstance().randomSlang());
return slangs;

}

}
9 changes: 0 additions & 9 deletions src/Controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package Controller;

import View.*;
import Model.*;

import java.util.ArrayList;

public class HomeController {
HomeView homeView;
ArrayList<Slang> slangArrayList = new ArrayList<Slang>();
public void init(){

}
}
41 changes: 32 additions & 9 deletions src/Model/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
import java.util.ArrayList;

public class Repository {
private AVLTree<Slang> slangAVLTree;
public AVLTree<Slang> slangAVLTree;
private AVLTree<Keyword> keywordAVLTree;
private ArrayList<Slang> slangHistory;
private ArrayList<Keyword> keywordHistory;
// singleton
private static Repository instance = new Repository();
private static final Repository instance = new Repository();
private Repository(){
slangAVLTree = new AVLTree<Slang>();
keywordAVLTree = new AVLTree<Keyword>();
slangHistory = new ArrayList<Slang>();
keywordHistory = new ArrayList<Keyword>();
try {
import_data("slang.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Repository getInstance(){ return instance;}

// debug


//---- CRUD modules
// get history
Expand All @@ -45,7 +52,14 @@ public Keyword findByKeyword(Keyword req){
// add slang and keyword
public void addSlang(Slang _slang){
// add to slang tree and keyword tree both
slangAVLTree.insert(_slang);
Slang search = slangAVLTree.find(_slang);
if(search != null){
search.addMeaning(_slang.getMeanings().get(0));
slangAVLTree.delete(search);
slangAVLTree.insert(search);
}
else
slangAVLTree.insert(_slang);
for(String i : _slang.getMeanings()){// add keyword to keywordAVL tree
String[] split = i.split("\\W+");
for(String j : split){
Expand Down Expand Up @@ -120,19 +134,28 @@ public void import_data(String _filepath) throws IOException {

while((line = bufferedReader.readLine())!= null){
String[] _split = line.split("`");
Slang slang = new Slang(_split[0], _split[1]);
addSlang(slang);
String[] _keyword = _split[1].split("[|]");
for(String i:_keyword){
Slang slang = new Slang(_split[0], i);
addSlang(slang);
}
}
System.out.println("successfully imported");
}
// export data
public void export_data(String _filepath){
}
// reset
public void reset(String _filepath) throws IOException {
instance = new Repository();
import_data(_filepath);

public void reset(String _filepath){
slangAVLTree = new AVLTree<Slang>();
keywordAVLTree = new AVLTree<Keyword>();
slangHistory = new ArrayList<Slang>();
keywordHistory = new ArrayList<Keyword>();
try {
import_data("slang.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
public Slang randomSlang(){
return slangAVLTree.randomNode();
Expand Down
69 changes: 69 additions & 0 deletions src/View/AddView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package View;
import Controller.Controller;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class AddView {

private class AddPanel extends JPanel{
JTextField slangTextField;
JTextField meaningTextField;
JButton button;
public AddPanel(){
setBorder(new EmptyBorder(60, 60, 60 ,60 ));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;

gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;

add(new JLabel("Slang: "));
slangTextField = new JTextField("", 15);
add(slangTextField,gbc);

add(new JLabel("Definition: "));
meaningTextField = new JTextField("", 15);
add(meaningTextField, gbc);

button = new JButton("Add Slang");
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Controller.getInstance().addSlang(
slangTextField.getText(),
meaningTextField.getText()
);
}
}
);
add(button);

}

}
public AddView(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new AddView.AddPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Loading

0 comments on commit a4d236d

Please sign in to comment.