Advertisement
lashrone1

ind 3

May 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.  
  4. static Scanner sc = new Scanner(System.in);
  5.  
  6.  
  7. static void set(Vector ve){
  8. System.out.println("Введите позицию напитка информацию которого хотите поменять: \n");
  9. int ans=sc.nextInt();
  10. if(ans<0||ans>ve.getsize()) {
  11. System.out.println("Введенное значение выходит за рамки размера массива обьектов!\n");
  12. set(ve);
  13. }
  14. add(ans,ve);
  15. }
  16.  
  17.  
  18. static void add(int pos,Vector ve){
  19. System.out.println("1)Алкогольный\n2)Нелкогольный\n3)Выход\t");
  20.  
  21. Baza ci;
  22. double price;
  23. double volume;
  24. String name;
  25. boolean temperature;
  26. boolean gaz;
  27. int oborot;
  28.  
  29. int ans=sc.nextInt();
  30. switch (ans){
  31. case(1):{
  32. ci=new Alcohol();
  33. System.out.println("Название:");
  34. name=sc.nextLine();
  35. ci.setName(name);
  36. System.out.println("Цена: ");
  37. price=sc.nextDouble();
  38. ci.setPrice(price);
  39. System.out.println("Обьем: ");
  40. volume=sc.nextDouble();
  41. ci.setVolume(volume);
  42. System.out.println("Температура(true=cold, false=warm): ");
  43. temperature=sc.nextBoolean();
  44. ci.setTemperature(temperature);
  45. System.out.println("Количество оборотов: ");
  46. oborot=sc.nextInt();
  47. ((Alcohol) ci).setObr(oborot);
  48. ve.insert(ci,pos);
  49. break;}
  50. case(2):{
  51. ci=new NeAlcohol();
  52. System.out.println("Введите инфрмацию о напитке : \nНазвание:");
  53. name=sc.nextLine();
  54. ci.setName(name);
  55. System.out.println("Цена: ");
  56. price=sc.nextDouble();
  57. ci.setPrice(price);
  58. System.out.println("Обьем: ");
  59. volume=sc.nextDouble();
  60. ci.setVolume(volume);
  61. System.out.println("Температура(true=cold, false=warm): ");
  62. temperature=sc.nextBoolean();
  63. ci.setTemperature(temperature);
  64. System.out.println("Состояние(газированное=true, негазированное=false): ");
  65. gaz=sc.nextBoolean();
  66. ((NeAlcohol) ci).setGaz(gaz);
  67. ve.insert(ci,pos);
  68. break;}
  69. case (3):
  70. break;
  71. default:
  72. System.out.println("Error!\n");
  73. add(pos,ve);
  74. break;
  75. }
  76. }
  77.  
  78. public static void main(String[] args) {
  79. Vector ve=new Vector();
  80. add(0,ve);
  81. add(1,ve);
  82. ve.print();
  83. set(ve);
  84. ve.print();
  85. ve.erase(0);
  86. ve.print();
  87.  
  88. }
  89. }
  90.  
  91. public class Vector {
  92. protected Baza[] arr=new Baza[10];
  93. protected int size=0;
  94.  
  95. public Vector(){ //конструктор по умолчанию
  96. }
  97.  
  98. private void increase(){ //приватный вспомогательный метод для увеличения массива
  99. Baza[] tmp=new Baza[arr.length+10];
  100. for(int i=0; i<arr.length; i++)
  101. tmp[i]=arr[i];
  102. arr=tmp;
  103. }
  104.  
  105. public int getsize(){ //получить текущий размер
  106. return size;
  107. }
  108.  
  109. public void getInfo(Baza obj,int pos) { //получить значение из ячейки
  110. if((pos<size)&&(pos>=0))
  111. obj.getInfo();
  112. else return;
  113. }
  114.  
  115. public void set(Baza obj, int pos){ //замещение элемента
  116. if((pos<size)&&(pos>=0)) { //позиция должна быть строго меньше размера но не меньше нуля
  117. arr[pos]=obj;
  118. }
  119. else System.out.println("Ошибка ввода! Неверные данные или элементы отсутствуют!");
  120. }
  121.  
  122. public void insert(Baza obj,int pos){ //добавление элемента
  123. if((pos<=size)&&(pos>=0)){ //позиция должна быть не больше размера и не меньше нуля
  124. for(int i=arr.length-1; i>pos; i--) //сдвиг элементов
  125. arr[i]=arr[i-1];
  126. arr[pos]=obj;
  127. size++;
  128. //System.out.println("Значение добавлено!");
  129. if(size==arr.length) //увеличение размера по надобности
  130. increase();
  131. }
  132. else System.out.println("Ошибка ввода! Неверные данные!");
  133.  
  134. }
  135. public void erase(int pos) { //удаление элемента со сдвигом
  136. if((pos<size)&&(pos>=0)){
  137. for(int i=pos; i<size-1; i++) //перезапись элементов
  138. arr[i]=arr[i+1];
  139. size--;
  140.  
  141. }
  142. else System.out.println("Ошибка ввода! Неверные данные!");
  143.  
  144. }
  145.  
  146.  
  147. public void print(){ //метод печати
  148. if(size==0)
  149. System.out.println("Массив пуст!");
  150. else {
  151. for(int i=0; i<size; i++)
  152. arr[i].getInfo();
  153. System.out.println();
  154. }
  155. }
  156. }
  157.  
  158. import java.util.Scanner;
  159.  
  160. abstract class Baza{
  161. private double price;
  162. private double volume;
  163. private String name;
  164. private boolean temperature;
  165.  
  166. Scanner sc=new Scanner(System.in);
  167.  
  168. public Baza(){}
  169.  
  170. public Baza(String name,double price,double volume,boolean temperature){
  171. setTemperature(temperature);
  172. setName(name);
  173. setPrice(price);
  174. setVolume(volume);
  175. }
  176.  
  177. public void setPrice(double price) {
  178. this.price = price;
  179. }
  180.  
  181. public double getPrice() {
  182. return price;
  183. }
  184.  
  185. public void setName(String name) {
  186. this.name = name;
  187. }
  188.  
  189. public String getName() {
  190. return name;
  191. }
  192.  
  193. public void setTemperature(boolean temperature) {
  194. if(temperature==true)
  195. this.temperature = true;
  196. else
  197. this.temperature = false;
  198. }
  199.  
  200. public void setVolume(double volume) {
  201. if(volume<0) {
  202. System.out.println("Введенный обьем меньше нуля!\nВведите обьем еще раз!\n");
  203. double vol=sc.nextDouble();
  204. setVolume(vol);
  205. }
  206. else
  207. this.volume = volume;
  208. }
  209.  
  210. public double getVolume() {
  211. return volume;
  212. }
  213.  
  214.  
  215. public String getTemperature(){
  216. if(this.temperature==true)
  217. return "Холодный";
  218. else
  219. return "Теплый";
  220. }
  221.  
  222. public void getInfo(){}
  223.  
  224. }
  225. public class Alcohol extends Baza{
  226.  
  227.  
  228.  
  229. private int oborot;
  230.  
  231. void Alcohol(){}
  232.  
  233. void Alcohol(int oborot){
  234.  
  235. this.oborot=oborot;
  236. }
  237.  
  238. public void setObr(int oborot) {
  239. if(oborot<0)
  240. return;
  241. else
  242. this.oborot=oborot;
  243. }
  244.  
  245. public int getOborot() {
  246. return oborot;
  247. }
  248.  
  249. public void getInfo(){
  250. System.out.println("Информация об Алкогольном напитке\nНазвание: "+getName()+"\n"+"Цена: "+getPrice()+"\n"+"Обьем: "+getVolume()+"\n"+"Температура: "+getTemperature()+"\n"+"Количество оборотов: "+getOborot()+"\n");
  251. }
  252.  
  253. }
  254. public class NeAlcohol extends Baza {
  255.  
  256.  
  257. private boolean gaz;
  258.  
  259. void NeAlcohol(){}
  260.  
  261. void NeAlcohol(boolean gaz){
  262. this.gaz=gaz;
  263. }
  264.  
  265. public void setGaz(boolean gaz) {
  266. this.gaz = gaz;
  267. }
  268.  
  269. public String getGaz() {
  270. if(gaz==true)
  271. return "Напиток газированный";
  272. else
  273. return "Напиток не газированный";
  274. }
  275.  
  276. public void getInfo(){
  277. System.out.println("Информация об Безалкогольном напитке\nНазвание: "+getName()+"\n"+"Цена: "+getPrice()+"\n"+"Обьем: "+getVolume()+"\n"+"Температура: "+getTemperature()+"\n"+ getGaz()+"\n");
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement