Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vacationTrip;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- class SLLNode {
- protected String city;
- protected int price;
- protected SLLNode succ;
- public SLLNode(String city,int price, SLLNode succ) {
- this.city = city;
- this.price=price;
- this.succ = succ;
- }
- }
- class SLL {
- private SLLNode first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public void insertFirst(String city, int price) {
- SLLNode ins = new SLLNode(city,price, first);
- first = ins;
- }
- public void insertLast(String city,int price) {
- if (first != null) {
- SLLNode tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode ins = new SLLNode(city, price, null);
- tmp.succ = ins;
- } else {
- insertFirst(city,price);
- }
- }
- public SLLNode getFirst() {
- return first;
- }
- public String delete(SLLNode node){
- if (first != null){
- SLLNode tmp = first;
- while (tmp.succ != node &&
- tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node){
- tmp.succ = tmp.succ.succ;
- return node.city;
- }
- // else throw Exception;
- }
- //else throw Exception;
- return null;
- }
- }
- public class Vacation {
- public static void main(String[] args) throws IOException {
- SLL vacation_list = new SLL();
- vacation_list.insertLast("Great Barier Reef", 100);
- vacation_list.insertLast("Paris", 50);
- vacation_list.insertLast("Bora Bora", 60);
- vacation_list.insertLast("Florence", 80);
- vacation_list.insertLast("Helsinki", 40);
- vacation_list.insertLast("London", 90);
- vacation_list.insertLast("Rome", 80);
- vacation_list.insertLast("New York City", 20);
- vacation_list.insertLast("Lost Angeles", 60);
- int budget=0;
- int answer=1;
- Scanner in = new Scanner(System.in);
- System.out.println("Insert your budget:");
- budget=in.nextInt();
- if(budget==0){
- System.out.println("You do not have sufficient funds");
- System.exit(0);
- }
- answer=vacation(budget,vacation_list);
- if(answer==1)
- {
- System.out.println("Happy travel");
- }
- else
- {
- System.out.println("Thank you for traveling with us.");
- }
- }
- private static int vacation(int budget, SLL vacation_list) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- String s=null;
- int answer=1;
- if(budget==0) return 0;
- System.out.println("Available cities for your bugdet are:");
- SLLNode tmp =vacation_list.getFirst();
- while(tmp!=null)
- {
- //if you want the price for return trip too you have 2 options
- //1. Enter double the price for the city when creating the list from line 92 and down
- //2.you can modify the code like tmp.price*2<budget
- if(tmp.price*2<=budget)
- {
- System.out.println(tmp.city);
- tmp=tmp.succ;
- }
- else
- tmp=tmp.succ;
- }
- //reseting temp to start from first node
- tmp=vacation_list.getFirst();
- System.out.println("Enter your destination:");
- s=bf.readLine();
- while(tmp!=null)
- {
- if(s.contentEquals(tmp.city))
- {
- //if you go with option 2, you should modify here tmp.price*2 and in the println too
- budget-=tmp.price*2;
- System.out.println("Your ticket price for "+ tmp.city+ " is: "+tmp.price*2);
- System.out.println("Your remaining budget is: "+budget);
- break;
- }
- tmp=tmp.succ;
- }
- tmp=vacation_list.getFirst();
- int flag=0;
- while(tmp!=null)
- {
- if(budget>tmp.price*2)
- {
- tmp=tmp.succ;
- flag=1;
- continue;
- }
- tmp=tmp.succ;
- }
- if(flag==1){
- System.out.println("Do you want to choose another destination? Enter Yes or No");
- s=bf.readLine();
- if(s.contentEquals("Yes"))
- {
- vacation(budget,vacation_list);
- }
- return 0;
- }
- System.out.println("You do not have sufficient funds for another destination.");
- return 0;
- }
- }
- 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.
- London, Great Barier Reef
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement