The experimental project of Java collection interface, exception handling, commonly used classes and collections and other technologies

1. The purpose of the experiment

Use and master most of the techniques in interfaces, exception handling, commonly used practical classes and integrated courses

2. Experimental content

1. Create a fruit interface, define the behavior of fruit being purchased in the interface, and then write multiple fruit interface implementation classes, such as Apple(), Banana(), Pear(). Create a main class again, and execute the Buy() method of each fruit interface implementation class in the main class.
2. In the main class, create an exception handling method check(), which is used to detect the number of pounds of fruits purchased.
3. In the main class, instantiate the Scanner class to implement user input

Three, the experimental environment

OS: Windows 10, compilation environment: eclipse

4. Description of the experimental process

Show some below 内联代码片.

Project:FruitShop
主类:FruitShop.java
Fruit接口:Fruit.java
各实现类:Apple.java、Banana.java、Pear.java
// FruitShop.java(源代码):
import java.util.Scanner;

public class FruitShop {
    
    
	public static double check(double i) throws Exception {
    
    // 创建check方法,用于检测购买水果的斤数
		if (i < 0)// 如果i小于0
			throw new Exception("所购买的水果不能小于0斤!");// 抛出一个Exception异常对象
		return i;
	}

	public static void main(String[] args) {
    
    
		int x = 0;// 购买水果选择项
		double i = 0;// 购买斤数
		double sum = 0;// 结算金额
		Fruit ap = new Apple();// 实例化Apple类
		Fruit ba = new Banana();// 实例化Banana类
		Fruit pe = new Pear();// 实例化Pear类
		Scanner sc = new Scanner(System.in);// 实例化Scanner类
		System.out.println("请选择你需要购买的水果:(苹果1、香蕉2、雪梨3)");
		x = sc.nextInt();// 输入所要购买水果的数字
		switch (x) {
    
    // 根据输入的数值,选择相应的输出语句
		case 1: {
    
    
			System.out.println("请输入你需要多少斤这种水果:");
			i = sc.nextDouble();// 输入要购买的斤数
			try {
    
    
				double a = check(i);// 调用check方法
				sum = ap.Buy(i);
				System.out.println("合计:" + sum + "元");
			} catch (Exception e) {
    
    // 捕获Exception异常
				System.out.println("数据逻辑错误!");
				System.out.println("原因:" + e.getMessage());
			}
			break;
		}

		case 2: {
    
    
			System.out.println("请输入你需要多少斤这种水果:");
			i = sc.nextDouble();
			try {
    
    
				double a = check(i);
				sum = ba.Buy(i);
				System.out.println("合计:" + sum + "元");
			} catch (Exception e) {
    
    // 捕获Exception异常
				System.out.println("数据逻辑错误!");
				System.out.println("原因:" + e.getMessage());
			}
			break;
		}
		case 3: {
    
    
			System.out.println("请输入你需要多少斤这种水果:");
			i = sc.nextDouble();
			try {
    
    
				double a = check(i);
				sum = pe.Buy(i);
				System.out.println("合计:" + sum + "元");
			} catch (Exception e) {
    
    // 捕获Exception异常
				System.out.println("数据逻辑错误!");
				System.out.println("原因:" + e.getMessage());
			}
			break;
		}
		default:
			System.out.println("以上没有匹配的");
		}
	}
}
// Fruit.java (源代码):
public interface Fruit {
    
    //创建Fruit接口
	public double Buy(double i);
}
// Apple.java (源代码):
public class Apple implements Fruit {
    
    //Apple实现接口
	public double Buy(double i) {
    
    
		double price = 5;// 水果单价
		double sum = price * i;// 水果总价
		return sum;
	}
}
// Banana.java (源代码):
public class Banana implements Fruit {
    
    // Banana实现接口
	public double Buy(double i) {
    
    
		double price = 6;// 水果单价
		double sum = price * i;// 水果总价
		return sum;
	}
}
// Pear.java (源代码):
public class Pear implements Fruit {
    
    // Pear实现接口
	public double Buy(double i) {
    
    
		double price = 7;// 水果单价
		double sum = price * i;// 水果总价
		return sum;
	}
}

Guess you like

Origin blog.csdn.net/weixin_43372169/article/details/110927554