JAVA运用的练习题求助
1.修改这个method to throw exception in two different case- 存货不足
- quantity 范围(1...500)
2.修改 driver class to catch and handle the exceptions
程序代码:
import java.until.*; import java.util.Scanner; public class Part { private String ID; private String name; private int stLevel; private int roLevel; private double uPrice; public Part(String ID, String name, int sL, int rL, double uP){ this.ID = ID; this.name = name; stLevel=sL; roLevel=rL; uPrice = uP; } public String getID(){return ID;} public int getSLevel(){return stLevel;} public void replenish(int qty){ stLevel += qty; } public double supply(int qty){ if (stLevel<qty) return -1.0; stLevel-=qty; if(stLevel<roLevel) System.out.println("Place order for" + ID); return qty*uPrice; } } class TestPart{ public static void man(String args[]){ Part P=new Part("s123", "Axle", 12, 80, 250.0); Scanner sc=new Scanner(System.in); boolean done=false; do{ System.out.print("Enter qty:"); int qty=sc.nextInt(); double amt= p.supply(qty); if(amt<0){ System.out.println("Insufficient Qty"); System.out.println("Curr.stock="+p.getSLevel()); } else{ System.out.println("Cost for supplying"+p.getID()+"=" + amt); done = true; } }while(!done); } }