QuestionHelpTrying to figure out how to code this program correctly

Posts 15 of 5 · Page 1 of 1
Trying to figure out how to code this program correctly
My program is based on Shipping Charges and what I have to do is tell the user how much tax is due and this will be based on the amount of miles the package travels as well as the weight. Now, I have the code written out, however, the problem I face is that for every 1000 miles traveled the tax goes up. The charges are as seen below:

*************** Table of Charges ******************
Rate per 1000
Weight of Package (Kg) Miles Shipped
************************************************** ***
1 Kg or less $1.70
Over 1 Kg and <= 5 Kg $2.20
Over 5 Kg and <= 10 Kg $6.70
Over 10 Kg $9.80

Cost will be doubled for distance > 1000 miles

So, for instance, if I had a package that weighed 1kg and traveled 2000 miles, its should be: 3.40.

So, my question is how do I have the program determine the tax of the object for every 1000 miles traveled? I thought about using if or switch statements, but that just seemed excessive and there must be a way to tell java for every thousand miles traveled multiply the tax by that number.

What I have so far:

Code:
import java.util.Scanner;
import java.math.*;
public class MyShippingCharges3{
private static double WeightOfPackage;
private static int MilesShipped;
private static double tax = 1.7;
private static double tax1 = 2.2;
private static double tax2 = 6.7;
private static double tax3 = 9.8;
private static double result;
   public static void main(String [] args){
      readData();
      calculations();
      printOutput();
      displayTable();
   }
      
   public static void readData(){
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter weight of package in KG: ");
      WeightOfPackage = sc.nextDouble();
      System.out.print("Enter how many miles the package will travel: ");
      MilesShipped = sc.nextInt();
   }
   
   public static void calculations(){
        if (WeightOfPackage <= 1 && MilesShipped <= 1000){
         result = tax;
            if (MilesShipped > 1000 && MilesShipped < 2000){
               result = tax * 2;
               }
         }
        
        else if (WeightOfPackage > 1 && WeightOfPackage <= 5) {
            result = 1000 / MilesShipped * tax1;
        } else if (WeightOfPackage > 5 && WeightOfPackage <= 10) {
            result = 1000 / MilesShipped * tax2;
        } else if (WeightOfPackage > 10) {
            result = 1000 / MilesShipped * tax3;
        }    }
   
   public static void printOutput(){
       System.out.print("Your cost is: " + result);
   
   }
   
   public static void displayTable(){
   
   
   }
   
}
The calculations method varies slightly because I was trying different things and for some reason, my first calculation outputs 0.0?
hmm it does look like you are taking a slightly more complicated rout than i would have taken and your syntax is something out of the ordinary... but to be honest there is no correct rout there is only a correct answer. now are you using an ide? if so i would recommend looking into the debugger it can help you out a ton. but for the sake of this answer i will tell you whats wrong. Here's what i did :

Code:
import java.util.Scanner;

public class MyShippingCharges {
	
	public static void main(String[] args) {
		
		//initialize variables (this is done within main because its unnessessary to have so many methods)
		int milesShipped;
		double weightOfPackage, tax = 1.7, tax1 = 2.2, tax2 = 6.7, tax3 = 9.8, result;
		
		//ask user input
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the weight of package in KG :");
		weightOfPackage = input.nextDouble();
		System.out.println("Enter distance shipped in miles :");
		milesShipped = input.nextInt();
		
		//first lets check how far the item traveled so we can get our multiplier
		float multiplier = 0;
		
		if (milesShipped <= 1000){
			multiplier = 1;
		}else {
			multiplier = milesShipped / 1000;
		}
		
		//second ill check the weight of the package
		if (weightOfPackage >= 10){
			//now multiply the tax by the (multiplier)
			
			result = multiplier*tax3;
			
		}else if (weightOfPackage >= 5){
			
			result = multiplier*tax2;
		
		}else if (weightOfPackage >= 1){
			
			result = multiplier*tax1;
		
		}else {
			
			result = multiplier*tax;
		}
		System.out.println("Your extimated cost will be : "+result);
		System.out.println("Thanks for using P4$$1V3's tax calculator!");
	}	
}
Please leave a thanks if this helped you
Quote Originally Posted by P4$$1V3 View Post
but for the sake of this answer i will tell you whats wrong
You haven't told OP what was wrong, nor have you provided information on what differs your version from his'. (An explanation I suppose)

Not to mention the style you have taken in your code is rather worse than the original's; You have jammed all the code in main, which is not an efficient way to work (unpleasant to look at, harder to modify & is bad on a larger scale)
Quote Originally Posted by Mayion View Post

You haven't told OP what was wrong, nor have you provided information on what differs your version from his'. (An explanation I suppose)

Not to mention the style you have taken in your code is rather worse than the original's; You have jammed all the code in main, which is not an efficient way to work (unpleasant to look at, harder to modify & is bad on a larger scale)
Oh sorry about that bro, it seemed like op was a beginner and possibly looking for help on homework or something so i just quickly coded up a very simple version of what he wanted. also please explain to me in detail how my rout is inefficient. For a a program this size its impossible to tell any difference in speed / time. also i did leave a few bread crumbs in my code on how it was done but you are right i didnt fix his code instead i just made my own and gave it to him which is pretty stupid.

also how is this unpleasant to look at? like wtf are you saying op's original code was easier to look at??? lol like who's the judge on how your code looks. as my philosophy / programming teacher in high-school said there is no correct rout there is only a correct answer... so what its all packed in main it works and pretty damn well.
"programming teacher in high-school said there is no correct rout there is only a correct answer"

Thats for starters.
Why?Example : God object
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?