Advertisement
Cnvmendoza

Module 1.3

Sep 1st, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.93 KB | None | 0 0
  1. //MobilePhone.java
  2. package phone;
  3.  
  4. /*
  5. For Runner.java
  6.  */
  7.  
  8. public class MobilePhone {
  9.     //Attributes
  10.     private float load;
  11.     private char network;
  12.     private float ratePerText;
  13.     private float ratePerCall;
  14.     //Error Checker
  15.     boolean negativeError = false;
  16.     //Constructor
  17.     public MobilePhone(float init_load, char init_network) {
  18.         load = init_load;
  19.         network = init_network;
  20.     }
  21.     //Methods
  22.     public float checkBalance() {
  23.         return load;
  24.     }
  25.     public void call(int minutes, char networkReceiver) {
  26.         //negativeError
  27.         negativeError = false;
  28.         //ratePerCall value
  29.         switch(network) {
  30.             case 's': ratePerCall = 3; break;
  31.             case 't': ratePerCall = 5; break;
  32.             case 'g': ratePerCall = 7; break;
  33.             default: break;
  34.         }
  35.         //Calculate
  36.         if (network == networkReceiver) {
  37.             //Cost
  38.             float cost = load;
  39.             cost -= minutes * ratePerCall;
  40.             //Find negative
  41.             if (cost < 0) {
  42.                 System.out.println("The load value cannot be negative.");
  43.                 negativeError = true;
  44.             }
  45.             else load  -= minutes * ratePerCall;
  46.         }
  47.         else {
  48.             //Cost
  49.             float cost = load;
  50.             cost -= minutes * (ratePerCall * 2);
  51.             //Find negative
  52.             if (cost < 0) {
  53.                 System.out.println("The load value cannot be negative.");
  54.                 negativeError = true;
  55.             }
  56.             else load -= minutes * (ratePerCall * 2);
  57.         }
  58.     }
  59.     public void text(int length, char networkReceiver) {
  60.         //negativeError
  61.         negativeError = false;
  62.         //ratePerText value
  63.         ratePerText = 1; //value is always 1
  64.         //price increases every 160 char
  65.         //Calculate
  66.         if (network == networkReceiver) {
  67.             //Cost
  68.             float cost = load;
  69.             cost -= (float) length / 160 * ratePerText;
  70.             //Find negative
  71.             if (cost < 0) {
  72.                 System.out.println("The load value cannot be negative.");
  73.                 negativeError = true;
  74.             }
  75.             else load -= (float) length / 160 * ratePerText;
  76.         }
  77.         else {
  78.             //Cost
  79.             float cost = load;
  80.             cost -= (float) length / 160 * (ratePerText * 2);
  81.             //Find negative
  82.             if (cost < 0) {
  83.                 System.out.println("The load value cannot be negative.");
  84.                 negativeError = true;
  85.             }
  86.             else load -= (float) length / 160 * (ratePerText * 2);
  87.         }
  88.     }
  89.     public void reload(float amount) {
  90.         if (amount < 0) System.out.println("The amount must not be a negative value.");
  91.         else load += amount;
  92.     }
  93.     public char getNetwork() {
  94.         return network;
  95.     }
  96.     public void setNetwork(char network) {
  97.         this.network = network;
  98.     }
  99.     public boolean getNegativeError() {
  100.         return negativeError;
  101.     }
  102. }
  103.  
  104. //Runner.java
  105. package phone;
  106.  
  107. /*
  108.  
  109.  */
  110.  
  111. import java.util.Scanner;
  112. import java.util.Random; //generate random receiver network
  113.  
  114. public class Runner {
  115.     public static void main(String[] args) {
  116.         boolean exit = false;
  117.         int option = 0;
  118.         char network = 's';
  119.         //Instance
  120.         MobilePhone phone = new MobilePhone((float) 0.0,'s');
  121.         //Start
  122.         do {
  123.             //generate random receiver network
  124.             int[] networkArray = {1, 2, 3};
  125.             int networkRandom = new Random().nextInt(networkArray.length);
  126.             switch(networkArray[networkRandom]) {
  127.                 case 1: network = 's'; break;
  128.                 case 2: network = 't'; break;
  129.                 case 3: network = 'g'; break;
  130.             }
  131.             //Display Text
  132.             System.out.println("1. Call");
  133.             System.out.println("2. Text");
  134.             System.out.println("3. Check Load");
  135.             System.out.println("4. Reload");
  136.             System.out.println("5. Network");
  137.             System.out.println("6. Exit");
  138.             System.out.printf("Input: ");
  139.             Scanner optionScanner = new Scanner(System.in);
  140.             option = optionScanner.nextInt();
  141.             //Action
  142.             switch(option) {
  143.                 case 1: //Call
  144.                     boolean exitCall = false;
  145.                     do {
  146.                        
  147.                         //Input
  148.                         System.out.printf("Phone Number Input: ");
  149.                         Scanner phoneNumber = new Scanner(System.in);
  150.                         String lineCall = phoneNumber.next();
  151.                         //Output Check
  152.                         if(lineCall.length() != 10) System.out.println("Please input 10 digits.");
  153.                         else {
  154.                             System.out.println("Calling " + lineCall + " with network " + network);
  155.                             System.out.printf("Minutes Called Input: ");
  156.                             Scanner minutesCalled = new Scanner(System.in);
  157.                             int minutesCall = minutesCalled.nextInt();
  158.                             phone.call(minutesCall, network);
  159.                             if(phone.getNegativeError()) break;
  160.                             else {
  161.                                 System.out.println("Called for " + minutesCall + " minutes at the rate of network " + phone.getNetwork() + ".");
  162.                                 System.out.printf("New Balance: Php %.2f\n", phone.checkBalance());
  163.                             }
  164.                                                            
  165.                             exitCall = !exitCall;
  166.                         }
  167.                     }while(!exitCall);
  168.                     break;
  169.                 case 2: //Text
  170.                     boolean exitText = false;
  171.                     do {
  172.                         //Input
  173.                         System.out.printf("Phone Number Input: ");
  174.                         Scanner phoneNumber = new Scanner(System.in);
  175.                         String lineText = phoneNumber.next();
  176.                         //Output Check
  177.                         if(lineText.length() != 10) System.out.println("Please input 10 digits.");
  178.                         else {
  179.                             System.out.println("Texting " + lineText + ".");
  180.                             System.out.printf("Message Input: ");
  181.                             Scanner textMessaged = new Scanner(System.in);
  182.                             String textMessage = textMessaged.nextLine();
  183.                             phone.text(textMessage.length(), network);
  184.                             if(phone.getNegativeError()) break;
  185.                             else {
  186.                                 System.out.println("Message successfully sent!");
  187.                                 System.out.printf("New Balance: Php %.2f\n", phone.checkBalance());
  188.                             }
  189.                             exitText = !exitText;
  190.                         }
  191.                     }while(!exitText);
  192.                     break;
  193.                 case 3: //Check Balance
  194.                     System.out.printf("Load: Php %.2f\n", phone.checkBalance());
  195.                     break;
  196.                 case 4: //Reload
  197.                     System.out.printf("Reload Input: ");
  198.                     Scanner amountAdded = new Scanner(System.in);
  199.                     int reloadValue = amountAdded.nextInt();
  200.                     phone.reload(reloadValue);
  201.                     System.out.printf("New Balance: Php %.2f\n", phone.checkBalance());
  202.                     break;
  203.                 case 5: //Network
  204.                     //Display
  205.                     System.out.println("Curent Network: " + phone.getNetwork());
  206.                     System.out.println("Change Network");
  207.                     System.out.println("1. Yes");
  208.                     System.out.println("2. No");
  209.                     System.out.printf("Input: ");
  210.                     //Input
  211.                     Scanner networkOption = new Scanner(System.in);
  212.                     int choice = networkOption.nextInt();
  213.                     if(choice == 1) {//Yes
  214.                         //Display
  215.                         System.out.println("Available networks: s, t, g");
  216.                         System.out.println("Network Input: ");
  217.                         //Input
  218.                         char tempNetwork = phone.getNetwork();
  219.                         Scanner networkAvailableOption = new Scanner(System.in);
  220.                         phone.setNetwork(networkAvailableOption.next().charAt(0));
  221.                         //Output
  222.                         if(phone.getNetwork() == 's') System.out.println("Changed to network " + phone.getNetwork());
  223.                         else if (phone.getNetwork() == 't') System.out.println("Changed to network " + phone.getNetwork());
  224.                         else if (phone.getNetwork() == 'g') System.out.println("Changed to network " + phone.getNetwork());
  225.                         else {
  226.                             phone.setNetwork(tempNetwork);
  227.                             System.out.println("Network remained as network " + phone.getNetwork() + ".");
  228.                         }
  229.                         break;
  230.                     }
  231.                 case 6: exit = !exit;
  232.             }
  233.         } while(!exit);
  234.     }
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement