Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- phone.java
- ==================
- package Cls;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Phone {
- String tel,name; //owner , telephone
- final int ZERO_PAYMENT=-1; //setup first payment
- int payment=ZERO_PAYMENT; //payment sum
- public Phone(String tel, String name) {
- this.tel = tel;
- this.name = name;
- }
- public Phone(Phone phone){
- this(phone.tel,phone.name);
- }
- public Phone(Scanner s){
- String rawData = s.nextLine(); //050-231-4567,Nevin,5800
- String[] splitString = rawData.split(","); // splitString[0] = 050-231=4567, stringSplit[1]=Nevin
- this.tel=splitString[0];
- this.name=splitString[1];
- this.payment=Integer.parseInt(splitString[2]);
- }
- public Phone(String fileName) throws FileNotFoundException{
- //this(scanner)->this(new scanner(file))->this(new scanner(new file(fileName)))
- this(new Scanner(new File(fileName)));
- }
- public void save(PrintWriter pw) {
- String data = this.tel+","+this.name+","+this.payment;
- pw.println(data);
- }
- public void save(String fileName) throws FileNotFoundException{
- File file = new File(fileName);
- PrintWriter pw = new PrintWriter(file);
- save(pw);
- pw.close();
- }
- @Override
- public String toString() {
- return "Phone{" +
- "tel='" + tel + '\'' +
- ", name='" + name + '\'' +
- ", payment=" + payment +
- '}';
- }
- }
- Phonebook.java
- ===============
- package Cls;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class PhoneBook {
- String name;
- Phone[] phone;
- int total=0;
- int max;
- public PhoneBook(String name,int max){
- this.name=name;
- this.max=max;
- this.phone = new Phone[max];
- }
- public PhoneBook(String fileName) throws FileNotFoundException {
- File file = new File(fileName);
- Scanner s = new Scanner(file);
- this.name=s.nextLine();
- this.max =s.nextInt();
- this.total=s.nextInt();
- this.phone = new Phone[max];
- for (int counter=0;counter<this.total;counter+=1){
- phone[counter] = new Phone(s);
- }
- }
- public boolean add(Phone phone){
- if (total==max) return false;
- //add the phone
- this.phone[total]=new Phone(phone);
- total+=1;
- //this.phone[total++]=new Phone(phone);
- return true;
- }
- public void save(String fileName) throws FileNotFoundException {
- File file = new File(fileName);
- PrintWriter pw = new PrintWriter(file);
- pw.println(this.name);
- pw.println(this.total);
- pw.println(this.max);
- for (int counter=0;counter<total;counter+=1){
- phone[counter].save(pw);
- }
- /*
- for (Phone item:phone){
- if (item!=null){
- item.save(pw);
- }
- }
- */
- pw.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement