Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. package vacationTrip;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Scanner;
  7.  
  8. class SLLNode {
  9. protected String city;
  10. protected int price;
  11. protected SLLNode succ;
  12.  
  13. public SLLNode(String city,int price, SLLNode succ) {
  14. this.city = city;
  15. this.price=price;
  16. this.succ = succ;
  17. }
  18.  
  19.  
  20. }
  21.  
  22. class SLL {
  23. private SLLNode first;
  24.  
  25. public SLL() {
  26. // Construct an empty SLL
  27. this.first = null;
  28. }
  29.  
  30. public void deleteList() {
  31. first = null;
  32. }
  33.  
  34. public void insertFirst(String city, int price) {
  35. SLLNode ins = new SLLNode(city,price, first);
  36. first = ins;
  37. }
  38.  
  39. public void insertLast(String city,int price) {
  40. if (first != null) {
  41. SLLNode tmp = first;
  42. while (tmp.succ != null)
  43. tmp = tmp.succ;
  44. SLLNode ins = new SLLNode(city, price, null);
  45. tmp.succ = ins;
  46. } else {
  47. insertFirst(city,price);
  48. }
  49. }
  50.  
  51. public SLLNode getFirst() {
  52. return first;
  53. }
  54.  
  55. public String delete(SLLNode node){
  56. if (first != null){
  57. SLLNode tmp = first;
  58. while (tmp.succ != node &&
  59. tmp.succ.succ != null)
  60. tmp = tmp.succ;
  61. if (tmp.succ == node){
  62. tmp.succ = tmp.succ.succ;
  63. return node.city;
  64. }
  65. // else throw Exception;
  66. }
  67. //else throw Exception;
  68. return null;
  69. }
  70. }
  71. public class Vacation {
  72.  
  73. public static void main(String[] args) throws IOException {
  74.  
  75. SLL vacation_list = new SLL();
  76. vacation_list.insertLast("Great Barier Reef", 100);
  77. vacation_list.insertLast("Paris", 50);
  78. vacation_list.insertLast("Bora Bora", 60);
  79. vacation_list.insertLast("Florence", 80);
  80. vacation_list.insertLast("Helsinki", 40);
  81. vacation_list.insertLast("London", 90);
  82. vacation_list.insertLast("Rome", 80);
  83. vacation_list.insertLast("New York City", 20);
  84. vacation_list.insertLast("Lost Angeles", 60);
  85.  
  86. int budget=0;
  87. int answer=1;
  88. Scanner in = new Scanner(System.in);
  89. System.out.println("Insert your budget:");
  90. budget=in.nextInt();
  91. if(budget==0){
  92. System.out.println("You do not have sufficient funds");
  93. System.exit(0);
  94. }
  95. answer=vacation(budget,vacation_list);
  96.  
  97. if(answer==1)
  98. {
  99. System.out.println("Happy travel");
  100. }
  101. else
  102. {
  103. System.out.println("Thank you for traveling with us.");
  104. }
  105.  
  106. }
  107.  
  108. private static int vacation(int budget, SLL vacation_list) throws IOException {
  109. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  110. String s=null;
  111. int answer=1;
  112. if(budget==0) return 0;
  113. System.out.println("Available cities for your bugdet are:");
  114.  
  115. SLLNode tmp =vacation_list.getFirst();
  116.  
  117. while(tmp!=null)
  118. {
  119. //if you want the price for return trip too you have 2 options
  120. //1. Enter double the price for the city when creating the list from line 92 and down
  121. //2.you can modify the code like tmp.price*2<budget
  122. if(tmp.price*2<=budget)
  123. {
  124. System.out.println(tmp.city);
  125. tmp=tmp.succ;
  126. }
  127. else
  128. tmp=tmp.succ;
  129. }
  130. //reseting temp to start from first node
  131. tmp=vacation_list.getFirst();
  132.  
  133. System.out.println("Enter your destination:");
  134. s=bf.readLine();
  135.  
  136. while(tmp!=null)
  137. {
  138. if(s.contentEquals(tmp.city))
  139. {
  140. //if you go with option 2, you should modify here tmp.price*2 and in the println too
  141. budget-=tmp.price*2;
  142. System.out.println("Your ticket price for "+ tmp.city+ " is: "+tmp.price*2);
  143. System.out.println("Your remaining budget is: "+budget);
  144. break;
  145. }
  146. tmp=tmp.succ;
  147. }
  148. tmp=vacation_list.getFirst();
  149. int flag=0;
  150. while(tmp!=null)
  151. {
  152. if(budget>tmp.price*2)
  153. {
  154. tmp=tmp.succ;
  155. flag=1;
  156. continue;
  157. }
  158. tmp=tmp.succ;
  159.  
  160. }
  161. if(flag==1){
  162. System.out.println("Do you want to choose another destination? Enter Yes or No");
  163. s=bf.readLine();
  164. if(s.contentEquals("Yes"))
  165. {
  166. vacation(budget,vacation_list);
  167. }
  168. return 0;
  169. }
  170. System.out.println("You do not have sufficient funds for another destination.");
  171. return 0;
  172.  
  173. }
  174.  
  175. }
  176.  
  177.  
  178. Ako vnese nekoj london, LOndON, Great BARIER reef, so tolower ke go sredam, megjutoa gore kaj toa avaliable destinations koga gi printam i za your ticket price for this.city is, sakam da gi pecati pravilno kako sto treba.
  179.  
  180. London, Great Barier Reef
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement