By GokiSoft.com| 13:53 26/03/2020|
Java Basic

Share Code - Hướng dẫn chữa bài quản lý rạp chiều phim - Ôn luyện interface

Hướng dẫn chữa bài tập

TEST- Chương trình quản lý rạp chiếu film bằng java


Source Code


/*
 * 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 giapchieuphim;

/**
 *
 * @author Diep.Tran
 */
public interface IStatus {
    void onStatus();
}


/*
 * 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 giapchieuphim;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Area implements IStatus{
    static enum STATUS {
        EMPTY, LESS, NORMAL, FULL
    }
    
    String currentTime;
    STATUS status;
    String name;

    public Area() {
        currentTime = "";
        status = STATUS.EMPTY;
    }

    public Area(String name) {
        this.name = name;
        currentTime = "";
        status = STATUS.EMPTY;
    }

    public Area(String currentTime, String name, STATUS status) {
        this.currentTime = currentTime;
        this.status = status;
        this.name = name;
    }

    public String getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(String currentTime) {
        this.currentTime = currentTime;
    }

    public STATUS getStatus() {
        return status;
    }

    public void setStatus(STATUS status) {
        autoCurrentTime();
        this.status = status;
    }
    
    public void autoCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("H:m:s dd:mm:yyyy");
        Date time = new Date();
        currentTime = format.format(time);
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("0. Empty");
        System.out.println("1. Less");
        System.out.println("2. Normal");
        System.out.println("3. Full");
        System.out.println("Choose: ");
        
        int option = input.nextInt();
        
        switch(option) {
            case 0:
                setStatus(STATUS.EMPTY);
                break;
            case 1:
                setStatus(STATUS.LESS);
                break;
            case 2:
                setStatus(STATUS.NORMAL);
                break;
            default:
                setStatus(STATUS.FULL);
                break;
        }
    }
    
    @Override
    public void onStatus() {
        switch(status) {
            case EMPTY:
                System.out.println(name + " dang trong");
                break;
            case LESS:
                System.out.println(name + " it khach");
                break;
            case NORMAL:
                System.out.println(name + " co khach vua du phuc vu");
                break;
            case FULL:
                System.out.println(name + " dong khach");
                break;
        }
    }
}



/*
 * 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 giapchieuphim;

/**
 *
 * @author Diep.Tran
 */
public class Food extends Area{
    public Food() {
        super("Quay ban do an");
    }
}



/*
 * 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 giapchieuphim;

/**
 *
 * @author Diep.Tran
 */
public class Parking extends Area{
    public Parking() {
        super("Bai do xe");
    }
}



/*
 * 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 giapchieuphim;

/**
 *
 * @author Diep.Tran
 */
public class TicketCounter extends Area{
    public TicketCounter() {
        super("Quay ban ve");
    }
}



/*
 * 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 giapchieuphim;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Room extends Area{
    //tong so ghe cua phong chieu
    int total;
    //so ghe dang ngoi
    int num;
    
    public Room() {
        super("Phong xem phim");
    }

    public Room(int total, int num) {
        super("Phong xem phim");
        this.total = total;
        this.num = num;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap tong so ghe: ");
        total = input.nextInt();
        
        System.out.println("Nhap so ghe da ngoi: ");
        num = input.nextInt();
        
        setupStatus();
    }
    
    public void setupStatus() {
        if(num == 0) {
            status = Area.STATUS.EMPTY;
        } else {
            int percent = (num * 100)/total;//se tu dong lam tron so nguyen
            //percent < 25% => LESS
            //percent >= 25% && < 70% => NORMAL
            //perent >= 70 % => FULL
            if(percent >= 70) {
                status = Area.STATUS.FULL;
            } else if(percent >= 25) {
                status = Area.STATUS.NORMAL;
            } else {
                status = Area.STATUS.LESS;
            }
        }
        autoCurrentTime();
    }
}



/*
 * 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 giapchieuphim;

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

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        List<IStatus> statusList = new ArrayList<>();
        
        System.out.println("Nhap 2 quay ban ve: ");
        for (int i = 0; i < 3; i++) {
            TicketCounter ticketCounter = new TicketCounter();
            ticketCounter.input();
            
            statusList.add(ticketCounter);
        }
        
        System.out.println("Nhap 2 quay ban do an: ");
        for (int i = 0; i < 2; i++) {
            Food food = new Food();
            food.input();
            
            statusList.add(food);
        }
        
        System.out.println("Nhap 2 bai do xe: ");
        for (int i = 0; i < 2; i++) {
            Parking parking = new Parking();
            parking.input();
            
            statusList.add(parking);
        }
        
        System.out.println("Nhap 5 phong chieu phim: ");
        for (int i = 0; i < 5; i++) {
            Room room = new Room();
            room.input();
            
            statusList.add(room);
        }
        
        //test dc roi.
        System.out.println("Hien thi thong tin trang thai: ");
        followStatus(statusList);
    }
    
    static void followStatus(List<IStatus> statusList) {
        for (IStatus iStatus : statusList) {
            iStatus.onStatus();
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)