6-1 Abnormality: Item safety inspection (20 points)

Review of old java questions
6-1 Exception: Item safety check (20 points)
This is a function question template. Write the title requirements here. The station inspects equipment for dangerous goods and will issue a warning if dangerous goods are found. Program to simulate the situation where dangerous goods are found by the equipment. The programming requirements are as follows:

By inheriting the Exception class, write a DangerException class. a) This abnormal class has a construction method. The construction method uses super to call the parent class construction method, using the string: "Dangerous goods!" to initialize the parent class variable message.
Write product category: Goods, which contains the following members: a) Private name attribute (String type), which represents the name of the product. b) Private isDanger attribute (boolean type), which indicates whether the product is dangerous. If it is dangerous, the value is true, otherwise it is fales. c) Write set and get methods for two private variables respectively.
Write a Machine class with the method checkBag (Goods goods) of this class. When the parameter goods is found to be dangerous goods, that is, when the isDanger property of goods is true, the method throws the object of DangerException.
Write the main class Check, create a product object in its main method, and use the Machine object to check the product.
Function interface definition:
Referee test program sample:

public class Main {
    
    
       public static void main(String args[]) {
    
              
          String[] name ={
    
    "苹果","炸药","西服","硫酸","手表","硫磺"};                  
          Goods[] goods = new Goods[name.length];                  
          for(int i= 0;i<name.length;i++) {
    
    
             goods[i] = new Goods();
             if(i%2==0) {
    
    
                goods[i].setDanger(false);
                goods[i].setName(name[i]);
             }
             else {
    
    
                goods[i].setDanger(true);
                goods[i].setName(name[i]);
             } 
          }          
          Machine machine = new Machine();
          for(int i= 0;i<goods.length;i++) {
    
    
              System.out.print(goods[i].getName());
              try {
    
     
                  machine.checkBag(goods[i]);
                  System.out.println(",检查通过\n");
              }catch(DangerException e) {
    
    
                  System.out.println(e.getMessage());
                  System.out.println(goods[i].getName()+",被禁止!\n"); 
              }
          }     
       } 
}

/* Please fill in the answer here*/
input example:
give a set of input here. E.g:

Output sample:
The corresponding output is given here. E.g:

苹果,检查通过

炸药属于危险品!
炸药,被禁止!

西服,检查通过

硫酸属于危险品!
硫酸,被禁止!

手表,检查通过

硫磺属于危险品!
硫磺,被禁止!

years:

import java.util.*;
public class Main {
    
    
	public static void main(String args[]) {
    
              
        String[] name ={
    
    "苹果","炸药","西服","硫酸","手表","硫磺"};                  
        Goods[] goods = new Goods[name.length];                  
        for(int i= 0;i<name.length;i++) {
    
    
           goods[i] = new Goods();
           if(i%2==0) {
    
    
              goods[i].setDanger(false);
              goods[i].setName(name[i]);
           }
           else {
    
    
              goods[i].setDanger(true);
              goods[i].setName(name[i]);
           } 
        }          
        Machine machine = new Machine();
        for(int i= 0;i<goods.length;i++) {
    
    
            System.out.print(goods[i].getName());
            try {
    
     
                machine.checkBag(goods[i]);
                System.out.println(",检查通过\n");
            }catch(DangerException e) {
    
    
                System.out.println(e.getMessage());
                System.out.println(goods[i].getName()+",被禁止!\n"); 
            }
        }     
     } 
}

class DangerException extends Exception{
    
    
	public DangerException() {
    
    
		super("属于危险品!");
	}//通过super向上初始化父类中的message
	//调用使用getMassage();
}

class Goods{
    
    
	String name;
	boolean isDanger;
	
	public void setName(String name) {
    
    
		this.name=name;
	}
	
	public void setDanger(boolean Danger) {
    
    
		this.isDanger=Danger;
	}
	
	public String getName() {
    
    
		return this.name;
	}
	
	public boolean getDanger() {
    
    
		return this.isDanger;
	}
	
}

class Machine{
    
    
	public void checkBag(Goods goods)throws DangerException{
    
    
		if(goods.isDanger==true) {
    
    
			throw new DangerException(); //抛出异常
		}
	}
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/111932677