Java check if item is defective

Topic details:

The factory inspects the equipment for defective products and issues a warning if it is found to be defective. The programming simulates the process of detecting defective products.

Write a product class Product, which has member variables name and isDefect (whether it is defective), and has get and set methods.

Write a subclass of Exception, DefectException, this subclass has the message property, there is a constructor DefectException() to assign "defective" to the message member, and there is a toShow() method to output the value of the message

Write a Machine class. When the method checkProduct(Product product) of this class finds that the parameter product is a defective product (the product's isDefect property is true), it will throw a DefectException exception object.

In the try part of the try...catch statement in the main method of the main class, the program makes the instance of the Machine class call the checkProduct method. If a defective product is found, the defective product is processed in the catch part of the try...catch statement.

Design class definition:

class Product { //完整编写
       boolean isDefect;
       String name;
       //补充代码


    }
class DefectException extends Exception { //完整编写
       String message;
      //补充代码


    }
class Machine {
      public void checkProduct(Product product) throws DefectException {
         if(product.isDefect()) {
             DefectException defect=new DefectException();
             //【代码1】   //抛出defect
         }
         else {
             System.out.print(product.getName()+"不是次品! ");
         }
      }
    }

Example of the referee test procedure:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
         Machine machine = new Machine();
          String name[] ;
          Scanner sc=new Scanner(System.in);
          name=sc.nextLine().split(" ");
          Product [] products = new Product[name.length]; //检查6件货物  
          for(int i= 0;i<name.length;i++) {
             products[i] = new Product();
             if(i%2==0) {
                products[i].setIsDefect(false);
                products[i].setName(name[i]);
             }
             else {
               products[i].setIsDefect(true);
               products[i].setName(name[i]);
             } 
          }
          for(int i= 0;i<products.length;i++) {
            try {
                machine.checkProduct(products[i]);
                System.out.println(products[i].getName()+"检查通过");
            }
            catch(DefectException e) {
               e.toShow();//【代码2】 //e调用toShow()方法
               System.out.println(products[i].getName()+"被禁止!"); 
            }
          }     
       } 
}


/* 将完整的Product类, DefectException类 和Machine类写在下面*/

Input sample:

电脑 炸药 西服 硫酸 手表 硫磺

no blank line at the end

Sample output:

电脑不是次品! 电脑检查通过
次品! 炸药被禁止!
西服不是次品! 西服检查通过
次品! 硫酸被禁止!
手表不是次品! 手表检查通过
次品! 硫磺被禁止!

no blank line at the end

Answer code:

class Product { // 完整编写
	boolean isDefect;
	String name;

	// 补充代码
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isDefect() {
		return isDefect;
	}

	public void setIsDefect(boolean defect) {
		isDefect = defect;
	}
}

class DefectException extends Exception { // 完整编写
	String message;

	// 补充代码
	DefectException() {
	}

	void toShow() {
		System.out.print("次品!");
	}
}

class Machine {
	public void checkProduct(Product product) throws DefectException {
		if (product.isDefect()) {
			DefectException defect = new DefectException();
			throw defect;
		} else {
			System.out.print(product.getName() + "不是次品! ");
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_54587141/article/details/120744571