用Java写一个电影网上购票系统的全部代码,要求具备文件读写功能,程序中的数据要以文件方式保存,程序中要求具备异常处理
时间: 2024-03-14 12:44:25 浏览: 75
基于Java的电影购票系统
好的,下面是一个可能的实现,注意这只是一个参考代码,你可以根据自己的需求进行修改和扩展:
MovieTicket.java
```java
import java.util.Date;
public class MovieTicket {
private String movieName; // 电影名称
private String cinemaName; // 影院名称
private Date dateTime; // 放映时间
private int seatNumber; // 座位号
private double price; // 价格
public MovieTicket(String movieName, String cinemaName, Date dateTime, int seatNumber, double price) {
this.movieName = movieName;
this.cinemaName = cinemaName;
this.dateTime = dateTime;
this.seatNumber = seatNumber;
this.price = price;
}
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getCinemaName() {
return cinemaName;
}
public void setCinemaName(String cinemaName) {
this.cinemaName = cinemaName;
}
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
public int getSeatNumber() {
return seatNumber;
}
public void setSeatNumber(int seatNumber) {
this.seatNumber = seatNumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "MovieTicket{" +
"movieName='" + movieName + '\'' +
", cinemaName='" + cinemaName + '\'' +
", dateTime=" + dateTime +
", seatNumber=" + seatNumber +
", price=" + price +
'}';
}
}
```
Cinema.java
```java
import java.util.ArrayList;
import java.util.List;
public class Cinema {
private String name; // 影院名称
private List<MovieTicket> tickets; // 影院售出的电影票
public Cinema(String name) {
this.name = name;
this.tickets = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MovieTicket> getTickets() {
return tickets;
}
public void setTickets(List<MovieTicket> tickets) {
this.tickets = tickets;
}
public void addTicket(MovieTicket ticket) {
tickets.add(ticket);
}
public void removeTicket(MovieTicket ticket) {
tickets.remove(ticket);
}
@Override
public String toString() {
return "Cinema{" +
"name='" + name + '\'' +
", tickets=" + tickets +
'}';
}
}
```
MovieTicketSystem.java
```java
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MovieTicketSystem {
private List<Cinema> cinemas; // 所有影院
public MovieTicketSystem() {
this.cinemas = new ArrayList<>();
}
public void addCinema(Cinema cinema) {
cinemas.add(cinema);
}
public void removeCinema(Cinema cinema) {
cinemas.remove(cinema);
}
public void sellTicket(MovieTicket ticket, Cinema cinema) throws Exception {
// 判断票是否已经售出
if (cinema.getTickets().contains(ticket)) {
throw new Exception("该电影票已经售出!");
}
// 判断座位是否已经被占用
for (MovieTicket t : cinema.getTickets()) {
if (t.getDateTime().equals(ticket.getDateTime())
&& t.getSeatNumber() == ticket.getSeatNumber()) {
throw new Exception("该座位已经被占用!");
}
}
cinema.addTicket(ticket);
}
public List<MovieTicket> getTickets(Cinema cinema) {
return cinema.getTickets();
}
public List<MovieTicket> getTicketsByMovie(String movieName) {
List<MovieTicket> result = new ArrayList<>();
for (Cinema cinema : cinemas) {
for (MovieTicket ticket : cinema.getTickets()) {
if (ticket.getMovieName().equals(movieName)) {
result.add(ticket);
}
}
}
return result;
}
public List<MovieTicket> getTicketsByCinema(String cinemaName) {
for (Cinema cinema : cinemas) {
if (cinema.getName().equals(cinemaName)) {
return cinema.getTickets();
}
}
return null;
}
public void saveToFile(String fileName) throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
for (Cinema cinema : cinemas) {
for (MovieTicket ticket : cinema.getTickets()) {
writer.println(String.format("%s,%s,%s,%d,%.2f",
ticket.getMovieName(),
cinema.getName(),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ticket.getDateTime()),
ticket.getSeatNumber(),
ticket.getPrice()));
}
}
}
}
public void loadFromFile(String fileName) throws IOException, ParseException {
cinemas.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
String movieName = fields[0];
String cinemaName = fields[1];
Date dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fields[2]);
int seatNumber = Integer.parseInt(fields[3]);
double price = Double.parseDouble(fields[4]);
MovieTicket ticket = new MovieTicket(movieName, cinemaName, dateTime, seatNumber, price);
Cinema cinema = null;
for (Cinema c : cinemas) {
if (c.getName().equals(cinemaName)) {
cinema = c;
break;
}
}
if (cinema == null) {
cinema = new Cinema(cinemaName);
cinemas.add(cinema);
}
cinema.addTicket(ticket);
}
}
}
@Override
public String toString() {
return "MovieTicketSystem{" +
"cinemas=" + cinemas +
'}';
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) throws Exception {
MovieTicketSystem system = new MovieTicketSystem();
// 添加影院
Cinema cinema1 = new Cinema("万达影城");
Cinema cinema2 = new Cinema("星美国际影城");
system.addCinema(cinema1);
system.addCinema(cinema2);
// 售出电影票
MovieTicket ticket1 = new MovieTicket("复仇者联盟4", "万达影城", new Date(), 10, 50.0);
MovieTicket ticket2 = new MovieTicket("流浪地球", "星美国际影城", new Date(), 20, 40.0);
system.sellTicket(ticket1, cinema1);
system.sellTicket(ticket2, cinema2);
// 获取电影票
List<MovieTicket> tickets1 = system.getTickets(cinema1);
List<MovieTicket> tickets2 = system.getTicketsByMovie("流浪地球");
List<MovieTicket> tickets3 = system.getTicketsByCinema("万达影城");
// 保存和加载数据
system.saveToFile("data.txt");
system.loadFromFile("data.txt");
System.out.println(system);
}
}
```
希望这个实现能够对你有所帮助!
阅读全文