HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Program for a packing company

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
companyprogramforpacking

Problem

It's my first year in college studying Java so please excuse my ignorance in programming. This is my 5th Task in my Sequential Programming assignment, where we had to design a program for a packing company to place orders and print a receipt. Could you please give me any advice you may have on my code?

```
package Lab;

import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
import javax.swing.UIManager.*;

public class Taskk5 extends JFrame {

private static String seller, qStr;
private static int quantity = 0, ppu = 0, bdiscount = 0, discount = 0, cinput, input;
int price = quantity ppu, TotalCost = price - (price / 100 discount);

//============================================== instance variables
JTextArea print = new JTextArea(10, 40);
JTextArea nopurchase = new JTextArea(10, 10);

//====================================================== constructor
public Taskk5() {
DecimalFormat d = new DecimalFormat("'£'0.00");
DecimalFormat p = new DecimalFormat("0'%'");

//... Set textarea's initial text, scrolling, and border.
print.setText("Type of Seller\tQuantity \tPrice per Unit\n"
+ "----------------------------------"
+ "--------------------------------\n"
+ seller + "\t " + quantity + " \t " + ppu
+ "\n--------------------------------"
+ "----------------------------------"
+ "\nSpecial Customer Discount: " + p.format(discount)
+ "\n--------------------------------"
+ "----------------------------------\n"
+ "\nTotal Cost: " + d.format(TotalCost));

JScrollPane scrollingArea = new JScrollPane(print);

//... Get the content pane, set layout, add to center
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scroll

Solution

By no means an exhaustive review, but here are a few general pointers:

  • Break up the code responsibilities into methods.



  • This not only makes your code more readable, but much easier to test and debug!



  • Use variable names that have meaning to people other than yourself.



  • This makes it easier for you to understand your code if you should ever need to come back to it later. It also makes it easier to get help (like now!)



  • eg. static variable qStr on line 12, d and p on lines 22 and 23.



  • Don't use Object as the type if you don't need to. You know these are going to be String arrays.



  • Magic numbers = bad



  • I know that you're not using float or double values anywhere, but since you're dealing with money it made me think of it as something you can just keep in the back of your mind for the future: https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency



  • I know it can be tempting, but try not to make all your variables private static... if they don't need to be! It definitely makes things convenient when you're new to programming, but it can get you in a lot of trouble down the road. https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil



  • Miscellaneous:



  • Extra ; on 157, 160, etc...



  • bdiscount is never used



  • Line 105 has a comment about Fahrenheit and Celsius :)



I have very little experience with Swing, so I can't really comment on that. Also, sorry if my line numbers are off, I just copied and pasted pretty quickly... Hope this helps!

Context

StackExchange Code Review Q#84151, answer score: 4

Revisions (0)

No revisions yet.