Goodiny777

PhoneBook

Mar 7th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. package fileWork.PhoneBook;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.util.Scanner;
  7.  
  8. public class Phone {
  9.     public String ownerName;
  10.     public String lineNumber;
  11.     public int lastPay;
  12.     private final int startPay = -1;
  13.  
  14.     public Phone(String lineNumber, String ownerName) {
  15.         this.ownerName = ownerName;
  16.         this.lineNumber = lineNumber;
  17.         this.lastPay = startPay;
  18.     }
  19.  
  20.     public Phone(Scanner scan) {
  21.         this.lineNumber = scan.next();
  22.         this.ownerName = scan.next();
  23.         this.lastPay = scan.nextInt();
  24.     }
  25.  
  26.     public Phone(String fileName) throws FileNotFoundException {
  27.         Scanner scan = new Scanner(new File(fileName));
  28.         this.lineNumber = scan.next();
  29.         this.ownerName = scan.next();
  30.         this.lastPay = scan.nextInt();
  31.     }
  32.  
  33.     public void save(PrintWriter pw) throws FileNotFoundException {
  34.         pw.print(this.lineNumber + " " + this.ownerName + " " + this.lastPay);
  35.         pw.println();
  36.     }
  37.  
  38.     public void save(String fileName) throws FileNotFoundException {
  39.         PrintWriter pw = new PrintWriter(new File(fileName));
  40.         pw.print(this.lineNumber + " " + this.ownerName + " " + this.lastPay);
  41.         pw.close();
  42.     }
  43.  
  44.     @Override
  45.     public String toString() {
  46.         return "LineNumber=" + lineNumber + '\n' +
  47.                 "ownerName=" + ownerName + '\n' +
  48.                 "lastPay=" + lastPay + '\n';
  49.     }
  50. }
  51.  
  52. ================================================================================================
  53.  
  54. package fileWork.PhoneBook;
  55.  
  56. import java.io.File;
  57. import java.io.FileNotFoundException;
  58. import java.io.PrintWriter;
  59. import java.util.Arrays;
  60. import java.util.Scanner;
  61.  
  62. public class PhoneBook {
  63.     public String ownerName;
  64.     public Phone[] numbers;
  65.     public int numsAmount;
  66.  
  67.     public PhoneBook(String ownerName, int maxAmount) {
  68.         this.ownerName = ownerName;
  69.         this.numbers = new Phone[maxAmount];
  70.         numsAmount = 0;
  71.     }
  72.  
  73.     public PhoneBook(String fileName) throws FileNotFoundException {
  74.         Scanner scan = new Scanner(new File(fileName));
  75.         if (scan.hasNextLine()) {
  76.             this.ownerName = scan.nextLine();
  77.         }
  78.         if (scan.hasNextLine()) {
  79.             this.numbers = new Phone[Integer.parseInt(scan.nextLine())];
  80.         }
  81.         if (scan.hasNextLine()) {
  82.             this.numsAmount = Integer.parseInt(scan.nextLine());
  83.         }
  84.         for (int i = 0; i < numsAmount; i += 1) {
  85.             numbers[i] = new Phone(scan);
  86.             //To make it just for
  87.             if (scan.hasNextLine()) {
  88.                 scan.nextLine();
  89.             }
  90.         }
  91.     }
  92.  
  93.     public void add(Phone phone) {
  94.         this.numbers[numsAmount] = phone;
  95.         numsAmount += 1;
  96.     }
  97.  
  98.     public void save(String fileName) throws FileNotFoundException {
  99.         PrintWriter pw = new PrintWriter(new File(fileName));
  100.         pw.println(ownerName);
  101.         pw.println(this.numbers.length);
  102.         pw.println(this.numsAmount);
  103.         for (int i = 0; i < this.numsAmount; i += 1) {
  104.             numbers[i].save(pw);
  105.         }
  106.         pw.close();
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         String res = "PhoneBook\n" +
  112.                 "ownerName=" + ownerName + '\n' +
  113.                 "numsAmount=" + numsAmount + '\n' +
  114.                 "numbers: \n";
  115.         for (int i=0; i<numbers.length; i+=1) {
  116.             if(numbers[i]!=null) {
  117.                 res += numbers[i].toString();
  118.             }
  119.         }
  120.         return res;
  121.     }
  122. }
  123.  
  124.  
  125. ====================================================================================================
  126.  
  127. package fileWork;
  128.  
  129. import fileWork.PhoneBook.*;
  130. import java.io.*;
  131. import java.util.Scanner;
  132.  
  133.  
  134. public class main {
  135.     public static void main(String[] args) throws IOException {
  136.         Phone p1 = new Phone("0546764039", "Misha");
  137.         Phone p2 = new Phone("0501234567", "Bob");
  138.         Phone p3 = new Phone("0529876573", "Lolo");
  139.         File f = new File("C:\\Users\\goodi\\IdeaProjects\\DataBaseWorking\\src\\res\\nums.txt");
  140.  
  141.         /*For 7 a-d
  142.         PrintWriter pw = new PrintWriter(f);
  143.         p1.save(pw);
  144.         p2.save(pw);
  145.         p3.save(pw);
  146.         pw.close();
  147.         fileToPrint(f);
  148.         */
  149.  
  150.         //For targil 13
  151.         PhoneBook pb1 = new PhoneBook("Misha",20);
  152.         pb1.add(p1);
  153.         pb1.add(p2);
  154.         pb1.add(p3);
  155.         pb1.save("nums.txt");
  156.  
  157.         PhoneBook pb2 = new PhoneBook("nums.txt");
  158.         System.out.println(pb2.toString());
  159.     }
  160.  
  161.     //Help function to write a file into console
  162.     public static void fileToPrint(File f) throws FileNotFoundException {
  163.         Scanner scan = new Scanner(f);
  164.         while(scan.hasNextLine()){
  165.             System.out.println(scan.nextLine());
  166.         }
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment