-4

I'm trying to make a stock system, when a user enters a number of an item they want it's subtracted from the total available. How would I do this? I'm a beginner so I'm not too sure

For My code below trying to get the input of sofa and minus it from a total

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import java.text.DecimalFormat;
import java.util.Arrays;

/**
   RoutinePanel class

*/

public class RoutinePanel extends JPanel
{
   // Named constants for charges
       private final double SOFA_CHARGE = 599.99;
       private final double COMPUTER_DESK_CHARGE = 129.99;
       private final double COFFEE_TABLE_CHARGE = 40.0;
       private final double ARMCHAIR_CHARGE = 229.99;
       private final double TV_STAND_CHARGE = 37.0;
       private final double CUSHION_CHARGE = 8.0;
       private final double BED_CHARGE = 145.0;
       private final double MATRESS_CHARGE = 299.0;
       private final double DUVET_CHARGE = 24.99;
       private final double PILLOW_CHARGE = 9.99;


           int SofaTotal = 999;
       TextField ComputerDesk;       // Check box for lube job
       JTextField Armchair; // Check box for radiator flush
       JTextField CoffeeTable;    // Check box for transmission flush
       JTextField TvStand;    // Check box for inspection
       JTextField Cushion;       // Check box for muffler replacement
       JTextField Bed;  // Check box for tire rotation
       JTextField Matress ;
       JTextField Duvet;
       JTextField Pillow;
       JLabel SofaLbl;
       JTextField Sofa;
       private JLabel ArmchairLbl;
       private JLabel ComputerDeskLbl;
       private JLabel CoffeeTableLbl;
       private JLabel TvStandLbl;
       private JLabel CushionLbl;
       private JLabel BedLbl;
       private JLabel MatressLbl;
       private JLabel DuvetLbl;
       private JLabel PillowLbl;
       private Color Colour;
       private JButton b;


   /**
      Constructor
   */

   public RoutinePanel()
   {
      // Create a DecimalFormat object.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");

       Sofa = new JTextField("0");

      SofaLbl  = new JLabel("Sofa");
      ArmchairLbl = new JLabel("Armchair");
      Armchair = new JTextField("0");
      ComputerDesk = new TextField("0");
      ComputerDeskLbl = new JLabel("Computer Desk");
      CoffeeTable = new JTextField("0");
      CoffeeTableLbl = new JLabel("Coffee Table");
      TvStand = new JTextField ("0");
      TvStandLbl = new JLabel ("TV Stand");
      Cushion = new JTextField ("0");
      CushionLbl = new JLabel ("Cushion");
      Bed = new JTextField ("0");
      BedLbl = new JLabel("0");
      Matress = new JTextField("0");
      MatressLbl = new JLabel("Matress");
      Duvet = new JTextField("0");
      DuvetLbl = new JLabel("Duvet");
      Pillow = new JTextField("0");
      PillowLbl = new JLabel("Pillow"); 

      b = new JButton("Choose a Colour");
      b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event)
      { Colour = JColorChooser.showDialog(null, "Pick your Colour", Colour);
    if (Colour==null)
        Colour=(Colour.WHITE);

      }
      }

      );




      // Create a GridLayout manager.
      setLayout(new GridLayout(18, 3));

      // Create a border.
      setBorder(BorderFactory.createTitledBorder("Routine Services"));


      add(SofaLbl);
      add(Sofa);
      add(b);
      add(ArmchairLbl);
      add(Armchair);
      add(ComputerDeskLbl);
      add(ComputerDesk);
      add(CoffeeTableLbl);
      add(CoffeeTable);
      add(TvStandLbl);
      add(TvStand);
      add(CushionLbl);
      add(Cushion);
      add(BedLbl);
      add(Bed);
      add(MatressLbl);
      add(Matress);
      add(DuvetLbl);
      add(Duvet);
      add(PillowLbl);
      add(Pillow);







   }
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

It's pretty easy.You simply need to subtract the sofa requested by user from the total sofa and display it in the sofa label!

  int sofaRequested=Integer.parseInt(Sofa.getText());
  int sofaLeft=SofaTotal-sofaRequested;
  SofaLbl.setText("The total number of Sofa left in stock is "+sofaLeft);
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • This is perfect Described it and now i understand thanks a lot! One thing, it's not subtracting from the total it displays the total in the label though – user3403781 Jun 24 '14 at 20:09
  • @user3403781-What did you enter in the Sofa TextField? – Am_I_Helpful Jun 24 '14 at 20:18
  • I entered 10 but nothing happened – user3403781 Jun 24 '14 at 20:19
  • Where did you add this piece of code which I have written? – Am_I_Helpful Jun 24 '14 at 20:19
  • You need to add a button for getting the output! Or else, put it in the JButton b and input in text field and press JButton b; You'll get your output as desired! Also, you can write an ActionPerformed event for the Sofa Text Field like JButton b and feed my answer into it.It'll render the correct output! – Am_I_Helpful Jun 24 '14 at 20:21
  • @user3403781-You need not be thankful for such a minor help,it's just a service.If you need more,please add shekhar suman to your list to remember at bad times!BTW,you are most welcome for what you shouldn't have been! – Am_I_Helpful Jun 24 '14 at 20:38