Advertisement
Guest User

GSM

a guest
Jan 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package PhoneCall;
  2.  
  3. class GSM {
  4.  
  5.     private String model;        // model of the phone;
  6.     private boolean hasSimCard;  // does the phone have SimCard;
  7.     private String simMobileNumber; // the phone number IF it has SimCard;
  8.     private int outgoingCallsDuration; // the sum of all outgoing calls (in minutes);
  9.     private Call lastIncomingCall;
  10.     private Call lastOutgoingCall;
  11.    
  12.     GSM(String model) {
  13.         this.model = model;
  14.     }
  15.    
  16.     public String getModel() {
  17.         return this.model;
  18.     }
  19.    
  20.     public String getSimMobileNumber() {
  21.         return this.simMobileNumber;
  22.     }
  23.    
  24.     public Call getLastIncomingCall() {
  25.         return this.lastIncomingCall;
  26.     }
  27.    
  28.     public Call getLastOutgoingCall() {
  29.         return this.lastOutgoingCall;
  30.     }
  31.    
  32.     public boolean hasSimCard () {
  33.         return this.hasSimCard;
  34.     }
  35.    
  36.     public int getOutgoingCallsDuration() {
  37.         return outgoingCallsDuration;
  38.     }
  39.    
  40.     public double getSumForCall() {
  41.         return (this.outgoingCallsDuration * Call.getPriceForAMinute());
  42.     }
  43.    
  44.     void insertSimCard(String mobileNumber) {
  45.        
  46.         if (!this.hasSimCard) {
  47.            
  48.             if (mobileNumber.length() != 10) {
  49.                 System.out.println("Invalid number. Try again:");
  50.                 return;
  51.             } else if ((mobileNumber.charAt(0) != '0') || (mobileNumber.charAt(1) != '8')) {
  52.                 System.out.println("Invalid number. Try again:");
  53.                 return;
  54.             }
  55.            
  56.             for (int i = 2; i < mobileNumber.length(); i++) {
  57.                 if (mobileNumber.charAt(i) < '0' || mobileNumber.charAt(i) > '9') {
  58.                     System.out.println("Invalid number. Try again:");
  59.                      return;
  60.                 }
  61.             }
  62.            
  63.             this.hasSimCard = true;
  64.             this.simMobileNumber = mobileNumber;
  65.         } else {
  66.             System.out.println("Sim Card has been already inserted. You have to remove it first.");
  67.         }
  68.     }
  69.    
  70.     void removeSimCard() {
  71.        
  72.         this.hasSimCard = false;
  73.         this.simMobileNumber = null;
  74.     }
  75.    
  76.     void call(GSM mobile, int duration) {
  77.        
  78.         if (this.hasSimCard) {
  79.             if (mobile.simMobileNumber.equals(this.simMobileNumber)) {
  80.                 System.out.println("Can not call to the same number.");
  81.                 return;
  82.             }
  83.             if (duration <= 0) {
  84.                 System.out.println("Invalid duration");
  85.                 return;
  86.             }
  87.             if (!mobile.hasSimCard) {
  88.                 System.out.println("Unidentified number.");
  89.                 return;
  90.             }
  91.            
  92.             this.lastOutgoingCall = new Call(this.simMobileNumber, mobile.simMobileNumber, duration); ;
  93.             mobile.lastIncomingCall = new Call(mobile.simMobileNumber, this.simMobileNumber, duration);
  94.             this.outgoingCallsDuration += duration;
  95.         } else {
  96.             System.out.println("No SIM Card inserted.");
  97.             return;
  98.         }
  99.     }
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement