Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classes;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class KeyboardDialog extends UserDialog{
- public void inputFromKeyboard (int actionType, MyVector<Yacht> vec) throws IOException, MyPriceException {
- Scanner sc = new Scanner(System.in);
- if (actionType == 1) {
- System.out.println("Ввведите 1 для добавления парусной яхты, 2 для добавления моторной яхты:");
- actionType = sc.nextInt();
- if (actionType == 1) {
- System.out.println("Input price of yacht:");
- double price = sc.nextDouble();
- System.out.println("Input name of yacht:");
- String name = sc.nextLine();
- System.out.println("Input class of yacht:");
- String yachtClass = sc.nextLine();
- try {
- vec.add(new SallingYacht(name, price, yachtClass));
- } catch (MyPriceException e) {
- System.out.println("Invalid price of yacht");
- }
- } else if (actionType == 2) {
- System.out.println("Input price of yacht:");
- double price = sc.nextDouble();
- System.out.println("Input name of yacht:");
- String name = sc.nextLine();
- System.out.println("Input type of engine :");
- String yachtEngine = sc.nextLine();
- try {
- vec.add(new MotorYacht(name, price, yachtEngine));
- } catch (MyPriceException e) {
- System.out.println("Invalid price of yacht");
- }
- }
- } else if (actionType == 2) {
- System.out.println("Simple iterated print container");
- System.out.println(vec.toString());
- } else if (actionType == 3) {
- System.out.println("Enter type of sort, if you want to sort by price - 1, by name - 2");
- int type = sc.nextInt();
- int sortType = -1;
- if (type == 1) {
- System.out.println("If you want sort in increase order enter 1, else enter 2:");
- sortType = sc.nextInt();
- if (sortType == 1) {
- vec.sort(new SortByPrice(), 0, vec.getCount());
- } else if (sortType == 2) {
- vec.sort(new SortByPrice(true), 0, vec.getCount());
- }
- } else if (type == 2) {
- System.out.println("If you want sort in increase order enter 1, else enter 2:");
- sortType = sc.nextInt();
- if (sortType == 1) {
- vec.sort(new SortByName(), 0, vec.getCount());
- } else if (sortType == 2) {
- vec.sort(new SortByName(true), 0, vec.getCount());
- }
- }
- } else if (actionType == 4) {
- System.out.println("Если хотите сохраненые данные контейнера нажмите 1, иначе 2:");
- actionType = sc.nextInt();
- if (actionType == 1) {
- ObjectOutputStream os = null;
- try {
- os = new ObjectOutputStream(new FileOutputStream("D:\\University\\5_course\\Java\\Container.dat"));
- os.writeObject(vec);
- } catch (FileNotFoundException e) {
- System.out.println(e);
- } finally {
- os.flush();
- os.close();
- System.out.println("Container succesfully saved.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment