Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program uses a text file input.txt containing the information below: Bobby J

ID: 3621420 • Letter: T

Question

The program uses a text file input.txt containing the information below:

Bobby Jones 1 15.30
Dorthy Smith 2 12.30
John Tolbert 3 11.90
Adam Johnson 2 10.30
Michael Daniel 2 18.55
Gilbert Jones 3 13.97
_______________________________
The text file contains student name, student type, and plan rate.

The text from the file above is to be read and displayed in combobox but displayed in the following format last name, first initial (Jones, B) with the default as select student.
Also there needs to be a text box for the user to input the number of weeks the person purchased.
The column of text containing a 1, 2, or 3 identifies the student type as freshman, sophomore or junior, respectively.
There also needs to be a clear button which clears the text box.
_____________________________________________________
MEAL PLAN CALCULATION
The total cost of the meal plan is calculated as follows:

Student Type 1 – Freshman
Plan Cost = (Plan Rate * 9) + (Plan Rate * (Weeks Used > 10))
A Freshman automatically pays for 9 weeks, even if they did not use that the meal plan that many weeks. Additionally, a Freshman pays the regular plan rate for any weeks over 10.

Student Type 2 – SophomoreJunior:
Plan Cost = (Plan Rate * Weeks Used) + (Plan Rate * 0.2 * (Weeks Used > 10))

A SophmoreJunior pays the plan rate for each week of use for the first 10 weeks, then “a fifth more” for all additional weeks used over 10.

Student Type 3 – Senior:
Plan Cost = (Plan Rate * Weeks Used)
_________________________________________
The results should show in a text field on the GUI panel:

_______________________________________________________

| |Please select student| <===combobox |___| <==textbox

|Student Name: First Last Name (from input.txt) <==output in text fields on panel
|Student Type: Freshman, SophomoreJunior, or Senior |
|Plan Rate: (from input.txt) |
|Calculated Plan Cost: Plan Cost |
| |

| _____ |

| |clear| |

|_______________________________________________|

Above is a rough of what the GUI should look like containing a combobox, textbox, textfields and clear button.

Below is the code that I have, the names show up in the drop down menu of the combo box but not in the correct format of last, first initial(the text file that is read from cannot be changed). There is no textbox for user input of weeks meal plan used and no text fields for the output.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class JComboBoxSample extends JFrame {
private JComboBox comboBox;
private JButton jbtclr;

//main method
public static void main(String[] args) {
new JComboBoxSample();
}

public JComboBoxSample() {
super("Meal Plan");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400, 400);
setLocationRelativeTo(null);

JPanel jpTextFields = new JPanel();


comboBox = new JComboBox();
add(comboBox);

jbtclr = new JButton("Clear");
jbtclr.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
displaySelectedItem();
}
});
add(jbtclr);

loadDataFromFile();
setVisible(true);
}

private void displaySelectedItem() {
Object item = comboBox.getSelectedItem();
if (item == null) {
JOptionPane.showMessageDialog(null, "No item selected");
} else {
// JOptionPane.showMessageDialog(null, item.toString());
}
}

private void loadDataFromFile() {
Scanner fileReader = new Scanner(getClass().getResourceAsStream(
"input.txt"));
try {
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (fileReader.hasNextLine()) {
model.addElement(fileReader.nextLine());
}
comboBox.setModel(model);
} finally {
fileReader.close();
}
}
}

Explanation / Answer

Dear, Tried a lot and able help you with only combo box data private void loadDataFromFile() { Scanner fileReader = new Scanner(getClass().getResourceAsStream( "input.txt")); try { DefaultComboBoxModel model = new DefaultComboBoxModel(); while (fileReader.hasNextLine()) { String str= fileReader.nextLine(); StringTokenizer tokens=new StringTokenizer(str); char letter=str.charAt(0); String str1=tokens.nextTokens(); str1=tokens.nextTokens(); str1=str1+" "+letter; model.addElement(str1); } comboBox.setModel(model); } finally { fileReader.close(); } Hope this will help you.