用java写一个系统,系统需具备基本的登录、注销及图书的简单销售功能
时间: 2024-06-12 21:03:46 浏览: 6
由于系统具体需求不明确,以下是一个简单的示例代码:
// 用户类
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
// 图书类
class Book {
private String name;
private double price;
private int quantity;
public Book(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
// 系统类
class System {
private static final String ADMIN_USERNAME = "admin";
private static final String ADMIN_PASSWORD = "123456";
private static User currentUser;
private static List<Book> books = new ArrayList<>();
public static void main(String[] args) {
books.add(new Book("Java Programming", 49.99, 5));
books.add(new Book("Python for Beginners", 29.99, 8));
books.add(new Book("Web Development with HTML and CSS", 19.99, 12));
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter your username: ");
String username = scanner.nextLine();
System.out.print("Enter your password: ");
String password = scanner.nextLine();
if (login(username, password)) {
System.out.println("Welcome, " + currentUser.getUsername() + "!");
break;
} else {
System.out.println("Invalid username or password. Please try again.");
}
}
while (true) {
System.out.println("1. View books");
System.out.println("2. Sell book");
System.out.println("3. Logout");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline character
switch (choice) {
case 1:
viewBooks();
break;
case 2:
sellBook(scanner);
break;
case 3:
logout();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static boolean login(String username, String password) {
if (username.equals(ADMIN_USERNAME) && password.equals(ADMIN_PASSWORD)) {
currentUser = new User(username, password);
return true;
}
return false;
}
private static void viewBooks() {
System.out.println("Name\tPrice\tQuantity");
for (Book book : books) {
System.out.println(book.getName() + "\t" + book.getPrice() + "\t" + book.getQuantity());
}
}
private static void sellBook(Scanner scanner) {
System.out.print("Enter book name: ");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // consume newline character
if (quantity > book.getQuantity()) {
System.out.println("Not enough stock.");
} else {
book.setQuantity(book.getQuantity() - quantity);
System.out.println("Total price: " + quantity * book.getPrice());
}
return;
}
}
System.out.println("Book not found.");
}
private static void logout() {
currentUser = null;
System.out.println("Logged out.");
}
}
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)