write code to create a hashmap object to keep a collection of names, where name is the key and its string length is the value then write code put the 2 following names into the hashmap: “wolfgang amadeus mozart” and “ludwig van beethoven”
时间: 2023-04-24 11:00:20 浏览: 64
请编写代码创建一个哈希映射对象,用于存储一组名字,其中名字是键,其字符串长度是值,然后编写代码将以下两个名字放入哈希映射中:“wolfgang amadeus mozart”和“ludwig van beethoven”。
相关问题
write java。The moreBook method takes the name of a user and a number of books (as a string) as arguments. The moreBook method of the controller then transforms the number of books from a string to an integer (using the Integer.parseInt static method) and calls the moreBook method of the library to increase the number of books borrowed or lent by the user (depending on what kind of user it is) of a specific user, by the given argument. • If no exception occurs then the moreBook method of the controller returns the empty string. • If the moreBook method of the library throws an UnknownUserException then the moreBook method of the controller must catch this exception and return as result the error message from the exception object. • If the moreBook method of the library throws a NotALenderException then the moreBook method of the controller must catch this exception and return as result the error message from the exception object. • If the parseInt method of the Integer class throws a NumberFormatException (because the user typed something which is not an integer) then the moreBook method of the controller must catch this exception and return as result the error message from the exception object.
Here is the Java code for the described functionality:
```
public class LibraryController {
private Library library;
public LibraryController(Library library) {
this.library = library;
}
public String moreBook(String userName, String numBooks) {
try {
int num = Integer.parseInt(numBooks);
library.moreBook(userName, num);
return "";
} catch (UnknownUserException e) {
return e.getMessage();
} catch (NotALenderException e) {
return e.getMessage();
} catch (NumberFormatException e) {
return e.getMessage();
}
}
}
public class Library {
private Map<String, User> users;
public Library() {
users = new HashMap<>();
}
public void moreBook(String userName, int numBooks) throws UnknownUserException, NotALenderException {
User user = users.get(userName);
if (user == null) {
throw new UnknownUserException("Unknown user: " + userName);
}
if (!(user instanceof Lender)) {
throw new NotALenderException(userName + " is not a lender");
}
((Lender) user).moreBooks(numBooks);
}
}
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Lender extends User {
private int numBooks;
public Lender(String name) {
super(name);
numBooks = 0;
}
public int getNumBooks() {
return numBooks;
}
public void moreBooks(int num) {
numBooks += num;
}
}
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}
```
The LibraryController class has a constructor that takes a Library object as a parameter. The moreBook method takes a user name and a number of books as arguments. It uses the Integer.parseInt static method to parse the number of books from a string to an integer. It then calls the moreBook method of the Library object to increase the number of books borrowed or lent by the user, depending on what kind of user it is.
If no exception occurs, the moreBook method of the controller returns the empty string. If the moreBook method of the library throws an UnknownUserException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the moreBook method of the library throws a NotALenderException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the parseInt method of the Integer class throws a NumberFormatException, the moreBook method of the controller catches this exception and returns the error message from the exception object.
The Library class has a map of users, where the key is the user name and the value is a User object. The moreBook method takes a user name and a number of books as arguments. It throws an UnknownUserException if the user is not found in the map, and a NotALenderException if the user is not an instance of the Lender class. Otherwise, it calls the moreBooks method of the Lender object to increase the number of books borrowed by the user.
The User class has a name field and a getName method.
The Lender class extends the User class and adds a numBooks field and a moreBooks method.
The UnknownUserException and NotALenderException classes extend the Exception class and have constructors that take an error message as a parameter.
entrySet()" should be iterated when both the key and value are neede
The `entrySet()` method in Java returns a set view of the mappings contained in a map. Each element in the set is a `Map.Entry` object, which represents a key-value pair in the map.
When you need to iterate through a map and access both the keys and values, it is more efficient to use the `entrySet()` method instead of calling the `keySet()` method and then retrieving each value using the `get()` method. This is because calling `get()` for each key requires a separate map lookup, whereas iterating through the `entrySet()` allows you to access both the key and value in one go.
Here is an example of how to use `entrySet()` to iterate through a map and access both the keys and values:
```
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}
```
This will output:
```
apple = 1
banana = 2
orange = 3
```