Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fileWork.PhoneBook;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Phone {
- public String ownerName;
- public String lineNumber;
- public int lastPay;
- private final int startPay = -1;
- public Phone(String lineNumber, String ownerName) {
- this.ownerName = ownerName;
- this.lineNumber = lineNumber;
- this.lastPay = startPay;
- }
- public Phone(Scanner scan) {
- this.lineNumber = scan.next();
- this.ownerName = scan.next();
- this.lastPay = scan.nextInt();
- }
- public Phone(String fileName) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(fileName));
- this.lineNumber = scan.next();
- this.ownerName = scan.next();
- this.lastPay = scan.nextInt();
- }
- public void save(PrintWriter pw) throws FileNotFoundException {
- pw.print(this.lineNumber + " " + this.ownerName + " " + this.lastPay);
- pw.println();
- }
- public void save(String fileName) throws FileNotFoundException {
- PrintWriter pw = new PrintWriter(new File(fileName));
- pw.print(this.lineNumber + " " + this.ownerName + " " + this.lastPay);
- pw.close();
- }
- @Override
- public String toString() {
- return "LineNumber=" + lineNumber + '\n' +
- "ownerName=" + ownerName + '\n' +
- "lastPay=" + lastPay + '\n';
- }
- }
- ================================================================================================
- package fileWork.PhoneBook;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Arrays;
- import java.util.Scanner;
- public class PhoneBook {
- public String ownerName;
- public Phone[] numbers;
- public int numsAmount;
- public PhoneBook(String ownerName, int maxAmount) {
- this.ownerName = ownerName;
- this.numbers = new Phone[maxAmount];
- numsAmount = 0;
- }
- public PhoneBook(String fileName) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(fileName));
- if (scan.hasNextLine()) {
- this.ownerName = scan.nextLine();
- }
- if (scan.hasNextLine()) {
- this.numbers = new Phone[Integer.parseInt(scan.nextLine())];
- }
- if (scan.hasNextLine()) {
- this.numsAmount = Integer.parseInt(scan.nextLine());
- }
- for (int i = 0; i < numsAmount; i += 1) {
- numbers[i] = new Phone(scan);
- //To make it just for
- if (scan.hasNextLine()) {
- scan.nextLine();
- }
- }
- }
- public void add(Phone phone) {
- this.numbers[numsAmount] = phone;
- numsAmount += 1;
- }
- public void save(String fileName) throws FileNotFoundException {
- PrintWriter pw = new PrintWriter(new File(fileName));
- pw.println(ownerName);
- pw.println(this.numbers.length);
- pw.println(this.numsAmount);
- for (int i = 0; i < this.numsAmount; i += 1) {
- numbers[i].save(pw);
- }
- pw.close();
- }
- @Override
- public String toString() {
- String res = "PhoneBook\n" +
- "ownerName=" + ownerName + '\n' +
- "numsAmount=" + numsAmount + '\n' +
- "numbers: \n";
- for (int i=0; i<numbers.length; i+=1) {
- if(numbers[i]!=null) {
- res += numbers[i].toString();
- }
- }
- return res;
- }
- }
- ====================================================================================================
- package fileWork;
- import fileWork.PhoneBook.*;
- import java.io.*;
- import java.util.Scanner;
- public class main {
- public static void main(String[] args) throws IOException {
- Phone p1 = new Phone("0546764039", "Misha");
- Phone p2 = new Phone("0501234567", "Bob");
- Phone p3 = new Phone("0529876573", "Lolo");
- File f = new File("C:\\Users\\goodi\\IdeaProjects\\DataBaseWorking\\src\\res\\nums.txt");
- /*For 7 a-d
- PrintWriter pw = new PrintWriter(f);
- p1.save(pw);
- p2.save(pw);
- p3.save(pw);
- pw.close();
- fileToPrint(f);
- */
- //For targil 13
- PhoneBook pb1 = new PhoneBook("Misha",20);
- pb1.add(p1);
- pb1.add(p2);
- pb1.add(p3);
- pb1.save("nums.txt");
- PhoneBook pb2 = new PhoneBook("nums.txt");
- System.out.println(pb2.toString());
- }
- //Help function to write a file into console
- public static void fileToPrint(File f) throws FileNotFoundException {
- Scanner scan = new Scanner(f);
- while(scan.hasNextLine()){
- System.out.println(scan.nextLine());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment