final project q5

This commit is contained in:
2022-05-24 22:40:38 +08:00
parent e2bd453330
commit 707750c9c8
9 changed files with 367 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the borrower class.
*/
public class Borrower extends User {
/**
* Constructor.
*
* @param name Borrower's name
* @param int book number of books borrowed by user
*/
public Borrower(String name, int book) throws NotALenderException {
super(name, book);
// check
if (book < 0) {
throw new NotALenderException("A new borrower cannot lend books.");
}
}
/**
* increase the number of books borrowed by the user.
*
* @param number Incrased number of books
* @throws NotALenderException
*/
public void moreBook(int number) throws NotALenderException {
int newBook = getBook() + number;
if (number < 0 && newBook < 0) {
throw new NotALenderException("A borrower cannot lend " + (-newBook) + " book(s).");
}
setBook(newBook);
}
/**
* Test.
*/
public static void testBorrower() {
try {
Borrower b = new Borrower("Bob", -1);
} catch (NotALenderException e) {
System.out.println(e.getMessage().equals("A new borrower cannot lend books."));
}
try {
Borrower b = new Borrower("Bob", 10);
System.out.println(b.getName() == "Bob");
System.out.println(b.getBook() == 10);
b.setBook(5);
System.out.println(b.getBook() == 5);
b.moreBook(2);
System.out.println(b.getBook() == 7);
b.moreBook(-2);
System.out.println(b.getBook() == 5);
b.moreBook(-5);
System.out.println(b.getBook() == 0);
b.moreBook(-1);
} catch (NotALenderException e) {
System.out.println(e.getMessage().equals("A borrower cannot lend 1 book(s)."));
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the CLI class.
*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class CLI {
private static Scanner input = new Scanner(System.in);
/**
* Read one line from CLI.
*
* @param message Hint printed to screen
* @return one line input
*/
private static String readLine(String message) {
System.out.print(message);
return input.nextLine();
}
/**
* Read one positive integer.
*
* @param message HINT printed to screen
* @return one positive integer
*/
private static int readPosInt(String message) {
while (true) {
System.out.print(message);
try {
int result = input.nextInt();
input.nextLine();
if (result >= 0) {
return result;
} else {
System.out.println("Positive integers only!");
}
} catch (InputMismatchException e) {
System.out.println("You must type an integer!");
input.nextLine();
}
}
}
public static void main(String[] args) {
String str1 = readLine("Type some text: ");
System.out.println("Text read is: " + str1);
int i = readPosInt("Type an integer: ");
System.out.println("Integer read is: " + i);
String str2 = readLine("Type some text: ");
System.out.println("Text read is: " + str2);
}
}

View File

@@ -0,0 +1,13 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the interface of IUser.
*/
public interface IUser {
public String getName();
public int getBook();
public void moreBook(int number) throws NotALenderException;
}

View File

@@ -0,0 +1,42 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the Lender class.
*/
public class Lender extends User {
/**
* Constructor.
*
* @param name The user's name.
* @param book number of bookds lent by the user. This will be store in negetive value.
*/
public Lender(String name, int book) {
// lender negetive book value
super(name, -book);
}
/**
* Increases the number of bookds.
*
* @param book the number of books.
*/
public void moreBook(int book) {
setBook(getBook() - book);
}
/**
* Test.
*/
public static void testLender() {
Lender l = new Lender("Anna", 5);
System.out.println(l.getName() == "Anna");
System.out.println(l.getBook() == -5);
l.setBook(-6);
System.out.println(l.getBook() == -6);
l.moreBook(2);
System.out.println(l.getBook() == -8);
l.moreBook(-9);
System.out.println(l.getBook() == 1);
}
}

View File

@@ -0,0 +1,106 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the Library class.
*/
import java.util.ArrayList;
public class Library {
private String name;
private ArrayList<IUser> users;
/**
* Constructor.
*
* @param name
*/
public Library(String name) {
this.name = name;
// init array list
users = new ArrayList<IUser>();
}
/**
* Add the user to the array list.
*
* @param user The user to be added
*/
public void addUser(IUser user) {
users.add(user);
}
/**
* The total number of bookds borrowed by all users.
*
* @return the number of books.
*/
public int totalBorrowedBooks() {
int total = 0;
for (IUser user : users) {
total += user.getBook();
}
return total;
}
/**
* Get the number of books borrowed by this user.
*
* @param name the name of the query user
* @return the number of books
* @throws UnknownUserException if user not found
*/
public int getBook(String name) throws UnknownUserException {
for (IUser user : users) {
if (user.getName().equals(name)) {
return user.getBook();
}
}
// user not found
throw new UnknownUserException("User " + name + " unknown.");
}
/**
* Change the number of books currently borrowed by that user.
*
* @param name the user's name
* @param number the number of books
* @throws UnknownUserException user not found
* @throws NotALenderException can not lend book
*/
public void moreBook(String name, int number) throws UnknownUserException, NotALenderException {
for (IUser user : users) {
if (user.getName().equals(name)) {
user.moreBook(number);
return;
}
}
// user not found
throw new UnknownUserException("User " + name + " unknown.");
}
/**
* Test.
*/
public static void testLibrary() {
Library li = new Library("UIC Library");
System.out.println(li.totalBorrowedBooks() == 0);
li.addUser(new Lender("L1", 10));
try {
System.out.println(li.getBook("L1") == -10);
System.out.println(li.totalBorrowedBooks() == -10);
li.addUser(new Borrower("B1", 20));
System.out.println(li.getBook("L1") == -10);
System.out.println(li.getBook("B1") == 20);
System.out.println(li.totalBorrowedBooks() == 10);
li.getBook("B2");
} catch (UnknownUserException ex) {
System.out.println(ex.getMessage().equals("User B2 unknown."));
} catch (NotALenderException ex) {
// This should never happen!
System.out.println(false);
}
// More test cases are needed
}
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
Library.testLibrary();
}
}

View File

@@ -0,0 +1,5 @@
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,69 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-25
* Description: This is the User class.
*/
public abstract class User implements IUser {
/**
* The user's name.
*/
private String name;
/**
* The number of books borrowed by the user.
*/
private int book;
/**
* Constructor.
*
* @param name The user's name.
* @param book The number of books borrowed by the user.
*/
public User(String name, int book) {
this.name = name;
this.book = book;
}
/**
* Getter of the user's name.
*
* @return The user's name.
*/
public String getName() {
return name;
}
/**
* Getter of the number of books borrowed by the user.
*
* @return The number of books borrowed by the user.
*/
public int getBook() {
return book;
}
/**
* Change the number of books borrowed by the user.
*
* @param book The number of books borrowed by the user.
*/
protected void setBook(int book) {
this.book = book;
}
/**
* Increase the number of books borrowed by the user.
*
* @param book The number of books borrowed by the user.
*/
public abstract void moreBook(int number) throws NotALenderException;
/**
* Test.
*/
public static void testUser() {
// abstract class not testing
}
}