Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class App {
- static void showWarehouse() throws FileNotFoundException {
- File file = new File("products.txt");
- if (file.length() == 0) {
- System.out.println("Plik nie istnieje lub jest pusty. " + "Czy to blad czy to blad, 404?");
- }
- Scanner read = new Scanner(file);
- String line = null;
- String currentProduct = null;
- while (read.hasNextLine()) {
- currentProduct = "";
- line = read.nextLine();
- for (int i = 0; i < line.length(); i++) {
- if (line.charAt(i) != ' ') {
- currentProduct += line.charAt(i);
- } else {
- break;
- }
- }
- int quantity = getQuantity(currentProduct);
- int price = getPrice(currentProduct);
- System.out.println("[NAZWA: " + currentProduct + "] [ILOSC: " + quantity + "] [CENA: " + price + "]");
- }
- }
- static boolean productAlreadyExists(String product) throws FileNotFoundException {
- File file = new File("products.txt");
- if (file.length() == 0) {
- return false;
- }
- Scanner read = new Scanner(file);
- String line = null;
- String currentStr = null;
- while (read.hasNextLine()) {
- currentStr = "";
- line = read.nextLine();
- for (int i = 0; i < line.length(); i++) {
- if (line.charAt(i) != ' ') {
- currentStr += line.charAt(i);
- } else {
- break;
- }
- }
- if (currentStr.equals(product)) {
- return true;
- }
- }
- return false;
- }
- static boolean lineContainsProduct(String line, String product) {
- String currentStr = "";
- for (int i = 0; i < line.length(); i++) {
- if (line.charAt(i) != ' ') {
- currentStr += line.charAt(i);
- } else {
- if (currentStr.equals(product)) {
- return true;
- } else {
- return false;
- }
- }
- }
- return false;
- }
- static int getQuantity(String product) throws FileNotFoundException {
- File file = new File("products.txt");
- if (file.length() == 0) {
- return 0;
- }
- if (productAlreadyExists(product) == false) {
- return 0;
- }
- Scanner read = new Scanner(file);
- String line = null;
- String currentStr = null;
- int quantity = 0;
- String numberStr = null;
- boolean startReading = false;
- while (read.hasNextLine()) {
- currentStr = "";
- line = read.nextLine();
- numberStr = "";
- if (lineContainsProduct(line, product)) {
- for (int i = 0; i < line.length(); i++) {
- if (startReading) {
- if (line.charAt(i) == ' ') {
- break;
- }
- numberStr += line.charAt(i);
- }
- if (line.charAt(i) == ' ') {
- startReading = true;
- }
- }
- }
- }
- if(numberStr.isEmpty()) {
- return 0;
- }
- return Integer.parseInt(numberStr);
- }
- static int getPrice(String product) throws FileNotFoundException {
- int price = 0;
- File file = new File("products.txt");
- if (file.length() == 0) {
- return 0;
- }
- if (productAlreadyExists(product) == false) {
- return 0;
- }
- Scanner read = new Scanner(file);
- String line = null;
- String currentStr = null;
- int quantity = 0;
- String numberStr = null;
- int spaces = 0;
- boolean startReading = false;
- while (read.hasNextLine()) {
- currentStr = "";
- line = read.nextLine();
- numberStr = "";
- if (lineContainsProduct(line, product)) {
- for (int i = 0; i < line.length(); i++) {
- if (startReading) {
- if (line.charAt(i) == ' ') {
- break;
- }
- numberStr += line.charAt(i);
- }
- if (line.charAt(i) == ' ') {
- spaces++;
- if (spaces == 2) {
- startReading = true;
- }
- }
- }
- }
- }
- if(numberStr.isEmpty()) {
- return 0;
- }
- return Integer.parseInt(numberStr);
- }
- static void addProduct(String name, int quantity, int price) throws IOException {
- PrintWriter save = null;
- FileWriter fr = null;
- File file = new File("products.txt");
- if (file.exists() == false) {
- save = new PrintWriter("products.txt");
- save.println(name + " " + quantity + " " + price);
- save.close();
- } else {
- if (productAlreadyExists(name)) {
- quantity = getQuantity(name) + quantity;
- removeProduct(name);
- }
- fr = new FileWriter(file, true);
- fr.write(name + " " + quantity + " " + price + "\n");
- fr.close();
- }
- }
- static void addProduct(String name, int quantity) {
- }
- static void removeProduct(String product) throws FileNotFoundException {
- File file = new File("products.txt");
- if (file.length() == 0) {
- System.out.println("Plik nie istnieje lub jest pusty. " + "Czy to blad czy to blad, 404?");
- return;
- }
- if (productAlreadyExists(product) == false) {
- System.out.println("Nie znaleziono " + product + " w magazynie.");
- return;
- }
- Scanner read = new Scanner(file);
- read = new Scanner(file);
- String line = null;
- int lines = 0;
- while (read.hasNextLine()) {
- lines++;
- read.nextLine();
- }
- String[] products = new String[lines];
- read = new Scanner(file);
- for (int i = 0; i < lines; i++) {
- products[i] = read.nextLine();
- }
- PrintWriter save = new PrintWriter("products.txt");
- for (int i = 0; i < lines; i++) {
- if (lineContainsProduct(products[i], product) == false) {
- save.println(products[i]);
- }
- }
- save.close();
- }
- static void removeProduct(String name, int quantity) throws IOException {
- if (quantity > getQuantity(name)) {
- removeProduct(name);
- return;
- }
- int newQuantity = getQuantity(name) - quantity;
- int price = getPrice(name);
- removeProduct(name);
- addProduct(name, newQuantity, price);
- }
- static void editProduct() throws IOException {
- Scanner sc = new Scanner(System.in);
- File file = new File("products.txt");
- if (file.length() == 0) {
- System.out.println("Plik products.txt nie istnieje");
- return;
- }
- System.out.println("Jaki produkt chcesz edytowac?");
- String name = sc.next();
- if (productAlreadyExists(name) == false) {
- System.out.println("Wybrany produkt nie istnieje");
- return;
- }
- System.out.println("Jaki parametr chcesz edytowac?");
- System.out.println("1 - nazwa");
- System.out.println("2 - cena");
- int key = sc.nextInt();
- switch (key) {
- case 1: {
- editName(name);
- break;
- }
- case 2: {
- editPrice(name);
- break;
- }
- }
- }
- static void editName(String product) throws IOException {
- Scanner sc = new Scanner(System.in);
- System.out.println("Podaj nowa nazwe dla " + product + " :");
- String newName = sc.next();
- int price = getPrice(product);
- int quantity = getQuantity(product);
- removeProduct(product);
- addProduct(newName, quantity, price);
- }
- static void editPrice(String product) throws IOException {
- Scanner sc = new Scanner(System.in);
- System.out.println("Podaj nowa cene dla " + product + " :");
- int newPrice = sc.nextInt();
- int quantity = getQuantity(product);
- removeProduct(product);
- addProduct(product, quantity, newPrice);
- }
- static void showMenu() throws IOException {
- Scanner sc = new Scanner(System.in);
- int key = 0;
- while (true) {
- System.out.println("===========================================");
- System.out.println("= Witaj w Pieronce! =");
- System.out.println("= Co chcesz zrobic? =");
- System.out.println("= 1. Wypisz co mamy na zapleczu. =");
- System.out.println("= 2. Dodaj produkt do magazynu. =");
- System.out.println("= 3. Usun produkt z magazynu. =");
- System.out.println("= 4. Edytuj produkt. =");
- System.out.println("= 5. Opusc sklep w trybie natychmiastowym.=");
- System.out.println("===========================================");
- System.out.println("Twoja decyzja: ");
- key = sc.nextInt();
- switch (key) {
- case 1: {
- showWarehouse();
- break;
- }
- case 2: {
- sc = new Scanner(System.in);
- System.out.println("Podaj nazwe produktu:");
- String name = sc.nextLine();
- System.out.println("Podaj ilosc sztuk: ");
- int quantity = sc.nextInt();
- int price = 0;
- if (productAlreadyExists(name)) {
- price = getPrice(name);
- } else {
- System.out.println("Podaj cene (w zl): ");
- price = sc.nextInt();
- }
- addProduct(name, quantity, price);
- break;
- }
- case 3: {
- sc = new Scanner(System.in);
- System.out.println("Jaki produkt chcesz usunac?");
- String name = sc.next();
- System.out.println("Ile sztuk chcesz usunac?");
- int quantity = sc.nextInt();
- removeProduct(name, quantity);
- break;
- }
- case 4: {
- editProduct();
- break;
- }
- case 5: {
- System.out.println("Zapraszamy ponownie!");
- return;
- }
- case 6: {
- }
- default: {
- System.out.println("Nie ma tutaj takiej opcji. Sproboj ponownie.");
- }
- }
- }
- }
- public static void main(String[] args) throws IOException {
- showMenu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement