Advertisement
eranseg

PhoneBook HW Assignment

Sep 11th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. //-------------------------- Phone -------------------------------
  2.  
  3. package Cls;
  4.  
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.PrintWriter;
  8. import java.util.Scanner;
  9.  
  10. public class Phone {
  11.  
  12.     private String phoneNumber;
  13.     private String owner;
  14.     private int lastPayment;
  15.     private final int LAST_PAYMENT = -1;
  16.  
  17.     public Phone(String phoneNumber, String owner) {
  18.         this.phoneNumber = phoneNumber;
  19.         this.owner = owner;
  20.         this.lastPayment = LAST_PAYMENT;
  21.     }
  22.  
  23.     public Phone(Scanner sc) throws FileNotFoundException {
  24.         parsePhone(sc.nextLine());
  25.     }
  26.  
  27.     public Phone(String fileName) throws FileNotFoundException {
  28.         File file = new File(fileName);
  29.         Scanner sc = new Scanner(file);
  30.         this.phoneNumber = sc.next();
  31.         this.owner = sc.next();
  32.         sc.close();
  33.     }
  34.  
  35.     public String getOwner() {
  36.         return owner;
  37.     }
  38.  
  39.     public String getPhoneNumber() {
  40.         return phoneNumber;
  41.     }
  42.  
  43.     public void setPhoneNumber(String phoneNumber) {
  44.         this.phoneNumber = phoneNumber;
  45.     }
  46.  
  47.     public int getLastPayment() {
  48.         return lastPayment;
  49.     }
  50.  
  51.     public void setLastPayment(int lastPayment) {
  52.         this.lastPayment = lastPayment;
  53.     }
  54.  
  55.     public void save(PrintWriter pw) throws FileNotFoundException {
  56.         pw.println(this.toString());
  57.     }
  58.  
  59.     public void save(String fileName) throws  FileNotFoundException {
  60.         File file = new File(fileName);
  61.         PrintWriter printWriter = new PrintWriter(file);
  62.         printWriter.println(this.toString());
  63.         printWriter.close();
  64.     }
  65.  
  66.     @Override
  67.     public String toString() {
  68.         return "<" + this.phoneNumber + ">, <" + this.owner + ">, <" + this.lastPayment + ">";
  69.     }
  70.  
  71.     // A utility method to parse the phone details from a string to the instance arguments
  72.     private void parsePhone(String str) {
  73.         this.phoneNumber = str.substring(str.indexOf('<')+1, str.indexOf('>'));
  74.         this.owner = str.substring(str.indexOf('>')+4, str.lastIndexOf('<')-3);
  75.         this.lastPayment = Integer.parseInt(str.substring(str.lastIndexOf('<')+1, str.lastIndexOf('>')));
  76.     }
  77. }
  78.  
  79. //----------------------- PhoneBook --------------------------
  80.  
  81. package Cls;
  82.  
  83. import java.io.File;
  84. import java.io.FileNotFoundException;
  85. import java.io.PrintWriter;
  86. import java.util.Scanner;
  87.  
  88. public class PhoneBook {
  89.  
  90.     //Instance variables
  91.     private String owner;
  92.     private Phone[] phones;
  93.     private int numOfPhones;
  94.     private int counter;
  95.  
  96.     //Constructors
  97.  
  98.     public PhoneBook(String owner, int numOfPhones) {
  99.         this.owner = owner;
  100.         this.numOfPhones = numOfPhones;
  101.         counter = 0;
  102.         phones = new Phone[numOfPhones];
  103.     }
  104.  
  105.     public PhoneBook(String fileName) throws FileNotFoundException {
  106.         File file = new File(fileName);
  107.         Scanner sc = new Scanner(file);
  108.         this.owner = sc.nextLine();
  109.         this.numOfPhones = sc.nextInt();
  110.         phones = new Phone[numOfPhones];
  111.         this.counter = sc.nextInt();
  112.         sc.nextLine();
  113.         for(int i = 0; i < counter; i++) {
  114.             phones[i] = new Phone(sc);
  115.         }
  116.         sc.close();
  117.     }
  118.  
  119.     // Instance methods
  120.  
  121.     public void add(Phone phone) {
  122.         phones[counter++] = phone;
  123.     }
  124.  
  125.     public void save(String filename) throws FileNotFoundException {
  126.         File file = new File(filename);
  127.         PrintWriter printWriter = new PrintWriter(file);
  128.         if(counter > 0) {
  129.             printWriter.println(this.owner);
  130.             printWriter.println(this.numOfPhones);
  131.             printWriter.println(this.counter);
  132.             for(int i = 0; i < counter; i++) {
  133.                 phones[i].save(printWriter);
  134.             }
  135.         }
  136.         printWriter.close();
  137.     }
  138.  
  139.     public void printPhonebook() {
  140.         System.out.println("Owner --> " + this.owner);
  141.         System.out.println("Max number of phone numbers --> " + this.numOfPhones);
  142.         System.out.println("Current number of phones --> " + this.counter);
  143.         System.out.println("\nPhones details:\n------------");
  144.         for(int i = 0; i < this.counter; i++) {
  145.             System.out.println(phones[i].toString());
  146.         }
  147.     }
  148. }
  149.  
  150. //----------------------------- Tester for both parts (convert the part you want to ignore to notes) ----
  151.  
  152. package Tester;
  153.  
  154. import Cls.Phone;
  155. import Cls.PhoneBook;
  156.  
  157. import java.io.File;
  158. import java.io.FileNotFoundException;
  159. import java.io.IOException;
  160. import java.io.PrintWriter;
  161. import java.util.Scanner;
  162.  
  163. public class Tester {
  164.  
  165.     public static void main(String[] args) {
  166.         //Test part 1
  167.         /*
  168.         Phone phone1 = new Phone("055-1234567", "Eran");
  169.         Phone phone2 = new Phone("077-7654321", "Ofri");
  170.         Phone phone3 = new Phone("052-5555555", "Noa");
  171.         String fileName = "PhoneBook.phones";
  172.         try {
  173.             save(phone1, phone2, phone3, fileName);
  174.             // Assigning phones 1-3 to phones 4-6
  175.             File file = new File(fileName);
  176.             Scanner sc = new Scanner(file);
  177.             Phone ph4 = new Phone(sc);
  178.             Phone ph5 = new Phone(sc);
  179.             Phone ph6 = new Phone(sc);
  180.             //Check phones 4-6 initialization
  181.             System.out.println(ph4.toString());
  182.             System.out.println(ph5.toString());
  183.             System.out.println(ph6.toString());
  184.             sc.close();
  185.         } catch(FileNotFoundException fnfe) {
  186.             System.out.println(fnfe.toString());
  187.         }
  188.     }
  189.  
  190.     private static void save(Phone ph1, Phone ph2, Phone ph3, String fName) throws FileNotFoundException {
  191.         File file = new File(fName);
  192.         PrintWriter printWriter = new PrintWriter(file);
  193.         ph1.save(printWriter);
  194.         ph2.save(printWriter);
  195.         ph3.save(printWriter);
  196.         printWriter.close();
  197.     }
  198.     */
  199.         //test part 2
  200.         try {
  201.             String fileName = "phonebook.phones";
  202.             //section a
  203.             PhoneBook phoneBook = new PhoneBook("Eran", 5);
  204.             //section b
  205.             phoneBook.add(new Phone("057-1234567", "Israel"));
  206.             phoneBook.add(new Phone("056-1111111", "Shalom"));
  207.             phoneBook.add(new Phone("054-8778787", "Yossi"));
  208.             //section c
  209.             phoneBook.save(fileName);
  210.             //section d
  211.             PhoneBook pb = new PhoneBook(fileName);
  212.             //section e
  213.             checkCorrectness(pb);
  214.  
  215.         } catch (FileNotFoundException fnfe) {
  216.             System.out.println(fnfe.toString());
  217.         }
  218.     }
  219.  
  220.     private static void checkCorrectness(PhoneBook phBook) {
  221.         phBook.printPhonebook();
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement