Question

In: Computer Science

Please fix this code I am having issues compiling it all together there is 3 codes...

Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it.

package assignement2;

import java.util.ArrayList;
import java.util.Scanner;

public class reg1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int number = input.nextInt();
input.nextLine();
for (int i = 0; i < number; i++) {
System.out.print("Enter item name: ");
int n = input.nextInt();
System.out.print("Enter cost of item: ");
double costs = input.nextDouble();
input.nextLine();
ArrayList<Item> items = new ArrayList<>();
ArrayList<Payment> paymentDone = new ArrayList<>();
boolean add = items.add(new Item(n,costs));
}
double total = 0.0;
int items = input.nextInt();
   for (Item i : items) {
total += i.getCost();
}
final double subT = total;
final double salesTax = 0.07 * subT;
final double totalPaymentToBeDone = subT + salesTax;
int itemNumber = 1;
for (Item i : items) {
System.out.println("Item " + itemNumber + ": " + i.getName() + ": " + i.getCost());
itemNumber += 1;
}
System.out.println("Total: " + totalPaymentToBeDone);
boolean fullPaymentDone = false;
double amountDue;
double total1 = totalPaymentToBeDone;
while (!fullPaymentDone) {
System.out.println("Please enter payment type.\n1. Cash\n2. Debit Card\n3. Credit card\n4. Check");
int choice = input.nextInt();
input.nextLine();
System.out.println("Enter the amount to pay with this type.");
double amount = input.nextDouble();
amountDue = total1 - amount;
double change = 0.0;
if (amountDue < 0)
change = Math.abs(amountDue);
System.out.println("Total after payment: " + amountDue + "\n");
if (amountDue > 0.0) {
total = amountDue;
continue;
} else {

for (Item i : items) {
System.out.println(((Item) i).getName() + ": " + i.getCost());
}
System.out.print("-----------------------------------------\n");
System.out.println("Subtotal:\t" + subT);
System.out.println("Tax: \t" + salesTax);
System.out.println("Total: \t" + totalPaymentToBeDone);

System.out.println("Change: \t" + change);
fullPaymentDone = true;
}
}
input.close();
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

package assignement2;

public class Payment {
   private double amount;
   private PaymentType paymentType;
   public Payment(double amount, PaymentType paymentType) {
   this.amount = amount;
   this.paymentType = paymentType;
   }
   public double getAmount() {
   return amount;
   }
   public void setAmount(double amount) {
   this.amount = amount;
   }
   public PaymentType getPaymentType() {
   return paymentType;
   }
   public void setPaymentType(PaymentType paymentType) {
   this.paymentType = paymentType;
   }   
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package assignement2;

public class Item {

private String name;
private double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}   
}

Solutions

Expert Solution

This is the code of your question which fix all the errors, I mentioned below with all the changes with comments so that you can easily understand the code. Copy and paste the code in java IDE or compiler and run it. I also provide the screenshots of output.

reg1.java :-

package assignement2;

import java.util.ArrayList;
import java.util.Scanner;

public class reg1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int number = input.nextInt();

/* declare arraylist outside the scope because
if you declare arraylist inside then it only accesible within scope */
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < number; i++) {
System.out.print("Enter item name: ");
String n = input.next();
System.out.print("Enter cost of item: ");
double costs = input.nextDouble();
boolean add = items.add(new Item(n,costs));
}
double total = 0.0;
for (Item i : items) {
total += i.getCost();
}
final double subT = total;
final double salesTax = 0.07 * subT;
final double totalPaymentToBeDone = subT + salesTax;
int itemNumber = 1;
for (Item i : items) {
System.out.println("Item " + itemNumber + ": " + i.getName() + ": " + i.getCost());
itemNumber += 1;
}
System.out.println("Total: " + totalPaymentToBeDone);
boolean fullPaymentDone = false;
double amountDue;
double total1 = totalPaymentToBeDone;
while (!fullPaymentDone) {
System.out.println("Please enter payment type.\n1. Cash\n2. Debit Card\n3. Credit card\n4. Check");
int choice = input.nextInt();
System.out.println("Enter the amount to pay with this type.");
double amount = input.nextDouble();
Payment paymentdone=new Payment(amount,choice);
amountDue = total1 - amount;
double change = 0.0;
if (amountDue < 0)
change = Math.abs(amountDue);
System.out.println("Total after payment: " + amountDue + "\n");
if (amountDue > 0.0) {
total = amountDue;
continue;
}
else {

for (Item i : items) {
System.out.println(((Item) i).getName() + ": " + i.getCost());
}
System.out.print("-----------------------------------------\n");
System.out.println("Subtotal:\t" + subT);
System.out.println("Tax: \t" + salesTax);
System.out.println("Total: \t" + totalPaymentToBeDone);

System.out.println("Change: \t" + change);
fullPaymentDone = true;
}
}
input.close();
}
}

Item.java :-

package assignement2;
public class Item {
private String name;
private double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}   
}

Payment.java :-

package assignement2;
public class Payment {
private double amount;
// take return type of variable paymentType as int
private int paymentType;
public Payment(double amount, int paymentType) {
this.amount = amount;
this.paymentType = paymentType;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public int getPaymentType() {
return paymentType;
}
public void setPaymentType(int paymentType) {
this.paymentType = paymentType;
}   
}

Screenshot of output :-


Related Solutions

I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
I have a project due and am having problems compiling a program: The only program I...
I have a project due and am having problems compiling a program: The only program I have written is check.c everything else is given and correct. Can you modify check.c to simply print out the chessboard, in other words, to get it to compile by any means thank you. The only thing you have to modify again is check.c nothing else. Just get it to print something thanks. program chess.c #include #include #include "chess.h" void get_valid_move(int mover) { int x_from,...
I have the below program and I am having issues putting all the output in one...
I have the below program and I am having issues putting all the output in one line instead of two. how can I transform the program for the user enter format ( HH:MM) Like: Enter time using 24 format ( HH:MM) : " and the user enter format " 14:25 then the program convert. instead of doing two lines make all one line. Currently I have the user enter first the Hour and then the minutes but i'd like to...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
My code does not compile, I am using vim on terminal and got several compiling errors...
My code does not compile, I am using vim on terminal and got several compiling errors this is C++ language I need help fixing my code below is the example expected run and my code. Example run (User inputs are highlighted): Enter your monthly salary: 5000 Enter number of months you worked in the past year: 10 Enter the cost of the car: 36000 Enter number of cars you’ve sold in the past year: 30 Enter number of misconducts observed...
Python 3 Fix code everytime i hit submit all the fields in the fomr should be...
Python 3 Fix code everytime i hit submit all the fields in the fomr should be cleaned # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1,...
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
This needs to be done in Excel and that is where I am having issues Healthy...
This needs to be done in Excel and that is where I am having issues Healthy Snacks Co. produces snack mixes. Recently, the company has decided to introduce a new snack mix that has peanuts, raisins, pretzels, dries cranberries, sunflower seeds and pistachios. Each bag of the new snack is designed in order to hold 250 grams of the snack. The company has decided to market the new product with a emphasis on its health benefits. After consulting nutritionists, Healthy...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT