Advertisement
malixds_

lab2

Sep 12th, 2022 (edited)
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Book{
  3.     Scanner scanner = new Scanner(System.in);
  4.     String name;
  5.     int year;
  6.     int pages;
  7.  
  8.     void setName(){
  9.         System.out.println("Введите название книги: ");
  10.         name = scanner.nextLine();
  11.     }
  12.     void getName(){
  13.         System.out.println(name);
  14.     }
  15.  
  16.     void setYear(){
  17.         System.out.println("Введите год написания книги: ");
  18.         year = scanner.nextInt();
  19.     }
  20.     void getYear(){
  21.         System.out.println(year);
  22.     }
  23.  
  24.     void setPages(){
  25.         System.out.println("Введите количество страниц книги: ");
  26.         pages = scanner.nextInt();
  27.     }
  28.     void getPages(){
  29.         System.out.println(pages);
  30.     }
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. import java.util.Scanner;
  38. class BookTest{
  39.     public static void main(String[] args) {
  40.         Book book = new Book();
  41.         Scanner scanner = new Scanner(System.in);
  42.         int check;
  43.         int i = 0;
  44.         boolean a = true;
  45.         boolean b = true;
  46.         while (a) {
  47.  
  48.             System.out.println("Введите 1 - Ввести данные, 2 - вывести данные, 3 - завершение программы");
  49.             check = scanner.nextInt();
  50.                 while (b) {
  51.                     if (check != 1 && check != 2 && check != 3) {
  52.                         System.out.println("Введены некорректные данные, повторите попытку");
  53.                         check = scanner.nextInt();
  54.                     }
  55.                     else if (check == 1 || check == 2 || check == 3) {
  56.                         b = false;
  57.                     }
  58.                 }
  59.             if (check == 1) {
  60.                 System.out.println("Название книги: ");
  61.                 book.setName();
  62.                 System.out.println("Год выпуска книги: ");
  63.                 book.setYear();
  64.                 System.out.println("Количество страниц книги: ");
  65.                 book.setPages();
  66.                 i+=1;
  67.             }
  68.             else if(check ==2){
  69.                 if(i > 0){
  70.                     book.getName();
  71.                     book.getYear();
  72.                     book.getPages();
  73.                 }
  74.                 else{
  75.                     System.out.println("Данные еще не внесены, повторите попытку");
  76.                 }
  77.             }
  78.  
  79.             else{
  80.                 System.out.println("Выход из программы");
  81.                 a = false;
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement