用Java写一个电影网上购票系统的全部代码,将相关信息写入txt文件
时间: 2024-03-14 19:45:20 浏览: 72
好的,我会给你提供一个简单的Java电影网上购票系统的代码示例,包含了相关信息写入txt文件的功能。请注意,这个代码示例并不完整,仅供参考,你可以根据自己的需求进行修改和完善。
```
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MovieTicketSystem {
private static final String MOVIE_FILE_NAME = "movies.txt";
private static final String CINEMA_FILE_NAME = "cinemas.txt";
private static final String TICKET_FILE_NAME = "tickets.txt";
private List<Movie> movies;
private List<Cinema> cinemas;
public MovieTicketSystem() {
this.movies = new ArrayList<>();
this.cinemas = new ArrayList<>();
loadMovies();
loadCinemas();
}
private void loadMovies() {
try {
File file = new File(MOVIE_FILE_NAME);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0];
String releaseDate = parts[1];
double price = Double.parseDouble(parts[2]);
Movie movie = new Movie(name, releaseDate, price);
movies.add(movie);
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadCinemas() {
try {
File file = new File(CINEMA_FILE_NAME);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0];
String location = parts[1];
List<Movie> movieList = new ArrayList<>();
for (int i = 2; i < parts.length; i++) {
String movieName = parts[i];
Movie movie = findMovieByName(movieName);
if (movie != null) {
movieList.add(movie);
}
}
Cinema cinema = new Cinema(name, location, movieList);
cinemas.add(cinema);
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private Movie findMovieByName(String name) {
for (Movie movie : movies) {
if (movie.getName().equals(name)) {
return movie;
}
}
return null;
}
public void showMovies() {
for (Movie movie : movies) {
System.out.println(movie.getName() + " - " + movie.getReleaseDate() + " - " + movie.getPrice());
}
}
public void selectMovie(String movieName, String cinemaName) {
Cinema cinema = findCinemaByName(cinemaName);
if (cinema != null) {
Movie movie = findMovieByName(movieName);
if (movie != null && cinema.getMovieList().contains(movie)) {
System.out.println("You selected " + movie.getName() + " at " + cinema.getName());
selectSeat(movie, cinema);
} else {
System.out.println("Invalid movie or cinema");
}
} else {
System.out.println("Invalid cinema");
}
}
private void selectSeat(Movie movie, Cinema cinema) {
// TODO: select seat
}
public void confirmPurchase(String movieName, String cinemaName, List<String> seats, String customerName) {
Cinema cinema = findCinemaByName(cinemaName);
if (cinema != null) {
Movie movie = findMovieByName(movieName);
if (movie != null && cinema.getMovieList().contains(movie)) {
double price = seats.size() * movie.getPrice();
String ticketInfo = customerName + "," + movie.getName() + "," + cinema.getName() + "," + seats.toString() + "," + price;
saveTicket(ticketInfo);
System.out.println("Purchase confirmed. Your ticket info: " + ticketInfo);
} else {
System.out.println("Invalid movie or cinema");
}
} else {
System.out.println("Invalid cinema");
}
}
private Cinema findCinemaByName(String name) {
for (Cinema cinema : cinemas) {
if (cinema.getName().equals(name)) {
return cinema;
}
}
return null;
}
private void saveTicket(String ticketInfo) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(TICKET_FILE_NAME, true));
writer.write(ticketInfo);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Movie {
private String name;
private String releaseDate;
private double price;
public Movie(String name, String releaseDate, double price) {
this.name = name;
this.releaseDate = releaseDate;
this.price = price;
}
public String getName() {
return name;
}
public String getReleaseDate() {
return releaseDate;
}
public double getPrice() {
return price;
}
}
class Cinema {
private String name;
private String location;
private List<Movie> movieList;
public Cinema(String name, String location, List<Movie> movieList) {
this.name = name;
this.location = location;
this.movieList = movieList;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public List<Movie> getMovieList() {
return movieList;
}
}
```
这个代码示例中,我们定义了MovieTicketSystem购票系统类,Movie电影类和Cinema电影院类。在MovieTicketSystem类中,我们实现了从txt文件中读取电影和电影院信息的方法,以及展示当前正在放映的电影列表、选择电影、选择座位、确认购买并生成购票信息等方法。在选择座位和确认购买方法中,我们将购票信息写入了另一个txt文件中。
请注意,为了简化代码,这个示例中并没有实现具体的座位选择功能,你需要根据自己的需求进行修改。此外,为了简化示例,这个代码中没有进行输入验证和错误处理,请在实际应用中进行相关处理。
阅读全文