通过java和mongodb数据库写出自行车管理系统的管理员页面、学员页面、自行车页面和查询页面的代码
时间: 2024-01-24 20:17:24 浏览: 68
以下是一个简单的自行车管理系统的Java代码示例,使用MongoDB作为后端数据库。
管理员页面:
```java
import java.util.Scanner;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
public class AdminPage {
private MongoDatabase database;
private MongoCollection<Document> bikeCollection;
public AdminPage(MongoDatabase database) {
this.database = database;
this.bikeCollection = database.getCollection("bikes");
}
public void display() {
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("1. Add bike");
System.out.println("2. Remove bike");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch(choice) {
case 1:
addBike();
break;
case 2:
removeBike();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
}
scanner.close();
}
private void addBike() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike ID: ");
int id = scanner.nextInt();
System.out.print("Enter bike name: ");
String name = scanner.next();
System.out.print("Enter bike type: ");
String type = scanner.next();
System.out.print("Enter bike price: ");
double price = scanner.nextDouble();
Document bike = new Document();
bike.append("_id", id)
.append("name", name)
.append("type", type)
.append("price", price);
bikeCollection.insertOne(bike);
System.out.println("Bike added successfully.");
}
private void removeBike() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike ID: ");
int id = scanner.nextInt();
bikeCollection.deleteOne(eq("_id", id));
System.out.println("Bike removed successfully.");
}
}
```
学员页面:
```java
import java.util.Scanner;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
public class StudentPage {
private MongoDatabase database;
private MongoCollection<Document> bikeCollection;
public StudentPage(MongoDatabase database) {
this.database = database;
this.bikeCollection = database.getCollection("bikes");
}
public void display() {
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("1. View all bikes");
System.out.println("2. View bike by ID");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch(choice) {
case 1:
viewAllBikes();
break;
case 2:
viewBikeById();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
}
scanner.close();
}
private void viewAllBikes() {
System.out.println("Bikes:");
for(Document doc : bikeCollection.find()) {
System.out.println(doc.toJson());
}
}
private void viewBikeById() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike ID: ");
int id = scanner.nextInt();
Document bike = bikeCollection.find(eq("_id", id)).first();
if (bike != null) {
System.out.println(bike.toJson());
} else {
System.out.println("Bike not found.");
}
}
}
```
自行车页面:
```java
import java.util.Scanner;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
public class BikePage {
private MongoDatabase database;
private MongoCollection<Document> bikeCollection;
public BikePage(MongoDatabase database) {
this.database = database;
this.bikeCollection = database.getCollection("bikes");
}
public void display() {
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("1. View all bikes");
System.out.println("2. View bike by ID");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch(choice) {
case 1:
viewAllBikes();
break;
case 2:
viewBikeById();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
}
scanner.close();
}
private void viewAllBikes() {
System.out.println("Bikes:");
for(Document doc : bikeCollection.find()) {
System.out.println(doc.toJson());
}
}
private void viewBikeById() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike ID: ");
int id = scanner.nextInt();
Document bike = bikeCollection.find(eq("_id", id)).first();
if (bike != null) {
System.out.println(bike.toJson());
} else {
System.out.println("Bike not found.");
}
}
}
```
查询页面:
```java
import java.util.Scanner;
import org.bson.Document;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
public class QueryPage {
private MongoDatabase database;
private MongoCollection<Document> bikeCollection;
public QueryPage(MongoDatabase database) {
this.database = database;
this.bikeCollection = database.getCollection("bikes");
}
public void display() {
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("1. Search bikes by name");
System.out.println("2. Search bikes by type");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch(choice) {
case 1:
searchBikesByName();
break;
case 2:
searchBikesByType();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
}
scanner.close();
}
private void searchBikesByName() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike name: ");
String name = scanner.next();
System.out.println("Bikes:");
for(Document doc : bikeCollection.find(eq("name", name))) {
System.out.println(doc.toJson());
}
}
private void searchBikesByType() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter bike type: ");
String type = scanner.next();
System.out.println("Bikes:");
for(Document doc : bikeCollection.find(eq("type", type))) {
System.out.println(doc.toJson());
}
}
}
```
阅读全文