Untitled

mail@pastecode.io avatar
unknown
java
a year ago
4.2 kB
1
Indexable
Never
package practice;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args)  {
        ContractorsRegister contractorRegister = new ContractorsRegister();
        boolean flag = false;

        while (!flag) {
            printMenu();
            System.out.print(">> ");

            switch(readInput()) {
                case 1 -> contractorRegister.displayContractorsList();
                case 2 -> contractorRegister.addContractor();
                case 3 -> System.out.println("Option 3");
                case 4 -> System.out.println("Option 4");
                case 5 -> System.out.println("Option 5");
                case 6 -> System.out.println("Option 6");
                case 0 -> flag = true;
            }
        }
    }

    private static void printMenu() {
        System.out.println("-".repeat(30));
        System.out.println("""
                Invoice manager v1.0
                
                [1] Contractors list
                [2] Add new contractor
                [3] Add invoice
                [4] Remove invoice
                [5] Modify invoice
                [6] History of transactions
                
                [0] EXIT
                """);
    }

    private static int readInput() {
        int input = 0;
        try {
            input = Integer.parseInt(scanner.nextLine().trim());
        } catch(NumberFormatException invalidInput) {
            System.out.printf("WARNING: Invalid input %n");
        }

        if ((input < 1) || (input > 6)) return -1;

        return input;
    }

}

public class ContractorsRegister {
    private static Scanner scanner = new Scanner(System.in);
    private ArrayList<Contractor> contractors;

    public ContractorsRegister() {
        this.contractors = new ArrayList<>(1000);
    }

    private Contractor getContractor(String contractorName) {
        for (var contractor: contractors) {
            if (contractor.getName().equalsIgnoreCase(contractorName)) {
                return contractor;
            }
        }
        System.out.printf("Contractor %s was not found.%n", contractorName);
        return null;
    }

    public void addContractor() {
        System.out.println("-".repeat(30));
        System.out.print("Enter new contractor's name: ");
        String name = scanner.nextLine().trim();

        if (getContractor(name) == null) {
            Contractor contractor = new Contractor(name);
            contractors.add(contractor);
            System.out.printf("Contractor %s was added.%n", name);
        } else {
            System.out.printf("Contractor %s already exists.%n", name);
        }
    }

    public void displayContractorsList() {
        System.out.println("-".repeat(30));
        int index = 1;
        for (Contractor contractor: contractors) {
            System.out.printf("%d - %s%n", index, contractor.getName());
            index++;
        }
        System.out.printf("%nPress enter to continue...");
        scanner.nextLine();
    }
}

public class Contractor {
    private static Scanner scanner = new Scanner(System.in);
    private String name;
    private ArrayList<Invoice> invoices;

    public Contractor(String name) {
        this.name = name.toUpperCase();
        this.invoices = new ArrayList<>(1000);
    }

    public String getName() {
        return this.name;
    }

    public void addInvoice(Invoice invoice, String contractorName) {
        if (invoice == null) return;

        for (var item: invoices) {
            if (item.number().equalsIgnoreCase(invoice.number())) {
                System.out.printf("Invoice with number %s already exists on this contractor.", invoice.number());
                scanner.nextLine();
            }
        }

        invoices.add(invoice);
    }

    public void displayInvoicesList(String contractorName) {

    }

}

public record Invoice(String date, String number, int daysToPay, double amount) {
}