📄 mainclass.java
字号:
package problem_11_2;
//MainClass.java
import java.io.*;
public class MainClass {
public static void main(String[] args){
try{
//输出提示输入信息
System.out.println("Selection pool: apple/pear/orange\nSelection combined with '+' is allowed, e.g. apple+pear+orange");
System.out.print("Please choose the variety of fruit mixed in the juice: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String choice = new String(br.readLine()); //读入水果选项
System.out.print("Provisional standard sugar rate = ");
Double stdSugarRate = Double.valueOf(br.readLine()); //读入标准含糖率
System.out.print("Provisional standard vitamin rate = ");
Double stdVitaminRate = Double.valueOf(br.readLine()); //读入标准含维生素率
System.out.print("Juice weight = ");
Double juiceWeight = Double.valueOf(br.readLine()); //读入果汁总重量
boolean isSelectionValid = true; //水果选项是否有效
Juice juice;
if (choice.equals("apple")) //选择苹果
juice = new Juice(juiceWeight, newApple());
else if(choice.equals("pear")) //选择梨
juice = new Juice(juiceWeight, newPear());
else if(choice.equals("orange")) //选择橘子
juice = new Juice(juiceWeight, newOrange());
else if(choice.equals("apple+pear") || choice.equals("pear+apple")) //选择苹果和梨
juice = new Juice(juiceWeight, newApple(), newPear());
else if(choice.equals("apple+orange") || choice.equals("orange+apple")) //选择苹果和橘子
juice = new Juice(juiceWeight, newApple(), newOrange());
else if(choice.equals("pear+orange") || choice.equals("orange+pear")) //选择梨和橘子
juice = new Juice(juiceWeight, newPear(), newOrange());
else if(choice.equals("apple+pear+orange") || choice.equals("apple+orange+pear") || choice.equals("pear+apple+orange") || choice.equals("pear+orange+apple") || choice.equals("orange+apple+pear") || choice.equals("orange+pear+apple")) //选择三种水果
juice = new Juice(juiceWeight, newApple(), newPear(), newOrange());
else{ //其他输入,无效
System.out.println("Invalid selection");
juice = new Juice(0);
isSelectionValid = false;
}
if(isSelectionValid){ //选择有效
System.out.println("\nAbout the juice:");
System.out.println("Total juice amount = " + juice.getTotalWeight());
System.out.println("Contained pure water = " + juice.getWeight());
System.out.println("Contained sugar = " + juice.getSugarAmount());
System.out.println("Contained vitamin =" + juice.getVitaminAmount());
System.out.println("Sugar rate = " + juice.getSugarRate());
System.out.println("Vitamin rate = " + juice.getVitaminRate());
if(stdSugarRate.compareTo(juice.getSugarRate()) <= 0) //果汁含糖率符合标准
System.out.println("Sugar rate is qualified");
else //果汁含糖率不符合标准
System.out.println("Sugar rate is not qualified");
if(stdVitaminRate.compareTo(juice.getVitaminRate()) <= 0) //果汁含维生素量符合标准
System.out.println("Vitamin rate is qualified");
else //果汁含维生素率不符合标准
System.out.println("Vitamin rate is not qualified");
}
}
catch(IOException e){
System.out.println(e);
}
}
//构造Apple类对象并返回
private static Apple newApple() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Apple weight = ");
return new Apple(Double.valueOf(br.readLine()));
}
//构造Pear类对象并返回
private static Pear newPear() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Pear weight = ");
return new Pear(Double.valueOf(br.readLine()));
}
//构造Orange类对象并返回
private static Orange newOrange() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Orange weight = ");
return new Orange(Double.valueOf(br.readLine()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -