#help my teacher rn is asking me

22 messages · Page 1 of 1 (latest)

lament kestrel
#

package finalscomprog;

import java.util.Scanner;

public class Finalscomprog {

public static void main(String[] args) {
   
    //final presentation..
    //Login
    //Assigned Program - Jewelry Shop Billing and Payment
    //Requirements of Program
    //*Array
    //*Conditional Statement
    //*Loop
    
    
    
    
    // Sign up, Get the variable for ruser and rpassword from the user
    Scanner io= new Scanner (System.in);
    
    System.out.println("Sign up");
    System.out.print("Enter your username: ");
    String ruser=io.nextLine();
    System.out.print("Enter your password: ");
    String rpassword=io.nextLine();
    Scanner scanner = new Scanner(System.in);
    System.out.println("User Registered");

    //Login, the ruser and rpassword variable must be the same to luser and lpass
    
    System.out.println("Enter your login details:");
    System.out.print("Enter username: ");
    String spare= io.nextLine();
    System.out.print("Enter password: ");
    String spare2= io.nextLine();
    
    
    
    
    // variables
    String[] products = {"Sapphire Necklace", "Forever Bracelet", "Diamond Earrings", "Emerald Ring"}; //Jewelries
    int[] prices = {320, 250, 475, 560}; // prices
    boolean[] availability = {true, true, true, true}; //availability of stock
    int[] cart = new int[4];// the picked items will be added to cart or the quantity of each product
    int total = 0; // The total value everything added on the cart
    boolean shopping = true; // used for checkout 

    // code to check if sign up and login are the same
    if (ruser.equals(spare) && rpassword.equals(spare2)) {
        System.out.println("Login successful!");
    } else {
        System.out.println("Login failed. Invalid username or password.");
        return;
        
        
    }
    
    
    // loop 
    while (shopping) {
        System.out.println("Welcome to ALAHAS IN BPSU! Please Choose a product:");
        for (int i = 0; i < products.length; i++) {
            if (availability[i]) {
                System.out.println((i + 1) + ". " + products[i] + " (Php" + prices[i] + ")");
            }
        }
        System.out.print("Enter product number (or 0 to check out): ");
        int choice = scanner.nextInt();
        if (choice == 0) {
            shopping = false;
            continue;
        }
        //continuation if user chose a product
        choice--;
        if (availability[choice]) {
            System.out.print("Enter quantity: ");
            int quantity = scanner.nextInt();
            cart[choice] += quantity;
            total += prices[choice] * quantity;                                 // overall total quantity
            System.out.println("Item added to cart!");
        
        }
    }

    //Receipt or Final PrintOut
    System.out.println("--- Receipt ---");
    System.out.println("-ALAHAS IN BPSU-");
    System.out.println("Owned & Operated by :LA ");
    
    for (int i = 0; i < products.length; i++) {
        if (cart[i] > 0) {
            System.out.println(products[i] + ": " + cart[i] + " x Php" + prices[i] + " = Php" + cart[i] * prices[i]);       //Final solution product: name +quantity + price = total
        }
    }

    System.out.println("Total: Php" + "                   = Php"+total);
    System.out.println("BPSU-CICT MAIN CAMPUS, Balanga City, Bataan 2100");
    System.out.println("This Receipt shall be valid for FIVE (5) years from the date of permit to use");
}

}

the thing is my teacher asked why did i put [4] here on my arrayint

tired perch
#

The 4 tells it how big the array should be. So it should have 4 items because you told it to create an array of length 4.

lament kestrel
#

but why does it also works if i put 10

#

when i only have 4 products

tired perch
#

Where'd you put 10?

lament kestrel
#

if i make it int[]cart = new int [10]

#

i run it and it still works the same

#

ummm like where is this connected to

#

the line of

tired perch
#

Because extra space doesn't hurt anything, just doesn't get used

lament kestrel
#

int[] cart = new int [4]

#

is it connected to

#

for (int i = 0; i < products.length; i++) {
if (cart[i] > 0) {
System.out.println(products[i] + ": " + cart[i] + " x Php" + prices[i] + " = Php" + cart[i] * prices[i]); //Final solution product: name +quantity + price = total
}

#

the

#

"products.length"

tired perch
#

It's not connected but so long as cart has at least as many items as the products array there won't be an error

lament kestrel
#

the question my teacher asked is

#

like why i put it in the code

#

like where is it connected to

tired perch
#

You have 4 products so you needed a cart of length 4

lament kestrel
#

which i have 4 products