Java实现水电费计费项目

mac2024-11-07  2

先写这样,后面再看能改成弹窗样式的

PropertyFee.java

抽象父类

public abstract class PropertyFee { double unit_price;// unit_price为单价 abstract double cost(double usage); // 计费方法:usage为用量, }

WaterCharge.java

水费子类

public class WaterCharge extends PropertyFee {//水费 double cost(double usage) {//usage为用水量:立方米,unit_price为水价:元/立方米 if(usage>=0 && usage <=220) { unit_price=3.45; } else if(usage>220 && usage <=360) { unit_price=4.83; } else if(usage>=361) { unit_price=5.83; } return usage*unit_price; } }

ElectricityFees.java

电费子类

public class ElectricityFees extends PropertyFee {//电费 double cost(double usage) {//usage为用电量:度,unit_price为电价:元/度 if(usage>=0 && usage <=1200) { unit_price=0.52; } else if(usage>1200 && usage <2400) { unit_price=0.57; } else if(usage>=2400) { unit_price=0.82; } return usage*unit_price; } }

GasFee.java

气费子类

public class GasFee extends PropertyFee {// 气费 double cost(double usage) {//usage为用气量:立方米,unit_price为气体单价:元/立方米 if (usage >= 0 && usage <= 500) { unit_price = 2.26; } else if (usage >= 501 && usage <= 660) { unit_price = 2.60; } else if (usage >= 661) { unit_price = 3.12; } return usage * unit_price; } }

Client.java

用户主类

import java.util.Scanner; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入查询内容的代号:1.水费,2.气费,3.电费"); while (input.hasNext()) { int code_name = input.nextInt(); if (code_name == 1) { System.out.println("输入用水量(立方米)"); double water_use = input.nextDouble(); WaterCharge user = new WaterCharge(); System.out.println("需要缴纳的水费:" + user.cost(water_use) + "元"); } else if (code_name == 2) { System.out.println("输入用气量(立方米)"); double gas_use = input.nextDouble(); GasFee user = new GasFee(); System.out.println("需要缴纳的气费:" + user.cost(gas_use) + "元"); } else if (code_name == 3) { System.out.println("输入用电量(度)"); double electric_use = input.nextDouble(); ElectricityFees user = new ElectricityFees(); System.out.println("需要缴纳的电费:" + user.cost(electric_use) + "元"); } else { System.out.println("输入代号有误"); break; } } } }

运行结果:

最新回复(0)