以下是10个Java毕业设计项目的详细描述和核心代码示例。每个项目都基于实际应用场景,代码使用Java编写,简洁明了地展示核心功能。项目设计考虑了可扩展性和实用性,适合作为毕业设计基础
1. 学生信息管理系统
描述:这个系统允许管理员管理学生信息,包括添加、删除、修改和查询学生记录。支持数据持久化存储到文件或数据库。
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "学号: " + id + ", 姓名: " + name + ", 年龄: " + age;
}
}
public class StudentManagement {
private static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("1.添加学生 2.查看所有学生 3.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("输入学号: ");
String id = scanner.next();
System.out.print("输入姓名: ");
String name = scanner.next();
System.out.print("输入年龄: ");
int age = scanner.nextInt();
students.add(new Student(id, name, age));
System.out.println("添加成功!");
break;
case 2:
for (Student s : students) {
System.out.println(s);
}
break;
default:
System.exit(0);
}
}
}
2. 在线书店系统
描述:用户可以在线浏览、搜索和购买书籍。系统包括库存管理、订单处理和用户认证功能。
import java.util.HashMap;
import java.util.Map;
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() { return title; }
public double getPrice() { return price; }
}
public class BookStore {
private static Map<String, Book> inventory = new HashMap<>();
public static void main(String[] args) {
inventory.put("B001", new Book("Java编程思想", 99.9));
inventory.put("B002", new Book("算法导论", 120.5));
System.out.println("可用书籍:");
for (Map.Entry<String, Book> entry : inventory.entrySet()) {
Book book = entry.getValue();
System.out.println("ID: " + entry.getKey() + ", 书名: " + book.getTitle() + ", 价格: $" + book.getPrice());
}
}
}
3. 即时聊天应用
描述:实现一个简单的客户端-服务器聊天程序,支持多用户在线实时消息发送和接收。
// 服务器端代码
import java.net.*;
import java.io.*;
public class ChatServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("服务器启动...");
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("客户端: " + inputLine);
out.println("服务器回复: " + inputLine);
}
}
}
// 客户端代码
import java.net.*;
import java.io.*;
public class ChatClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("服务器: " + in.readLine());
}
}
}
4. 库存管理系统
描述:用于企业跟踪产品库存,支持入库、出库、库存查询和报表生成。
import java.util.HashMap;
class Product {
private String name;
private int quantity;
public Product(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public void addStock(int amount) { quantity += amount; }
public void removeStock(int amount) { quantity -= amount; }
public int getQuantity() { return quantity; }
public String getName() { return name; }
}
public class InventorySystem {
private static HashMap<String, Product> products = new HashMap<>();
public static void main(String[] args) {
products.put("P100", new Product("笔记本电脑", 50));
products.get("P100").addStock(10);
System.out.println("当前库存: " + products.get("P100").getName() + " - 数量: " + products.get("P100").getQuantity());
}
}
5. ATM模拟系统
描述:模拟银行ATM机功能,包括账户登录、余额查询、存款、取款和转账。
class Account {
private String accountId;
private double balance;
public Account(String accountId, double balance) {
this.accountId = accountId;
this.balance = balance;
}
public void deposit(double amount) { balance += amount; }
public void withdraw(double amount) { balance -= amount; }
public double getBalance() { return balance; }
}
public class ATMSystem {
private static Account account = new Account("A12345", 1000.0);
public static void main(String[] args) {
System.out.println("欢迎使用ATM");
account.deposit(500.0);
System.out.println("存款后余额: $" + account.getBalance());
account.withdraw(200.0);
System.out.println("取款后余额: $" + account.getBalance());
}
}
6. 酒店预订系统
描述:用户可查询酒店房间、预订房间和取消预订。系统包括房间状态管理和预订记录。
import java.util.ArrayList;
class Room {
private int number;
private boolean isBooked;
public Room(int number) {
this.number = number;
this.isBooked = false;
}
public void book() { isBooked = true; }
public void cancel() { isBooked = false; }
public boolean isAvailable() { return !isBooked; }
public int getNumber() { return number; }
}
public class HotelBooking {
private static ArrayList<Room> rooms = new ArrayList<>();
public static void main(String[] args) {
rooms.add(new Room(101));
rooms.add(new Room(102));
rooms.get(0).book();
System.out.println("房间101状态: " + (rooms.get(0).isAvailable() ? "可用" : "已预订"));
}
}
7. 图书馆管理系统
描述:管理图书借阅、归还和库存。包括用户管理、图书搜索和逾期计算。
import java.util.Date;
class BookItem {
private String isbn;
private String title;
private boolean isBorrowed;
private Date dueDate;
public BookItem(String isbn, String title) {
this.isbn = isbn;
this.title = title;
this.isBorrowed = false;
}
public void borrow() {
isBorrowed = true;
dueDate = new Date(System.currentTimeMillis() + 14 * 24 * 60 * 60 * 1000); // 14天后到期
}
public void returnBook() { isBorrowed = false; }
public boolean isAvailable() { return !isBorrowed; }
}
public class LibrarySystem {
public static void main(String[] args) {
BookItem book = new BookItem("978-7-121-12345", "Java核心技术");
book.borrow();
System.out.println("图书状态: " + (book.isAvailable() ? "在库" : "已借出"));
}
}
8. 文件加密解密工具
描述:使用简单算法(如AES或自定义)实现文件的加密和解密,支持命令行操作。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class FileEncryptor {
public static void main(String[] args) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String plainText = "机密数据";
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
System.out.println("加密后: " + Base64.getEncoder().encodeToString(encryptedBytes));
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
System.out.println("解密后: " + new String(decryptedBytes));
}
}
9. 在线投票系统
描述:用户可创建投票主题、投票和查看结果。系统确保每个用户只投一次票。
import java.util.HashMap;
import java.util.HashSet;
class Poll {
private String question;
private HashMap<String, Integer> options;
private HashSet<String> voters;
public Poll(String question) {
this.question = question;
this.options = new HashMap<>();
this.voters = new HashSet<>();
}
public void addOption(String option) { options.put(option, 0); }
public void vote(String voterId, String option) {
if (!voters.contains(voterId)) {
voters.add(voterId);
options.put(option, options.getOrDefault(option, 0) + 1);
}
}
public void displayResults() {
System.out.println("投票结果: " + question);
for (String opt : options.keySet()) {
System.out.println(opt + ": " + options.get(opt) + "票");
}
}
}
public class VotingSystem {
public static void main(String[] args) {
Poll poll = new Poll("你最喜欢的编程语言?");
poll.addOption("Java");
poll.addOption("Python");
poll.vote("user1", "Java");
poll.vote("user2", "Python");
poll.displayResults();
}
}
10. 贪吃蛇游戏
描述:基于Swing的简单贪吃蛇游戏,包括蛇的移动、食物生成和碰撞检测。
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
public class SnakeGame extends JFrame implements KeyListener {
private LinkedList<Point> snake;
private Point food;
private int direction; // 0:上, 1:右, 2:下, 3:左
public SnakeGame() {
snake = new LinkedList<>();
snake.add(new Point(5, 5));
food = new Point(10, 10);
direction = 1;
addKeyListener(this);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Point p : snake) {
g.fillRect(p.x * 20, p.y * 20, 20, 20);
}
g.setColor(Color.RED);
g.fillRect(food.x * 20, food.y * 20, 20, 20);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && direction != 2) direction = 0;
else if (key == KeyEvent.VK_RIGHT && direction != 3) direction = 1;
else if (key == KeyEvent.VK_DOWN && direction != 0) direction = 2;
else if (key == KeyEvent.VK_LEFT && direction != 1) direction = 3;
}
// 省略其他KeyListener方法和游戏循环逻辑
public static void main(String[] args) {
new SnakeGame();
}
}
这些项目覆盖了常见领域,如数据库应用、网络编程、游戏开发等。每个项目都提供了核心功能代码,您可以根据需求扩展为完整系统。建议使用IDE如IntelliJ IDEA或Eclipse进行开发,并结合MySQL或文件存储实现数据持久化。