JAVA learning from scratch-pre02
task1
topic description
Construct a Bookset class to represent a set of books composed of multiple books. The Bookset class is required to contain attributes: book title, unit price (unit price refers to the price of a single book in a series) and the number of books containing a single book (we stipulate The unit price of each book in the series is the same). The corresponding program that needs to be completed can query the name, unit price, quantity, and total price of the book series. A set of books is given at the beginning.
7 operations:
type | attribute | significance | output |
---|---|---|---|
1/2/3 | none | Query the name/unit price/quantity of Bookset | Corresponding name/unit price/quantity |
4 | name |
Bookset name changed toname |
none |
5 | price |
Bookset price changed toprice |
none |
6 | num |
Bookset Quantity changed tonum |
none |
7 | none | Query the total price of Bookset | A float representing the total price |
Notice
- Floating point numbers are double precision, and integers are long
answer
As the first question, the investigation is to create a new class and encapsulation class. You can see that the first 6 operations are the setters and getters corresponding to the 3 properties. This can be directly and automatically generated by laziness:
Click on the variable to generate the setter and getter Alt+Insert
, and click Generate.
In this way, the first 6 operations have been completed, and a method to return the total price can be added.
Generally speaking, there are no pitfalls, the main thing is to be familiar with submission. Here I use a slacker-exclusive gadget (Github Desktop), directly clone the corresponding warehouse of gitlab to the local, then copy and paste the code (.java file) into the folder just cloned, and finally commit and push. Can. (The graphical interface is really fragrant)
There is also the code style, you can use it for each file first Ctrl+Alt+L
, and it will be adjusted automatically (the number of lines and word limits must be changed by yourself), and then use checkstyle to change it.
task2
topic description
To create multiple bookshelves:
- Each shelf can store multiple sets of books
- You can perform corresponding operations on the series of books in the bookshelf
The operation is as follows:
- Ask for the price of the most expensive book on a shelf
- Ask the total price of all the books in a certain bookshelf (note that the total price is not the sum of unit prices, the total price is related to the number of books in the series)
- Add a set of books to a shelf
- Remove a set of books from a shelf
Notice
Need to use BigDecimal, BigInteger, pay special attention to the total price written in task1 should also be replaced with BigDecimal
answer
Create a new Bookshelf class, make a container inside (you can use Hashmap) to store Bookset, and implement 4 operation methods in the Bookshelf class. Create another container in main to hold Bookshelf as our library.
ArrayList<Bookshelf> lib = new ArrayList<Bookshelf>();
for (int i = 0; i < n; i++) {
Bookshelf bookshelf = new Bookshelf();
lib.add(bookshelf);
}//别忘了初始化添加n个书架
When querying a certain bookshelf, use it lib.get(i - 1)
to operate on the i-th bookshelf. (Note that bookshelves start from number 1)
task3
topic description
- Create different book series classes. book series should
- conform to a certain inheritance relationship
- Equipped with information query method
- Create a unified bookshelf class for managing book series instances. The bookshelf class should
- Equipped with corresponding methods for adding series , removing series and querying series
The series is divided into three categories, Art (text), Science (reason), and Other. The Art category is subdivided into Novel, Poetry and OtherA. Science is divided into Math, Computer and OtherS.
book type | Attributes | attribute type |
---|---|---|
Other | Same as the base class Bookset | Same as the base class Bookset |
OtherA | Including all properties of Bookset, newly added minimum reading age age | The original attributes of Bookset remain unchanged, age is an integer |
Novel | Including all the attributes of OtherA, the newly added finish flag finish | The original property of OtherA remains unchanged, finish is boolean type |
Poetry | Include all the attributes of OtherA, add the new author author | The original attributes of OtherA remain unchanged, and author is a string |
OtherS | Including all properties of Bookset, newly added year of publication | The original attributes of Bookset remain unchanged, and year is an integer |
Math | Including all attributes of OtherS, newly added use grade | The original attribute of OtherS remains unchanged, and the grade is an integer |
Computer | Including all attributes of OtherS, newly added professional type | The original attributes of OtherS remain unchanged, and the professional type is a string |
The operations that need to be implemented are:
type | attribute | significance | output |
---|---|---|---|
1 | i name |
The series information of the i book title on the shelf with the query numbername |
All properties of the series, each item is separated by a space (consistent with the input order) |
2 | i |
i How many series of books are there in the bookshelf of the query number |
an integer |
3 | i |
The total number of books in the shelf of the query i number |
an integer |
4 | i type name price num var1 var2 (no newline) |
Move to i the No. bookshelf the series of books whose type is type , book name name , unit price is price , and total number is num (may have attributes var1, var2) |
none |
5 | i name |
Remove titled series from i shelf numbername |
An integer representing the total number of books remaining on the shelf after being removed from the series |
answer
- factory pattern
Using the factory mode is actually to encapsulate the construction of various Bookset classes into a new class BookFactory with a switch, which is specially used for the production of Bookset.
public class BookFactory {
public static Bookset getBookset(String type, String name, double price, long num,String var1, String var2) {
switch (type) {
case "Other": return new Other(name,price,num);
case "OtherA": return new OtherA(name,price,num,Integer.parseInt(var1));
case "OtherS": return new OtherS(name,price,num,Integer.parseInt(var1));
case "Novel":
return new Novel(name,price,num,Integer.parseInt(var1),Boolean.parseBoolean(var2));
case "Poetry": return new Poetry(name,price,num,Integer.parseInt(var1),var2);
case "Math":
return new Math(name,price,num,Integer.parseInt(var1),Integer.parseInt(var2));
case "Computer": return new Computer(name,price,num,Integer.parseInt(var1),var2);
default: return null;
}
}
}
Here I directly pass in all the parameters in the form of strings, and after switching, convert var1 and var2 to the required format for different Booksets.
In Main, the method of reading a line of strings at a time is adopted, and then the line is divided by split:
String inputtext = scanner.nextLine();
String[] values = inputtext.split(" ");//使用时直接写values[i]并转为相应的格式
For var1 and var2, you only need to judge the length of values.
- inheritance relationship
Replace Bookset with an abstract class, and add a BookType property and a toString method (this is implemented by subclasses)
private Booktype booktype;
public abstract String toString();
Then Art, Science, Other inherit Bookset; Novel, Poetry and OtherA inherit Art; Math, Computer and OtherS inherit Science. Add its unique attributes and implement the toString method in each subclass.
task4
topic description
type | abnormal | output |
---|---|---|
1 | i name A book with a title does not exist on a non-empty shelf |
Oh, no! We don’t have name . |
1 2 3 | i No. bookshelf is empty bookshelf |
Oh, no! This is empty. |
4 | i name A book with title already exists on shelf number |
Oh, no! The name exist. |
5 | i name Book with title does not exist on shelf number |
mei you wo zhen mei you. |
Notice
The input format of the topic does not necessarily have to be entered exactly according to the format, and there may be redundant parameter input.
answer
Here I throw an exception directly (
for example
int length = booksetlist.size();
if (length == 0) {
throw new Exception("Oh, no! This is empty.");
}
Then try catch in main.
Secondly, according to the input method of my task3, no matter how many parameters are input later, it has nothing to do with me.
task5
topic description
Added merge bookshelf operation:
type | attribute | significance | output |
---|---|---|---|
6 | i j |
Merge i the numbered the numbered bookshelfj |
An integer representing the number of the new bookshelf |
answer
Add compareTo and clone to Bookset, and implement it by subclasses, such as the comparison of Poetry:
@Override
public Boolean compareTo(Bookset bs) {
if (bs.getBooktype() != this.getBooktype()) {
//如果比较的Bookset类型都不一样的话肯定不是一种书
return false;
}
Poetry p = (Poetry) bs;
return super.compareTo(bs) && p.getAuthor().equals(this.author);
}
You can merge them later.