Commit f41cfa35 authored by andrea.munoz-white's avatar andrea.munoz-white

pog

parents
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>VandingMachine_Andrea_MunozWhiteside</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jButton1" min="-2" max="-2" attributes="0"/>
<Component id="DropDownBox" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jScrollPane1" min="-2" pref="327" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="67" max="-2" attributes="0"/>
<Component id="Transaction1" min="-2" pref="186" max="-2" attributes="0"/>
</Group>
</Group>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="InputAmount" min="-2" pref="114" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="14" max="-2" attributes="0"/>
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
<Component id="Change" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="18" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="TotalCost" min="-2" max="-2" attributes="0"/>
<Component id="jButton2" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="35" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="33" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jScrollPane1" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="33" max="-2" attributes="0"/>
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="InputAmount" min="-2" pref="36" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="jButton2" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="98" max="-2" attributes="0"/>
<Component id="DropDownBox" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="50" max="-2" attributes="0"/>
<Component id="jButton1" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="19" max="32767" attributes="0"/>
<Component id="TotalCost" min="-2" pref="30" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="39" max="-2" attributes="0"/>
<Component id="Change" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="117" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace min="-2" pref="45" max="-2" attributes="0"/>
<Component id="Transaction1" min="-2" pref="38" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="35" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JComboBox" name="DropDownBox">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="6">
<StringItem index="0" value="Chocolate"/>
<StringItem index="1" value="Water"/>
<StringItem index="2" value="Sweet"/>
<StringItem index="3" value="Drink"/>
<StringItem index="4" value="Snack"/>
<StringItem index="5" value=" "/>
</StringArray>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="DropDownBoxActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="SelectedProducts">
<Properties>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JButton" name="jButton1">
<Properties>
<Property name="text" type="java.lang.String" value="Select item"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JTextField" name="InputAmount">
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="InputAmountActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="TotalCost">
<Properties>
<Property name="text" type="java.lang.String" value="Total Cost (&#xa3;)"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="jButton2">
<Properties>
<Property name="text" type="java.lang.String" value="Enter"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton2ActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="Transaction1">
</Component>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" value="Enter amount:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="Change">
<Properties>
<Property name="text" type="java.lang.String" value="Change (&#xa3;)"/>
</Properties>
</Component>
</SubComponents>
</Form>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author andrea.munoz-white
*/
import java.io.FileWriter;
//used to edit a file
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
//used to create a file
public class VendingMachine extends javax.swing.JFrame {
Object[][] VendingItems =
//Here I created a 2 dimensional array called VendingItems, I made it an object array so I can have different variables inside the array
{
//Code, Product, Price(£), Quantity
{"01", "Chocolate", (double)2.0, (Integer)20},
// The first two inputs into the array are strings, then I entered a double and then an integer
{"02", "Water", (double)0.5, (Integer)20},
{"03", "Drink", (double)1, (Integer)20},
{"04", "Snack", (double)2, (Integer)20},
{"05", "Sweet", (double)1, (Integer)20}
};
//Variables:
Double cost = 0.0;
Double amount;
Double change1 = 0.0;
int quantc = 0;
int quantw = 0;
int quantd = 0;
int quantsn = 0;
int quantsw = 0;
// Creates new form VendingMachine
public VendingMachine() {
initComponents();
}
private void emptytext(){
SelectedProducts.setText(null);
InputAmount.setText(null);
TotalCost.setText("Total Cost (£) - ");
//Creates a method that will clear out the Text Area, Text field and the label so the user can use the program again
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
DropDownBox = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
SelectedProducts = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
InputAmount = new javax.swing.JTextField();
TotalCost = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
Transaction1 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
Change = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
DropDownBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Chocolate", "Water", "Sweet", "Drink", "Snack", " " }));
DropDownBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DropDownBoxActionPerformed(evt);
}
});
SelectedProducts.setColumns(20);
SelectedProducts.setRows(5);
jScrollPane1.setViewportView(SelectedProducts);
jButton1.setText("Select item");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
InputAmount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
InputAmountActionPerformed(evt);
}
});
TotalCost.setText("Total Cost (£)");
jButton2.setText("Enter");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setText("Enter amount:");
Change.setText("Change (£)");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(DropDownBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(Transaction1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(InputAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(Change)))
.addContainerGap(18, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(TotalCost)
.addComponent(jButton2))
.addGap(35, 35, 35))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(InputAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(DropDownBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(jButton1)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addComponent(TotalCost, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)
.addComponent(Change)
.addGap(117, 117, 117)))
.addGap(45, 45, 45)
.addComponent(Transaction1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void InputAmountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InputAmountActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_InputAmountActionPerformed
private void DropDownBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DropDownBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_DropDownBoxActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String product = (String)DropDownBox.getSelectedItem( );
//Creates a variable called product that stores the item we have selected
for ( int i = 0; i < 5; ++i ) {
for ( int j = 0; j < 4; ++j ) {
//for loop to find the product name in the 2D array
if ( VendingItems[i][j] == product )
//if the product name in the 2D array equals to the prooduct name selected
{
if ("Chocolate".equals(product)){
//if the product selected is Chocolate
if((int)VendingItems[i][j+2] > quantc){
//This searches the quantity in the array of the specific product and searches if it's greater of quantc which is now set to 0
quantc +=1;
//ads one to quantc since we have slected one chocolate
SelectedProducts.append(product + "\n");
//prints the selected product to the text area, /n creates a new lie
cost += (double)VendingItems[i][j+1];
// this changes the string called cost to the variable inside the 2D array, it searches it by the specific product and adding one to j because of the position
TotalCost.setText("Total Cost (£) - " + cost);
//Modifies the lable called Total Cost to show the cost of the overall products.
}
}
if ("Water".equals(product)){
if((int)VendingItems[i][j+2] > quantw){
quantw +=1;
SelectedProducts.append(product + "\n");
cost += (double)VendingItems[i][j+1];
TotalCost.setText("Total Cost (£) - " + cost);
}}
if ("Drink".equals(product)){
if((int)VendingItems[i][j+2] > quantd){
quantd +=1;
SelectedProducts.append(product + "\n");
cost += (double)VendingItems[i][j+1];
TotalCost.setText("Total Cost (£) - " + cost);}}
if ("Snack".equals(product)){
if((int)VendingItems[i][j+2] > quantsn){
quantsn +=1;
SelectedProducts.append(product + "\n");
cost += (double)VendingItems[i][j+1];
TotalCost.setText("Total Cost (£) - " + cost);}}
if ("Sweet".equals(product)){
if((int)VendingItems[i][j+2] > quantsw){
quantsw +=1;
SelectedProducts.append(product + "\n");
cost += (double)VendingItems[i][j+1];
TotalCost.setText("Total Cost (£) - " + cost);}}
}
}
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
amount = Double.parseDouble(InputAmount.getText());
// gets the input from the text field and stores it into a Double called amount
if (cost <= amount) {
// if the cost of the overall products is smaller or equal to the amount entered
if((int)VendingItems[0][3] > quantc){
//checks the quantity of the specific product which in this case is Chocolate. This compares to see if it's greater than the amount selected
VendingItems[0][3] = ((int)VendingItems[0][3] - quantc);
//deletes the quantity selected to the available stock of the specific item
}
if((int)VendingItems[1][3] > quantw){
VendingItems[1][3] = ((int)VendingItems[1][3] - quantw);
}
if((int)VendingItems[2][3] > quantd){
VendingItems[2][3] = ((int)VendingItems[2][3] - quantd);
}
if((int)VendingItems[3][3] > quantsn){
VendingItems[3][3] = ((int)VendingItems[3][3] - quantsn);
}
if((int)VendingItems[4][3] > quantsw){
VendingItems[4][3] = ((int)VendingItems[4][3] - quantsw);
Transaction1.setText("Please take your drink");
//once all the possible items have been approved and the user has inputed the correct amount the program prints that the user can collect their products
change1 = amount - cost;
//This finds the change for the user
Change.setText("Change (£) - " + change1 );
//prints the change in the label called Change
File file = new File ("/Users/andrea.munoz-white/Documents/reciept.txt");
//Creates a file in the specified direcotry
FileWriter writer = new FileWriter(file);
writer.write("Order : \n" + SelectedProducts.getText() +
"\nEntered Amount (£): " + amount +
"\nTotal Price (£): " + cost +
"\nChange Due (£): " + change1);
writer.close();
//used this webpage for refrence https://www.journaldev.com/20891/java-filewriter-example
emptytext();
// calls the method empty text
} else{
Transaction1.setText("Insufficient amount");
// if the user input amount is less than the total amount then the program prints that the user doesn't have enough money.
}
}//GEN-LAST:event_jButton2ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(VendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VendingMachine().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel Change;
private javax.swing.JComboBox<String> DropDownBox;
private javax.swing.JTextField InputAmount;
private javax.swing.JTextArea SelectedProducts;
private javax.swing.JLabel TotalCost;
private javax.swing.JLabel Transaction1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment