Java开发是一项非常广泛的技能,对于一个Java开发者来说,拥有多个实际项目经验是非常重要的。在简历中列出这些项目可以帮助雇主更好地了解你的技能和经验。本文将为大家介绍如何写几个Java开发项目,并附上代码示例。

项目一:学生信息管理系统

学生信息管理系统是一个常见的项目,用于存储和管理学生的基本信息。下面是一个简单的示例代码,用于添加和显示学生信息。

public class Student {
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // 省略getter和setter方法
    
    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }
}

public class StudentManagementSystem {
    private List<Student> students;
    
    public StudentManagementSystem() {
        students = new ArrayList<>();
    }
    
    public void addStudent(Student student) {
        students.add(student);
    }
    
    public void displayStudents() {
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        
        Student student1 = new Student("John", 18);
        sms.addStudent(student1);
        
        Student student2 = new Student("Jane", 20);
        sms.addStudent(student2);
        
        sms.displayStudents();
    }
}

通过运行以上代码,我们可以在控制台上看到输出结果:

Name: John, Age: 18
Name: Jane, Age: 20

项目二:图书管理系统

图书管理系统是另一个常见的项目,用于存储和管理图书的信息。下面是一个简单的示例代码,用于添加和查询图书信息。

public class Book {
    private String title;
    private String author;
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
    
    // 省略getter和setter方法
    
    @Override
    public String toString() {
        return "Title: " + title + ", Author: " + author;
    }
}

public class BookManagementSystem {
    private List<Book> books;
    
    public BookManagementSystem() {
        books = new ArrayList<>();
    }
    
    public void addBook(Book book) {
        books.add(book);
    }
    
    public Book findBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                return book;
            }
        }
        return null;
    }
}

public class Main {
    public static void main(String[] args) {
        BookManagementSystem bms = new BookManagementSystem();
        
        Book book1 = new Book("Java入门指南", "张三");
        bms.addBook(book1);
        
        Book book2 = new Book("Python编程基础", "李四");
        bms.addBook(book2);
        
        Book book = bms.findBook("Java入门指南");
        if (book != null) {
            System.out.println(book);
        } else {
            System.out.println("Book not found");
        }
    }
}

通过运行以上代码,我们可以在控制台上看到输出结果:

Title: Java入门指南, Author: 张三

项目三:在线商城

在线商城是一个复杂的项目,用于展示和销售商品。下面是一个简单的示例代码,用于添加和显示商品信息。

public class Product {
    private String name;
    private double price;
    
    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }
    
    // 省略getter和setter方法
    
    @Override
    public String toString() {
        return "Name: " + name + ", Price: $" + price;
    }
}

public class ShoppingCart {
    private List<Product> products;
    
    public ShoppingCart() {
        products = new ArrayList<>();
    }
    
    public void addProduct(Product product) {
        products.add(product);
    }
    
    public void displayProducts() {
        for (Product product : products) {
            System.out.println(product);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        
        Product product1 = new Product("iPhone 12", 999.99);
        cart.addProduct(product1);
        
        Product product2 = new Product("AirPods Pro", 249.99);