Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.90 KB | None | 0 0
  1. package app;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.Scanner;
  9.  
  10. public class App {
  11.  
  12.     static void showWarehouse() throws FileNotFoundException {
  13.         File file = new File("products.txt");
  14.         if (file.length() == 0) {
  15.             System.out.println("Plik nie istnieje lub jest pusty. " + "Czy to blad czy to blad, 404?");
  16.         }
  17.         Scanner read = new Scanner(file);
  18.         String line = null;
  19.         String currentProduct = null;
  20.         while (read.hasNextLine()) {
  21.             currentProduct = "";
  22.             line = read.nextLine();
  23.             for (int i = 0; i < line.length(); i++) {
  24.                 if (line.charAt(i) != ' ') {
  25.                     currentProduct += line.charAt(i);
  26.                 } else {
  27.                     break;
  28.                 }
  29.             }
  30.             int quantity = getQuantity(currentProduct);
  31.             int price = getPrice(currentProduct);
  32.             System.out.println("[NAZWA: " + currentProduct + "] [ILOSC: " + quantity + "] [CENA: " + price + "]");
  33.         }
  34.     }
  35.  
  36.     static boolean productAlreadyExists(String product) throws FileNotFoundException {
  37.         File file = new File("products.txt");
  38.         if (file.length() == 0) {
  39.             return false;
  40.         }
  41.         Scanner read = new Scanner(file);
  42.         String line = null;
  43.         String currentStr = null;
  44.         while (read.hasNextLine()) {
  45.             currentStr = "";
  46.             line = read.nextLine();
  47.             for (int i = 0; i < line.length(); i++) {
  48.                 if (line.charAt(i) != ' ') {
  49.                     currentStr += line.charAt(i);
  50.                 } else {
  51.                     break;
  52.                 }
  53.             }
  54.             if (currentStr.equals(product)) {
  55.                 return true;
  56.             }
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     static boolean lineContainsProduct(String line, String product) {
  62.         String currentStr = "";
  63.         for (int i = 0; i < line.length(); i++) {
  64.             if (line.charAt(i) != ' ') {
  65.                 currentStr += line.charAt(i);
  66.             } else {
  67.                 if (currentStr.equals(product)) {
  68.                     return true;
  69.                 } else {
  70.                     return false;
  71.                 }
  72.             }
  73.         }
  74.         return false;
  75.     }
  76.  
  77.     static int getQuantity(String product) throws FileNotFoundException {
  78.         File file = new File("products.txt");
  79.         if (file.length() == 0) {
  80.             return 0;
  81.         }
  82.         if (productAlreadyExists(product) == false) {
  83.             return 0;
  84.         }
  85.         Scanner read = new Scanner(file);
  86.         String line = null;
  87.         String currentStr = null;
  88.         int quantity = 0;
  89.         String numberStr = null;
  90.         boolean startReading = false;
  91.         while (read.hasNextLine()) {
  92.             currentStr = "";
  93.             line = read.nextLine();
  94.             numberStr = "";
  95.             if (lineContainsProduct(line, product)) {
  96.                 for (int i = 0; i < line.length(); i++) {
  97.                     if (startReading) {
  98.                         if (line.charAt(i) == ' ') {
  99.                             break;
  100.                         }
  101.                         numberStr += line.charAt(i);
  102.                     }
  103.  
  104.                     if (line.charAt(i) == ' ') {
  105.                         startReading = true;
  106.                     }
  107.  
  108.                 }
  109.             }
  110.  
  111.         }
  112.         if(numberStr.isEmpty()) {
  113.             return 0;
  114.         }
  115.         return Integer.parseInt(numberStr);
  116.     }
  117.  
  118.     static int getPrice(String product) throws FileNotFoundException {
  119.         int price = 0;
  120.         File file = new File("products.txt");
  121.         if (file.length() == 0) {
  122.             return 0;
  123.         }
  124.         if (productAlreadyExists(product) == false) {
  125.             return 0;
  126.         }
  127.         Scanner read = new Scanner(file);
  128.         String line = null;
  129.         String currentStr = null;
  130.         int quantity = 0;
  131.         String numberStr = null;
  132.         int spaces = 0;
  133.         boolean startReading = false;
  134.         while (read.hasNextLine()) {
  135.             currentStr = "";
  136.             line = read.nextLine();
  137.             numberStr = "";
  138.             if (lineContainsProduct(line, product)) {
  139.                 for (int i = 0; i < line.length(); i++) {
  140.                     if (startReading) {
  141.                         if (line.charAt(i) == ' ') {
  142.                             break;
  143.                         }
  144.                         numberStr += line.charAt(i);
  145.                     }
  146.  
  147.                     if (line.charAt(i) == ' ') {
  148.                         spaces++;
  149.                         if (spaces == 2) {
  150.                             startReading = true;
  151.                         }
  152.                     }
  153.  
  154.                 }
  155.             }
  156.  
  157.         }
  158.         if(numberStr.isEmpty()) {
  159.             return 0;
  160.         }
  161.         return Integer.parseInt(numberStr);
  162.     }
  163.  
  164.     static void addProduct(String name, int quantity, int price) throws IOException {
  165.         PrintWriter save = null;
  166.         FileWriter fr = null;
  167.         File file = new File("products.txt");
  168.         if (file.exists() == false) {
  169.             save = new PrintWriter("products.txt");
  170.             save.println(name + " " + quantity + " " + price);
  171.             save.close();
  172.         } else {
  173.             if (productAlreadyExists(name)) {
  174.                 quantity = getQuantity(name) + quantity;
  175.                 removeProduct(name);
  176.             }
  177.             fr = new FileWriter(file, true);
  178.             fr.write(name + " " + quantity + " " + price + "\n");
  179.             fr.close();
  180.         }
  181.  
  182.     }
  183.  
  184.     static void addProduct(String name, int quantity) {
  185.  
  186.     }
  187.  
  188.     static void removeProduct(String product) throws FileNotFoundException {
  189.         File file = new File("products.txt");
  190.         if (file.length() == 0) {
  191.             System.out.println("Plik nie istnieje lub jest pusty. " + "Czy to blad czy to blad, 404?");
  192.             return;
  193.         }
  194.         if (productAlreadyExists(product) == false) {
  195.             System.out.println("Nie znaleziono " + product + " w magazynie.");
  196.             return;
  197.         }
  198.         Scanner read = new Scanner(file);
  199.         read = new Scanner(file);
  200.         String line = null;
  201.         int lines = 0;
  202.         while (read.hasNextLine()) {
  203.             lines++;
  204.             read.nextLine();
  205.         }
  206.         String[] products = new String[lines];
  207.         read = new Scanner(file);
  208.         for (int i = 0; i < lines; i++) {
  209.             products[i] = read.nextLine();
  210.         }
  211.         PrintWriter save = new PrintWriter("products.txt");
  212.         for (int i = 0; i < lines; i++) {
  213.             if (lineContainsProduct(products[i], product) == false) {
  214.                 save.println(products[i]);
  215.             }
  216.  
  217.         }
  218.         save.close();
  219.     }
  220.  
  221.     static void removeProduct(String name, int quantity) throws IOException {
  222.         if (quantity > getQuantity(name)) {
  223.             removeProduct(name);
  224.             return;
  225.         }
  226.         int newQuantity = getQuantity(name) - quantity;
  227.         int price = getPrice(name);
  228.         removeProduct(name);
  229.         addProduct(name, newQuantity, price);
  230.  
  231.     }
  232.  
  233.     static void editProduct() throws IOException {
  234.         Scanner sc = new Scanner(System.in);
  235.         File file = new File("products.txt");
  236.         if (file.length() == 0) {
  237.             System.out.println("Plik products.txt nie istnieje");
  238.             return;
  239.         }
  240.         System.out.println("Jaki produkt chcesz edytowac?");
  241.         String name = sc.next();
  242.         if (productAlreadyExists(name) == false) {
  243.             System.out.println("Wybrany produkt nie istnieje");
  244.             return;
  245.         }
  246.         System.out.println("Jaki parametr chcesz edytowac?");
  247.         System.out.println("1 - nazwa");
  248.         System.out.println("2 - cena");
  249.         int key = sc.nextInt();
  250.         switch (key) {
  251.         case 1: {
  252.             editName(name);
  253.             break;
  254.         }
  255.         case 2: {
  256.             editPrice(name);
  257.             break;
  258.         }
  259.         }
  260.     }
  261.  
  262.     static void editName(String product) throws IOException {
  263.         Scanner sc = new Scanner(System.in);
  264.         System.out.println("Podaj nowa nazwe dla " + product + " :");
  265.         String newName = sc.next();
  266.         int price = getPrice(product);
  267.         int quantity = getQuantity(product);
  268.         removeProduct(product);
  269.         addProduct(newName, quantity, price);
  270.     }
  271.  
  272.     static void editPrice(String product) throws IOException {
  273.         Scanner sc = new Scanner(System.in);
  274.         System.out.println("Podaj nowa cene dla " + product + " :");
  275.         int newPrice = sc.nextInt();
  276.         int quantity = getQuantity(product);
  277.         removeProduct(product);
  278.         addProduct(product, quantity, newPrice);
  279.     }
  280.  
  281.     static void showMenu() throws IOException {
  282.         Scanner sc = new Scanner(System.in);
  283.         int key = 0;
  284.         while (true) {
  285.             System.out.println("===========================================");
  286.             System.out.println("= Witaj w Pieronce!                       =");
  287.             System.out.println("= Co chcesz zrobic?                       =");
  288.             System.out.println("= 1. Wypisz co mamy na zapleczu.          =");
  289.             System.out.println("= 2. Dodaj produkt do magazynu.           =");
  290.             System.out.println("= 3. Usun produkt z magazynu.             =");
  291.             System.out.println("= 4. Edytuj produkt.                      =");
  292.             System.out.println("= 5. Opusc sklep w trybie natychmiastowym.=");
  293.             System.out.println("===========================================");
  294.             System.out.println("Twoja decyzja: ");
  295.             key = sc.nextInt();
  296.             switch (key) {
  297.             case 1: {
  298.                 showWarehouse();
  299.                 break;
  300.             }
  301.             case 2: {
  302.                 sc = new Scanner(System.in);
  303.                 System.out.println("Podaj nazwe produktu:");
  304.                 String name = sc.nextLine();
  305.                 System.out.println("Podaj ilosc sztuk: ");
  306.                 int quantity = sc.nextInt();
  307.                 int price = 0;
  308.                 if (productAlreadyExists(name)) {
  309.                     price = getPrice(name);
  310.                 } else {
  311.                     System.out.println("Podaj cene (w zl): ");
  312.                     price = sc.nextInt();
  313.                 }
  314.                 addProduct(name, quantity, price);
  315.                 break;
  316.             }
  317.             case 3: {
  318.                 sc = new Scanner(System.in);
  319.                 System.out.println("Jaki produkt chcesz usunac?");
  320.                 String name = sc.next();
  321.                 System.out.println("Ile sztuk chcesz usunac?");
  322.                 int quantity = sc.nextInt();
  323.                 removeProduct(name, quantity);
  324.                 break;
  325.             }
  326.             case 4: {
  327.                 editProduct();
  328.                 break;
  329.             }
  330.             case 5: {
  331.                 System.out.println("Zapraszamy ponownie!");
  332.                 return;
  333.             }
  334.             case 6: {
  335.  
  336.             }
  337.             default: {
  338.                 System.out.println("Nie ma tutaj takiej opcji. Sproboj ponownie.");
  339.             }
  340.             }
  341.         }
  342.     }
  343.  
  344.     public static void main(String[] args) throws IOException {
  345.         showMenu();
  346.  
  347.     }
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement