大连理工大学小学期java上机猫狗选民工厂模式汽车管理

本文探讨了一个选民投票系统与宠物护理应用,展示了信息技术如何促进民主参与和宠物健康管理。Voter类实现了投票功能,而Pet类及其子类展示了宠物的互动和健康状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

选民:

package work;

class Voter{
   private
   int id;
   public
   static int count=0;
   static int wn=0;
   static int ln=0;
   static int zn=0;
   Voter(int i){
	   this.id=i;
   }
    static int Wn() {
		return wn;
	   
   }
    static int Ln() {
		return ln;
	   
   }
    static int Zn() {
		return zn;
	   
   }
   void Vote(String name_) {
	  if(count<100) {
		  System.out.println("您已投票给"+name_+"\n");
		  count++;
		  switch(name_) {
		  case "王五" : wn++;break;
		  case "李四" : ln++;break;
		  case "张三" : zn++;break;
		  }
		
	  }else System.out.println("投票结束\n");
   }
	
}


public class work1 {

	 public static void main(String[] args) {
	       Voter[] v=new Voter[200];
	        //创建选民,并投票
	        for (int i = 0; i <200; i++) {
	             v[i] =new Voter(i);
	            
	        }
	        for (int i = 0; i <102; i=i+3) {
	             v[i].Vote("王五");
	             v[i+1].Vote("李四");
	             v[i+2].Vote("张三");
	        }
	        System.out.println("投票结束\n王五票数:"+Voter.Wn()+" 李四票数"+Voter.Ln()+" 张三票数:"+Voter.Zn());

	    }



}

 

猫狗:

package p2;

class Pet{
	public
	String name;
	int health_value;
	int love_value;
	
	void hostpital() {
		health_value++;
		System.out.println("健康度增加了!\n");
	}
	Pet(String name_){
		this.name=name_;
		health_value=100;
		love_value=10;
		
	}
}
class Dog extends Pet{

	private
	String sort;
	public
	void play() {
		System.out.println(name+"的亲密度度是"+love_value+"\n");
		love_value++;
		System.out.println(name+"的亲密度增加了!\n");
	}

	Dog(String name_ ,String sort_) {
		super(name_);
		sort=sort_;
	}
	void show() {
		System.out.println("name="+name+" sort="+sort+" health_value="+health_value+"  love_value="+love_value+"\n");

	}
}
class Cat extends Pet{

	private
	String sex;
	public
	void touch() {
		System.out.println(name+"的亲密度度是"+love_value+"\n");
		love_value++;
		System.out.println(name+"的亲密度增加了!\n");
	}

	Cat(String name_,String sex_) {
		super(name_);
		sex=sex_;
	}
	void show() {
		System.out.println("name="+name+" sex="+sex+" health_value="+health_value+"  love_value="+love_value+"\n");


	}
}
public class t2 {

	public static void main(String[] args) {
		Pet d1 = new Dog("富贵","金毛");
		Pet c1 = new Cat("喵喵","母");
	if(d1.love_value<20&&d1 instanceof Dog) {	
    
			((Dog) d1).play();
			
		
	}
	if(c1.love_value<20&&c1 instanceof Cat) {	
	    
	     ((Cat) c1).touch();
	
    }
	((Dog) d1).show();
	((Cat) c1).show();
	
	
}

	


}

汽车管理:BuyMycomputer类放入service包

package service;
import java.util.Scanner;

import model.Computer;
import model.Pc;
/**
 * 汽车租赁管理类
 */
public class BuyMyComputer {
	public static void main(String[]args){
		Scanner input=new Scanner(System.in);
		CompOperation motoMgr =new CompOperation();
		motoMgr.init();
		Computer moto=null;//汽车

		System.out.println("1、查看电脑信息 \t2、购买电脑信息");		
				
		int pcType=input.nextInt();
		String Brand="";
		String Hardisk="";
	    String Ram="";
		if(pcType==1){
			System.out.print("序号     品牌   类型     内存   硬盘   显示器     CPU   电池    机箱    价格(元)\n");	
			System.out.print(" 1       DELL  台式机    8G     1T     14寸      I7            立式    6000 \n");	
			System.out.print(" 2       联想   笔记本   4G     500G    10寸     I5    9芯             4500 \n");
			int choose2=input.nextInt();
			if(choose2==1){
				Brand="DELL";
				Hardisk="1T";
				Ram="8G";
				
			}else if(choose2==2){
				Brand="联想";
				Hardisk="500G";
				Ram="4G";
				
			
			}
		}
		moto=motoMgr.Purchase(Brand,Ram,Hardisk);//获得 
		
		System.out.print("请输入支付方式: 1. 完整支付 2.分期支付\n");
		int money=moto.getPrice();
		int choose5=input.nextInt();
		if(choose5==1) {
			System.out.print(" 你选择了完整支付,价格为:"+money+"元,可以选择赠品(1、鼠标  2、U盘)\n");
			int choose6=input.nextInt();
			if(choose6==1){
				System.out.print("恭喜你购买了联想笔记本电脑,价格为"+money+"元,赠品为鼠标");
			}else if(choose6==2){
				System.out.print("恭喜你购买了联想笔记本电脑,价格为"+money+"元,赠品为键盘");
			}
				
		}else if(choose5==2) {
			System.out.print(" 你选择了分期支付,价格为:"+money+"元,可以选择分期期数(1、12期  2、24期)\n");
			int choose7=input.nextInt();
			if(choose7==1){
				System.out.print("恭喜你购买了DELL台式电脑,价格为"+money+"元,分期为12期");
			}else if(choose7==2){
				System.out.print("恭喜你购买了DELL台式电脑,价格为"+money+"元,分期为24期");
			}
		
		}
		
		
	}
}

ComOperation类放入service包

package service;


import model.Laptop;

import model.Pc;
import model.Computer;
import java.util.*;

/**
 * 汽车业务
 */
public class CompOperation {	
	public Computer computers[] = new Computer[2];
	public CompOperation() {
		
	}
	public void init(){
		computers[0] = new Laptop("DELL", "8G", "1T","14寸","I7",6000,"立式");
		computers[1] = new Pc("联想", "4G", "500G","10寸","I5",4500,"9芯");
	}
	/**
	 * 租赁汽车 
	 * @return 汽车
	 */
	public Computer Purchase(String b,String r,String h){
		Computer computer = new Computer();
		for (Computer mycomputer : computers) {
			if(mycomputer instanceof Laptop){
				Laptop laptop=(Laptop)mycomputer;
				if(laptop.getBrand().equals(b)&&laptop.getRam().equals(r)){
					computer=laptop;
					break;
				}
			}else{
				Pc pc=(Pc)mycomputer;
				if(pc.getBrand().equals(b)&&pc.getHardisk().equals(h)){
					computer=pc;
					break;
				}
			}
		}
		return computer;//返回一个汽车对象
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值