按类图模拟把大象(Elephant)装入冰箱(Refriger),按输入实现把动物Animal【大象Elephant或狮子Lion】装进家用电器Electric【冰箱Refriger或微波炉Microwave】
输入格式:
Put the 动物类名 动物名字 into the 电器类名 电器名字。
例如:
Put the Lion Jack into the Microvate Robert。
输出格式:
create 电器类名 电器名字
create 动物类名 动物名字
电器类名 电器名字 is open 动物类名 动物名字 is in 电器类名 电器名字
电器类名 电器名字 is close
输入样例:
在这里给出一组输入。例如:
Put the Lion Jack into the Microwave Robert
输出样例:
在这里给出相应的输出。例如:
create Microwave Robert
create Lion Jack
Microwave Robert is open Lion
Jack is in Microwave Robert
Microwave Robert is close
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String tmp=input.nextLine();
String regex="Put\\s+the\\s+(\\w+)\\s+(\\w+)\\s+into\\s+the\\s+(\\w+)\\s+(\\w+)";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(tmp);
if(matcher.find())
{
String animalclass=matcher.group(1);//动物类名
String animalName=matcher.group(2);//动物名字
String electricclass=matcher.group(3);//电器类名
String electricName = matcher.group(4);//电器名字
Electric electric;
if(electricclass.equals("Refriger"))
{
electric = new Refriger(electricName);
}
else
electric=new Microwave(electricName);
Animal animal;
if(animalclass.equals("Elephant"))
{
animal = new Elephant(animalName);
}
else
animal=new Lion(animalName);
electric.openDoor();
animal.enterElectric(electric);
electric.closeDoor();
}
}
}
class Refriger extends Electric {
Refriger(String electricName){
super(electricName);
super.setElectricclass("Refriger");
System.out.println("create Refriger "+super.getElectricName());
}
Refriger(){
}
}
class Microwave extends Electric {
Microwave(String electricName){
super(electricName);
super.setElectricclass("Microwave");
System.out.println("create Microwave "+super.getElectricName());
}
Microwave(){
}
}
class Electric{
private String electricName;
private String electricclass;
Electric(String electricName)
{
this.electricName=electricName;
}
Electric(){
}
public String getElectricName() {
return electricName;
}
public void setElectricclass(String electricclass) {
this.electricclass = electricclass;
}
public String getElectricclass() {
return electricclass;
}
public void openDoor()
{
System.out.println(electricclass+" "+electricName+" is open");
}
public void closeDoor()
{
System.out.println(electricclass+" "+electricName+" is close");
}
}
class Animal{
Electric electric;
private String animalName;
private String animalclass;
Animal(){
}
Animal(String animalName){
this.animalName=animalName;
}
public void setAnimalclass(String animalclass) {
this.animalclass = animalclass;
}
public String getAnimalclass() {
return animalclass;
}
public String getAnimalName() {
return animalName;
}
public void enterElectric(Electric electric){
System.out.println(animalclass+" "+animalName+" is in "+electric.getElectricclass()+" "+electric.getElectricName());
}
}
class Elephant extends Animal{
Elephant(String animalName){
super(animalName);
super.setAnimalclass("Elephant");
System.out.println("create Elephant "+super.getAnimalName());
}
Elephant(){
}
}
class Lion extends Animal{
Lion(String animalName){
super(animalName);
super.setAnimalclass("Lion");
System.out.println("create Lion "+super.getAnimalName());
}
Lion(){
}
}