Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.
Is there any way I can translate this into python?
Is there any way I can translate this into python?
public class assn4
{
// variables for the amount of denominations in the register
public static Integer ones = new Integer(0);
public static Integer fives = new Integer(0);
public static Integer tens = new Integer(0);
public static Integer twenties = new Integer(0);
public static Integer total = new Integer(0);
public static Integer sales = new Integer(0);
// variables for the denominations given in the input
public static Integer inOnes = new Integer(0);
public static Integer inFives = new Integer(0);
public static Integer inTens = new Integer(0);
public static Integer inTwenties = new Integer(0);
/*
This function takes in the command line arguments and does 2 things.
It checks if the arguments were valid input and if so it calls the
appropriate methods to deal with it.
*/
public static void checkInput(String[] args)
{
if (args.length == 0) { // if there are no arguments, the arguments are invalid
System.exit(1);
}
int equalPos = java.util.Arrays.asList(args).indexOf("="); // finds the position of = in arguments
if(!args[0].equals("report") && equalPos == -1) { // if the action isn't report and there is no equals sign
System.exit(1); // exit since the arguments are invalid
}
if(args.length == 1) { // if there is only one argument, the only valid argument would be "report"
if(args[0].equals("report")) {
report();
return;
} else {
System.exit(1);
}
}
// split the argument array into 2
String[] leftSide = Arrays.copyOfRange(args, 0, equalPos);
String[] rightSide = Arrays.copyOfRange(args, equalPos+1, args.length);
if(leftSide[0].equals("init")) { // if the command is init
if(leftSide.length == 2 && isInteger(leftSide[1]) &&
rightSide.length > 0 && rightSide.length <= 4) { // check right number of arguments
for (String s : rightSide) { // make sure right side arguments are integers
boolean isInt = isInteger(s);
if(!isInt) {
System.exit(1);
}
}
init(leftSide, rightSide);
return;
} else {
System.exit(1);
}
}
if(leftSide[0].equals("purchase")) {
if(leftSide.length == 2 && isInteger(leftSide[1])
&& rightSide.length > 0 && rightSide.length <= 4) {
for (String s : rightSide) {
boolean isInt = isInteger(s);
if(!isInt) {
System.exit(1);
}
}
purchase(leftSide, rightSide);
return;
} else {
System.exit(1);
}
}
if(leftSide[0].equals("change")) {
if(leftSide.length > 1 && leftSide.length <= 5 &&
rightSide.length > 0 && rightSide.length <= 4) {
for (String s : rightSide) { // make sure right side args are ints
boolean isInt = isInteger(s);
if(!isInt) {
System.exit(1);
}
}
for (String s : leftSide) { // make sure left side args are ints
boolean isInt = isInteger(s);
if(!isInt && !s.equals("change")) { // handle "change" not being an int
System.exit(1);
}
}
change(leftSide, rightSide);
return;
} else {
System.exit(1);
}
}
System.exit(1); // if the first argument wasn't a defined command, the arguments are invalid
}
/*
This function takes a string and determines if it is an integer or not.
It does this by checking if each character in the string is a digit. If
one character is not a digit, the string is not an integer.
It returns true if s is equal to an integer, false otherwise
*/
public static boolean isInteger(String s)
{
for (int i=0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
return false;
}
}
return true;
}
/*
This method is called when the command "report" is used. It retreives the drawer
contents then prints them in the format:
Sales : Total = Ones Fives Tens Twenties
*/
public static void report()
{
getDrawer();
System.out.println(sales + " : " + total + " = " + ones + " " + fives +
" " + tens + " " + twenties);
System.exit(0);
}
/*
This function reads from a file named "drawer.txt" to get the information of the current
drawer. If the file does not exist or it cannot read the file it will exit the program with
a exit status of 4.
*/
public static void getDrawer()
{
String fileName = "drawer.txt";
String line;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<Integer> list = new ArrayList<Integer>();
while((line = bufferedReader.readLine()) != null) {
list.add(Integer.parseInt(line));
}
ones = list.get(0);
fives = list.get(1);
tens = list.get(2);
twenties = list.get(3);
total = list.get(4);
sales = list.get(5);
} catch(FileNotFoundException ex) {
System.exit(4);
} catch(IOException ex) {
System.exit(4);
}
}
/*
This function parses the arguments that were input to the right side of the equals sign.
Depending on the number of arguments, it sets the denominations apporpriately.
*/
public static void getInputDenom(String[] rightSide)
{
if(rightSide.length > 0) {
inOnes = Integer.parseInt(rightSide[0]);
if(rightSide.length > 1) {
inFives = Integer.parseInt(rightSide[1]);
if(rightSide.length > 2) {
inTens = Integer.parseInt(rightSide[2]);
if (rightSide.length > 3) {
inTwenties = Integer.parseInt(rightSide[3]);
}
}
}
}
}
/*
This function is called when the command for the register is "init". It takes the command line arguments
split into two as arguments. It then checks to make sure the arguments jibe. If they do, it writes the
info to the file. If not, it exits with a status of 0.
*/
public static void init(String[] leftSide, String[] rightSide)
{
getInputDenom(rightSide); // get the denominations of bills from the right side
int calcTotal = inOnes + (5 * inFives) + (10 * inTens) + (20 * inTwenties);
int leftTotal = Integer.parseInt(leftSide[1]);
if(calcTotal == leftTotal) {
writeFile(inOnes, inFives, inTens, inTwenties, calcTotal, sales);
System.exit(0);
} else {
System.exit(2);
}
}
/*
This method is invoked when the command to the register is "purchase". It takes the argument array
split into two as arguments. It gets the denominations from the input as well as the current status
of the drawer. If the purchase price is equal to or smaller than the money given, it calls the
function to dispens change.
*/
public static void purchase(String[] leftSide, String[] rightSide)
{
getInputDenom(rightSide);
getDrawer();
int calcTotal = inOnes + (5 * inFives) + (10 * inTens) + (20 * inTwenties);
int purchaseAmount = Integer.parseInt(leftSide[1]);
if(calcTotal >= purchaseAmount) {
dispenseChange(calcTotal, purchaseAmount);
System.exit(0);
} else {
System.exit(2);
}
}
/*
This function dispenses the change for the purchase if it is possible. It takes the total amount of
money paid and the purchase amount as arguments. Given the cash in the drawer, it then goes through
every possible combination of change (from highest denominations to lowest). Once it hits a match
where the combination of bills in the drawer matches the necessary change to be dispensed, it prints the
change given and then updates the drawer contents appropriately.
*/
public static void dispenseChange(int calcTotal, int purchaseAmount)
{
// adds the given bills to the drawer in case they can be used to make change
ones += inOnes;
fives += inFives;
tens += inTens;
twenties += inTwenties;
total += calcTotal;
int totalChange = calcTotal - purchaseAmount; // change needed to be dispersed is the money given - price
int tmpChange;
boolean solutionFound = false; // flag to see if a solution was found
outerLoop: // used to break from entire loop once solution is found
for (int i = twenties; i >= 0; i--) {
for(int j = tens; j>=0; j--) {
for(int k = fives; k>=0; k--) {
for(int o = ones; o>=0; o--) {
tmpChange = totalChange;
if(i != 0 && (i*20) <= tmpChange) {
tmpChange = tmpChange % (i*20) ;
}
if(j != 0 && (i*10) <= tmpChange) {
tmpChange = tmpChange % (j*10);
}
if(k != 0 && (i*5) <= tmpChange) {
tmpChange = tmpChange % (k*5);
}
if(o != 0) {
tmpChange = tmpChange % o;
}
if(tmpChange == 0 && totalChange ==
((20*i) + (10*j) + (5*k) + o)) {
ones -= o;
fives -= k;
tens -= j;
twenties -= i;
System.out.println(o + " " + k + " " +
j + " " + i);
solutionFound = true;
break outerLoop;
}
}
}
}
}
if(!solutionFound) {
System.exit(3);
}
total -= totalChange;
sales += purchaseAmount;
writeFile(ones, fives, tens, twenties, total, sales);
return;
}
/*
This method gives back the requested change as long as they are requesting an equal amount of money
and the drawer has the denominations that they requested. It takes the argument array split into two
as the arguments. It then gets the drawer contents, the denominations they give, and the denominations
they request. Those are stored in variables named in the style:
ones - content in drawer inOnes - requested denominations givenOnes - given denominations
If the change can be given, it prints out what change was given and writes to the file the updated
drawer.
*/
public static void change(String[] leftSide, String[] rightSide)
{
getInputDenom(rightSide);
getDrawer();
int givenOnes = 0;
int givenFives = 0;
int givenTens = 0;
int givenTwenties = 0;
if(leftSide.length > 1) {
givenOnes = Integer.parseInt(leftSide[1]);
if(leftSide.length > 2) {
givenFives = Integer.parseInt(leftSide[2]);
if(leftSide.length > 3) {
givenTens = Integer.parseInt(leftSide[3]);
if (leftSide.length > 4) {
givenTwenties = Integer.parseInt(leftSide[4]);
}
}
}
}
int givenTotal = (20*givenTwenties + 10*givenTens + 5*givenFives + givenOnes);
int requestedTotal = (20*inTwenties + 10*inTens + 5*inFives + inOnes);
if(givenTotal != requestedTotal) {
System.exit(2);
}
// add the given denominations to drawer so they can be used if necessary
ones += givenOnes;
fives += givenFives;
tens += givenTens;
twenties += givenTwenties;
if(ones >= inOnes && fives >= inFives && tens >= inTens && twenties >= inTwenties) {
ones -= inOnes;
fives -= inFives;
tens -= inTens;
twenties -= inTwenties;
System.out.println(inOnes + " " + inFives + " " + inTens + " " + inTwenties);
writeFile(ones, fives, tens, twenties, total, sales);
System.exit(0);
} else {
System.exit(3);
}
}
/*
This method writes to the file "drawer.txt" the information about the drawer. It takes the
denominations, total cash in the drawer, and the total sales as arguments. The file is written
with one field on each line. The field order is: ones, fives, tens, twenties, total, sales.
If there is an error with writing to the file, it exits with a status of 4.
*/
public static void writeFile(int ones, int fives, int tens, int twenties, int total, int sales)
{
String fileName = "drawer.txt";
try {
FileWriter fileWriter = new FileWriter(fileName, false);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(String.valueOf(ones));
bufferedWriter.newLine();
bufferedWriter.write(String.valueOf(fives));
bufferedWriter.newLine();
bufferedWriter.write(String.valueOf(tens));
bufferedWriter.newLine();
bufferedWriter.write(String.valueOf(twenties));
bufferedWriter.newLine();
bufferedWriter.write(String.valueOf(total));
bufferedWriter.newLine();
bufferedWriter.write(String.valueOf(sales));
bufferedWriter.close();
} catch(IOException ex) {
System.exit(4);
}
}
/*
Main simply calls a function to check the input and react accordingly.
*/
public static void main(String[] args) throws IOException
{
checkInput(args);
}
}