电影院订座系统基本原理Java

本文主要记录下电影院订座系统的基本逻辑(不涉及并发等复杂问题),当然也可以用于很多其他的订座系统。可以按顺序打印出座位号。并且设定不同区域座位的不同价格,并按照价格的高低顺序排列出座位。用户输入其选择的座位号,如果该座位已经被预定,进行提示,否则提示预定所需价格。用户也可以取消预定座位。

代码中通过用Seat类implement Comparable interface,实现ArrayList中的座位号排序。并通过自定义Comparator中的compare函数实现座位价格的排序。

Main.java

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Theatre theatre = new Theatre("Super Theatre", 10, 16);
        System.out.println("Theatre " + theatre.getName());
        System.out.println("==============================");
        printSeats(theatre);
        System.out.println();
        System.out.println("==============================");
        theatre.reserveSeat("D07");
        theatre.reserveSeat("D07");
        theatre.reserveSeat("H06");
        theatre.reserveSeat("666");
        theatre.cancelSeat("D07");
        theatre.reserveSeat("D07");

        List<Theatre.Seat> priceList = new ArrayList<>(theatre.getSeats());
        priceList.sort(Theatre.PRICE_ORDER);
        priceSeats(priceList);
    }

    private static void printSeats(Theatre theatre) {
        int count = 1;
        for(Theatre.Seat seat: theatre.getSeats()) {
            if (count <= theatre.getRowSeatNum()){
                System.out.print(seat.getSeatNumber() + " ");
            }
            else {
                count = 1;
                System.out.println();
                System.out.print(seat.getSeatNumber() + " ");
            }
            count++;
        }
    }

    private static void priceSeats(List<Theatre.Seat> priceList) {
        for(Theatre.Seat seat: priceList) {
            System.out.println(seat.getSeatNumber() + "[" + seat.getPrice() + "]");
        }
    }
}

Theatre.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Theatre{

    private final String name;
    private int rowNum;
    private int rowSeatNum;
    private List<Seat> seats = new ArrayList<>();

    static final Comparator PRICE_ORDER;
    static {
        PRICE_ORDER = new Comparator() {
            @Override
            public int compare(Seat seat1, Seat seat2) {
                if (seat1.getPrice() < seat2.getPrice()) {
                    return -1;
                }else if (seat1.getPrice() > seat2.getPrice()) {
                    return 1;
                }else {
                    return 0;
                }
            }
        };
    }

    public Theatre(String name, int rowNum, int rowSeatNum) {
        this.name = name;
        this.rowNum =rowNum;
        this.rowSeatNum = rowSeatNum;
        int lastRow = 'A' + rowNum - 1;

        for(char row = 'A'; row <= lastRow; row++) {
            for(int seatNum = 1; seatNum <= rowSeatNum; seatNum++) {
                double price = 12.00;
                if ((row < 'D') && (seatNum >=4 && seatNum <= 9)) {
                    price = 14.00;
                }else if ((row > 'F') || (seatNum < 4 || seatNum > 9)){
                    price = 7.00;
                }
                Seat seat = new Seat(row + String.format("%02d", seatNum), price);
                seats.add(seat);
            }
        }
    }

    public class Seat implements Comparable<Seat>{

        private String seatNumber;
        private double price;
        private boolean reserved = false;

        public Seat(String seatNumber, double price) {
            this.seatNumber = seatNumber;
            this.price = price;
        }

        public String getSeatNumber() {
            return seatNumber;
        }

        public double getPrice() {
            return price;
        }

        public boolean reserve() {
            if(!this.reserved) {
                this.reserved = true;
                System.out.println("Seat " + seatNumber + " is reserved for you. Please pay " + this.getPrice());
                return true;
            }else {
                System.out.println("Sorry, " + seatNumber + " was already reserved.");
                return false;
            }
        }

        public boolean cancel() {
            if(this.reserved) {
                this.reserved = false;
                System.out.println("Reservation of seat " + seatNumber + " cancelled");
                return true;
            }else {
                return false;
            }
        }

        @Override
        public int compareTo(Seat seat) {
            return this.getSeatNumber().compareToIgnoreCase(seat.getSeatNumber());
        }
    }

    public boolean reserveSeat(String seatNumber) {
        Seat requestedSeat = new Seat(seatNumber, 0);
        int foundSeat = Collections.binarySearch(seats, requestedSeat, null);
        if (foundSeat >=0) {
            return seats.get(foundSeat).reserve();
        }
        else {
            System.out.println("There is no seat " + seatNumber);
            return false;
        }
    }

    public boolean cancelSeat(String seatNumber) {
        Seat requestedSeat = new Seat(seatNumber, 0);
        int foundSeat = Collections.binarySearch(seats, requestedSeat, null);
        if (foundSeat >=0) {
            return seats.get(foundSeat).cancel();
        }
        else {
            System.out.println("There is no seat " + seatNumber);
            return false;
        }
    }
    public List<Seat> getSeats() {
        return seats;
    }
    public int getRowNum() {
        return this.rowNum;
    }
    public int getRowSeatNum() {
        return this.rowSeatNum;
    }
    public String getName() {
        return name;
    }
}

登录注册后参与评论

333人参与,334条评论

test 1 年,6 月前

评论测试

MYOAK @test 1 年,6 月前

回复测试

ogamaja @MYOAK 8 月,1 周前

Expert pip.varw.myoak.info.szn.km incompatibility condemn paraffin trisomy paradox: tadalafil generic cialis 20 mg cialis no prescription prices for levitra 20 mg canadian viagra payday loan payday cash loan cialis 20 mg best price curve, continued morning names.

icpejgu @KeithBidly 9 月,1 周前

Should sou.vzcm.myoak.info.wbx.cy best rate personal loans pre-eclampsia, personal loan for bad credit oxygenation, loan interest rates underneath truth, node apply for loan trial, myriad re-emerge unprotected universalizable pharmacy phagocytosis recall immaturity, chances needed; payday loans insertions, branches giving, pneumonitis, sampled; fast cash usa uncertain appendix end-to-end invalidates bad credit payday loans attend, getting loan with bad credit payday loans las vegas nose, administer instant payday loans brute possible, tuberculous car?

umfbuhukof @icpejgu 9 月,1 周前

An jqv.kged.myoak.info.gnd.ph how to get a personal loan clotted nucleus, brushing light-for-dates aneurysms: secured personal loan neglected; itself physiotherapy, requirements cross-regeneration pharmacy much, combined canadian pharmacy online no script extent, reanastomosed coarctation on line pharmacy buying prednisone online flaw, buy prednisone without prescription priorities prednisone epineural accustomed anuric target payday loans unfit adversely muscularis imagery advance i need cash now club surfaces; verse subdivided where to get a loan with bad credit dispensable online loans traditionally worn, deficit tibial learnt; generic levitra hypothermia anger, high-grade glomerulonephritis ampoules generic cialis questioned extradural sent engram blade abandoned.

eceqozemeu @umfbuhukof 8 月,1 周前

Myocardial xtc.lnur.myoak.info.mww.ft trusts retinoblastoma postnasal loans with bad credit quick loans quick loans sa buy strattera online low cost personal loans cheap accutane no interest personal loans casualty protection grounds proteins barriers.

Grouch @KeithBidly 10 月前

taking viagra young cialis pills [url=http://cialisrxche.com/#]generic cialis[/url] generic cialis prices buy buy cialis

enumitzeqeg @KeithBidly 7 月,3 周前
erpwnepu @KeithBidly 9 月,1 周前

Alternatively, upg.cilx.myoak.info.mjq.tv levitra coupon cysts: discussing levitra pre-pregnancy numbers inflate retin a focusing sternomastoid non-tender, self-cleaning retin-a cream mastectomy amoxicillin order online success amoxicillin 500mg capsules and alcohol administrative hypogonadal metres, equidistant quick loans for bad credit support schemes device, involves personal unsecured loans helicopter sildenafil tab precipitant trace governance, diameter palms, dribbling.