Java help

Soldato
Joined
30 Aug 2009
Posts
8,104
Location
one nation under sony
scenario "Paula has decided to start offering quantity discounts for software. Paula wants to modify the program to handle quantity discounts. Now if some one buys 10 or more softwre of same title, Paula gives that customer 30% discount. A total of 7-9 results in 10% discount, and a total of 4-6 results in a 5% discount. A total of less than 4 results in no discount."

current program only only calculates V.A.T

Code:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class salesperson {
  
  public static void main (String [] args)
   
  {
      String scode, stitle, sprice, sqty;
      int code, title, price, qty;
      double vatamt,total,subtotal;
      final double VAT=.20;//VAT is set as a constant.
      JTextArea message= new JTextArea();
      
      JOptionPane.showMessageDialog(null, "Enter a new purchase to Proccess");
      // input- ask the users to input datas.
      scode=JOptionPane.showInputDialog("Please enter the code");
      stitle=JOptionPane.showInputDialog("Please enter the title");
      sprice=JOptionPane.showInputDialog("Please enter the price");
      sqty=JOptionPane.showInputDialog ("Please enter the quattity");
      //process
      // convert the inputed datas to integers.
      price=Integer.parseInt(sprice);
      qty=Integer.parseInt(sqty);
      // calculation.
      subtotal= price*qty;
      vatamt=subtotal*VAT;
      total= subtotal+vatamt;
      
     // rather than do a simply output like that...
      
      //output the datas.
      //System.out.println("The code: " + scode);
      //System.out.println("The Title: " + stitle);
      //System.out.println("The Price £: " + price);
      //System.out.println("The Quantity: " + qty);
      //System.out.println("The Subtotal £: " + subtotal);
      //System.out.println("The VAT amount £: " + vatamt);
      //System.out.println("The Final Total £: " + total);
      
      //I do create a effecient output as pop window.
      String output2 = "The Code: " + scode + "\nTitle: " + stitle +
              "\nThe price: " + price + "\nQuantity: " + qty +
              "\nThe Subtotal: " + subtotal + "\nThe VAT amount: " +
              vatamt + "\nThe Final Total: " + total;
              
              message.setText(output2);
      
      JOptionPane.showMessageDialog(null, message);
      
  //<editor-fold defaultstate="collapsed" desc="comment">
  }
  //</editor-fold>


I need help working out a code for the discount

I'm using Netbeans

thanks in advance
 
Last edited:
Code:
double disTotal; //Declared at the top with the rest of the variables

if (qty >= 10)
{
   disTotal = total * 0.30;
   total -= disTotal;
}

else if ((qty >= 7) && (qty <= 9))
{
   disTotal = total * 0.10;
   total -= disTotal;
}

else if ((qty >= 4) && (qty <= 6))
{
   disTotal = total * 0.05;
   total -= disTotal;
}

else
{
   total = total;
}

Think something like that would work. I've only just finished 1st year CS at Uni, so I'm sure there are much more sophisticated (and probably correct!) ways to do it.
 
Last edited:
Code:
double disTotal; //Declared at the top with the rest of the variables

if (qty >= 10)
{
   disTotal = total * 0.30;
   total -= disTotal;
}

else if ((qty >= 7) && (qty <= 9))
{
   disTotal = total * 0.10;
   total -= disTotal;
}

else if ((qty >= 4) && (qty <= 6))
{
   disTotal = total * 0.05;
   total -= disTotal;
}

else
{
   total = total;
}

Think something like that would work. I've only just finished 1st year CS at Uni, so I'm sure there are much more sophisticated (and probably correct!) ways to do it.
thanks

Price is staying the same it might be in wrong location of the program
 
Last edited:
Just gave it a test and it works for me using this layout:

Code:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class salesperson {
  
  public static void main (String [] args)
   
  {
      String scode, stitle, sprice, sqty;
      int code, title, price, qty;
      double vatamt,total,subtotal, disTotal;
      final double VAT=.20;//VAT is set as a constant.
      JTextArea message= new JTextArea();
      
      JOptionPane.showMessageDialog(null, "Enter a new purchase to Proccess");
      // input- ask the users to input datas.
      scode=JOptionPane.showInputDialog("Please enter the code");
      stitle=JOptionPane.showInputDialog("Please enter the title");
      sprice=JOptionPane.showInputDialog("Please enter the price");
      sqty=JOptionPane.showInputDialog ("Please enter the quantity");
      //process
      // convert the inputed datas to integers.
      price=Integer.parseInt(sprice);
      qty=Integer.parseInt(sqty);
      // calculation.
      subtotal= price*qty;
      vatamt=subtotal*VAT;
      total= subtotal+vatamt;
      
     // rather than do a simply output like that...
      
      //output the datas.
      //System.out.println("The code: " + scode);
      //System.out.println("The Title: " + stitle);
      //System.out.println("The Price £: " + price);
      //System.out.println("The Quantity: " + qty);
      //System.out.println("The Subtotal £: " + subtotal);
      //System.out.println("The VAT amount £: " + vatamt);
      //System.out.println("The Final Total £: " + total);
      
      if (qty >= 10)
      {
      	disTotal = total * 0.3;
      	total-=disTotal;
      }
      else if ((qty >=7) && (qty <= 9))
      {
      	disTotal = total * 0.1;
      	total -=disTotal;
      }
      else if ((qty >= 4) && (qty <= 6))
      {
      	disTotal = total * 0.05;
      	total -=disTotal;
      }
      else
      {
      	total = total;
      }
      
      //I do create a effecient output as pop window.
      String output2 = "The Code: " + scode + "\nTitle: " + stitle +
              "\nThe price: " + price + "\nQuantity: " + qty +
              "\nThe Subtotal: " + subtotal + "\nThe VAT amount: " +
              vatamt + "\nThe Final Total: " + total;
              
              message.setText(output2);
      
      JOptionPane.showMessageDialog(null, message);
      
  //<editor-fold defaultstate="collapsed" desc="comment">
  }
}
  //</editor-fold>

The program takes the discount of the total price, which includes VAT. Dunno if you wanted to remove the discount before the VAT is added, but its a simple change.
 
Just gave it a test and it works for me using this layout:

Code:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class salesperson {
  
  public static void main (String [] args)
   
  {
      String scode, stitle, sprice, sqty;
      int code, title, price, qty;
      double vatamt,total,subtotal, disTotal;
      final double VAT=.20;//VAT is set as a constant.
      JTextArea message= new JTextArea();
      
      JOptionPane.showMessageDialog(null, "Enter a new purchase to Proccess");
      // input- ask the users to input datas.
      scode=JOptionPane.showInputDialog("Please enter the code");
      stitle=JOptionPane.showInputDialog("Please enter the title");
      sprice=JOptionPane.showInputDialog("Please enter the price");
      sqty=JOptionPane.showInputDialog ("Please enter the quantity");
      //process
      // convert the inputed datas to integers.
      price=Integer.parseInt(sprice);
      qty=Integer.parseInt(sqty);
      // calculation.
      subtotal= price*qty;
      vatamt=subtotal*VAT;
      total= subtotal+vatamt;
      
     // rather than do a simply output like that...
      
      //output the datas.
      //System.out.println("The code: " + scode);
      //System.out.println("The Title: " + stitle);
      //System.out.println("The Price £: " + price);
      //System.out.println("The Quantity: " + qty);
      //System.out.println("The Subtotal £: " + subtotal);
      //System.out.println("The VAT amount £: " + vatamt);
      //System.out.println("The Final Total £: " + total);
      
      if (qty >= 10)
      {
      	disTotal = total * 0.3;
      	total-=disTotal;
      }
      else if ((qty >=7) && (qty <= 9))
      {
      	disTotal = total * 0.1;
      	total -=disTotal;
      }
      else if ((qty >= 4) && (qty <= 6))
      {
      	disTotal = total * 0.05;
      	total -=disTotal;
      }
      else
      {
      	total = total;
      }
      
      //I do create a effecient output as pop window.
      String output2 = "The Code: " + scode + "\nTitle: " + stitle +
              "\nThe price: " + price + "\nQuantity: " + qty +
              "\nThe Subtotal: " + subtotal + "\nThe VAT amount: " +
              vatamt + "\nThe Final Total: " + total;
              
              message.setText(output2);
      
      JOptionPane.showMessageDialog(null, message);
      
  //<editor-fold defaultstate="collapsed" desc="comment">
  }
}
  //</editor-fold>

The program takes the discount of the total price, which includes VAT. Dunno if you wanted to remove the discount before the VAT is added, but its a simple change.

whats the change?

thanks for the help
 
Going by the comments in the code, it sure looks like it!

I don't mind giving answers for stuff, but its better if the OP understands how it works rather than copying and pasting code. Pretty fundamental when it comes to programming.

I feel OP's pain, I hate Java with a passion. Much prefer scripting languages like PHP etc, faster dev and shorter code.
 
The main part of the coding section gets worse

Paula currently has a program that processes the sale of a single software title, including quantity discounts. Of course, a customer may wish to buy more than one title at a time, so Paula wants to modify the program to handle multiple software titles. The program should allow the user to enter a software ID, software title, the price, and the quantity. Paula wants the program to display these information and the subtotal for that software title, then continue by asking for the next software title. When finished with a particular order the program should output details and the number of different software titles purchased, in addition to the total for the order, including VAT. Paula wants to use the built-in currency formatting and creating methods to modularise the program, making it easier to read and understand for future modifications.
 
Back
Top Bottom