Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2016 05:09
Show Gist options
  • Save anonymous/015022f7ad55ff9664e2edaea6a58d5a to your computer and use it in GitHub Desktop.
Save anonymous/015022f7ad55ff9664e2edaea6a58d5a to your computer and use it in GitHub Desktop.
//this is my current code
//how do i make it bold?
//http://stackoverflow.com/questions/41102850/changing-jtable-row-font-to-bold/41103969
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class TableForPrice extends JPanel {
int minPeople = 10;
int maxPeople = 36;
int carersNum = 2;
int packageType;
int NumPeople = Integer.parseInt(JOptionPane.showInputDialog("How many people want to go to the outing?"));
JTable jt;
public TableForPrice() {
if (NumPeople > 24) {
carersNum = 3;
}
NumPeople = NumPeople + carersNum;
JOptionPane.showMessageDialog(null,
"The number of people has been updated to " + NumPeople + " to include the carers");
// end the program if there is out of range people
if (NumPeople < 10) {
JOptionPane.showMessageDialog(null, "There are not enough people for the outing");
System.exit(0);
} else if (NumPeople > 36) {
JOptionPane.showMessageDialog(null, "There are too many people for the outing");
System.exit(0);
}
// type of package - small/medium/large
if (NumPeople <= 16) {
packageType = 1;
} else if (NumPeople <= 26) {
packageType = 2;
} else {
packageType = 3;
}
double priceT1 = 0.0;
// calculate price Task 1
if (packageType == 1) {
priceT1 = (NumPeople * 14) + (NumPeople * 21) + 150;
} else if (packageType == 2) {
priceT1 = (NumPeople * 13.5) + (NumPeople * 20) + 190;
} else if (packageType == 3) {
priceT1 = (NumPeople * 13) + (NumPeople * 19) + 225;
}
String[] columns = { "Number of People", "Hire of Coach", "Cost per meal", "Cost per theatre ticket" };
String[][] data = { { "12-16", "$150", "$14", "$21" }, { "17-26", "$190", "$13.50", "$20" },
{ "27-39", "$225", "$13", "$19" } };
// add JLabels to show the "total cost for x people"
JLabel lblTotalCostFor = new JLabel("Total Cost for");
JLabel numPeople = new JLabel(String.valueOf(NumPeople));
JLabel People = new JLabel("People: $");
JLabel Price = new JLabel(String.valueOf(priceT1));
jt = new JTable(data, columns) {
public boolean isCellEditable(int data, int columns) {
return false;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns) {
Component c = super.prepareRenderer(r, data, columns);
return c;
}
};
jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
jt.setPreferredScrollableViewportSize(new Dimension(540, 48));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
// the items are shown in order so they need to be added in order
add(jps);
add(lblTotalCostFor);
add(numPeople);
add(People);
add(Price);
}
public static void main(String[] args) {
JFrame jf = new JFrame("Cost of Outing");
TableForPrice t = new TableForPrice();
jf.setSize(570, 250);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment