Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PhoneCall;
- class GSM {
- private String model; // model of the phone;
- private boolean hasSimCard; // does the phone have SimCard;
- private String simMobileNumber; // the phone number IF it has SimCard;
- private int outgoingCallsDuration; // the sum of all outgoing calls (in minutes);
- private Call lastIncomingCall;
- private Call lastOutgoingCall;
- GSM(String model) {
- this.model = model;
- }
- public String getModel() {
- return this.model;
- }
- public String getSimMobileNumber() {
- return this.simMobileNumber;
- }
- public Call getLastIncomingCall() {
- return this.lastIncomingCall;
- }
- public Call getLastOutgoingCall() {
- return this.lastOutgoingCall;
- }
- public boolean hasSimCard () {
- return this.hasSimCard;
- }
- public int getOutgoingCallsDuration() {
- return outgoingCallsDuration;
- }
- public double getSumForCall() {
- return (this.outgoingCallsDuration * Call.getPriceForAMinute());
- }
- void insertSimCard(String mobileNumber) {
- if (!this.hasSimCard) {
- if (mobileNumber.length() != 10) {
- System.out.println("Invalid number. Try again:");
- return;
- } else if ((mobileNumber.charAt(0) != '0') || (mobileNumber.charAt(1) != '8')) {
- System.out.println("Invalid number. Try again:");
- return;
- }
- for (int i = 2; i < mobileNumber.length(); i++) {
- if (mobileNumber.charAt(i) < '0' || mobileNumber.charAt(i) > '9') {
- System.out.println("Invalid number. Try again:");
- return;
- }
- }
- this.hasSimCard = true;
- this.simMobileNumber = mobileNumber;
- } else {
- System.out.println("Sim Card has been already inserted. You have to remove it first.");
- }
- }
- void removeSimCard() {
- this.hasSimCard = false;
- this.simMobileNumber = null;
- }
- void call(GSM mobile, int duration) {
- if (this.hasSimCard) {
- if (mobile.simMobileNumber.equals(this.simMobileNumber)) {
- System.out.println("Can not call to the same number.");
- return;
- }
- if (duration <= 0) {
- System.out.println("Invalid duration");
- return;
- }
- if (!mobile.hasSimCard) {
- System.out.println("Unidentified number.");
- return;
- }
- this.lastOutgoingCall = new Call(this.simMobileNumber, mobile.simMobileNumber, duration); ;
- mobile.lastIncomingCall = new Call(mobile.simMobileNumber, this.simMobileNumber, duration);
- this.outgoingCallsDuration += duration;
- } else {
- System.out.println("No SIM Card inserted.");
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement