import java.text.
SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
public class Vaccine {
private String name;
private String code;
private int quantity;
private Date expirationDate;
private double price;
private Date lastInjectedDate;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
// Constructor
public Vaccine(String name, String code, int quantity, Date expirationDate, double price, Date
lastInjectedDate) {
this.name = name;
this.code = code;
this.quantity = quantity;
this.expirationDate = expirationDate;
this.price = price;
this.lastInjectedDate = lastInjectedDate;
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public Date getExpirationDate() { return expirationDate; }
public void setExpirationDate(Date expirationDate) { this.expirationDate = expirationDate; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public Date getLastInjectedDate() { return lastInjectedDate; }
public void setLastInjectedDate(Date lastInjectedDate) { this.lastInjectedDate = lastInjectedDate; }
// toString method
@Override
public String toString() {
return "Vaccine{" +
"name='" + name + '\'' +
", code='" + code + '\'' +
", quantity=" + quantity +
", expirationDate=" + dateFormat.format(expirationDate) +
", price=" + price +
", lastInjectedDate=" + dateFormat.format(lastInjectedDate) +
'}';
// equals method
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vaccine vaccine = (Vaccine) o;
return code.equals(vaccine.code);
}
@Override
public int hashCode() {
return Objects.hash(code);
Step 3: Create the VaccineManager Class
1. Right-click on the Source Packages folder again.
2. Select New > Java Class.
3. Name the class VaccineManager and click Finish.
4. Copy and paste the following code into the VaccineManager class file:
java
Sao chép mã
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class VaccineManager {
private List<Vaccine> vaccines = new ArrayList<>();
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
// Load vaccines from file
public void loadVaccinesFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(", ");
Map<String, String> attributes = new HashMap<>();
for (String field : fields) {
String[] keyValue = field.split("=");
attributes.put(keyValue[0], keyValue[1]);
Vaccine vaccine = parseVaccine(attributes);
if (vaccine != null) {
vaccines.add(vaccine);
displayVaccines();
} catch (IOException | ParseException e) {
System.out.println("Error loading vaccines: " + e.getMessage());
private Vaccine parseVaccine(Map<String, String> attributes) throws ParseException {
try {
String code = attributes.get("code");
String name = attributes.get("name");
int quantity = Integer.parseInt(attributes.get("quantity"));
Date expirationDate = dateFormat.parse(attributes.get("expirationDate"));
double price = Double.parseDouble(attributes.get("price"));
Date lastInjectedDate = dateFormat.parse(attributes.get("lastInjectedDate"));
return new Vaccine(name, code, quantity, expirationDate, price, lastInjectedDate);
} catch (NumberFormatException | ParseException e) {
System.out.println("Error parsing vaccine: " + e.getMessage());
return null;
}
}
public void addVaccine(Vaccine vaccine) {
if (!vaccines.contains(vaccine)) {
vaccines.add(vaccine);
System.out.println("Vaccine added successfully.");
} else {
System.out.println("Vaccine with this code already exists.");
public void deleteVaccine(String code) {
vaccines.removeIf(v -> v.getCode().equals(code));
System.out.println("Vaccine deleted successfully.");
public void sortVaccinesByName() {
vaccines.sort(Comparator.comparing(Vaccine::getName));
System.out.println("Vaccines sorted by name.");
public void saveVaccinesToFile(String filename) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
for (Vaccine vaccine : vaccines) {
writer.write(vaccine.toString());
writer.newLine();
System.out.println("Vaccines saved to file successfully.");
} catch (IOException e) {
System.out.println("Error saving vaccines: " + e.getMessage());
public void displayVaccines() {
vaccines.forEach(System.out::println);
Step 4: Create the Main Class to Run the Application
1. Right-click on Source Packages > New > Java Class.
2. Name the class Main and click Finish.
3. Copy and paste the following code into the Main class file:
java
Sao chép mã
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
VaccineManager manager = new VaccineManager();
Scanner scanner = new Scanner(System.in);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
int choice;
do {
System.out.println("\nVaccine Management System");
System.out.println("1. Display All Vaccines From File");
System.out.println("2. Add a Vaccine");
System.out.println("3. Delete a Vaccine by code");
System.out.println("4. Sort Vaccines by Name");
System.out.println("5. Save to File");
System.out.println("6. Exit");
System.out.print("Enter selection: ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
manager.loadVaccinesFromFile("vaccines_input.txt");
break;
case 2:
System.out.print("Enter code: ");
String code = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter expiration date (dd/MM/yyyy): ");
String expDate = scanner.nextLine();
System.out.print("Enter price: ");
double price = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter last injected date (dd/MM/yyyy): ");
String lastInjected = scanner.nextLine();
manager.addVaccine(new Vaccine(name, code, quantity,
dateFormat.parse(expDate), price, dateFormat.parse(lastInjected)));
break;
case 3:
System.out.print("Enter code of vaccine to delete: ");
String deleteCode = scanner.nextLine();
manager.deleteVaccine(deleteCode);
break;
case 4:
manager.sortVaccinesByName();
break;
case 5:
manager.saveVaccinesToFile("vaccines_output.txt");
break;
case 6:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
} while (choice != 6);
scanner.close();
Step 5: Run the Program
1. In NetBeans, right-click on the Main class and select Run File.
2. Follow the menu options in the console to test each feature.
Make sure you have the file vaccines_input.txt with the sample data in the project directory to load the
vaccine data successfully.
4o