用Java代码实现下面的需求:Create an application called DBTest.java that emulates a small database program that stores and retrieves records relating to products. Use the RandomAccessFile class and a flat file. Records in the database should consist of string names and integer quantities. Your program should allow the user to display a record, update a record, and add a new record.
时间: 2023-07-14 09:12:34 浏览: 107
以下是实现上述需求的Java代码,注释中有详细说明。
```java
import java.io.*;
public class DBTest {
// The size of each record in bytes
private static final int RECORD_SIZE = 64;
public static void main(String[] args) {
try {
// Create a RandomAccessFile object with the name "products.db"
RandomAccessFile file = new RandomAccessFile("products.db", "rw");
// Display a menu to the user
System.out.println("1. Display a record");
System.out.println("2. Update a record");
System.out.println("3. Add a new record");
System.out.print("Enter your choice: ");
// Read the user's choice
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice = Integer.parseInt(br.readLine());
switch (choice) {
case 1:
// Display a record
System.out.print("Enter the record number to display: ");
int recordNumber = Integer.parseInt(br.readLine());
displayRecord(file, recordNumber);
break;
case 2:
// Update a record
System.out.print("Enter the record number to update: ");
recordNumber = Integer.parseInt(br.readLine());
updateRecord(file, recordNumber);
break;
case 3:
// Add a new record
addRecord(file);
break;
default:
System.out.println("Invalid choice");
break;
}
// Close the file
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to display a record
private static void displayRecord(RandomAccessFile file, int recordNumber) throws IOException {
// Seek to the start of the record
file.seek((recordNumber - 1) * RECORD_SIZE);
// Read the name and quantity fields from the record
String name = file.readUTF();
int quantity = file.readInt();
// Display the record to the user
System.out.println("Name: " + name);
System.out.println("Quantity: " + quantity);
}
// Method to update a record
private static void updateRecord(RandomAccessFile file, int recordNumber) throws IOException {
// Seek to the start of the record
file.seek((recordNumber - 1) * RECORD_SIZE);
// Read the name and quantity fields from the record
String name = file.readUTF();
int quantity = file.readInt();
// Display the current values to the user
System.out.println("Current name: " + name);
System.out.println("Current quantity: " + quantity);
// Prompt the user for new values
System.out.print("Enter new name: ");
name = br.readLine();
System.out.print("Enter new quantity: ");
quantity = Integer.parseInt(br.readLine());
// Write the updated values back to the record
file.seek((recordNumber - 1) * RECORD_SIZE);
file.writeUTF(name);
file.writeInt(quantity);
System.out.println("Record updated");
}
// Method to add a new record
private static void addRecord(RandomAccessFile file) throws IOException {
// Prompt the user for the new record's name and quantity
System.out.print("Enter name of new record: ");
String name = br.readLine();
System.out.print("Enter quantity of new record: ");
int quantity = Integer.parseInt(br.readLine());
// Seek to the end of the file
file.seek(file.length());
// Write the new record to the file
file.writeUTF(name);
file.writeInt(quantity);
System.out.println("Record added");
}
}
```
阅读全文