Java电影购票系统
时间: 2023-12-21 18:05:00 浏览: 43
以下是Java电影购票系统的设计和实现:
1. 首先,我们需要定义一个Movie类,该类包含电影的名称,导演,演员,类型和时长等属性。
```java
public class Movie {
private String name;
private String director;
private String[] actors;
private String type;
private int duration;
public Movie(String name, String director, String[] actors, String type, int duration) {
this.name = name;
this.director = director;
this.actors = actors;
this.type = type;
this.duration = duration;
}
// getter and setter methods
}
```
2. 接下来,我们需要定义一个Cinema类,该类包含电影院的名称,地址和座位等属性,以及一个Movie类型的数组,表示该电影院正在上映的电影。
```java
public class Cinema {
private String name;
private String address;
private int seats;
private Movie[] movies;
public Cinema(String name, String address, int seats, Movie[] movies) {
this.name = name;
this.address = address;
this.seats = seats;
this.movies = movies;
}
// getter and setter methods
}
```
3. 然后,我们需要定义一个Ticket类,该类包含电影票的价格,座位号和电影等属性。
```java
public class Ticket {
private double price;
private int seatNumber;
private Movie movie;
public Ticket(double price, int seatNumber, Movie movie) {
this.price = price;
this.seatNumber = seatNumber;
this.movie = movie;
}
// getter and setter methods
}
```
4. 最后,我们需要定义一个TicketBookingSystem类,该类包含一个Cinema类型的数组,表示所有的电影院,以及一个Ticket类型的数组,表示所有已售出的电影票。
```java
public class TicketBookingSystem {
private Cinema[] cinemas;
private Ticket[] tickets;
public TicketBookingSystem(Cinema[] cinemas) {
this.cinemas = cinemas;
this.tickets = new Ticket[0];
}
// method to book a ticket
public void bookTicket(int cinemaIndex, int movieIndex, int seatNumber) {
Cinema cinema = cinemas[cinemaIndex];
Movie movie = cinema.getMovies()[movieIndex];
double price = calculatePrice(movie.getDuration());
Ticket ticket = new Ticket(price, seatNumber, movie);
tickets = Arrays.copyOf(tickets, tickets.length + 1);
tickets[tickets.length - 1] = ticket;
}
// method to calculate ticket price based on movie duration
private double calculatePrice(int duration) {
if (duration <= 60) {
return 8.0;
} else if (duration <= 120) {
return 12.0;
} else {
return 15.0;
}
}
// getter and setter methods
}
```
以上就是Java电影购票系统的设计和实现。用户可以通过调用TicketBookingSystem类的bookTicket方法来购买电影票。系统会根据电影的时长自动计算票价,并将售出的电影票保存在tickets数组中。