Advertisement
Guest User

phone book

a guest
Jun 26th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. phone.java
  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.     String tel,name; //owner , telephone
  12.     final int ZERO_PAYMENT=-1; //setup first payment
  13.     int payment=ZERO_PAYMENT; //payment sum
  14.  
  15.     public Phone(String tel, String name) {
  16.         this.tel = tel;
  17.         this.name = name;
  18.     }
  19.  
  20.     public Phone(Phone phone){
  21.         this(phone.tel,phone.name);
  22.     }
  23.  
  24.     public Phone(Scanner s){
  25.         String rawData = s.nextLine(); //050-231-4567,Nevin,5800
  26.         String[] splitString = rawData.split(","); // splitString[0] = 050-231=4567, stringSplit[1]=Nevin
  27.         this.tel=splitString[0];
  28.         this.name=splitString[1];
  29.         this.payment=Integer.parseInt(splitString[2]);
  30.     }
  31.  
  32.     public Phone(String fileName) throws FileNotFoundException{
  33.         //this(scanner)->this(new scanner(file))->this(new scanner(new file(fileName)))
  34.         this(new Scanner(new File(fileName)));
  35.     }
  36.  
  37.     public void save(PrintWriter pw) {
  38.         String data = this.tel+","+this.name+","+this.payment;
  39.         pw.println(data);
  40.     }
  41.  
  42.     public void save(String fileName) throws FileNotFoundException{
  43.         File file = new File(fileName);
  44.         PrintWriter pw = new PrintWriter(file);
  45.         save(pw);
  46.         pw.close();
  47.     }
  48.  
  49.     @Override
  50.     public String toString() {
  51.         return "Phone{" +
  52.                 "tel='" + tel + '\'' +
  53.                 ", name='" + name + '\'' +
  54.                 ", payment=" + payment +
  55.                 '}';
  56.     }
  57. }
  58.  
  59.  
  60.  
  61. Phonebook.java
  62. ===============
  63. package Cls;
  64.  
  65. import java.io.File;
  66. import java.io.FileNotFoundException;
  67. import java.io.PrintWriter;
  68. import java.util.Scanner;
  69.  
  70. public class PhoneBook {
  71.     String name;
  72.     Phone[] phone;
  73.     int total=0;
  74.     int max;
  75.  
  76.     public PhoneBook(String name,int max){
  77.         this.name=name;
  78.         this.max=max;
  79.         this.phone = new Phone[max];
  80.     }
  81.  
  82.     public PhoneBook(String fileName) throws FileNotFoundException {
  83.         File file = new File(fileName);
  84.         Scanner s = new Scanner(file);
  85.  
  86.         this.name=s.nextLine();
  87.         this.max =s.nextInt();
  88.         this.total=s.nextInt();
  89.         this.phone = new Phone[max];
  90.  
  91.         for (int counter=0;counter<this.total;counter+=1){
  92.             phone[counter] = new Phone(s);
  93.         }
  94.     }
  95.  
  96.     public boolean add(Phone phone){
  97.         if (total==max) return false;
  98.  
  99.         //add the phone
  100.         this.phone[total]=new Phone(phone);
  101.         total+=1;
  102.         //this.phone[total++]=new Phone(phone);
  103.  
  104.         return true;
  105.     }
  106.  
  107.     public void save(String fileName) throws FileNotFoundException {
  108.         File file = new File(fileName);
  109.         PrintWriter pw = new PrintWriter(file);
  110.  
  111.         pw.println(this.name);
  112.         pw.println(this.total);
  113.         pw.println(this.max);
  114.  
  115.         for (int counter=0;counter<total;counter+=1){
  116.             phone[counter].save(pw);
  117.         }
  118.  
  119.         /*
  120.         for (Phone item:phone){
  121.             if (item!=null){
  122.                 item.save(pw);
  123.             }
  124.         }
  125.         */
  126.  
  127.         pw.close();
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement