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! ");
        }

    }

}
相关推荐
冷雨夜中漫步8 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
暖馒10 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
m0_7369191010 小时前
C++代码风格检查工具
开发语言·c++·算法
2501_9449347310 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
黎雁·泠崖11 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_7634724612 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ12 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
lly20240613 小时前
C++ 文件和流
开发语言
m0_7066532313 小时前
分布式系统安全通信
开发语言·c++·算法
寻寻觅觅☆13 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++