zzk.213 2022-05-11 21:40 采纳率: 87.8%
浏览 36
已结题

问问图中紫色标注部分要怎么实现 图书管理系统中的一个问题 在第一部分代码case2 的case2里 就是说现在要怎么解决那个异常

img

import java.util.Scanner;

public class CLI {
public static void main(String[] args) {
Library l = new Library("UIC Library");
int i = 0;
while (i != 6) {
i = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");

        switch (i) {
        case 1:
            System.out.println("Total number of borrowed books:" +l.totalBorrowedBooks());
            break;
        case 2:
            int j = readPosInt("Type the user role (lender:1 borrower:2):");
            switch (j) {
            case 1:
                String str1 = readLine("Enter the name of the user:");
                int k = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Lender " + str1 +" lending "+ k + " book(s) has been added.");
                Lender ll=new Lender(str1,k);
                l.addUser(ll);
                break;
            case 2:
                String str2 = readLine("Enter the name of the user:");
                int p = readPosInt("Enter the initial number of borrowed books:");
                System.out.println("Borrower " + str2 +" borrowing "+ p + " book(s) has been added.");
                
                /*这里要怎么实现????
                Borrower bb = new Borrower(str2,p);
                l.addUser(bb);
                try {
                    Borrower b=new Borrower(str2,-1);
                } catch (NotALenderException e) {
                    System.out.println("BUG!This must never happen!");
                    System.exit(1);
                }
                l.addUser(bb);*/
                break;
            default:
                System.out.println("Unknown action!");
                break;
            }
            break;
        case 3:
            System.out.println(3);
            break;
        case 4:
            System.out.println(4);
            break;
        case 5:
            System.out.println(5);
            break;
        case 6:
            System.out.println("Goodbye!");
            System.exit(0);
            break;
        default:
            System.out.println("Unknown action!");
            break;
        }
    }
    
}

public static String readLine(String str) {
    System.out.print(str);
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine();
}

public static int readPosInt(String str) {
    while (true) {
        try {
            System.out.print(str);
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            if (i < 0) {
                System.out.println("Positive integers only!");
                continue;
            }
            return i;
        } catch (Exception e) {
            System.out.println("You must type an integer!");
        }

    }
}

}

public class Borrower extends User {
public Borrower(String name, int book) throws NotALenderException {
super(name, book);
if (this.getBook() <= 0) {
throw new NotALenderException("A new borrower cannot lend books.");
} else {
super.setBook(super.getBook());
}
}

@Override
public void moreBook(int number) throws NotALenderException {
    if (getBook() >= 0) {
        super.setBook(super.getBook() + number);
    } else {
        throw new NotALenderException("A new borrower cannot lend" + (-(getBook() + number)) + "book(s).");
    }
}

}

  • 写回答

2条回答 默认 最新

  • 太空眼睛 Java领域新星创作者 2022-05-11 22:47
    关注
    import java.util.Scanner;
    
    public class CLI {
    public static void main(String[] args) {
    Library l = new Library("UIC Library");
    int i = 0;
    while (i != 6) {
    i = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");
    
            switch (i) {
            case 1:
                System.out.println("Total number of borrowed books:" +l.totalBorrowedBooks());
                break;
            case 2:
                int j = readPosInt("Type the user role (lender:1 borrower:2):");
                switch (j) {
                case 1:
                    String str1 = readLine("Enter the name of the user:");
                    int k = readPosInt("Enter the initial number of borrowed books:");
                    System.out.println("Lender " + str1 +" lending "+ k + " book(s) has been added.");
                    Lender ll=new Lender(str1,k);
                    l.addUser(ll);
                    break;
                case 2:
                    String str2 = readLine("Enter the name of the user:");
                    int p = readPosInt("Enter the initial number of borrowed books:");
                    System.out.println("Borrower " + str2 +" borrowing "+ p + " book(s) has been added.");
    
                    try {
                        Borrower bb = new Borrower(str2,p);
                        l.addUser(bb);
                    } catch (NotALenderException e) {
                        System.out.println("BUG!This must never happen!");
                        System.exit(1);
                    }
    
                    break;
                default:
                    System.out.println("Unknown action!");
                    break;
                }
                break;
            case 3:
                System.out.println(3);
                break;
            case 4:
                System.out.println(4);
                break;
            case 5:
                System.out.println(5);
                break;
            case 6:
                System.out.println("Goodbye!");
                System.exit(0);
                break;
            default:
                System.out.println("Unknown action!");
                break;
            }
        }
        
    }
     
    public static String readLine(String str) {
        System.out.print(str);
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }
     
    public static int readPosInt(String str) {
        while (true) {
            try {
                System.out.print(str);
                Scanner scanner = new Scanner(System.in);
                int i = scanner.nextInt();
                if (i < 0) {
                    System.out.println("Positive integers only!");
                    continue;
                }
                return i;
            } catch (Exception e) {
                System.out.println("You must type an integer!");
            }
     
        }
    }
    }
    
    public class Borrower extends User {
    public Borrower(String name, int book) throws NotALenderException {
    super(name, book);
    if (this.getBook() <= 0) {
    throw new NotALenderException("A new borrower cannot lend books.");
    } else {
    super.setBook(super.getBook());
    }
    }
    
    @Override
    public void moreBook(int number) throws NotALenderException {
        if (getBook() >= 0) {
            super.setBook(super.getBook() + number);
        } else {
            throw new NotALenderException("A new borrower cannot lend" + (-(getBook() + number)) + "book(s).");
        }
    }
    }
    

    如有帮助,请采纳,十分感谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月11日

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线