wwwwwwjava

复制代码
public class Word {
    private String word;
    private String meaning;

    public Word(String word,String meaning){
        this.word=word;
        this.meaning=meaning;
    }

    public String getWord(){
        return  word;
    }

    public void setWord(String word){
        this.word=word;
    }

    public String getMeaning(){
        return meaning;
    }

    public void setMeaning(String meaning) {
        this.meaning = meaning;
    }

    @Override
    public String toString() {
        return word+": "+meaning;
    }
}
复制代码
import java.util.Scanner;

public class VocabularyManager {
    Word[] w=new Word[100];
    int count=0;
    Scanner sc =new Scanner(System.in);

    public VocabularyManager(){
        w[0]=new Word("Adaption","Adjusting to Current Environment");
        w[1]=new Word("Temptation","Getting attracted to something and can't resist");
        count=2;
    }

    public void displayAll(){
        for(int i=0;i<count;i++){
            System.out.println(w[i].getWord()+"="+w[i].getMeaning());
        }
    }

    public void searchWord(String word_name){
        boolean flag=false;
        for(int i=0;i<count;i++){
            if(w[i].getWord().equals(word_name)){
                System.out.println("Word is Found ");
                System.out.println(w[i].getWord()+"="+w[i].getMeaning());
                flag=true;
                break;
            }
        }
        if(!flag){
            System.out.println("No Such word exist in the Dictionary");
        }
    }

    public void addWord(){
        System.out.println("Enter new Word: ");
        String newWord=sc.nextLine();
        System.out.println("Enter meaning: ");
        String newMeaning=sc.nextLine();

        if(count<w.length){
            w[count]=new Word(newWord,newMeaning);
            count++;
            System.out.println("Word added ");
        }
        else{
            System.out.println("Full!");
        }

    }

    public void deleteWord(){
        System.out.println("Enter Word to delete: ");
        String word_name=sc.nextLine();
        boolean flag=false;

        for(int i=0;i<count;i++){
            if(w[i].getWord().equals(word_name)){
                flag=true;
                for(int j=i;j<count-1;j++){
                    w[j]=w[j+1];
                }
                count--;
                System.out.println("Word deleted ");
                break;
            }
        }

        if(!flag){
            System.out.println("Word not found!");
        }
    }

    public void updateWord(){
        System.out.println("Enter word to update: ");
        String newWord=sc.nextLine();
        boolean flag=false;

        for(int i=0;i<count;i++){
            if(w[i].getWord().equals(newWord)){
                flag=true;
                System.out.println("Enter word new meaning: ");
                String newMeaning=sc.nextLine();
                w[i].setMeaning(newMeaning);
                System.out.println("Word updated successfully");
                break;
            }
        }

        if(!flag){
            System.out.println("");
        }
    }

    public void teacherMenu(){
        int choice;



        do {
            System.out.println("Teacher Menu: ");
            System.out.println("0. Exit ");
            System.out.println("1.display All ");
            System.out.println("2.search word");
            System.out.println("3.add word ");
            System.out.println("4.delete word");
            System.out.println("5.update word");

            System.out.println("Enter your choice: ");
            choice=sc.nextInt();
            sc.nextLine();

            switch (choice) {
                case 0:
                    System.out.println("Exit!");
                    break;
                case 1:
                    displayAll();
                    break;
                case 2:
                    System.out.println("Enter word to search: ");
                    String S_word = sc.nextLine();
                    searchWord(S_word);
                    break;
                case 3:
                    addWord();
                    break;
                case 4:
                    deleteWord();
                    break;
                case 5:
                    updateWord();
                    break;
                default:
                    System.out.println("Invalid operation! ");
            }
        }while(choice!=0);
    }


    public void studentMenu(){
        int choice;

        do {
            System.out.println("Student Menu: ");
            System.out.println("0. Exit ");
            System.out.println("1.display All ");
            System.out.println("2.search word");


            System.out.println("Enter your choice: ");
            choice=sc.nextInt();
            sc.nextLine();

            switch (choice) {
                case 0:
                    System.out.println("Exit!");
                    break;
                case 1:
                    displayAll();
                    break;
                case 2:
                    System.out.println("Enter word to search: ");
                    String S_word = sc.nextLine();
                    searchWord(S_word);
                    break;
                default:
                    System.out.println("Invalid operation! ");
            }
        }while(choice!=0);

    }

    public boolean checkLogin(String username,String password,String identity){
        if (identity.equals("teacher")){
            return username.equals("teacher")&&password.equals("teacher123");
        }else if(identity.equals("student")){
            return username.equals("student")&&password.equals("student123");

        }
        return false;
    }




    public static void main(String[] args) {
        VocabularyManager m=new VocabularyManager();
        Scanner sc=new Scanner(System.in);

        System.out.println("*** Vocabulary Manager ***");
        System.out.println("Please Enter your identity: ");


        String identity=sc.nextLine();

        System.out.println("Username: ");
        String username=sc.nextLine();
        System.out.println("Password: ");
        String password=sc.nextLine();

        boolean result=m.checkLogin(username,password,identity);

        if(result){
            System.out.println("Login successfully! ");

            if(identity.equals("teacher")){
                m.teacherMenu();
            }else{
                m.studentMenu();
            }
        }else{
            System.out.println("Login failed! ");
        }

    }

}
相关推荐
唐青枫22 分钟前
别再把命令行参数写成一团:C#.NET System.CommandLine 实战详解
c#·.net
大白同学42139 分钟前
初识Qt——创建第一个Qt项目
开发语言·qt
祈禾40 分钟前
计算机组成原理之编译、汇编、链接、解释
开发语言·汇编·笔记·学习
hez201043 分钟前
让 .NET 11 的泛型虚方法变得更快!
c#·.net·.net core·clr·compiler
吴声子夜歌1 小时前
正则指引——Ruby
开发语言·正则表达式·ruby
nnerddboy1 小时前
Rust教程06:ESP32-rust环境搭建
开发语言·后端·rust
nnerddboy1 小时前
Rust教程03:函数,控制流与所有权
开发语言·后端·rust
linux修理工1 小时前
内存占用 99% 且 Java 程序较多的优化建议
java·开发语言
weixin_460443561 小时前
企业考试系统如何对接OA、钉钉和企业微信?SSO单点登录、组织同步与权限一致性设计
java·开发语言·数据库
nnerddboy2 小时前
Rust教程05:结构体,枚举与模式匹配
开发语言·网络·rust