By GokiSoft.com| 15:20 14/07/2023|
Java Advanced

Java Basic - Phần mềm AI LÀ TRIỆU PHÚ BT1376

Java Basic - Phần mềm AI LÀ TRIỆU PHÚ

Bạn là lập trình viên được thuê thiết kế phần mềm ai là triệu phú với các chức năng sau.

1. Nhập dữ liệu

2. Tham gia thi

3. Thoát chương trình

Khi người dùng lựa chọn 1 thì thực hiện chức năng nhập liệu các câu hỏi. Mỗi câu hỏi có cấu trúc sau

- Nhập tiêu đề câu hỏi

- Option 1 .. N (Sau mỗi lần nhập option hỏi người dùng có nhập tiếp ko => nếu ko thì kết thúc nhập option)

- Câu trả lời chính xác

=> Sau mỗi lần nhập câu hỏi thì add dữ liệu vào file (.txt hoặc nhị phân)

Khi người dùng lựa chọn 2: Thực hiện lấy ngẫu nhiên 15 câu hỏi trong kho dữ liệu (nếu kho dữ liệu nhỏ 15 câu hỏi => Thì lấy hết dữ liệu ra)

Sau đó hiển thị lần lượt các câu hỏi cho người dùng chơi.

Kết thúc in kết quả của người chơi. 

Hãy phát triển phần mềm & nhập liệu => Sau đó mời người bạn của bạn thử sức cuộc thi này.

Liên kết rút gọn:

https://gokisoft.com/1376

Bình luận

avatar
trung [C1907L]
2020-05-13 13:17:08



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AiLaTrieuPhu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author student
 */
public class Question {
    String title;
    List<String> options;
    Integer answer;

    public Question() {
        options = new ArrayList<>();
    }
    
    public void input() {
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao tieu de cau hoi");
        title = inp.nextLine();
        do {
            System.out.println("Nhap vao option");
            options.add(inp.nextLine());
            System.out.println("Continue (y/n) ?");
        } while (inp.nextLine().compareToIgnoreCase("y")==0);
        System.out.println("Nhap vao cau tra loi");
        answer = Integer.parseInt(inp.nextLine());
    }
    
    public boolean checkAnswer() {
        Scanner inp = new Scanner(System.in);
        System.out.println(title);
        int i = 1;
        options.stream().forEach((option) -> {
            System.out.println(i+". "+option);
        });
        System.out.println("Your answer: ?");
        return (Integer.parseInt(inp.nextLine()) == answer);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getOptions() {
        return options;
    }

    public void setOptions(List<String> options) {
        this.options = options;
    }

    public int getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }
    
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AiLaTrieuPhu;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class DataMgr {
    private static DataMgr instance;
    List<Question> lstQuestion;

    public DataMgr() {
        lstQuestion = new ArrayList<>();
    }
    
    public static synchronized DataMgr getInstance() {
        if (instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }
    
    public static void readFromFile() {
        getInstance().lstQuestion = new ArrayList<>();
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("data.dat");
            ois = new ObjectInputStream(fis);
            Object obj = null;
            while((obj = ois.readObject()) != null) {
                Question ques = (Question) obj;
                getInstance().lstQuestion.add(ques);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    public static void input(){
        Scanner inp = new Scanner(System.in);
        do {
            Question newQ = new Question();
            newQ.input();
            getInstance().lstQuestion.add(newQ);
            System.out.println("Continue (y/n) ?");
        } while (inp.nextLine().compareToIgnoreCase("y")==0);
    }
    
    public static void writeToFile() {
        String fileName = "data.dat";
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("data.dat");
            oos = new ObjectOutputStream(fos);
            for (Question ques : getInstance().lstQuestion) {
                oos.writeObject(ques);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AiLaTrieuPhu;

import java.util.Scanner;

/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
        int option;
        int score;
        do {
            Scanner inp = new Scanner(System.in);
            showMenu();
            option = Integer.parseInt(inp.nextLine());
            switch(option) {
                case 1:
                    DataMgr.input();
                    DataMgr.writeToFile();
                    break;
                case 2:
                    score = 0;
                    DataMgr.readFromFile();
                    if (DataMgr.getInstance().lstQuestion.size() < 16) {
                        for (Question ques : DataMgr.getInstance().lstQuestion) {
                            if (ques.checkAnswer()) score++;
                        }
                    }else {
                        for (int i = 0 ; i < 15; i++) {
                            
                        }
                    }
                    break;
                case 3:
                    System.out.println("Bye");
                    break;
                default:
                    System.out.println("Invalid input");
            }
        } while (option != 3);
    }
    
    public static void showMenu() {
        System.out.println("1. Nhập dữ liệu");
        System.out.println("2. Tham gia thi");
        System.out.println("3. Thoát chương trình");
    }
}