prestige

Untitled

Dec 26th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package classes;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectOutputStream;
  7. import java.util.Scanner;
  8.  
  9. public class KeyboardDialog extends UserDialog{
  10. public void inputFromKeyboard (int actionType, MyVector<Yacht> vec) throws IOException, MyPriceException {
  11. Scanner sc = new Scanner(System.in);
  12. if (actionType == 1) {
  13. System.out.println("Ввведите 1 для добавления парусной яхты, 2 для добавления моторной яхты:");
  14. actionType = sc.nextInt();
  15. if (actionType == 1) {
  16. System.out.println("Input price of yacht:");
  17. double price = sc.nextDouble();
  18. System.out.println("Input name of yacht:");
  19. String name = sc.nextLine();
  20. System.out.println("Input class of yacht:");
  21. String yachtClass = sc.nextLine();
  22. try {
  23. vec.add(new SallingYacht(name, price, yachtClass));
  24. } catch (MyPriceException e) {
  25. System.out.println("Invalid price of yacht");
  26. }
  27. } else if (actionType == 2) {
  28. System.out.println("Input price of yacht:");
  29. double price = sc.nextDouble();
  30. System.out.println("Input name of yacht:");
  31. String name = sc.nextLine();
  32. System.out.println("Input type of engine :");
  33. String yachtEngine = sc.nextLine();
  34. try {
  35. vec.add(new MotorYacht(name, price, yachtEngine));
  36. } catch (MyPriceException e) {
  37. System.out.println("Invalid price of yacht");
  38. }
  39. }
  40. } else if (actionType == 2) {
  41. System.out.println("Simple iterated print container");
  42. System.out.println(vec.toString());
  43. } else if (actionType == 3) {
  44. System.out.println("Enter type of sort, if you want to sort by price - 1, by name - 2");
  45. int type = sc.nextInt();
  46. int sortType = -1;
  47. if (type == 1) {
  48. System.out.println("If you want sort in increase order enter 1, else enter 2:");
  49. sortType = sc.nextInt();
  50. if (sortType == 1) {
  51. vec.sort(new SortByPrice(), 0, vec.getCount());
  52. } else if (sortType == 2) {
  53. vec.sort(new SortByPrice(true), 0, vec.getCount());
  54. }
  55. } else if (type == 2) {
  56. System.out.println("If you want sort in increase order enter 1, else enter 2:");
  57. sortType = sc.nextInt();
  58. if (sortType == 1) {
  59. vec.sort(new SortByName(), 0, vec.getCount());
  60. } else if (sortType == 2) {
  61. vec.sort(new SortByName(true), 0, vec.getCount());
  62. }
  63. }
  64. } else if (actionType == 4) {
  65. System.out.println("Если хотите сохраненые данные контейнера нажмите 1, иначе 2:");
  66. actionType = sc.nextInt();
  67. if (actionType == 1) {
  68. ObjectOutputStream os = null;
  69. try {
  70. os = new ObjectOutputStream(new FileOutputStream("D:\\University\\5_course\\Java\\Container.dat"));
  71. os.writeObject(vec);
  72. } catch (FileNotFoundException e) {
  73. System.out.println(e);
  74. } finally {
  75. os.flush();
  76. os.close();
  77. System.out.println("Container succesfully saved.");
  78. }
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment