java - 我的 Java 作业需要一些帮助,这是一个用于管理电话的简单程序
问题描述
A1。创建一个名为 PhoneCall 的抽象类,其中包括一个用于电话号码的字符串字段和一个用于通话价格的双字段。
A2。此外,包括一个需要电话号码参数并将价格设置为 0.0 的构造函数。包括价格的设置方法。还包括三个抽象的 get 方法——一个返回电话号码,另一个返回通话价格,第三个显示有关通话的信息。
A3。创建 PhoneCall 的两个子类:IncomingPhoneCall 和 OutgoingPhoneCall。
- IncomingPhoneCall 构造函数将其电话号码参数传递给其父构造函数,并将通话价格设置为 0.02。显示电话信息的方法显示电话号码、费率和通话价格(与费率相同)。
-----OutgoingPhoneCall 类包括一个额外的字段,以分钟为单位保存呼叫时间。构造函数需要电话号码和时间。价格为每分钟0.04,显示方式显示通话详情,包括电话号码、每分钟费率、分钟数、总价。
---编写一个应用程序,演示您可以实例化和显示 IncomingPhoneCall 和 OutgoingPhoneCall 对象。将文件保存为 PhoneCall.java、IncomingPhoneCall.java、OutgoingPhoneCall.java 和 DemoPhoneCalls.java。
我只在下面做了:
public abstract class PhoneCall
{
private String phoneno;
private double price;
public PhoneCall(String phoneno, double price)
{
this.phoneno = phoneno;
this.price = price;
}
public String getphoneno()
{
return phoneno;
}
public double getprice()
{
return price = 0.0;
}
public static void main(String[] args) {
}
}
public class IncomingPhoneCall extends PhoneCall
{
public IncomingPhoneCall(String phoneno, double price) {
super(phoneno, price);
}
}
public class OutgoingPhoneCall extends PhoneCall
{
public OutgoingPhoneCall(String phoneno, double price) {
super(phoneno, price);
}
}
解决方案
abstract class PhoneCall
{
String phoneNumber;
double price;
PhoneCall(String phoneNumber)
{
this.phoneNumber = phoneNumber;
this.price = 0.0;
}
public String getPhoneNumber() {
return phoneNumber;
}
public double getPrice() {
return price;
}
public abstract void setPrice();
}
class IncomingPhoneCall extends PhoneCall {
final static double RATE=0.02;
IncomingPhoneCall(String phoneNumber){
super(phoneNumber);
setPrice();
}
public void setPrice() {
price = 0.02;
}
void info(){
System.out.println("Incoming phone call"+getPhoneNumber()+
" "+RATE+" per call.Total is $"+getPrice());
}
public String getPhoneNumber()
{
return phoneNumber;
}
public double getPrice()
{
return price;
}
}
class OutgoingPhoneCall extends PhoneCall {
final static double RATE = 0.04;
private int minutes;
OutgoingPhoneCall(String phoneNumber, int minutes){
super(phoneNumber);
this.minutes = minutes;
setPrice();;
}
public void setPrice() {
price = 0.04;
}
void info() {
System.out.println("Outgoing phone call " + getPhoneNumber() + " "
+ RATE + " per minute at " + minutes + " minutes. Total is $" + price*minutes);
}
public String getPhoneNumber()
{
return phoneNumber;
}
public double getPrice()
{
return price;
}
}
public class DemoPhoneCalls {
public static void main(String [] args) {
IncomingPhoneCall incomingPhoneCall=new IncomingPhoneCall("310-332-0908");
OutgoingPhoneCall outgoingPhoneCall=new OutgoingPhoneCall("310-000-0102",20);
incomingPhoneCall.info();
outgoingPhoneCall.info();
}
}