刚学完java 集合和IO流,想做了一个简单的小项目来练手,于是我就运用一些基础知识做了这个水果摊。水果摊的主要功能有:
主菜单
public class MyList {
public static void main(String[] args) throws ClassNotFoundException, FileNotFoundException, IOException {
System.out.println("欢迎来到水果摊");
Scanner scanner =new Scanner(System.in);
int f=0;
while(true) {
System.out.println("请选择业务类型:");
System.out.println("0.进货,1.查看列表,2.购买水果,3.查看购物车,4.结账,5"
+ "退出");
int m =scanner.nextInt();
switch(m) {
case 0:Stock.Stock();
break;
case 1:Kind.Kind();
break;
case 2:Buy.Buy();
break;
case 3:Shoppings.Shoppings();
break;
case 4:Pay.Pay();
break;
case 5:System.out.println("感谢惠顾!");
f=1;
break;
default:
System.out.println("输入错误,请重新输入");
break;
}
if(f==1)break;
}
}
}
进货
package com.vector.units;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.vector.dao.Fruit;
import com.vector.service.FileOutput;
public class Stock {
static List<Fruit> list;
public static void Stock() throws FileNotFoundException {
List<Fruit> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String name;
double price;
int i = 1;
while (true) {
System.out.println("请输入第" + i + "种水果的种类:");
name = scanner.next();
System.out.println("请输入第" + i + "种水果的价格:");
price = scanner.nextInt();
Fruit sg = new Fruit(name, price);
list.add(sg);
setList(list);
System.out.println("输入0继续,输入1结束进货");
i++;
int f = scanner.nextInt();
if (f == 1) {
break;
}
}
FileOutput.FileOutput();
}
public static List<Fruit> getList() {
return list;
}
public static void setList(List<Fruit> list) {
Stock.list = list;
}
}
查看水果列表
package com.vector.units;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import com.vector.dao.Fruit;
import com.vector.service.FileInput;
import com.vector.service.FileOutput;
public class Kind {
static List<Fruit> list = new ArrayList<Fruit>();
static Object ob;
public static void Kind() throws ClassNotFoundException, FileNotFoundException, IOException {
FileInput fileInput = new FileInput();
list = (List<Fruit>) fileInput.FileInput().readObject();
Kind.setList(list);
System.out.println("总共有" + list.size() + "种水果,分别为:");
for (Fruit s : list) {
System.out.println(s);
}
}
public static List<Fruit> getList() {
return list;
}
public static void setList(List<Fruit> list) {
Kind.list = list;
}
public static Object getOb() {
return ob;
}
public static void setOb(Object ob) {
Kind.ob = ob;
}
}
购买水果
package com.vector.units;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import com.vector.dao.Fruit;
import com.vector.service.Mapp;
public class Buy {
static String name;
static List<Integer> M;
static Map<String, Double> fruit3;
public static void Buy() throws FileNotFoundException {
Map<String, Fruit> fruit = Mapp.Mapp();
Map<String, Double> fruit2 = Mapp.Mapp1();
Map<String, Double> fruit3 = Mapp.Mapp2();
String name;
int m;
List<Integer> M = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入你要购买的水果名称:");
name = scanner.next();
Buy.setName(name);
if (fruit.get(name) == null) {
System.out.println("输入错误,请重新选择输入!");
break;
}
System.out.println(fruit.get(name));
int f1 = 0;
Set ss1 = fruit3.entrySet();
Iterator it = ss1.iterator();
int t = 0;
int n = 0;
while (it.hasNext()) {
Object obj = it.next();
Map.Entry entry = (Map.Entry) obj;
if (entry.getKey().equals(name)) {
t = 1;
break;
}
n++;
}
int m1;
int m2;
if (t == 1) {
System.out.println("请输入购买的斤数:");
m1 = scanner.nextInt();
m2 = m1 + M.get(n);
M.set(n, m2);
} else {
fruit3.put(name, fruit2.get(name));
// System.out.println(fruit3.get(name));
System.out.println("请输入购买的斤数:");
m = scanner.nextInt();
M.add(m);
}
Buy.setFruit3(fruit3);
System.out.println("输入0继续,输入1结束");
int f = scanner.nextInt();
if (f == 1) {
break;
}
}
Buy.setM(M);
}
public static String getName() {
return name;
}
public static void setName(String name) {
Buy.name = name;
}
public static Map<String, Double> getFruit3() {
return fruit3;
}
public static List<Integer> getM() {
return M;
}
public static void setM(List<Integer> m) {
M = m;
}
public static void setFruit3(Map<String, Double> fruit3) {
Buy.fruit3 = fruit3;
}
}
购物车
package com.vector.units;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import com.vector.units.Buy;
import com.vector.service.Mapp;
public class Shoppings {
static double Total;
public static void Shoppings() throws FileNotFoundException {
Map<String, Double> fruit = Buy.getFruit3();
double n = 0;
List<Integer> M = Buy.getM();
int m = 0;
for (String key : fruit.keySet()) {
System.out.println("水果名称: " + key + ",水果的价格:" + fruit.get(key) + "/斤" + "水果的数量" + M.get(m) + "斤");
n += M.get(m) * fruit.get(key);
m++;
}
Shoppings.setTotal(n);
}
public static double getTotal() {
return Total;
}
public static void setTotal(double total) {
Total = total;
}
}
结账
package com.vector.units;
public class Pay {
public static void Pay() {
double inTotal = Shoppings.getTotal();
System.out.println("请付钱"+inTotal+"元");
}
}
下面的类都是一些用于实现功能的其他类
水果信息
package com.vector.dao;
import java.io.Serializable;
public class Fruit implements Serializable {
String name;
double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Fruit(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "水果名称:" + name + ",价格" + price + "/斤";
}
}
写出文件
public class FileOutput {
public static FileOutputStream FileOutput() throws FileNotFoundException {
File file =new File ("D:\\eclip\\Fruit\\list1.txt");
FileOutputStream fileOut = new FileOutputStream(file);
try {
ObjectOutputStream objectOut =new ObjectOutputStream(fileOut);
objectOut.writeObject(Kind.getList());
objectOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileOut;
}
}
读入
package com.vector.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class FileInput {
public ObjectInputStream FileInput() throws FileNotFoundException, IOException {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("D:\\eclip\\Fruit\\list1.txt")));
return in;
}
}
Map工具类
package com.vector.service;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.vector.dao.Fruit;
import com.vector.units.Kind;
import com.vector.units.Stock;
public class Mapp {
public static Map<String, Fruit> Mapp() throws FileNotFoundException {
List<Fruit> list = Kind.getList();
Map<String, Fruit> fruit = new HashMap<String, Fruit>();
for (Fruit list1 : list) {
fruit.put(list1.getName(), list1);
}
return fruit;
}
public static Map<String, Double> Mapp1() throws FileNotFoundException {
List<Fruit> list = Kind.getList();
Map<String, Double> fruit2 = new HashMap<String, Double>();
for (Fruit list2 : list) {
fruit2.put(list2.getName(), list2.getPrice());
}
return fruit2;
}
public static Map<String, Double> Mapp2() throws FileNotFoundException {
Map<String, Double> fruit3 = new HashMap<String, Double>();
return fruit3;
}
}
由于是用来练习的小项目,代码写的有些粗糙,还望大家见谅。
.
.
.
.
.
.
.
.