a)问题描述:编写一个JAVA程序,用面向对象设计的方法编写一个电话卡的类。包括卡号、密码、余额、拨入号码等
b)基本要求:类的属性有卡号、密码、余额、拨入号码,电话卡的常用操作可以用连接电话方法、返回余额方法与通电话方法来实现。
c)方法功能描述:
构造方法(PhoneCard(卡号,密码,余额,拨入号码))可以完成属性值初始化赋值,并判断余额,余额为负就退出系统,请在构造方法中将初始时的连接置为false即表示没有连接。 卡号long
cardNumber;密码private int password,余额double
balance,拨入号码string connectNumber boolean connected
连接电话方法(performConnection(卡号,密码))可以完成检查卡号和密码,它是只有在卡号和密码相匹配时才连接 返回余额方法(getBalance())得到电话卡的余额 通电话方法(performDial())是模拟通过过程中,余额会不断减少,每调用此方法,电话卡的余额减少0。5元,打一次电话调用一次performDial()。
class PhoneCard
{
long can;
int ps;
double ba;
String con;
boolean connected=false;
PhoneCard(long cardNumber, int password,double balance,String
connectNumber)
{
can=cardNumber;
ps=password;
ba=balance;
con=connectNumber;
}
void performConnection(long cardNumber, int password)
{
if(can!=cardNumber)
System.out.println("号码不对");
else if(can==cardNumber&&ps!=password)
System.out.println("密码不对");
else if(can==cardNumber&&ps==password)
{
this.getBalance();
connected=true;
this.performDial();
}
}
public double getBalance()
{
if(ba<=0)
{
System.out.println("余额不足!");
System.exit(0);
}
else
return ba;
return ba;
}
double performDial()
{
System.out.println("本次通话将扣除0.5元");
return ba-0.5;
}
}
public class call1
{
public static void main(String[] args)
{
PhoneCard phonecard=new
PhoneCard(88888888,123456,100.00,"110");
phonecard.performConnection(11111111,45);
phonecard.performConnection(88888888,45);
phonecard.performConnection(88888888,123456);
phonecard.getBalance();
}
};