This is the question I was trying to answer:
Courier service. You have a courier service where the different routes are fixed. Create an
application (for example using array) where you can include (a fixed size) price table for different
distances your company carries packages. First ask for the user to input daily prices for all the
different routes. Then ask the user to input a departing and a destination for one delivery. Finally
provide the user with the correct price for the route.
The code I've got is below and it is working, but I want it to be that the user can input the daily prices (as asked in the exercise). I tried it with replacing the myNumbers part by
{
Scanner sc = new Scanner(System.in);
int myNumbers[] = new int[5];
for (int i=0;i<myNumbers.length;i++) {
System.out.println("Give a number");
myNumbers[i]=sc.nextInt();
}
}
That doesn´t work unfortunately. What should I do? Could anyone please help me? Thank you in advance!
import java.util.*;
public class CourierService {
public static int getPrice (int location, int destination) {
int [] [] myNumbers = {{0,10,33,99,11}, {10,0,88,55,11}, {33,88,0,66,11},
{99,55,66,0,11}};
int price = myNumbers[location] [destination];
return(price);
}
public static void main(String[] args ) {
int location;
int destination;
int price;
Scanner skanneri = new Scanner(System.in);
// read in users location
System.out.println("Welcome to the price calculator");
System.out.println("What is your location?");
System.out.println("0. Center ");
System.out.println("1. Kaisaniemi ");
System.out.println("2. Kamppi");
System.out.println("3. Eira ");
System.out.println("4. Kallio ");
System.out.println(" ");
System.out.println("Write number 0 to 4 ");
location=skanneri.nextInt();
// read in users destination
System.out.println("Welcome to the price calculator");
System.out.println("What is your location?");
System.out.println("0. Center ");
System.out.println("1. Kaisaniemi ");
System.out.println("2. Kamppi");
System.out.println("3. Eira ");
System.out.println("4. Kallio ");
System.out.println(" ");
System.out.println("Write number 0 to 4 ");
destination=skanneri.nextInt();
//find out the price and display it
price=getPrice(location, destination); //send to the getPrice
System.out.println(" This delivery costs "+price+" euros");
}
}