Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package oi;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Monitoria {
  6. public static void main(String[] args) {
  7. Scanner in = new Scanner(System.in);
  8. List a = new List();
  9. a.add("Paulo", 2.0, 3);
  10. a.add("Lo", 7.6789, 2);
  11. a.add("K", 5, 9);
  12. a.add("Digo", 1, 1);
  13. a.add("Digos", 878, 1);
  14.  
  15.  
  16. a.remove(6);
  17. a.print();
  18.  
  19. }
  20. }
  21.  
  22. class List {
  23. private String name;
  24. private Double price;
  25. private Integer qtd;
  26. private List next;
  27.  
  28. public List() {
  29. this.name = null;
  30. this.price = null;
  31. this.qtd = null;
  32. this.next = null;
  33. }
  34.  
  35. public List(String s, double p, int q) {
  36. this.name = s;
  37. this.price = p;
  38. this.qtd = q;
  39. this.next = null;
  40. }
  41.  
  42. public void add(String s, double p, int q) {
  43. if (this.next == null) {
  44. if(this.price==null) {
  45. this.price=p;
  46. this.name=s;
  47. this.qtd = q;
  48. }else {
  49. this.next = new List(s, p, q);
  50. }
  51. } else {
  52. this.next.add(s, p, q);
  53. }
  54. }
  55.  
  56. public void remove(double p) {
  57. if(this.next!=null && this.price!=null) { // TUDO MENOS O ULTIMO
  58. if(this.price>p) {
  59. this.price = this.next.price;
  60. this.name = this.next.name;
  61. this.qtd = this.next.qtd;
  62. this.next = this.next.next;
  63. if(this.next!=null) {
  64. this.next.remove(p);
  65. }
  66. }
  67. else {
  68. this.next.remove(p);
  69. }
  70. } if(this.price!=null && this.price>p) {
  71. this.price = null;
  72. this.name =null;
  73. this.qtd = null;
  74. }
  75.  
  76. }
  77.  
  78. public void print() {
  79. if (this.next == null && this.name != null) { // ULTIMO
  80. System.out.println(this.name);
  81. } else if (this.next != null) {
  82. if (this.name == null) {
  83. this.next.print();
  84. } else {
  85. System.out.println(this.name);
  86. this.next.print();
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement