public class PhoneCard {
private final int id // 卡号
private final int password //密码
private double cash //剩余金额
private double cashPerMin = 0.2// 每分钟收费
private Scanner sc = new Scanner(System.in)
public PhoneCard(int id,int password,double cash) {
this.id = id
this.password = password
this.cash = cash
}
/**
* 拨号计费
* @param time 拨号时间 单位分钟
* 返回剩余金额
*/
public double callSomeone(double time) {
System.out.println("请输入卡号和密码,用空格隔开")
int id = sc.nextInt()
int pw = sc.nextInt()
if(this.id == id && this.password == pw) {
this.cash = this.cash - (double)time * cashPerMin
return cash
} else {
System.out.println("密码错误")
return -1
}
}
}
public class TestPhoneCard {
public static void main(String[] args) {
PhoneCard pc = new PhoneCard(1, 1, 100) //初始化,卡号,密码,余额
double money = pc.callSomeone(10) //拨打10分钟
System.out.println("余额为" + money + "元")
money = pc.callSomeone(90) //继续拨打
System.out.println("余额为" + money + "元")
}
}
不知道为什么没人做